view xena_import.py @ 16:7f03b062f330 default tip

Cleaned up the user interface a bit
author melissacline
date Tue, 23 Sep 2014 21:18:54 -0700
parents a2a7096897a8
children
line wrap: on
line source

#!/usr/bin/env python

"""
  xena_import.py: import a dataset into Xena

  Given a cmdline-specified genomic data file and a cmdline-specified Xena 
  directory, import the genomic data fle into Xena.  This requires assembling
  the necessary json file, based on cmdline input.
"""

import argparse
import json
import os
import shutil

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("genomicDataPathname", type=str)
    parser.add_argument("cohort", type=str)
    parser.add_argument("type", type=str)
    args = parser.parse_args()

    xenaBaseDir = os.getenv("XENA_BASE_DIR", "~")
    xenaFileDir = xenaBaseDir + "/files"

    # Assemble the metadata in JSON format
    metadata = { 'cohort': args.cohort, 'type': args.type }
    jsonMetadata = json.dumps(metadata, indent=2) 
    
    # Write the metadata to a file in the Xena directory.  Use the filename
    # of the genomic data file, with an added .json extension.
    genomicDataFilename = args.genomicDataPathname.split("/")[-1]
    jsonMetadataPathname = "%s/%s.json" % (xenaFileDir, 
                                           genomicDataFilename)
    #fp = open("/inside/home/cline/tmp/xena_import.out", "w")
    #fp.write("xena file dir %s\n" % (xenaFileDir))
    #fp.write("copying metadata to %s and data to %s" % (jsonMetadataPathname, 
    #                                                    xenaFileDir))
    #fp.close()

    fp = open(jsonMetadataPathname, "w")
    fp.write("%s\n" % (jsonMetadata))
    fp.close()

    # Finally, copy the genomic data into the Xena directory
    try:
        shutil.copy(args.genomicDataPathname, xenaFileDir)
    except:
        print "Unexpected error", sys.exc_info()[0]
    else:
        print "Data copied successfully to Xena"

if __name__ == "__main__":
    main()