view synapseGetDataset.py @ 7:1d150e860c4d

Expanded the functionality of the merge genomic datasets tool, to generate an output dataset with the file (or label) indicating where each column came from
author melissacline
date Mon, 09 Mar 2015 19:58:03 -0700
parents 60efb9214eaa
children ae91153d3fc2
line wrap: on
line source

#!/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()