-
Notifications
You must be signed in to change notification settings - Fork 1
/
anselmo.py
executable file
·331 lines (260 loc) · 10.2 KB
/
anselmo.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
import pdb
import getopt
import stats
import sys
import os
import glob
import shutil
from time import strftime
import pprint
pp = pprint.PrettyPrinter(indent=4)
#######################################################
def easterEgg():
print u"""
Así es la verdad -respondió Anselmo-, y con esa confianza te hago saber,
amigo Lotario, que el deseo que me fatiga es pensar si Camila, mi esposa,
es tan buena y tan perfeta como yo pienso; y no puedo enterarme en esta
verdad, si no es probándola de manera que la prueba manifieste los quilates
de su bondad, como el fuego muestra los del oro.
Don Quixote, Part Two, Chapter XXXIII."""
#------------------------------------------------------
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 getMedianIndex(varList = [], variance = 0):
closestFileIndex = 0
lowestDifference = sys.float_info.max
for elementIndex in xrange(len(varList)):
varDifference = abs(variance - float(varList[elementIndex][1]))
if varDifference < lowestDifference:
lowestDifference = varDifference
closestFileIndex = elementIndex
return closestFileIndex
#------------------------------------------------------
def copyFilesWithPrefix(fileList = [],
folder = "",
prefix = "",
message = "Error!",
tag = ""):
logList = []
logList.append([])
logList.append([message])
invariablePart = os.path.join(folder, prefix)
for originalFileName in fileList:
FNSuffix = originalFileName.split("_")[-1]
FNDirectory = os.path.dirname(originalFileName)
newBaseName = tag + "_" + prefix + "_" + FNSuffix
newFileName = os.path.join(FNDirectory, newBaseName)
logList.append(["%s\t->\t%s" % (os.path.basename(originalFileName), newBaseName)])
shutil.copyfile(originalFileName, newFileName)
return logList
#------------------------------------------------------
def printHelp(version = None, advanced = False):
print """
Anselmo %s is a program made in the Jesus Vazquez Cardiovascular Proteomics
Lab at Centro Nacional de Investigaciones Cardiovasculares, used to identify
the integration which holds the median variance from a set of randomised
SanXoT integrations (i.e., integrations performed using SanXoT's parametre -R).
Anselmo needs at least:
1) the prefix (by using the -f argument) of the set of experiments, i.e. when
the info files of the randomisations are such as:
heartExperiment1_infoFile.txt
heartExperiment2_infoFile.txt
...
heartExperimentZ_infoFile.txt
the prefix is considered to be "heartExperiment".
2) the folder where all those info files are (they must be in the same folder),
using the -p argument.
After reading the variances in the set of info files, Anselmo identifies the
info file containing the median of the set of variances, and then it renames
the files using its prefix, i.e. using the previous example, and assuming the
median of the variance is in experiment labelled "9", then it copies the files
heartExperiment9_infoFile.txt
heartExperiment9_higherLevel.txt
heartExperiment9_outStats.txt
...
into
med_heartExperiment_infoFile.txt
med_heartExperiment_higherLevel.txt
med_heartExperiment_outStats.txt
...
Usage: anselmo.py -p[folder] -fyeast_nullHypothesis [OPTIONS]""" % version
if advanced:
print """
-h, --help Display this help and exit.
-a, --analysis=string
Use a prefix for the output files (for the current
version, this only affects the log file).
-f, --prefix=string Prefix used for the integration of the N randomised
experiments.
-g, --extraprefix=string
Additional copy for another set of files produced by
an integration, using the same tag as the median
calculated via -f.
-L, --logfile=filename
To use a non-default name for the log file.
-m, --mediantag=string
To use a non-default tag for the copied files, which
will be used as prefix (default is "med").
-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.
"""
else:
print """
Use -H or --advanced-help for more details."""
return
#------------------------------------------------------
def main(argv):
version = "v0.05"
analysisName = ""
analysisFolder = ""
logFile = ""
# in data
prefix = ""
extraPrefix = ""
medianTag = "med"
# default filenames
defaultInfoFileSuffix = "_infoFile.txt"
defaultLogFile = "logFile"
defaultAnalysisName = "medianSelection"
# default extensions
defaultTableExtension = ".xls"
defaultTextExtension = ".txt"
verbose = True
logList = [["Anselmo " + version], ["Start: " + strftime("%Y-%m-%d %H:%M:%S")]]
try:
opts, args = getopt.getopt(argv, "a:p:f:g:m:L:hH", ["analysis=", "folder=", "prefix=", "extraprefix=", "mediantag=", "logfile=", "help", "egg", "easteregg"])
except getopt.GetoptError:
logList.append(["Error while getting parameters."])
stats.saveFile(infoFile, logList, "INFO FILE")
sys.exit(2)
if len(opts) == 0:
printHelp(version, True)
sys.exit()
for opt, arg in opts:
if opt in ("-a", "--analysis"):
analysisName = arg
elif opt in ("-p", "--place", "--folder"):
analysisFolder = arg
elif opt in ("-f", "--prefix"):
prefix = arg
elif opt in ("-g", "--extraprefix"):
extraPrefix = arg
elif opt in ("-L", "--logfile"):
logFile = arg
elif opt in ("-m", "--mediantag"):
medianTag = arg
elif opt in ("-h", "--help"):
printHelp(version)
sys.exit()
elif opt in ("-H", "--advanced-help"):
printHelp(version, advanced = True)
sys.exit()
elif opt in ("--egg", "--easteregg"):
easterEgg()
sys.exit()
# REGION: FILE NAMES SETUP
if len(analysisName) == 0:
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
# next "if" disables extra copy when extraPrefix is same as prefix
if len(extraPrefix) > 0 and extraPrefix == prefix:
extraPrefix = ""
# input
# output
if len(logFile) == 0:
logFile = os.path.join(analysisFolder, analysisName + "_" + defaultLogFile + defaultTextExtension)
##logList.append(["Median variance = " + "poner***"])
# END REGION: FILE NAMES SETUP
# get infoFile list
infoFileList = glob.glob(os.path.join(analysisFolder, prefix + "*" + defaultInfoFileSuffix))
logList.append([])
logList.append(["Folder = " + analysisFolder])
logList.append([])
logList.append(["Info files with prefix \"%s\"" % prefix])
varList = []
for varFile in infoFileList:
variance, varianceOk = stats.extractVarianceFromVarFile(varFile, verbose = False)
if varianceOk:
varList.append([varFile, variance])
# get info file with median variance
varList = stats.sortByIndex(varList, 1)
medianVariance = stats.medianByIndex(varList, 1)
medianIndex = getMedianIndex(varList = varList, variance = medianVariance)
for element in varList:
if element[0] == varList[medianIndex][0]:
logList.append(["%s, variance = %f [taken]" % (os.path.basename(element[0]), element[1])])
else:
logList.append(["%s, variance = %f" % (os.path.basename(element[0]), element[1])])
# get prefix of median experiment
medianInfoFile = os.path.basename(varList[medianIndex][0])
randTag = medianInfoFile[len(prefix):len(medianInfoFile) - len(defaultInfoFileSuffix)]
medianPrefix = prefix + randTag
extraMedianPrefix = ""
if len(extraPrefix) > 0: extraMedianPrefix = extraPrefix + randTag
# get file list with specific prefix
medianExperimentFileList = glob.glob(os.path.join(analysisFolder, medianPrefix + "*.*"))
extraPrefixFileList = []
if len(extraMedianPrefix) > 0:
extraPrefixFileList = glob.glob(os.path.join(analysisFolder, extraMedianPrefix + "*.*"))
# copy files including median tag
extraLogList = copyFilesWithPrefix(fileList = medianExperimentFileList,
folder = analysisFolder,
prefix = prefix,
message = "Renamed files:",
tag = medianTag)
logList.extend(extraLogList)
if len(extraPrefixFileList) > 0:
extraLogList = copyFilesWithPrefix(fileList = extraPrefixFileList,
folder = analysisFolder,
prefix = extraPrefix,
message = "Renamed extra files:",
tag = medianTag)
logList.extend(extraLogList)
# save logFile
stats.saveFile(logFile, logList, "INFO FILE")
#######################################################
if __name__ == "__main__":
main(sys.argv[1:])