view synapseGetDataset.py @ 60:bf57076e27b9 default tip

change genomicSegment input data
author jingchunzhu@gmail.com
date Tue, 27 Oct 2015 16:07:09 -0700
parents e81019e3ac99
children
line wrap: on
line source

#!/usr/bin/env python
"""Download a dataset from Synapse into Galaxy """

import argparse
import json
import re
import synapseclient
import sys
import zipfile

class InputError(Exception):
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)


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):
    if re.search("\.zip$", entity.path):
        zf = zipfile.ZipFile(entity.path)
        if len(zf.namelist()) > 1:
            raise InputError(len(zf.namelist())), "Error: more than one input file"
        else:
            data = zf.read(zf.namelist()[0])
            fpOut = open(dataPathname, "w")
            fpOut.write(data)
            fpOut.close()
    else:
        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()