-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_trackhub.py
More file actions
67 lines (57 loc) · 2.34 KB
/
create_trackhub.py
File metadata and controls
67 lines (57 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import json, re, os
import boto3
import trackhub
s3_client = boto3.client('s3')
def handler(event, context):
print("received event: " + json.dumps(event, indent=2))
print("body is" + json.dumps(event['body'], indent=2))
body = json.loads(event['body'])
bucket = body['s3BucketName']
# /tmp is the only writeable system
tmp_dir = '/tmp/' + body['hubName']
# First we initialize the components of a track hub
hub, genomes_file, genome, trackdb = trackhub.default_hub(
hub_name=body['hubName'],
short_label=body['shortLabel'],
long_label=body['longLabel'],
genome=body['genome'],
email=body['email'])
# Next, we add a track for every sample.
for file_info in body['samples']:
file_URL = file_info['URL']
if 'name' in file_info:
name = file_info['name']
else:
name = file_info['shortLabel']
name = re.sub(' ', '_', name)
name = trackhub.helpers.sanitize(name)
track = trackhub.Track(
name=name, # track names can't have any spaces or special chars.
url=file_URL, # filename to build this track from
visibility='full', # shows the full signal
color=file_info['color'],
autoScale='on', # allow the track to autoscale
tracktype=file_info['trackType'], # required when making a track
)
# Each track is added to the trackdb
trackdb.add_tracks(track)
# In this example we "upload" the hub locally. Files are created in the
# "example_hub" directory, along with symlinks to the tracks' data files.
trackhub.upload.stage_hub(hub=hub, staging=tmp_dir)
# sync the directory to s3
for r, d, f in os.walk(tmp_dir):
for file in f:
full_path = os.path.join(r, file)
s3_path = re.sub(tmp_dir, body['hubName'], full_path)
print("writing {} to {}".format(full_path, s3_path))
s3_client.upload_file(full_path, bucket, s3_path,
ExtraArgs={'ACL': 'public-read'})
hubPath = "https://s3-us-west-2.amazonaws.com/dm-trackhubs/{}/{}".format(
body['hubName'],
body['hubName'] + '.hub.txt'
)
return {
"isBase64Encoded": False,
"statusCode": 200,
"body": json.dumps({'hubPath': hubPath})
}