-
Notifications
You must be signed in to change notification settings - Fork 1
/
sanxotsieve.py
executable file
·531 lines (438 loc) · 19.1 KB
/
sanxotsieve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
import pdb
import getopt
import stats
import sanxot
import sys
import os
import gc
from time import strftime
import pprint
pp = pprint.PrettyPrinter(indent=4)
#######################################################
class mode:
onlyOne = 1
onePerHigher = 2
#------------------------------------------------------
class higherResult:
def __init__(self, id2 = None, Xj = None, Vj = None):
self.id2 = id2
self.Xj = Xj
self.Vj = Vj
#------------------------------------------------------
class lowerResult:
def __init__(self, id1 = None, XiXj = None, Wj = None):
self.id1 = id1
self.XiXj = XiXj
self.Wj = Wj
#------------------------------------------------------
class statsResult:
# statsResult.id2 --> higher level identifier
# statsResult.Xj --> higher level X
# statsResult.Vj --> higher level V
# statsResult.id1 --> lower level identifier
# statsResult.Xi --> lower level X (log2Ratio)
# statsResult.Vi --> lower level weight without adding variance
# statsResult.Wij --> lower level weight including variance
# statsResult.nij --> lower level number of elements within the higher level element
# statsResult.Zij --> distance in sigmas
# statsResult.FDRij --> false discovery rate
def __init__(self, id2 = None, Xj = None, Vj = None, id1 = None, Xi = None, Vi = None, Wij = None, nij = None, Zij = None, FDRij = None):
self.id2 = id2
self.Xj = Xj
self.Vj = Vj
self.id1 = id1
self.Xi = Xi
self.Vi = Vi
self.Wij = Wij
self.nij = nij
self.Zij = Zij
self.FDRij = FDRij
#------------------------------------------------------
def detectRelationWithLeastFDR(statsData, FDRLimit = 0.01, modeUsed = mode.onlyOne):
# modes
# mode.onlyOne -> removes the relation with least FDR, if it has FDR < FDRLimit
# mode.onePerHigher -> removes one relation for each higher level element, if it has FDR < FDRLimit
#***
relationsToRemove = []
sortedStats = stats.sortByInstance(statsData, "FDRij", isDescendent = False)[::-1]
if modeUsed == mode.onlyOne:
leastFDR = sys.float_info.max
leastFDRRow = []
for statsRow in sortedStats:
if statsRow.FDRij < leastFDR:
leastFDR = statsRow.FDRij
leastFDRRow = statsRow
if leastFDRRow != []:
if leastFDRRow.FDRij < FDRLimit:
newRelationToRemove = [leastFDRRow.id2, leastFDRRow.id1]
relationsToRemove = [newRelationToRemove]
if modeUsed == mode.onePerHigher:
for statsRow in sortedStats:
if statsRow.FDRij < FDRLimit:
higherSelection = stats.filterByElement(relationsToRemove, statsRow.id2)
if len(higherSelection) == 0:
# add new outlier relation
newRelationToRemove = [statsRow.id2, statsRow.id1, statsRow.FDRij, abs(statsRow.Zij)]
relationsToRemove.append(newRelationToRemove)
else:
# warning!! none should have len(higherSelection) > 1
if len(higherSelection) == 1:
# check which has the least FDRij, or the biggest |Zij| if FDRij == 0
if (statsRow.FDRij < higherSelection[0][2] or (statsRow.FDRij == 0 and abs(statsRow.Zij) > higherSelection[0][3])):
newRelationToRemove = [statsRow.id2, statsRow.id1, statsRow.FDRij, abs(statsRow.Zij)]
relationsToRemove.remove(higherSelection[0])
relationsToRemove.append(newRelationToRemove)
relationsToRemove = stats.extractColumns(relationsToRemove, 0, 1)
return relationsToRemove
#------------------------------------------------------
def detectOutliers(statsData, FDRLimit = 0.01):
relationUnderFDR = []
for row in statsData:
if row.FDRij < FDRLimit:
relationUnderFDR.append([row.id2, row.id1])
return relationUnderFDR
#------------------------------------------------------
def removeOutliers(relations, relationsToRemove):
# note that here outliers are removed independently of their tags
# just removing all rows where id1 and id2 coincides
newRelations = relations[:]
if len(relationsToRemove) > 0:
for eachRelation in relationsToRemove:
#
# while eachRelation in newRelations:
# newRelations.remove(eachRelation)
#
# above previous code was simpler, but was buggy
# as it was not removing relationsToRemove including tags
# now, instead of removing eachRelation, it removes newRelations[i]
# keep this for future reference
i = 0
while i < len(newRelations):
if newRelations[i][0] == eachRelation[0] and newRelations[i][1] == eachRelation[1]:
newRelations.remove(newRelations[i])
else:
i += 1
return newRelations
#------------------------------------------------------
def getRelationsWithoutOutliers(data, relations, variance, FDRLimit = 0.01, modeUsed = mode.onlyOne, removeDuplicateUpper = False):
# this method is included only for backward compatibility
newRelations = relations[:]
removedRelations = []
startingLoop = True
outliers = []
while len(outliers) > 0 or startingLoop:
startingLoop = False
newRelations = removeOutliers(newRelations, outliers)
removedRelations.extend(outliers)
newVariance, dummyHigher, newStats, dummyLower, logResults, success = \
sanxot.integrate(data = data,
relations = newRelations,
varianceSeed = variance,
forceParameters = True,
removeDuplicateUpper = removeDuplicateUpper)
totOutliers = detectOutliers(newStats, FDRLimit = FDRLimit)
outliers = detectRelationWithLeastFDR(newStats, FDRLimit = FDRLimit, modeUsed = modeUsed)
print
if len(outliers) > 0:
print "%i outliers found, removing %i of them, and recalculating..." % (len(totOutliers), len(outliers))
else:
print "No outliers found at %f FDR." % FDRLimit
print
dummyHigher = []
newStats = []
dummyLower = []
totOutliers = []
gc.collect()
return newRelations, removedRelations, logResults
#------------------------------------------------------
def tagRelationsWithoutOutliers(data, relations, variance, FDRLimit = 0.01, modeUsed = mode.onlyOne, removeDuplicateUpper = False, tags = "!out", outlierTag = "out", logicOperatorsAsWords = False):
newRelations = relations[:]
removedRelations = []
startingLoop = True
outliers = []
while len(outliers) > 0 or startingLoop:
startingLoop = False
newRelations = stats.addTagToRelations(newRelations, outliers, outlierTag)
removedRelations.extend(outliers)
newVariance, dummyHigher, newStats, dummyLower, logResults, success = \
sanxot.integrate(data = data,
relations = newRelations,
varianceSeed = variance,
forceParameters = True,
removeDuplicateUpper = removeDuplicateUpper,
tags = tags,
logicOperatorsAsWords = logicOperatorsAsWords)
totOutliers = detectOutliers(newStats, FDRLimit = FDRLimit)
outliers = detectRelationWithLeastFDR(newStats, FDRLimit = FDRLimit, modeUsed = modeUsed)
print
if len(outliers) > 0:
print "%i outliers found, tagging %i of them as 'out', and recalculating..." % (len(totOutliers), len(outliers))
else:
print "No outliers found at %f FDR." % FDRLimit
print
dummyHigher = []
newStats = []
dummyLower = []
totOutliers = []
gc.collect()
return newRelations, removedRelations, logResults
#------------------------------------------------------
def printHelp(version = None, advanced = False):
print """
SanXoTSieve %s is a program made in the Jesus Vazquez Cardiovascular
Proteomics Lab at Centro Nacional de Investigaciones Cardiovasculares, used to
perform automatical removal of lower level outliers in an integration
performed using the SanXoT integrator.
SanXoTSieve needs the two input files of a SanXoT integration
(see SanXoT's help): commands -d and -r, respectively.
And the resulting variance of the integration that has been performed:
commands -V (assigned from the info file of the integration.) or -v.
... and delivers two output files:
* a new relations file (by default suffixed "_tagged"), which is
identical to the original relations file, but tagging in the third column
the relations marked as outlier.
* the log file.
Usage: sanxotsieve.py -d[data file] -r[relations file] -V[info file] [OPTIONS]""" % version
if advanced:
print """
-h, --help Display basic help and exit.
-H, --advanced-help Display this help and exit.
-a, --analysis=string
Use a prefix for the output files. If this is not
provided, then the prefix will be garnered from the data
file.
-b, --no-verbose Do not print result summary after executing.
-d, --datafile=filename
Data file with identificators of the lowel level in the
first column, measured values (x) in the second column,
and weights (v) in the third column.
-D, --removeduplicateupper
When merging data with relations table, remove duplicate
higher level elements (not removed by default).
-f, --fdrlimit=float
Use an FDR limit different than 0.01 (1%).
-L, --infofile=filename
To use a non-default name for the log file.
-n, --newrelfile=filename
To use a non-default name for the relations file
containing the tagged outliers.
-o, --outlierrelfile=filename
To use a non-default name for the relations responsible
of outliers (note that outlier relations are only saved
when the --oldway option is active)
-p, --place, --folder=foldername
To use a different common folder for the output files.
If this is not provided, the folder used will be the
same as the input folder.
-r, --relfile, --relationsfile=filename
Relations file, with identificators of the higher level
in the first column, and identificators of the lower
level in the second column.
-u, --one-to-one Remove only one outlier per cycle. This is slightly more
accurate than the default mode (where the outermost
outlier of each category with outliers is removed in
each cycle), but usually exacerbatingly slow.
-v, --var, --varianceseed=double
Variance used in the concerning integration.
Default is 0.001.
-V, --varfile=filename
Get the variance value from a text file. It must contain
a line (not more than once) with the text
"Variance = [double]". This suits the info file from a
previous integration (see -L in SanXoT).
--oldway Do it the old way: instead of tagging, create two
separated relation files, with and without outliers.
--outliertag=string To select a non-default tag for outliers (default: out)
--tags=string To define a tag to distinguish groups to perform the
integration. The tag can be used by inclusion, such as
--tags="mod"
or by exclusion, putting first the "!" symbol, such as
--tags="!out"
Tags should be included in a third column of the
relations file. Note that the tag "!out" for outliers is
implicit.
Different tags can be combined using logical operators
"and" (&), "or" (|), and "not" (!), and parentheses.
Some examples:
--tags="!out&mod"
--tags="!out&(dig0|dig1)"
--tags="(!dig0&!dig1)|mod1"
--tags="mod1|mod2|mod3"
"""
else:
print """
Use -H or --advanced-help for more details."""
return
#------------------------------------------------------
def main(argv):
version = "v0.17"
analysisName = ""
analysisFolder = ""
varianceSeed = 0.001
FDRLimit = 0.01
varianceSeedProvided = False
removeDuplicateUpper = False
tags = "!out"
outlierTag = "out"
logicOperatorsAsWords = False
dataFile = ""
relationsFile = ""
newRelFile = ""
removedRelFile = ""
defaultDataFile = "data"
defaultRelationsFile = "rels"
defaultTaggedRelFile = "tagged"
defaultNewRelFile = "cleaned"
defaultRemovedRelFile = "outliers"
defaultOutputInfo = "infoFile"
infoFile = ""
varFile = ""
defaultTableExtension = ".tsv"
defaultTextExtension = ".txt"
defaultGraphExtension = ".png"
verbose = True
oldWay = False # instead of tagging outliers, separating relations files, the old way
modeUsed = mode.onePerHigher
logList = [["SanXoTSieve " + version], ["Start: " + strftime("%Y-%m-%d %H:%M:%S")]]
try:
opts, args = getopt.getopt(argv, "a:p:v:d:r:n:L:V:f:ubDhH", ["analysis=", "folder=", "varianceseed=", "datafile=", "relfile=", "newrelfile=", "outlierrelfile=", "infofile=", "varfile=", "fdrlimit=", "one-to-one", "no-verbose", "randomise", "removeduplicateupper", "help", "advanced-help", "tags=", "outliertag=", "oldway", "word-operators"])
except getopt.GetoptError:
logList.append(["Error while getting parameters."])
stats.saveFile(infoFile, logList, "INFO FILE")
sys.exit(2)
if len(opts) == 0:
printHelp(version)
sys.exit()
for opt, arg in opts:
if opt in ("-a", "--analysis"):
analysisName = arg
if opt in ("-p", "--place", "--folder"):
analysisFolder = arg
if opt in ("-v", "--var", "--varianceseed"):
varianceSeed = float(arg)
varianceSeedProvided = True
elif opt in ("-d", "--datafile"):
dataFile = arg
elif opt in ("-r", "--relfile", "--relationsfile"):
relationsFile = arg
elif opt in ("-n", "--newrelfile"):
removedRelFile = arg
elif opt in ("-L", "--infofile"):
infoFile = arg
elif opt in ("-V", "--varfile"):
varFile = arg
elif opt in ("-u", "--one-to-one"):
modeUsed = mode.onlyOne
elif opt in ("-b", "--no-verbose"):
verbose = False
elif opt in ("--oldway"):
oldWay = True
elif opt in ("-f", "--fdrlimit"):
FDRLimit = float(arg)
elif opt in ("-D", "--removeduplicateupper"):
removeDuplicateUpper = True
elif opt in ("--tags"):
if arg.strip().lower() != "!out":
tags = "!out&(" + arg + ")"
elif opt in ("--word-operators"):
logicOperatorsAsWords = True
elif opt in ("--outliertag"):
outlierTag = "out"
elif opt in ("-h", "--help"):
printHelp(version)
sys.exit()
elif opt in ("-H", "--advanced-help"):
printHelp(version, advanced = True)
sys.exit()
# REGION: FILE NAMES SETUP
if len(analysisName) == 0:
if len(dataFile) > 0:
analysisName = os.path.splitext(os.path.basename(dataFile))[0]
else:
analysisName = defaultAnalysisName
if len(os.path.dirname(analysisName)) > 0:
analysisNameFirstPart = os.path.dirname(analysisName)
analysisName = os.path.basename(analysisName)
if len(analysisFolder) == 0:
analysisFolder = analysisNameFirstPart
if len(dataFile) > 0 and len(analysisFolder) == 0:
if len(os.path.dirname(dataFile)) > 0:
analysisFolder = os.path.dirname(dataFile)
# input
if len(dataFile) == 0:
dataFile = os.path.join(analysisFolder, analysisName + "_" + defaultDataFile + defaultTableExtension)
if len(os.path.dirname(dataFile)) == 0 and len(analysisFolder) > 0:
dataFile = os.path.join(analysisFolder, dataFile)
if len(os.path.dirname(varFile)) == 0 and len(os.path.basename(varFile)) > 0:
varFile = os.path.join(analysisFolder, varFile)
if len(varFile) > 0 and not varianceSeedProvided:
varianceSeed, varianceOk = stats.extractVarianceFromVarFile(varFile, verbose = verbose, defaultSeed = varianceSeed)
if not varianceOk:
logList.append(["Variance not found in text file."])
stats.saveFile(infoFile, logList, "INFO FILE")
sys.exit()
if len(relationsFile) == 0:
relationsFile = os.path.join(analysisFolder, analysisName + "_" + defaultRelationsFile + defaultTableExtension)
if len(os.path.dirname(relationsFile)) == 0:
relationsFile = os.path.join(analysisFolder, relationsFile)
# output
if len(newRelFile) == 0:
if oldWay: # suffix: "cleaned"
newRelFile = os.path.join(analysisFolder, analysisName + "_" + defaultNewRelFile + defaultTableExtension)
else: # suffix: "tagged"
newRelFile = os.path.join(analysisFolder, analysisName + "_" + defaultTaggedRelFile + defaultTableExtension)
if len(removedRelFile) == 0:
removedRelFile = os.path.join(analysisFolder, analysisName + "_" + defaultRemovedRelFile + defaultTableExtension)
if len(os.path.dirname(newRelFile)) == 0:
newRelFile = os.path.join(analysisFolder, newRelFile)
if len(os.path.dirname(removedRelFile)) == 0:
removedRelFile = os.path.join(analysisFolder, removedRelFile)
if len(infoFile) == 0:
infoFile = os.path.join(analysisFolder, analysisName + "_" + defaultOutputInfo + defaultTextExtension)
logList.append(["Variance seed = " + str(varianceSeed)])
logList.append(["Input data file: " + dataFile])
logList.append(["Input relations file: " + relationsFile])
if oldWay:
logList.append(["Output relations file without outliers: " + newRelFile])
logList.append(["Output relations file with outliers only: " + removedRelFile])
logList.append(["Removing duplicate higher level elements: " + str(removeDuplicateUpper)])
logList.append(["OldWay option activated: outliers are removed instead of tagged"])
else:
logList.append(["Relations file tagging outliers: " + newRelFile])
logList.append(["Tags to filter relations: " + tags])
logList.append(["Tag used for outliers: " + outlierTag])
# pp.pprint(logList)
# sys.exit()
# END REGION: FILE NAMES SETUP
relations = stats.loadRelationsFile(relationsFile)
data = stats.loadInputDataFile(dataFile)
if oldWay:
# only for backward compatibility. Note that tags are not supported
newRelations, removedRelations, logResults = \
getRelationsWithoutOutliers(data,
relations,
varianceSeed,
FDRLimit = FDRLimit,
modeUsed = modeUsed,
removeDuplicateUpper = removeDuplicateUpper)
else:
newRelations, removedRelations, logResults = \
tagRelationsWithoutOutliers(data,
relations,
varianceSeed,
FDRLimit = FDRLimit,
modeUsed = modeUsed,
removeDuplicateUpper = removeDuplicateUpper,
tags = tags,
outlierTag = outlierTag,
logicOperatorsAsWords = logicOperatorsAsWords)
if oldWay:
stats.saveFile(newRelFile, newRelations, "idsup\tidinf")
else:
stats.saveFile(newRelFile, newRelations, "idsup\tidinf\ttags")
stats.saveFile(infoFile, logList, "INFO FILE")
if oldWay:
stats.saveFile(removedRelFile, removedRelations, "idsup\tidinf")
#######################################################
if __name__ == "__main__":
main(sys.argv[1:])