diff synapseGetDataset.py @ 0:60efb9214eaa

Uploaded
author melissacline
date Wed, 14 Jan 2015 13:54:03 -0500
parents
children ae91153d3fc2
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/synapseGetDataset.py	Wed Jan 14 13:54:03 2015 -0500
@@ -0,0 +1,63 @@
+#!/usr/bin/env python
+"""Download a dataset from Synapse into Galaxy"""
+
+import argparse
+import json
+import synapseclient
+import sys
+
+def saveMetadata(entity, metadataPathname):
+    fp = open(metadataPathname, "w")
+    entityMetadata = dict(entity.properties.items() 
+                          + entity.annotations.items())
+    jsonMetadata = json.dumps(entityMetadata)
+    fp.write("%s\n" % (jsonMetadata))
+    fp.close()
+
+def saveData(entity, dataPathname):
+    fpIn = open(entity.path)
+    fpOut = open(dataPathname, "w")
+    for row in fpIn:
+        fpOut.write(row)
+    fpIn.close()
+    fpOut.close()
+
+def main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument("entityId", type=str)
+    parser.add_argument("email", type=str)
+    parser.add_argument("outputMetadataFile", type=str)
+    parser.add_argument("outputDataFile", type=str)
+    parser.add_argument("--apiKey", type=str, default=None)
+    parser.add_argument("--password", type=str, default=None)
+    args = parser.parse_args()
+    
+    syn = synapseclient.Synapse()
+    assert(args.apiKey != None or args.password != None)
+    try:
+        if args.apiKey is not None:
+            syn.login(email=args.email, apiKey=args.apiKey)
+        else:
+            syn.login(email=args.email, password = args.password)
+    except:
+        print "Login Unsuccessful\n"
+        sys.exit(-1)
+    else:
+        try:
+            entity=syn.get(args.entityId)
+        except:
+            exc_type, exc_value, exc_traceback = sys.exc_info()
+            lines = traceback.format_exception(exc_type, exc_value, 
+                                               exc_traceback)
+            allLines = ''.join('!! ' + line for line in lines)
+            print "Unsuccessful: error %s\n" % allLines
+            sys.exit(-1)
+        else: 
+            saveMetadata(entity, args.outputMetadataFile)
+            saveData(entity, args.outputDataFile)
+            sys.exit(0)
+
+if __name__ == "__main__":
+    main()
+
+