-
Notifications
You must be signed in to change notification settings - Fork 1
/
sanson.py
executable file
·1207 lines (966 loc) · 41.4 KB
/
sanson.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pdb
import sys
import getopt
import stats
import os
import subprocess
from scipy.stats import norm
from time import strftime
import pprint
pp = pprint.PrettyPrinter(indent=4)
#######################################################
def easterEgg():
print u"""
... apeándose de Rocinante, fue sobre el de los Espejos, y, quitándole las
lazadas del yelmo para ver si era muerto y para que le diese el aire si acaso
estaba vivo; y vio... ¿Quién podrá decir lo que vio, sin causar admiración,
maravilla y espanto a los que lo oyeren? Vio, dice la historia, el rostro
mesmo, la misma figura, el mesmo aspecto, la misma fisonomía, la mesma
efigie, la pespetiva mesma del bachiller Sansón Carrasco.
Don Quixote, Part Two, Chapter XIV."""
#------------------------------------------------------
class higherResult:
def __init__(self, id2 = None, Xj = None, Vj = None):
self.id2 = id2
self.Xj = Xj
self.Vj = Vj
#------------------------------------------------------
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 scalarProduct(vectorA, vectorB):
# returns the percent of elements of A
# that are contained in B
# where 1 means all the elements of A are present in B
# and 0 means A and B are orthogonal
# next is just to speed up a bit the program
if len(vectorA) < len(vectorB):
vectorShort = vectorA
vectorLong = vectorB
else:
vectorShort = vectorB
vectorLong = vectorA
product = 0
for element in vectorShort:
first = stats.firstIndex(vectorLong, element)
if first == -1: pass
else: product += 1
return product
#------------------------------------------------------
def createSimNodes(nodeList = None,
extraData = None,
minFontSize = 10.0,
maxFontSize = 70.0,
normalFontSize = 14.0,
subData = None,
graphLimits = 6.0,
lineFeed = " ",
altMax = 5,
defaultNodeColour = "#ffff80",
errorNodeColour = "#8080ff",
minColour = "#00ff00",
middleColour = "#ffffff",
maxColour = "#ff0000",
bgColour = "#ffffd8",
defaultNodeTextColour = "#000000",
nonParetoOpacity = 0.5,
paretoInfo = None):
# WARNING! nodeList and extraData must be in the same order
nodesText = ""
averageN = 40
# start of NMax and NMin calculation --> put into a different method ***
NMax = averageN
NMin = averageN
if extraData:
for i in xrange(len(extraData)):
NValue = -1
try:
NValue = int(extraData[i][1])
except:
if subData:
for i in xrange(len(subData)):
NValue = -1
try:
NValue = len(subData[0][i + 2][1])
if NValue > 0:
if NValue > NMax: NMax = NValue
if NValue < NMin: NMin = NValue
except:
pass
break
if NValue > 0:
if NValue > NMax: NMax = NValue
if NValue < NMin: NMin = NValue
# end of NMax and NMin calculation
for i in xrange(len(nodeList)):
node = nodeList[i]
NText = ""
nodeColour, NValue, altText, nodeFontColour = stats.getNodeColourList(node,
elementNumber = i,
subData = subData,
extraData = extraData,
ZLimit = graphLimits,
maxAltTextLinesPerSide = altMax,
defaultNodeColour = defaultNodeColour,
errorNodeColour = errorNodeColour,
minColour = minColour,
middleColour = middleColour,
maxColour = maxColour,
defaultNodeTextColour = defaultNodeTextColour,
nonParetoOpacity = nonParetoOpacity,
paretoInfo = paretoInfo,
bgColour = bgColour)
if extraData and NValue == -1:
try:
NValue = int(extraData[i][1])
except:
pass
if NValue > 0:
NText = lineFeed + "(n = %i)" % NValue
nodeFontSize = stats.getNodeFontSize(NValue = NValue,
NMax = NMax,
NMin = NMin,
normalFontSize = normalFontSize,
minFontSize = minFontSize,
maxFontSize = maxFontSize)
# pdb.set_trace()
nodesText += """\t%s [label = "%s", tooltip = "%s", style = "rounded, filled, striped", penwidth = "0", fillcolor = "%s", fontsize = "%f", fontcolor = "%s", shape = "box"];\n""" % (stats.fixNodeName(node), stats.fixNodeNameLength(node) + NText, altText, nodeColour, nodeFontSize, nodeFontColour)
return nodesText
#------------------------------------------------------
def getSimArrowList(simMatrix = None, simLimit = 1.0, NMatrix = None):
arrowList = []
for i in xrange(len(simMatrix[0])):
for j in xrange(len(simMatrix[0])):
if j > 0 and i > 0:
if i != j:
if simMatrix[i][j] >= simLimit:
nodeParent = simMatrix[0][j]
nodeChild = simMatrix[0][i]
if NMatrix: arrowList.append([nodeParent, nodeChild, simMatrix[i][j], NMatrix[i][j]])
else: arrowList.append([nodeParent, nodeChild, simMatrix[i][j]])
return arrowList
#------------------------------------------------------
def createSimLinks(arrowList = None, nodeDefaultColour = "#303030", minArrowWidth = 1.0, maxArrowWidth = 15.0):
linksText = ""
for arrow in arrowList:
nodeParent = arrow[0]
nodeChild = arrow[1]
arrowWidth = minArrowWidth
arrowColour = nodeDefaultColour
arrowLabelText = ""
if len(arrow) > 2:
arrowWidth = float(arrow[2]) * (maxArrowWidth - minArrowWidth) + minArrowWidth
arrowColour = stats.extrapolateColour(arrow[2], minColour = "#ffffd8", middleColour = "#979784", maxColour = "#303030")
if len(arrow) > 3:
# then the NMatrix has been calculated, and N is included
arrowLabelText = str(arrow[3])
else:
# NMatrix not calculated, similarity number included
arrowLabelText = "%.2f" % round(arrow[2], 2)
linksText += """\t%s -> %s [penwidth = %f, color = "%s", label = "%s", labelfontsize = 20.0];\n""" % (stats.fixNodeName(nodeParent), stats.fixNodeName(nodeChild), arrowWidth, arrowColour, arrowLabelText)
return linksText
#------------------------------------------------------
def createGVFileText(simMatrix = None,
simLimit = 1.0,
extraData = None,
subData = None,
bgColour = "#ffffd8",
NMatrix = None,
graphLimits = 6.0,
altMax = 5,
defaultNodeColour = "#ffff80",
errorNodeColour = "#8080ff",
minColour = "#00ff00",
middleColour = "#ffffff",
maxColour = "#ff0000",
defaultNodeTextColour = "#000000",
nonParetoOpacity = 0.5,
paretoInfo = None,
minFontSize = 10.0,
maxFontSize = 70.0):
nodeList = simMatrix[0][1:]
nodesText = createSimNodes(nodeList,
extraData = extraData,
subData = subData,
graphLimits = graphLimits,
altMax = altMax,
defaultNodeColour = defaultNodeColour,
errorNodeColour = errorNodeColour,
minColour = minColour,
middleColour = middleColour,
maxColour = maxColour,
defaultNodeTextColour = defaultNodeTextColour,
nonParetoOpacity = nonParetoOpacity,
paretoInfo = paretoInfo,
bgColour = bgColour,
minFontSize = minFontSize,
maxFontSize = maxFontSize)
arrowList = getSimArrowList(simMatrix, simLimit, NMatrix = NMatrix)
linksText = createSimLinks(arrowList)
GVFileText = """digraph similarityGraph {\n\tbgcolor = "%s";\n\n%s\n%s}""" % (bgColour, nodesText, linksText)
return GVFileText
#------------------------------------------------------
def createDOTGraph(simMatrix = None,
simLimit = 1.0,
DOTProgramLocation = r"%ProgramFiles%\Graphviz2.30\bin",
outputGVFile = "",
simGraphFile = "",
extraData = None,
subData = None,
NMatrix = None,
graphLimits = 6.0,
graphFileFormat = "png",
altMax = 5,
defaultNodeColour = "#ffff80",
errorNodeColour = "#8080ff",
minColour = "#00ff00",
middleColour = "#ffffff",
maxColour = "#ff0000",
defaultNodeTextColour = "#000000",
nonParetoOpacity = 0.5,
paretoInfo = None,
minFontSize = 10.0,
maxFontSize = 70.0,
graphDPI = 96.0,
graphRatio = 0.0):
paretoInfoIncluded = False
for paretoElement in paretoInfo:
if paretoElement[1]:
paretoInfoIncluded = True
break
if not paretoInfoIncluded:
# no Pareto information included, so it's better to show all categories
nonParetoOpacity = 1.0
print """
Warning: no information included for Pareto front calculation.
"""
DOTProgram = "dot"
if "win" in sys.platform:
DOTIniPath = stats.joinLocationAndFile(os.path.dirname(os.path.realpath(sys.argv[0])), "dot.ini")
DOTProgramLocationCheck = stats.getFromIni(DOTIniPath, "dotlocation")
if len(DOTProgramLocationCheck) > 0: DOTProgramLocation = DOTProgramLocationCheck
DOTProgram = stats.joinLocationAndFile(DOTProgramLocation, "dot.exe")
GVFileText = createGVFileText(simMatrix = simMatrix,
simLimit = simLimit,
extraData = extraData,
subData = subData,
NMatrix = NMatrix,
graphLimits = graphLimits,
altMax = altMax,
defaultNodeColour = defaultNodeColour,
errorNodeColour = errorNodeColour,
minColour = minColour,
middleColour = middleColour,
maxColour = maxColour,
defaultNodeTextColour = defaultNodeTextColour,
nonParetoOpacity = nonParetoOpacity,
paretoInfo = paretoInfo,
minFontSize = minFontSize,
maxFontSize = maxFontSize)
stats.saveTextFile(outputGVFile, GVFileText)
if "win" in sys.platform:
DOTCommandLine = """"%s" -Gdpi=%f -Gratio=%f -T%s "%s" -o"%s\"""" % (DOTProgram, graphDPI, graphRatio, graphFileFormat, outputGVFile, simGraphFile)
else:
DOTCommandLine = "%s -Gdpi=%f -Gratio=%f -T%s %s -o%s" % (DOTProgram, graphDPI, graphRatio, graphFileFormat, outputGVFile, simGraphFile)
print DOTCommandLine
try:
if "win" in sys.platform:
subprocess.call(DOTCommandLine)
else:
subprocess.call(DOTCommandLine, shell=True)
except:
if "win" in sys.platform:
print """
*** ERROR ***
The graph could not be generated, because the dot.exe program could not be
found. Please, check:
1) that you have installed Graphviz,
which is freely available at http://www.graphviz.org/
2) that you have included the path of the program folder in the dot.ini
file (which should be in the same folder as this program)
"""
else:
print """
*** ERROR ***
The graph could not be generated, because the dot program could not be
found. Please, check:
1) that you have installed Graphviz,
which is freely available at http://www.graphviz.org/
2) that the dot program is available from the shell.
"""
return
#------------------------------------------------------
def SMatrix(data = None):
N = len(data)
theMatrix = []
NMatrix = []
noneTypeError = "\nError: category '%s' doesn't seem to have any proteins.\nPlease, check the input files."
for j in xrange(N + 1):
if j == 0: # add first row
currentRow = []
currentRowN = []
for i in xrange(N + 1):
if i == 0:
currentRow.append("")
currentRowN.append("")
else:
currentRow.append(data[i - 1][0])
currentRowN.append(data[i - 1][0])
theMatrix.append(currentRow)
NMatrix.append(currentRowN)
else:
print "analysing element #%i: %s" % ((j - 1), str(data[j - 1][0]))
currentRow = []
currentRowN = []
for i in xrange(N + 1):
if i == 0:
currentRow.append(data[j - 1][0])
currentRowN.append(data[j - 1][0])
else:
if data[j - 1][1] is None:
print noneTypeError % data[j - 1][0]
sys.exit()
if data[i - 1][1] is None:
print noneTypeError % data[i - 1][0]
sys.exit()
dotProduct = scalarProduct(data[j - 1][1], (data[i - 1][1]))
currentRow.append(float(dotProduct) / float(len(data[j - 1][1])))
currentRowN.append(dotProduct)
theMatrix.append(currentRow)
NMatrix.append(currentRowN)
return theMatrix, NMatrix
#------------------------------------------------------
def splitMatrixWithHeaders(originalMatrix):
if len(originalMatrix) > 1:
boolMatrix = stats.zeroMatrix(len(originalMatrix) - 1)
else:
return None, None, "Matrix must be larger than 1x1"
elementListColumns = []
for i in xrange(len(originalMatrix)):
if len(originalMatrix) != len(originalMatrix[i]):
# must be a square matrix
return None, "Non square matrix!"
if i == 0:
elementList = originalMatrix[i][1:]
else:
for j in xrange(len(originalMatrix[i])):
if j == 0:
elementListColumns.append(originalMatrix[i][0])
else:
boolMatrix[i - 1][j - 1] = originalMatrix[i][j]
# for the moment, it will only work with symmetrical matrices
if elementListColumns != elementList: return None, None, "Rows and columns do not match!"
for i in xrange(len(boolMatrix)):
if boolMatrix[i][i] != 1: return None, None, "Main diagonal different than unity!"
# end of matrix arrangements
return boolMatrix, elementList, "Ok"
#------------------------------------------------------
def getClusters(originalMatrix):
# sample for debugging
# m = [[None, "a", "b", "c", "d", "e"], ["a", 1, 1, 0, 0, 0], ["b", 1, 1, 0, 0, 0], ["c", 0, 1, 1, 0, 0], ["d", 0, 0, 0, 1, 0], ["e", 1, 0, 0, 0, 1]]
# clusterList should be [['a', 'b', 'c', 'e'], ['d']]
# clex = [['regulation of cell-matrix adhesion', 'positive regulation of angiogenesis', 'regulation of cell morphogenesis involved in differentiation'], ['regulation of actin filament polymerization', 'regulation of actin cytoskeleton organization', 'regulation of protein complex assembly'], ['negative regulation of NF-kappaB transcription factor activity'], ['regulation of mitochondrial membrane potential', 'regulation of fibroblast proliferation', 'regulation of homeostatic process'], ['positive regulation of reactive oxygen species metabolic process'], ['neuromuscular processcontrolling balance', 'neuromuscular process'], ['retina development in camera-type eye', 'axon guidance'], ['regulation of ion homeostasis', 'regulation of homeostatic process'], ['heart development', 'muscle cell development'], ['actin filament-based movement']]
boolMatrix, headers, message = splitMatrixWithHeaders(originalMatrix)
clusterList = [[]]
for i in xrange(len(boolMatrix)):
parentAdded = False
parent = headers[i]
for j in xrange(len(boolMatrix[i])):
if i != j:
if int(boolMatrix[i][j]) == 1:
child = headers[j]
# if neither parent nor child are present, create new cluster
# if they both are present, do nothing
# if only one of them is present, add the other
createNewCluster = True
for c in clusterList:
if parent in c and child in c:
createNewCluster = False
parentAdded = True
break
if parent in c and not child in c:
c.append(child)
createNewCluster = False
parentAdded = True
break
if child in c and not parent in c:
c.append(parent)
createNewCluster = False
parentAdded = True
break
if createNewCluster:
clusterList.append([parent, child])
parentAdded = True
if not parentAdded:
for c in clusterList:
if parent in c: parentAdded = True
if not parentAdded:
clusterList.append([headers[i]])
clusterList = clusterList[1:] # removing the initial void cluster
# sometimes as clusters "grow" independently,
# it is seen that two clusters are different parts of the same
# cluster; the next lines are provided to merge these two parts
# that can occur occasionally
initialCheck = True
clustersToMerge = None
while clustersToMerge or initialCheck:
initialCheck = False
clustersToMerge = detectClustersToMerge(clusterList)
if clustersToMerge:
clusterList = mergeClusters(clusterList, clustersToMerge[0], clustersToMerge[1])
return clusterList, message
#------------------------------------------------------
def mergeClusters(clusterList, i, j):
newCluster = clusterList[i][:]
for clusterElement in clusterList[j]:
if not clusterElement in newCluster: newCluster.append(clusterElement)
newClusterList = []
for k in xrange(len(clusterList)):
if k != i and k != j:
newClusterList.append(clusterList[k])
newClusterList.append(newCluster)
return newClusterList
#------------------------------------------------------
def detectClustersToMerge(clusterList):
# as soon as a couple is detected,
# this method provides those two clusters
for i in xrange(len(clusterList)):
for clusterElement1 in clusterList[i]:
for j in xrange(len(clusterList)):
if i != j:
for clusterElement2 in clusterList[j]:
if clusterElement1 == clusterElement2:
return [i, j]
return None
#------------------------------------------------------
def associateElements(inStats = "", uFile = "", relFile = ""):
results = []
relations = stats.loadRelationsFile(relFile)
relations = stats.sortByIndex(relations, 0)
statsData = stats.loadStatsDataFile(inStats)
ZijList = []
for element in statsData:
ZijList.append([element[3], element[7]])
theorList = []
experList = []
N = len(ZijList)
for i in xrange(N):
theorList.append([ZijList[i][0], ZijList[i][1], norm.cdf(float(ZijList[i][1]))])
experList.append([ZijList[i][0], ZijList[i][1], (float(i) + 0.5) / float(N)])
higherElements = stats.load2stringList(uFile, removeCommas = True)
# WARNING! higherElements must be a list of lists
# with each sublist being id, n, Z, FDR, X
elementList = []
if higherElements[0] == ['id', 'Z', 'n']:
# this means the list comes from SanXoTSqueezer
# so the header and the extra columns have to be removed
for element in higherElements[1:]:
# switch to id, n, Z, FDR
elementList.append([element[0], element[2], element[1], float("nan"), float("nan")])
if higherElements[0] == ['id', 'n', 'Z', 'FDR']:
# this means it does not contain X, so a nan is put on its place
for element in higherElements[1:]:
elementList.append([element[0], element[1], element[2], element[3], float("nan")])
if higherElements[0] == ['id', 'n', 'Z', 'FDR', 'X']:
for element in higherElements[1:]:
elementList.append([element[0], element[1], element[2], element[3], element[4]])
# otherwise
if higherElements[0] != ['id', 'Z', 'n'] and higherElements[0] != ['id', 'n', 'Z', 'FDR'] and higherElements[0] != ['id', 'n', 'Z', 'FDR', 'X']:
for element in higherElements:
elementList.append([element[0], float("nan"), float("nan"), float("nan"), float("nan")])
statsData = stats.sortByIndex(statsData, 7)
relationsFirstColumn = stats.extractColumns(relations, 0)
relationsSecondColumn = stats.extractColumns(relations, 1)
experListFirstColumn = stats.extractColumns(experList, 0)
for uElement in elementList:
lowerElementList = []
first = stats.firstIndex(relationsFirstColumn, uElement[0])
if first > -1: # -1 means it is not in the list
notInList = 0
last = stats.lastIndex(relationsFirstColumn, uElement[0])
lowerElements = relationsSecondColumn[first:last + 1] # "+1" is to include the last one
for element in lowerElements:
lowerIndex = stats.firstIndex(experListFirstColumn, element)
if lowerIndex > -1: # -1 means it is not in the list
lowerElementList.append(element)
else:
notInList += 1
lowerElementList = stats.sortByIndex(lowerElementList, 0)
results.append([uElement[0], lowerElementList])
else:
if len(uElement[0].strip()) > 0:
results.append([uElement[0], None])
return results, elementList, ""
#------------------------------------------------------
def clustersWithElements(cluster, NElements):
counter = 0
for i in xrange(len(cluster)):
if len(cluster[i]) >= NElements: counter += 1
return counter
#------------------------------------------------------
def getBestFNumber(similarityMatrix,
stepFNumber = 0.1,
initialFNumber = 0.0,
finalFNumber = 1.0,
verbose = False):
if initialFNumber < 0.0: initialFNumber = 0.0
if finalFNumber > 1.0: finalFNumber = 1.0
if stepFNumber <= 0.0: stepFNumber = 0.1
frange = stats.forstep(initialFNumber, finalFNumber, stepFNumber)
FNumberArray = []
bestCNumber = 0
bestFNumber = 1.0
bestBooleanSimMatrix = []
bestClusterVector = []
for FNumber in frange:
booleanSimMatrix = stats.booleaniseMatrix(similarityMatrix, threshold = FNumber)
clusterVector, message = getClusters(booleanSimMatrix)
CNumber = getCNumber(clusterVector)
if CNumber > bestCNumber:
bestCNumber = CNumber
bestFNumber = FNumber
FNumberArray = [FNumber]
else:
if CNumber == bestCNumber:
FNumberArray.append(FNumber)
if verbose: print "FNumber = %f: there are at least %i clusters with %i elements." % (FNumber, CNumber, CNumber)
FNumberArray.sort()
medianFNumber = FNumberArray[(len(FNumberArray) - 1) / 2]
booleanSimMatrix = stats.booleaniseMatrix(similarityMatrix, threshold = medianFNumber)
bestClusterVector, message = getClusters(booleanSimMatrix)
return medianFNumber, bestBooleanSimMatrix, bestClusterVector, bestCNumber
#------------------------------------------------------
def getCNumber(cluster):
# cl = [["a", "b", "c"], ["d", "e"], ["f", "g"]]
# for debugging, output is 2
# cl = [["a", "b", "c"], ["d", "e"], ["f", "g"], ["h", "i", "j"], ["k", "l", "m", "n"]]
# for debugging, output is 3
tentativeCNumber = len(cluster)
CNumber = 0
while tentativeCNumber > 0:
if clustersWithElements(cluster, tentativeCNumber) >= tentativeCNumber:
CNumber = tentativeCNumber
break
tentativeCNumber -= 1
return CNumber
#------------------------------------------------------
def getParetoInfo(clusterVector = None, extraData = None):
paretoInfo = []
extraDataWithClusters = extraData[:]
if clusterVector and extraData:
for i in xrange(len(clusterVector)):
clusterProvisionalList = []
dataList = []
clusterName = "Cluster #%i" % i
for clusterElement in clusterVector[i]:
nValue = float("nan")
extraDataIndex = stats.firstIndex(stats.extractColumns(extraDataWithClusters, 0), clusterElement)
if str(extraData[extraDataIndex][1]).lower() != "nan":
nValue = int(extraData[extraDataIndex][1])
clusterProvisionalList.append([extraDataIndex,
nValue, # n
float(extraData[extraDataIndex][4]), #X
extraData[extraDataIndex][0]]) # id
dataList.append([nValue,
abs(float(extraData[extraDataIndex][4]))])
for clusterElement in clusterProvisionalList:
dataPoint = [clusterElement[1], abs(clusterElement[2])] # {n, X}
extraDataWithClusters[clusterElement[0]].extend([clusterName, stats.isParetoFront(dataPoint, dataList)])
paretoInfo.append([extraDataWithClusters[clusterElement[0]][0], extraDataWithClusters[clusterElement[0]][6]])
return paretoInfo, extraDataWithClusters
#------------------------------------------------------
def printHelp(version = None):
print """
Sanson %s is a program made in the Jesus Vazquez Cardiovascular
Proteomics Lab at Centro Nacional de Investigaciones Cardiovasculares, used to
generate the similarity graph of a set of categories.
A similarity graph is a graph that shows the relationship between a set of
categories by taking into account how many proteins they share. This is
measured with a variable f such that for categories c1 and c2, we get:
f(c1, c2) = (#proteins shared by c1 and c2) / (#proteins of c1)
for instance:
* if c1 == c2, we get f(c1, c2) = f(c2, c1) = 1;
* if c1 and c2 do not share any proteins, we get f(c1, c2) = f(c2, c1) = 0;
* if c2 is contained in c1, we get f(c1, c2) <= 1, f(c2, c1) = 1, etc
If no f number is given with the parametres (-e), then the program
automatically calculates the best f number, by maximising both the number of
category clusters and the number categories within each cluster.
Sanson needs three input files:
* a stats file, the outStats file from SanXoT (using the -z command)
* a higher level list to graph (using the -c command)
* a relations file (using -r command)
And delivers five output files:
* the graph in PNG format (default suffix: "_simGraph.png")
* the DOT language text file used to generate the graph (default suffix:
"_simGraph.gv")
* a table showing the clusters generated (default suffix: "_outClusters")
* the similarity matrix used to generate the graph (default suffix:
"_outSimilarities")
* a log file (default suffix: "_logFile")
Usage: sanson.py -z[stats file] -r[relations file] -c[higher level list file] [OPTIONS]
-h, --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
stats file.
-b, --nosubstats To avoid colouring the boxes according to the proteins
that are in the concerning category (in this case, the
box is coloured using the Zij of the category, when this
information is available in the higher level list to
graph, see -c command).
-c, --list=filename The text file containing the higher level elements whose
categories we want to relate. If the first element is
not taken, it might help saving the file with ANSI
format. If a header is used, then it must be in the form
"id>n>Z>FDR" or "id>Z>n" (where ">" means "tab").
-d, --dotfile=filename
To use a non-default name for the text file in DOT
language, which is used to generate the graph.
-e, --similarity=float
To override the calculation of the optimal f number (see
above for more details).
-g, --graphformat=string
File format for the similarity graph (default is "png").
-G, --outgraph=filename
To use a non-default name for the graph file.
-l, --graphlimits=integer
To set the +- limits of the most intense red/green
colours in the graph (default is 6).
-L, --logfile=filename
To use a non-default name for the log file.
-m, --simfile=string
To use a non-default name for the similarity matrix
file.
-N, --altmax=integer
Maximum number of lower level elements that the alt text
of the higher level node will show per side. For
instance, for N = 3, alt text will show all the elements
up to six; beyond this, only the first and last three
will be shown. (Default is N = 5.) (Note that this will
have effect if the SVG format is used.)
-p, --place, --folder=foldername
To use a different common folder for the output files.
If this is not provided, the the folder used will be the
same as the stats file 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.
-s, --outcluster=filename
To use a non-default name for the file containg the
list of clusters.
-z, --outstats=filename
The outStats file from a SanXoT integration.
--graphdpi=float To define the resolution (in DPI, dots per inch) for the
output graph. (Default is 96.0)
--graphratio=float To define the height/width ratio in the output graph.
(Default is 0.0, which means the ratio is not adjusted,
si the ratio is automatically set by graphviz)
--minfontsize=float To define the minimum font size in nodes. If larger than
maxfontsize, the maxfontsize will be used (so
minfontsize = maxfontsize). (Default is 10.0)
--maxfontsize=float To define the maximum font size in nodes.
(Default is 70.0)
--nonparetoopacity=float
To "downlight" nodes not part of the Pareto front.
(default = 0.5, 0.0 means node color = background,
1.0 means no difference between Pareto front and
non-Pareto font)
--selectednodecolor=#rrggbb, --selectednodecolour=#rrggbb
--defaultnodecolor=#rrggbb, --defaultnodecolour=#rrggbb
--defaultnodetextcolor=#rrggbb, --defaultnodetextcolour=#rrggbb
--errornodecolor=#rrggbb, --errornodecolour=#rrggbb
--middlecolor=#rrggbb, --middlecolour=#rrggbb
--mincolor=#rrggbb, --mincolour=#rrggbb
--maxcolor=#rrggbb, --maxcolour=#rrggbb
""" % version
return
#------------------------------------------------------
def main(argv):
version = "v1.13"
verbose = False
similarityLimit = -1.0 # if remain as -1, it will be calculated
graphLimits = 6.0
analysisName = ""
useSubStats = True
defaultAnalysisName = "sanxot"
analysisFolder = ""
# input files
inStats = ""
defaultStatsFile = "stats"
defaultRelationsFile = "rels"
defaultTableExtension = ".tsv"
defaultTextExtension = ".txt"
defaultDOTExtension = ".gv"
relationsFile = ""
upperLevelToGraphFile = ""
# output files
defaultUpperLevelToGraphFile = "ulst"
defaultOutputGraph = "simGraph"
defaultLogFile = "logFile"
defaultSimilarityMatrixFile = "outSimilarities"
defaultOutputGVFileName = "simGraph"
defaultOutputClusterFileName = "outClusters"
logFile = ""
graphFile = ""
dotFile = ""
outCluster = ""
similarityMatrixFile = ""
graphFileFormat = "png"
altMax = 5
selectedNodeColour = "#ff9090"
defaultNodeColour = "#ffff80"
errorNodeColour = "#8080ff"
minColour = "#00ff00"
middleColour = "#ffffff"
maxColour = "#ff0000"
defaultNodeTextColour = "#000000"
nonParetoOpacity = 0.5
minFontSize = 10.0
maxFontSize = 70.0
graphDPI = 96.0
graphRatio = 0.0
logList = [["Sanson " + version], ["Start: " + strftime("%Y-%m-%d %H:%M:%S")]]
try:
opts, args = getopt.getopt(argv, "a:p:z:r:c:L:G:m:l:d:e:s:d:g:N:bhk", ["analysis=", "folder=", "place=", "statsfile=", "relfile=", "relationsfile=", "list=", "logfile=", "graphfile=", "simfile=", "graphlimits=", "similarity=", "dotfile=", "outcluster=", "graphformat=", "altmax=", "selectednodecolour=", "selectednodecolor=", "defaultnodecolour=", "defaultnodecolor=", "defaultnodetextcolour=", "defaultnodetextcolor=", "errornodecolour=", "errornodecolor=", "mincolour=", "mincolor=", "middlecolour=", "middlecolor=", "maxcolour=", "maxcolor=","nonparetoopacity=", "minfontsize=", "maxfontsize=", "graphdpi=", "graphratio=", "nosubstats", "help", "egg", "easteregg"])
except getopt.GetoptError:
logList.append(["Error while getting parameters."])
sys.exit(2)
if len(opts) == 0:
printHelp(version)
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 ("-z", "--statsfile"):
inStats = arg
elif opt in ("-r", "--relfile", "--relationsfile"):
relationsFile = arg
elif opt in ("-c", "--list"):
upperLevelToGraphFile = arg
elif opt in ("-L", "--logfile"):
logFile = arg
elif opt in ("-G", "--graphfile"):
graphFile = arg
elif opt in ("-m", "--simfile"):
similarityMatrixFile = arg
elif opt in ("-l", "--graphlimits"):
graphLimits = float(arg)
elif opt in ("-e", "--similarity"):
similarityLimit = float(arg)
elif opt in ("-d", "--dotfile"):
dotFile = float(arg)
elif opt in ("-s", "--outcluster"):
outCluster = float(arg)
elif opt in ("-b", "--nosubstats"):
useSubStats = False
elif opt in ("--nonparetoopacity"):
nonParetoOpacity = float(arg)
elif opt in ("-N", "--altmax"):
altMax = int(arg)
elif opt in ("-g", "--graphformat"):
graphFileFormat = arg.lower().strip()
if graphFileFormat == "jpeg": graphFileFormat = "jpg"
if graphFileFormat != "png" and \
graphFileFormat != "svg" and \
graphFileFormat != "jpg" and \
graphFileFormat != "tif" and \
graphFileFormat != "tiff" and \
graphFileFormat != "pdf" and \
graphFileFormat != "bmp" and \
graphFileFormat != "gif":
print
print "Warning: graph format \"%s\" is not supported,\npng will be used instead." % graphFileFormat
print
graphFileFormat = "png"
elif opt in("--selectednodecolour", "--selectednodecolor"):
selectedNodeColour = arg