annotate synapseGetDataset.py @ 13:dd93e7d1bf01

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