| 0 | 1 #!/usr/bin/env python | 
|  | 2 """ | 
|  | 3 Author: Timothy Tickle | 
|  | 4 Description: Converts between BIOM and PCL files. If a PCL file is read, an equivalent BIOM file will be written; if a BIOM file is read, an equivalent pcl file will be written. | 
|  | 5 """ | 
|  | 6 | 
|  | 7 __author__ = "Timothy Tickle" | 
|  | 8 __copyright__ = "Copyright 2013" | 
|  | 9 __credits__ = ["Timothy Tickle","George Weingart"] | 
|  | 10 __license__ = "" | 
|  | 11 __version__ = "" | 
|  | 12 __maintainer__ = "Timothy Tickle" | 
|  | 13 __email__ = "ttickle@hsph.harvard.edu" | 
|  | 14 __status__ = "Development" | 
|  | 15 | 
|  | 16 from AbundanceTable import AbundanceTable | 
|  | 17 import argparse | 
|  | 18 from ConstantsBreadCrumbs import ConstantsBreadCrumbs | 
|  | 19 import os | 
|  | 20 import sys | 
|  | 21 | 
|  | 22 | 
|  | 23 #Set up arguments reader | 
|  | 24 argp = argparse.ArgumentParser( prog = "convertBetweenBIOMAndPCL.py", | 
|  | 25     description = """Converts a PCL file to a BIOM file and visa versa.""" ) | 
|  | 26 | 
|  | 27 #Arguments | 
|  | 28 #For table | 
|  | 29 argp.add_argument("-i","--id", dest="sID", default = None, metavar= "Sample ID", help="The metadata indicating the sample ID.") | 
|  | 30 argp.add_argument("-l","--meta", dest = "sLastMetadataName", default = None, metavar= "Last Metadata Name", help="The last listed metadata before the first data measurement in the pcl file or to be in the pcl file.") | 
|  | 31 argp.add_argument("-r","--rowMetadataID", dest = "sLastMetadataRow", default = None,  metavar = "Last Row Metadata Column", help = "If row metadata is present in a PCL file, what is the id of the last row metadata column (most right column that contains row metadata). PCL file only.") | 
|  | 32 argp.add_argument("-f","--delim", dest = "cFileDelimiter", action= "store", metavar="File Delimiter", default="\t", help="File delimiter, default tab") | 
|  | 33 argp.add_argument("strFileAbund", metavar = "Abundance file", help ="Input data file") | 
|  | 34 argp.add_argument("strOutputFile", default = "", nargs="?", metavar = "Selection Output File", help ="Output file") | 
|  | 35 | 
|  | 36 args = argp.parse_args( ) | 
|  | 37 | 
|  | 38 # Make the output file name (if not given) and get the type of output file name | 
|  | 39 # Change the extension from BIOM to pcl | 
|  | 40 lsFilePieces = os.path.splitext(args.strFileAbund) | 
|  | 41 strOutputFileType = ConstantsBreadCrumbs.c_strPCLFile if lsFilePieces[-1]=="."+ConstantsBreadCrumbs.c_strBiomFile else ConstantsBreadCrumbs.c_strBiomFile | 
|  | 42 | 
|  | 43 if not args.strOutputFile: | 
|  | 44   args.strOutputFile = lsFilePieces[0] + "." + strOutputFileType | 
|  | 45 | 
|  | 46 # Set the last metadata to the id if not given. | 
|  | 47 if not args.sLastMetadataName: | 
|  | 48   args.sLastMetadataName = args.sID | 
|  | 49 | 
|  | 50 # Read in abundance table | 
|  | 51 abndTable = AbundanceTable.funcMakeFromFile(args.strFileAbund, cDelimiter=args.cFileDelimiter, sMetadataID=args.sID, sLastMetadataRow = args.sLastMetadataRow, sLastMetadata=args.sLastMetadataName, xOutputFile=args.strOutputFile) | 
|  | 52 if not abndTable: | 
|  | 53   print("Could not create an abundance table from the given file and settings.") | 
|  | 54 else: | 
|  | 55   abndTable.funcWriteToFile(args.strOutputFile, cDelimiter=args.cFileDelimiter, cFileType=strOutputFileType) |