annotate galaxy_micropita/src/breadcrumbs/scripts/scriptManipulateTable.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: Performs common manipulations on tables
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 2012"
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
9 __credits__ = ["Timothy Tickle"]
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@sph.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 import argparse
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
17 import csv
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
18 import sys
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
19 import re
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
20 import os
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
21 import numpy as np
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
22 from src.AbundanceTable import AbundanceTable
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
23 #from src.PCA import PCA
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
24 from src.ValidateData import ValidateData
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
25
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
26 #Set up arguments reader
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
27 argp = argparse.ArgumentParser( prog = "scriptManipulateTable.py",
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
28 description = """Performs common manipulations on tables.\nExample: python scriptManipulateTable.py -i TID -l STSite Test.pcl""" )
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
29
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
30 #Arguments
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
31 #Describe table
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
32 argp.add_argument("-i","--id", dest="sIDName", default="ID", help="Abundance Table ID")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
33 argp.add_argument("-l","--meta", dest="sLastMetadataName", help="Last metadata name")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
34 argp.add_argument("-d","--fileDelim", dest= "cFileDelimiter", action= "store", default="\t", help="File delimiter, default tab")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
35 argp.add_argument("-f","--featureDelim", dest= "cFeatureDelimiter", action= "store", default="|", help="Feature (eg. bug or function) delimiter, default '|'")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
36
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
37 #Checked x 2
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
38 argp.add_argument("-n","--doNorm", dest="fNormalize", action="store_true", default=False, help="Flag to turn on normalization")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
39 argp.add_argument("-s","--doSum", dest="fSum", action="store_true", default=False, help="Flag to turn on summation")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
40
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
41 #Unsupervised filtering
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
42 argp.add_argument("-A","--doFilterAbundance", dest="strFilterAbundance", action="store", default=None, help="Turns on filtering by abundance (remove features that do not have a minimum abundance in a minimum number of samples); Should be a real number and an integer in the form 'minAbundance,minSamples', (should be performed on a normalized file).")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
43 argp.add_argument("-P","--doFilterPercentile", dest="strFilterPercentile", action="store", default=None, help="Turns on filtering by percentile Should be two numbers between 0 and 1 in the form 'percentile,percentage'. (should be performed on a normalized file).")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
44 argp.add_argument("-O","--doFilterOccurrence", dest="strFilterOccurence", action="store", default=None, help="Turns on filtering by occurrence. Should be two integers in the form 'minSequence,minSample' (should NOT be performed on a normalized file).")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
45 #argp.add_argument("-D","--doFilterDeviation", dest="dCuttOff", action="store", type=float, default=None, help="Flag to turn on filtering by standard deviation (should NOT be performed on a normalized file).")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
46
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
47 #Change bug membership
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
48 argp.add_argument("-t","--makeTerminal", dest="fMakeTerminal", action="store_true", default=False, help="Works reduces the file to teminal features in the original file.")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
49 argp.add_argument("-u","--reduceOTUs", dest="fRemoveOTUs", action="store_true", default=False, help="Remove otu entries from file.")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
50 argp.add_argument("-c","--reduceToClade", dest="iClade", action="store", type=int, default=None, help="Specify a level of clade to reduce to [].")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
51 argp.add_argument("-b","--reduceToFeatures", dest="strFeatures", action="store", default=None, help="Reduce measurements to certain features (bugs or functions). This can be a comma delimited string (of atleast 2 bugs) or a file.")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
52
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
53 #Manipulate based on metadata
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
54 #Checked
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
55 argp.add_argument("-y","--stratifyBy", dest="strStratifyBy", action="store", default=None, help="Metadata to stratify tables by.")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
56 argp.add_argument("-r","--removeMetadata", dest="strRemoveMetadata", action="store", default=None, help="Remove samples of this metadata and value (format comma delimited string with metadata id first and the values to remove after 'id,lvalue1,value2').")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
57
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
58 #Manipulate lineage
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
59 #Checked
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
60 argp.add_argument("-x","--doPrefixClades", dest="fPrefixClades", action="store_true", default=False, help="Flag to turn on adding prefixes to clades to better identify them, for example s__ will be placed infront of each species.")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
61
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
62 #Combine tables
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
63 #argp.add_argument("-m","--combineIntersect", dest="fCombineIntersect", action="store_true", default=False, help="Combine two tables including only common features/metadata (intersection).")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
64 #argp.add_argument("-e","--combineUnion", dest="fCombineUnion", action="store_true", default=False, help="Combine two tables (union).")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
65
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
66 #Dimensionality Reduction
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
67 #argp.add_argument("-p","--doPCA", dest="fDoPCA",action="store_true", default=False, help="Flag to turn on adding metabugs and metametadata by performing PCA on each of bug relative abundance and continuous metadata and add the resulting components")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
68
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
69 #Checked
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
70 argp.add_argument("-o","--output", dest="strOutFile", action="store", default=None, help="Indicate output pcl file.")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
71 argp.add_argument("strFileAbund", help ="Input data file")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
72
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
73 args = argp.parse_args( )
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
74
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
75 # Creat output file if needed.
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
76 if not args.strOutFile:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
77 args.strOutFile = os.path.splitext(args.strFileAbund)[0]+"-mod.pcl"
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
78 lsPieces = os.path.splitext(args.strOutFile)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
79
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
80 #List of abundance tables
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
81 lsTables = []
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
82
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
83 #Read in abundance table
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
84 abndTable = AbundanceTable.funcMakeFromFile(xInputFile=args.strFileAbund,
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
85 cDelimiter = args.cFileDelimiter,
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
86 sMetadataID = args.sIDName,
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
87 sLastMetadata = args.sLastMetadataName,
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
88 lOccurenceFilter = None,
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
89 cFeatureNameDelimiter=args.cFeatureDelimiter,
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
90 xOutputFile = args.strOutFile)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
91
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
92 #TODO Check filtering, can not have some filtering together
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
93
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
94 # Make feature list
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
95 lsFeatures = []
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
96 if args.strFeatures:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
97 print "Get features not completed"
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
98 # if "," in args.strFeatures:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
99 # lsFeatures = args.strFeatures.split(",")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
100 # print "ManipulateTable::Reading in feature list "+str(len(lsFeatures))+"."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
101 # else:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
102 # csvr = csv.reader(open(args.strFeatures, "rU"))
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
103 # print "ManipulateTable::Reading in feature file "+args.strFeatures+"."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
104 # for lsLine in csvr:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
105 # lsFeatures.extend(lsLine)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
106
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
107 lsTables.append(abndTable)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
108
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
109 # Do summing
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
110 #Sum if need
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
111 if args.fSum:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
112 for abndTable in lsTables:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
113 print "ManipulateTable::"+abndTable.funcGetName()+" had "+str(len(abndTable.funcGetFeatureNames()))+" features before summing."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
114 fResult = abndTable.funcSumClades()
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
115 if fResult:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
116 print "ManipulateTable::"+abndTable.funcGetName()+" was summed."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
117 print "ManipulateTable::"+abndTable.funcGetName()+" has "+str(len(abndTable.funcGetFeatureNames()))+" features after summing."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
118 else:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
119 print "ManipulateTable::ERROR. "+abndTable.funcGetName()+" was NOT summed."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
120
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
121 # Filter on counts
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
122 if args.strFilterOccurence:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
123 iMinimumSequence,iMinimumSample = args.strFilterOccurence.split(",")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
124 for abndTable in lsTables:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
125 if abndTable.funcIsNormalized():
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
126 print "ManipulateTable::ERROR. "+abndTable.funcGetName()+" is normalized and can not be filtered by occurence. This filter needs counts."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
127 else:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
128 fResult = abndTable.funcFilterAbundanceBySequenceOccurence(iMinSequence = int(iMinimumSequence), iMinSamples = int(iMinimumSample))
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
129 if fResult:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
130 print "ManipulateTable::"+abndTable.funcGetName()+" was filtered by occurence and now has "+str(len(abndTable.funcGetFeatureNames()))+" features."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
131 else:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
132 print "ManipulateTable::ERROR. "+abndTable.funcGetName()+" was NOT filtered by occurence."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
133
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
134 # Change bug membership
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
135 if args.fMakeTerminal:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
136 lsTerminalTables = []
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
137 for abndTable in lsTables:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
138 print "ManipulateTable::"+abndTable.funcGetName()+" had "+str(len(abndTable.funcGetFeatureNames()))+" features before making terminal."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
139 abndTable = abndTable.funcGetFeatureAbundanceTable(abndTable.funcGetTerminalNodes())
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
140 if abndTable:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
141 print "ManipulateTable::"+abndTable.funcGetName()+" has "+str(len(abndTable.funcGetFeatureNames()))+" terminal features."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
142 lsTerminalTables.append(abndTable)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
143 else:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
144 print "ManipulateTable::ERROR. "+abndTable.funcGetName()+" was not made terminal."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
145 lsTables = lsTerminalTables
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
146
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
147 if args.fRemoveOTUs:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
148 lsNotOTUs = []
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
149 for abndTable in lsTables:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
150 print "ManipulateTable::"+abndTable.funcGetName()+" had "+str(len(abndTable.funcGetFeatureNames()))+" features before removing OTUs."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
151 abndTable = abndTable.funcGetWithoutOTUs()
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
152 if abndTable:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
153 print "ManipulateTable::"+abndTable.funcGetName()+" had OTUs removed and now has "+str(len(abndTable.funcGetFeatureNames()))+" features."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
154 lsNotOTUs.append(abndTable)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
155 else:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
156 print "ManipulateTable::ERROR. "+abndTable.funcGetName()+" OTUs were not removed."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
157 lsTables = lsNotOTUs
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
158
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
159 if args.iClade:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
160 for abndTable in lsTables:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
161 fResult = abndTable.funcReduceFeaturesToCladeLevel(args.iClade)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
162 if fResult:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
163 print "ManipulateTable::"+abndTable.funcGetName()+" was reduced to clade level "+str(args.iClade)+"."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
164 else:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
165 print "ManipulateTable::ERROR. "+abndTable.funcGetName()+" was NOT reduced in clade levels."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
166
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
167 if args.strFeatures:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
168 for abndTable in lsTables:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
169 fResult = abndTable.funcGetFeatureAbundanceTable(lsFeatures)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
170 if fResult:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
171 print "ManipulateTable::"+abndTable.funcGetName()+" has been reduced to given features and now has "+str(len(abndTable.funcGetFeatureNames()))+" features."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
172 else:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
173 print "ManipulateTable::ERROR. "+abndTable.funcGetName()+" could not be reduced to the given list."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
174
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
175 if args.strRemoveMetadata:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
176 lsMetadata = args.strRemoveMetadata.split(",")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
177 for abndTable in lsTables:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
178 fResult = abndTable.funcRemoveSamplesByMetadata(sMetadata=lsMetadata[0], lValuesToRemove=lsMetadata[1:])
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
179 if fResult:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
180 print "ManipulateTable::"+abndTable.funcGetName()+" has had samples removed and now has "+str(len(abndTable.funcGetSampleNames()))+" samples."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
181 else:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
182 print "ManipulateTable::ERROR. Could not remove samples from "+abndTable.funcGetName()+"."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
183
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
184 # Normalize if needed
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
185 if args.fNormalize:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
186 for abndTable in lsTables:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
187 fResult = abndTable.funcNormalize()
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
188 if fResult:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
189 print "ManipulateTable::"+abndTable.funcGetName()+" was normalized."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
190 else:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
191 print "ManipulateTable::"+abndTable.funcGetName()+" was NOT normalized."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
192
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
193 # Filter on percentile
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
194 if args.strFilterPercentile:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
195 dPercentile,dPercentage = args.strFilterPercentile.split(",")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
196 for abndTable in lsTables:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
197 if abndTable.funcIsNormalized():
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
198 fResult = abndTable.funcFilterAbundanceByPercentile(dPercentileCutOff = float(dPercentile), dPercentageAbovePercentile = float(dPercentage))
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
199 if fResult:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
200 print "ManipulateTable::"+abndTable.funcGetName()+" has been reduced by percentile and now has "+str(len(abndTable.funcGetFeatureNames()))+" features."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
201 else:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
202 print "ManipulateTable::ERROR. "+abndTable.funcGetName()+" could not be reduced by percentile."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
203 else:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
204 print "ManipulateTable::"+abndTable.funcGetName()+" was NOT normalized and so the percentile filter is invalid, please indicate to normalize the table."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
205
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
206 # Filter on abundance (should go after filter on percentile because the filter on percentile
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
207 # needs the full distribution of features in a sample
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
208 if args.strFilterAbundance:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
209 dAbundance,iMinSamples = args.strFilterAbundance.split(",")
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
210 dAbundance = float(dAbundance)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
211 iMinSamples = int(iMinSamples)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
212 for abndTable in lsTables:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
213 if abndTable.funcIsNormalized():
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
214 fResult = abndTable.funcFilterAbundanceByMinValue(dMinAbundance=dAbundance,iMinSamples=iMinSamples)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
215 if fResult:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
216 print "ManipulateTable::"+abndTable.funcGetName()+" has been reduced by minimum relative abundance value and now has "+str(len(abndTable.funcGetFeatureNames()))+" features."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
217 else:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
218 print "ManipulateTable::ERROR. "+abndTable.funcGetName()+" could not be reduced by percentile."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
219 else:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
220 print "ManipulateTable::"+abndTable.funcGetName()+" was NOT normalized and so the abundance filter is invalid, please indicate to normalize the table."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
221
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
222 #if args.dCuttOff:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
223 # print "Standard deviation filtering not completed"
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
224 # for abndTable in lsTables:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
225 # abndTable.funcFilterFeatureBySD(dMinSDCuttOff=args.dCuttOff)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
226 # if fResult:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
227 # print "ManipulateTable::"+abndTable.funcGetName()+" has been reduced by standard deviation and now has "+str(len(abndTable.funcGetFeatureNames()))+" features."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
228 # else:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
229 # print "ManipulateTable::ERROR. "+abndTable.funcGetName()+" could not be reduced by standard devation."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
230
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
231 # Need to normalize again after abundance data filtering given removing features breaks the normalization
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
232 # This happends twice because normalization is required to make the abundance data to filter on ;-)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
233 # Normalize if needed
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
234 if args.fNormalize:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
235 for abndTable in lsTables:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
236 fResult = abndTable.funcNormalize()
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
237 if fResult:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
238 print "ManipulateTable::"+abndTable.funcGetName()+" was normalized after filtering on abundance data."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
239
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
240 #Manipulate lineage
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
241 if args.fPrefixClades:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
242 for abndTable in lsTables:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
243 fResult = abndTable.funcAddCladePrefixToFeatures()
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
244 if fResult:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
245 print "ManipulateTable::Clade Prefix was added to "+abndTable.funcGetName()
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
246 else:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
247 print "ManipulateTable::ERROR. Clade Prefix was NOT added to "+abndTable.funcGetName()
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
248
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
249 # Under development
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
250 # Reduce dimensionality
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
251 #if args.fDoPCA:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
252 # pcaCur = PCA()
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
253 # for abndTable in lsTables:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
254 #
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
255 # # Add data features
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
256 # # Make data components and add to abundance table
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
257 # pcaCur.loadData(abndTable,True)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
258 # pcaCur.run(fASTransform=True)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
259 # ldVariance = pcaCur.getVariance()
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
260 # lldComponents = pcaCur.getComponents()
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
261 # # Make Names
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
262 # lsNamesData = ["Data_PC"+str((tpleVariance[0]+1))+"_"+re.sub("[\.|-]","_",str(tpleVariance[1])) for tpleVariance in enumerate(ldVariance)]
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
263 # abndTable.funcAddDataFeature(lsNamesData,lldComponents)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
264 #
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
265 # # Add metadata features
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
266 # # Convert metadata to an input for PCA
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
267 # pcaCur.loadData(pcaCur.convertMetadataForPCA(abndTable),False)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
268 # fSuccessful = pcaCur.run(fASTransform=False)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
269 # if(fSuccessful):
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
270 # ldVariance = pcaCur.getVariance()
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
271 # lldComponents = pcaCur.getComponents()
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
272 # # Make Names
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
273 # lsNamesMetadata = ["Metadata_PC"+str((tpleVariance[0]+1))+"_"+re.sub("[\.|-]","_",str(tpleVariance[1])) for tpleVariance in enumerate(ldVariance)]
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
274 # # Make metadata components and add to abundance
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
275 # llsMetadata = [list(npdRow) for npdRow in lldComponents]
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
276 # abndTable.funcAddMetadataFeature(lsNamesMetadata, llsMetadata)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
277 # else:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
278 # print "ManipulateTable::No metadata to PCA, no PCA components added to file based on metadata"
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
279
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
280 #Manipulate based on metadata
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
281 if args.strStratifyBy:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
282 labndStratifiedTables = []
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
283 for abndTable in lsTables:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
284 labndResult = abndTable.funcStratifyByMetadata(strMetadata=args.strStratifyBy)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
285 print "ManipulateTable::"+abndTable.funcGetName()+" was stratified by "+args.strStratifyBy+" in to "+str(len(labndResult))+" tables."
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
286 labndStratifiedTables.extend(labndResult)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
287 lsTables = labndStratifiedTables
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
288
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
289 if len(lsTables) == 1:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
290 lsTables[0].funcWriteToFile(args.strOutFile)
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
291 else:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
292 iIndex = 1
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
293 for abndManTable in lsTables:
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
294 abndManTable.funcWriteToFile(lsPieces[0]+str(iIndex)+lsPieces[1])
8fb4630ab314 Uploaded
sagun98
parents:
diff changeset
295 iIndex = iIndex + 1