Mercurial > repos > melissacline > ucsc_cancer_utilities
annotate synapseGetDataset.py @ 29:d776b829f83f
Rolling the synapseClient dependency back to version 1.1
author | melissacline |
---|---|
date | Wed, 22 Jul 2015 16:59:35 -0700 |
parents | d1104ad3646a |
children | e81019e3ac99 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2
d1104ad3646a
Trying one more time to get this push to happen...
melissacline
parents:
1
diff
changeset
|
2 """Download a dataset from Synapse into Galaxy """ |
0 | 3 |
4 import argparse | |
5 import json | |
6 import synapseclient | |
7 import sys | |
1
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
8 import zipfile |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
9 |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
10 class InputError(Exception): |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
11 def __init__(self, value): |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
12 self.value = value |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
13 def __str__(self): |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
14 return repr(self.value) |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
15 |
0 | 16 |
17 def saveMetadata(entity, metadataPathname): | |
18 fp = open(metadataPathname, "w") | |
19 entityMetadata = dict(entity.properties.items() | |
20 + entity.annotations.items()) | |
21 jsonMetadata = json.dumps(entityMetadata) | |
22 fp.write("%s\n" % (jsonMetadata)) | |
23 fp.close() | |
24 | |
25 def saveData(entity, dataPathname): | |
1
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
26 if entity.properties['contentType'] == "application/zip": |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
27 zf = zipfile.ZipFile(entity.path) |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
28 if len(zf.namelist()) > 1: |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
29 raise InputError(len(zf.namelist())), "Error: more than one input file" |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
30 else: |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
31 data = zf.read(zf.namelist()[0]) |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
32 fpOut = open(dataPathname, "w") |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
33 fpOut.write(data) |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
34 fpOut.close() |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
35 else: |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
36 fpIn = open(entity.path) |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
37 fpOut = open(dataPathname, "w") |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
38 for row in fpIn: |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
39 fpOut.write(row) |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
40 fpIn.close() |
ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
melissacline
parents:
0
diff
changeset
|
41 fpOut.close() |
0 | 42 |
43 def main(): | |
44 parser = argparse.ArgumentParser() | |
45 parser.add_argument("entityId", type=str) | |
46 parser.add_argument("email", type=str) | |
47 parser.add_argument("outputMetadataFile", type=str) | |
48 parser.add_argument("outputDataFile", type=str) | |
49 parser.add_argument("--apiKey", type=str, default=None) | |
50 parser.add_argument("--password", type=str, default=None) | |
51 args = parser.parse_args() | |
52 | |
53 syn = synapseclient.Synapse() | |
54 assert(args.apiKey != None or args.password != None) | |
55 try: | |
56 if args.apiKey is not None: | |
57 syn.login(email=args.email, apiKey=args.apiKey) | |
58 else: | |
59 syn.login(email=args.email, password = args.password) | |
60 except: | |
61 print "Login Unsuccessful\n" | |
62 sys.exit(-1) | |
63 else: | |
64 try: | |
65 entity=syn.get(args.entityId) | |
66 except: | |
67 exc_type, exc_value, exc_traceback = sys.exc_info() | |
68 lines = traceback.format_exception(exc_type, exc_value, | |
69 exc_traceback) | |
70 allLines = ''.join('!! ' + line for line in lines) | |
71 print "Unsuccessful: error %s\n" % allLines | |
72 sys.exit(-1) | |
73 else: | |
74 saveMetadata(entity, args.outputMetadataFile) | |
75 saveData(entity, args.outputDataFile) | |
76 sys.exit(0) | |
77 | |
78 if __name__ == "__main__": | |
79 main() | |
80 | |
81 |