annotate src/breadcrumbs/scripts/scriptConvertBetweenBIOMAndPCL.py @ 0:0de566f21448 draft default tip

v2
author sagun98
date Thu, 03 Jun 2021 18:13:32 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
sagun98
parents:
diff changeset
1 #!/usr/bin/env python
sagun98
parents:
diff changeset
2 """
sagun98
parents:
diff changeset
3 Author: Timothy Tickle
sagun98
parents:
diff changeset
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.
sagun98
parents:
diff changeset
5 """
sagun98
parents:
diff changeset
6
sagun98
parents:
diff changeset
7 __author__ = "Timothy Tickle"
sagun98
parents:
diff changeset
8 __copyright__ = "Copyright 2013"
sagun98
parents:
diff changeset
9 __credits__ = ["Timothy Tickle","George Weingart"]
sagun98
parents:
diff changeset
10 __license__ = ""
sagun98
parents:
diff changeset
11 __version__ = ""
sagun98
parents:
diff changeset
12 __maintainer__ = "Timothy Tickle"
sagun98
parents:
diff changeset
13 __email__ = "ttickle@hsph.harvard.edu"
sagun98
parents:
diff changeset
14 __status__ = "Development"
sagun98
parents:
diff changeset
15
sagun98
parents:
diff changeset
16 from AbundanceTable import AbundanceTable
sagun98
parents:
diff changeset
17 import argparse
sagun98
parents:
diff changeset
18 from ConstantsBreadCrumbs import ConstantsBreadCrumbs
sagun98
parents:
diff changeset
19 import os
sagun98
parents:
diff changeset
20 import sys
sagun98
parents:
diff changeset
21
sagun98
parents:
diff changeset
22
sagun98
parents:
diff changeset
23 #Set up arguments reader
sagun98
parents:
diff changeset
24 argp = argparse.ArgumentParser( prog = "convertBetweenBIOMAndPCL.py",
sagun98
parents:
diff changeset
25 description = """Converts a PCL file to a BIOM file and visa versa.""" )
sagun98
parents:
diff changeset
26
sagun98
parents:
diff changeset
27 #Arguments
sagun98
parents:
diff changeset
28 #For table
sagun98
parents:
diff changeset
29 argp.add_argument("-i","--id", dest="sID", default = None, metavar= "Sample ID", help="The metadata indicating the sample ID.")
sagun98
parents:
diff changeset
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.")
sagun98
parents:
diff changeset
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.")
sagun98
parents:
diff changeset
32 argp.add_argument("-f","--delim", dest = "cFileDelimiter", action= "store", metavar="File Delimiter", default="\t", help="File delimiter, default tab")
sagun98
parents:
diff changeset
33 argp.add_argument("strFileAbund", metavar = "Abundance file", help ="Input data file")
sagun98
parents:
diff changeset
34 argp.add_argument("strOutputFile", default = "", nargs="?", metavar = "Selection Output File", help ="Output file")
sagun98
parents:
diff changeset
35
sagun98
parents:
diff changeset
36 args = argp.parse_args( )
sagun98
parents:
diff changeset
37
sagun98
parents:
diff changeset
38 # Make the output file name (if not given) and get the type of output file name
sagun98
parents:
diff changeset
39 # Change the extension from BIOM to pcl
sagun98
parents:
diff changeset
40 lsFilePieces = os.path.splitext(args.strFileAbund)
sagun98
parents:
diff changeset
41 strOutputFileType = ConstantsBreadCrumbs.c_strPCLFile if lsFilePieces[-1]=="."+ConstantsBreadCrumbs.c_strBiomFile else ConstantsBreadCrumbs.c_strBiomFile
sagun98
parents:
diff changeset
42
sagun98
parents:
diff changeset
43 if not args.strOutputFile:
sagun98
parents:
diff changeset
44 args.strOutputFile = lsFilePieces[0] + "." + strOutputFileType
sagun98
parents:
diff changeset
45
sagun98
parents:
diff changeset
46 # Set the last metadata to the id if not given.
sagun98
parents:
diff changeset
47 if not args.sLastMetadataName:
sagun98
parents:
diff changeset
48 args.sLastMetadataName = args.sID
sagun98
parents:
diff changeset
49
sagun98
parents:
diff changeset
50 # Read in abundance table
sagun98
parents:
diff changeset
51 abndTable = AbundanceTable.funcMakeFromFile(args.strFileAbund, cDelimiter=args.cFileDelimiter, sMetadataID=args.sID, sLastMetadataRow = args.sLastMetadataRow, sLastMetadata=args.sLastMetadataName, xOutputFile=args.strOutputFile)
sagun98
parents:
diff changeset
52 if not abndTable:
sagun98
parents:
diff changeset
53 print("Could not create an abundance table from the given file and settings.")
sagun98
parents:
diff changeset
54 else:
sagun98
parents:
diff changeset
55 abndTable.funcWriteToFile(args.strOutputFile, cDelimiter=args.cFileDelimiter, cFileType=strOutputFileType)