annotate galaxy_micropita/src/breadcrumbs/scripts/scriptConvertBetweenBIOMAndPCL.py @ 3:8fb4630ab314 draft default tip

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