Mercurial > repos > melissacline > ucsc_cancer_utilities
comparison synapseGetDataset.py @ 0:60efb9214eaa
Uploaded
author | melissacline |
---|---|
date | Wed, 14 Jan 2015 13:54:03 -0500 |
parents | |
children | ae91153d3fc2 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:60efb9214eaa |
---|---|
1 #!/usr/bin/env python | |
2 """Download a dataset from Synapse into Galaxy""" | |
3 | |
4 import argparse | |
5 import json | |
6 import synapseclient | |
7 import sys | |
8 | |
9 def saveMetadata(entity, metadataPathname): | |
10 fp = open(metadataPathname, "w") | |
11 entityMetadata = dict(entity.properties.items() | |
12 + entity.annotations.items()) | |
13 jsonMetadata = json.dumps(entityMetadata) | |
14 fp.write("%s\n" % (jsonMetadata)) | |
15 fp.close() | |
16 | |
17 def saveData(entity, dataPathname): | |
18 fpIn = open(entity.path) | |
19 fpOut = open(dataPathname, "w") | |
20 for row in fpIn: | |
21 fpOut.write(row) | |
22 fpIn.close() | |
23 fpOut.close() | |
24 | |
25 def main(): | |
26 parser = argparse.ArgumentParser() | |
27 parser.add_argument("entityId", type=str) | |
28 parser.add_argument("email", type=str) | |
29 parser.add_argument("outputMetadataFile", type=str) | |
30 parser.add_argument("outputDataFile", type=str) | |
31 parser.add_argument("--apiKey", type=str, default=None) | |
32 parser.add_argument("--password", type=str, default=None) | |
33 args = parser.parse_args() | |
34 | |
35 syn = synapseclient.Synapse() | |
36 assert(args.apiKey != None or args.password != None) | |
37 try: | |
38 if args.apiKey is not None: | |
39 syn.login(email=args.email, apiKey=args.apiKey) | |
40 else: | |
41 syn.login(email=args.email, password = args.password) | |
42 except: | |
43 print "Login Unsuccessful\n" | |
44 sys.exit(-1) | |
45 else: | |
46 try: | |
47 entity=syn.get(args.entityId) | |
48 except: | |
49 exc_type, exc_value, exc_traceback = sys.exc_info() | |
50 lines = traceback.format_exception(exc_type, exc_value, | |
51 exc_traceback) | |
52 allLines = ''.join('!! ' + line for line in lines) | |
53 print "Unsuccessful: error %s\n" % allLines | |
54 sys.exit(-1) | |
55 else: | |
56 saveMetadata(entity, args.outputMetadataFile) | |
57 saveData(entity, args.outputDataFile) | |
58 sys.exit(0) | |
59 | |
60 if __name__ == "__main__": | |
61 main() | |
62 | |
63 |