forked from albertwcheng/albert-bioinformatics-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
albertcommon.py
executable file
·1033 lines (805 loc) · 22.2 KB
/
albertcommon.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
'''
albertcommon.py
A module containing all the common functions for the scripts
Copyright 2010 Wu Albert Cheng <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
from math import floor
import sys
import types
import re
import os
from ConfigParser import ConfigParser
from random import shuffle,randint
def findNextCombinationOfIdx(N,k,idxV):
for i in range(k-1,-1,-1):
if idxV[i]<N-k+i:
idxV[i]+=1
#reset the higher levels:
for j in range(i+1,k):
idxV[j]=idxV[j-1]+1
return True
return False
def findAllCombinations(X,k): #find all k-subsets of X
combinations=[]
idxV=[]
N=len(X)
#init idxV
for i in range(0,k):
idxV.append(i)
#this is the first combination
combinations.append(getSubvector(X,idxV))
#now proceed to find other combinations
while findNextCombinationOfIdx(N,k,idxV):
combinations.append(getSubvector(X,idxV))
return combinations
def NextCombinationOn(X,k,idxV=None):
N=len(X)
if idxV==None: #get the first combination and return the idxV new
idxV=[]
for i in range(0,k):
idxV.append(i)
elif not findNextCombinationOfIdx(N,k,idxV):
return None,None
return getSubvector(X,idxV),idxV
def String_findAll(s,sub,start=0,end=-1):
if end!=-1:
endSearch=end
else:
endSearch=len(s)
curStart=start
locations=[]
while True:
idx=s.find(sub,curStart,endSearch)
if idx<0:
break
locations.append(idx)
curStart=idx+1
return locations
def getAttrWithDefaultValue(attrMatrix,attrName,defaultValue):
try:
return attrMatrix[attrName]
except KeyError:
return defaultValue
def getLevel2AttrWithDefaultValue(attrMatrix,targetName,attrName,defaultValue):
try:
return attrMatrix[targetName][attrName]
except KeyError:
return defaultValue
def readNamedAttrMatrix(filename):
'''
!optkey1 optvalue1
!optkey2 optvalue2
Target AttrName1 AttrName2 ...
target1 value(1,1) ...
target2 ... ...
:::::
'''
attrMatrix=dict()
fil=open(filename)
lino=0
for lin in fil:
lin=lin.rstrip("\r\n")
if len(lin)<1:
continue
elif lin[0]=='#':
continue #commented lines
elif lin[0]=='!':
key,value=lin.split("\t") #keep the exclamation mark.
attrMatrix[key]=value
continue
lino+=1 #only if row is not empty and is not !-options, increment lino
fields=lin.split("\t")
if lino==1:
attrNames=fields[1:]
else:
targetName=fields[0]
targetDict=dict()
attrMatrix[targetName]=targetDict
for attrName,value in zip(attrNames,fields[1:]):
targetDict[attrName]=value
fil.close()
return attrMatrix
def RE_findOverlappingInstancesOfCompiledRegex(there, thestring):
#total = 0
start = 0
#there = re.compile(pattern)
locations=[]
while True:
mo = there.search(thestring, start)
if mo is None:
return locations
#total += 1
moStart=mo.start()
moEnd=mo.end()
locations.append((moStart,moEnd))
start = 1 + moStart
return locations
def RE_findOverlappingInstancesOfRegexString(pattern, thestring):
return RE_findOverlappingInstancesOfCompiledRegex(re.compile(pattern),thestring)
def generic_istream(filename):
#print >> stderr,"opening ",filename
if filename=="-":
return sys.stdin
else:
return open(filename)
def medianOfSortedList(L):
lList=len(L)
if lList%2==0:
return (L[lList/2-1]+L[lList/2])/2.0
else:
return L[lList/2]
def sortedList(L):
LC=L[:]
LC.sort()
return LC
def multiplyVector(L,by):
LC=[]
for x in L:
LC.append(x*by)
return LC
def vectorComparator(v1,v2):
try:
for v1v,v2v in zip(v1,v2):
if v1v<v2v:
return -1
elif v1v>v2v:
return 1
except IndexError:
if len(v1v)<len(v2v):
return -1
elif len(v1v)>len(v2v):
return 1
else:
#strange
print >> sys.stderr,"strange",v1,v2
return 0
return 0
def SecondFieldComparator(x,y):
if x[1]<y[1]:
return -1
elif x[1]>y[1]:
return 1
else:
return 0
def ToIndexedTuples(L):
IT=[]
for i in range(0,len(L)):
IT.append([i,L[i]])
return IT
def rank(L,tieDivide):
RANK=L[:]
IT=ToIndexedTuples(L)
IT.sort(SecondFieldComparator)
#print >> sys.stderr,"IT is",IT
if tieDivide:
crank=0
divider=1
startI=0
for curI in range(0,len(IT)+1):
if curI!=len(IT):
it=IT[curI]
curValue=it[1]
if curI==0:
#startI=curI
#divider=1
prev=curValue
continue #nothing else to do
if prev!=curValue or curI==len(IT):
if divider>1:
thisRank=float(2*(crank+1)+divider-1)/2
else:
thisRank=crank+1
for setI in range(startI,curI):
RANK[IT[setI][0]]=thisRank
if curI==len(IT):
break
crank+=divider
startI=curI
divider=1
prev=curValue
else:
divider+=1
else:
for i,it in zip(range(0,len(IT)),IT):
origindex,value=it
RANK[origindex]=i+1
return RANK
def StrListToFloatList(L):
LC=[]
for s in L:
LC.append(float(s))
return LC
def randomChooseK(X,k):
combination=[]
lowerchoice=0
N=len(X)
for i in range(0,k):
thischoice=randint(lowerchoice,N-k+i)
combination.append(X[thischoice])
lowerchoice=thischoice+1
return combination
def advRandomChooseK(X,k):
combination=[]
loconstraint=[]
hiconstraint=[]
N=len(X)
posV=range(0,k)
for i in posV:
loconstraint.append(i)
hiconstraint.append(N-k+i)
#now shuffle
shuffle(posV)
for i in posV:
#choose one from the constrained interval
choice=randint(loconstraint[i],hiconstraint[i])
combination.append(X[choice])
hiconstraint[i]=choice
loconstraint[i]=choice
#now update the -1 and +1 constraint
j=i #propagate down
while j>0:
hiconstraint[j-1]=min(loconstraint[j]-1,hiconstraint[j-1])
j-=1
j=i
while j<k-1:
loconstraint[j+1]=max(hiconstraint[j]+1,loconstraint[j+1])
j+=1
return combination
def advRandomChooseKNoRepeat(X,k,history,trials=1000):
combination=advRandomChooseK(X,k)
tcombination=tuple(combination)
T=1
while tcombination in history:
T+=1
if T>trials:
return None
combination=advRandomChooseK(X,k)
tcombination=tuple(combination)
history.add(tcombination)
return combination
def randomChooseKNoRepeat(X,k,history,trials=1000):
combination=randomChooseK(X,k)
tcombination=tuple(combination)
T=1
while tcombination in history:
T+=1
if T>trials:
return None
combination=randomChooseK(X,k)
tcombination=tuple(combination)
history.add(tcombination)
return combination
def randomShuffleNoRepeat(X,history,trials=1000):
T=0
while tuple(X) in history:
T+=1
if T>trials:
return False
shuffle(X)
history.add(tuple(X))
return True
def toStrList(L):
LC=[]
for i in L:
LC.append(str(i))
return LC
def medianOfList(L):
return medianOfSortedList(sortedList(L))
def selectListByIndexVector(L,I):
newL=[]
try:
for i in I:
newL.append(L[i])
return newL
except:
return []
def percentileOfList(p,L):
LSorted=L[:]
LSorted.sort()
l=len(LSorted)
i=int(round(float(l-1)*p))
return LSorted[i]
def countIfInRangeInc(low,hi,L):
c=0
for x in L:
if low<=x and x<=hi:
c+=1
return c
def countIf(needles,L):
c=0
for x in L:
if x in needles:
c+=1
return c
def divMod(num,div):
k=int(num)/int(div)
d=int(num)%int(div)
return k,d
def dissociatekd(n):
k=floor(n)
d=n-k
return k,d
def percentilesOfSortedList(L,precentiles):
N=len(L)
RESULT=[]
for percentile in precentiles:
n=float(percentile)/100*(N-1)+1
k,d=dissociatekd(n)
if k==1:
val=L[0]
elif k==N:
val=L[N-1]
else:
val=L[int(k-1)]+d*(L[int(k)]-L[int(k-1)])
RESULT.append(val)
return RESULT
def percentilesOfList(L,percentiles):
return percentilesOfSortedList(sortedList(L),percentiles)
def outlierBounds(L):
LQ,UQ=percentilesOfList(L,[25,75])
IQ=UQ-LQ
return LQ-1.5*IQ,UQ+1.5*IQ
def rangeListFromRangeString(rangestring,transform):
rangeValues=[]
rangestring=rangestring.strip()
if rangestring=='0' or rangestring=='-' or rangestring=="":
return rangeValues #mod 3/31/2009
col1splits=rangestring.split(",")
for col1split in col1splits:
col1rangesplit=col1split.split("-")
if len(col1rangesplit)==1:
rangeValues.append(int(col1rangesplit[0])+transform)
else:
try:
for coltoAdd in range(int(col1rangesplit[0])+transform,int(col1rangesplit[1])+1+transform):
rangeValues.append(coltoAdd)
except ValueError:
pass
return rangeValues
def multiColJoinField(headerFields,val):
#print >> sys.stderr, val
splitons=val.split("%")
#print >> sys.stderr,splitons
filename=splitons[0]
if len(splitons)>1:
sep=replaceSpecialChar(splitons[1])
else:
sep="\t"
istream= generic_istream(filename)
fieldsToInclude=[]
for line in istream:
line=line.strip("\r\n")
fieldsToInclude.extend(line.split(sep))
indices=dict()
indRange=range(0,len(headerFields))
for i,headerF in zip(indRange,headerFields):
if headerF in fieldsToInclude:
if not indices.has_key(headerF):
indices[headerF]=[i]
else:
indices[headerF].append(i)
istream.close()
#print >> sys.stderr,indices
indicesOut=[]
for fieldToInclude in fieldsToInclude:
try:
indicesOut.extend(indices[fieldToInclude])
except KeyError:
pass
#print >> sys.stderr,indicesOut
return indicesOut
def rangeListFromRangeStringAdv(headerFields,rangestring,transform):
rangeValues=[]
rangestring=rangestring.strip()
if rangestring=='0' or rangestring=='-' or rangestring=="":
return rangeValues #mod 3/31/2009
sortMode=0 #0:no sort, 1: ascending, -1: descending
col1splits=rangestring.split(",")
for curColSplitI in range(0,len(col1splits)):
col1split=col1splits[curColSplitI]
col1rangesplit=col1split.split("-")
for i in range(0,len(col1rangesplit)):
#print col1rangesplit[i],
col1rangesplit[i]=replaceSpecialChar(col1rangesplit[i])
#print col1rangesplit[i]
if len(col1rangesplit)==1:
director=col1rangesplit[0][0]
if director in [".","@","%"]: #it's a header string!
if director=='.':
indices=multiColIndicesFromHeaderMerged(headerFields,[col1rangesplit[0][1:]])
elif director=="%":
indices=multiColJoinField(headerFields,col1rangesplit[0][1:])
else:
p=re.compile(col1rangesplit[0][1:])
indices=multiColIndicesFromHeaderMerged(headerFields,[p])
if(len(indices)==0):
print >> sys.stderr,"Error:",col1rangesplit[0][1:],"not found/matched in header"
sys.exit()
for inx in indices:
rangeValues.append(inx+1+transform)
elif director=='_':
rangeValues.append(len(headerFields)-int(col1rangesplit[0][1:])+1+transform)
elif director=='a':
sortMode=1
elif director=='d':
sortMode=-1
elif director=='+':
rangeValues.extend(range(0,len(headerFields)))
elif director=='x':
#added 7/15/2010 for excel styled column id
rangeValues.append(excelIndxToCol0(col1rangesplit[0][1:])+1+transform) #converted back to 1-based and then do transform
else:
rangeValues.append(int(col1rangesplit[0])+transform)
else: # x-x a range!
try:
#handle the left value of the bound
if col1rangesplit[0][0]=='.':
#print >> sys.stderr, headerFields
indices=multiColIndicesFromHeaderMerged(headerFields,[col1rangesplit[0][1:]])
if(len(indices)==0):
print >> sys.stderr,"Error:",col1rangesplit[0][1:],"not found in header"
sys.exit()
rangeL=min(indices)+1+transform
elif col1rangesplit[0][0]=='@': #added 12/10/2009
p=re.compile(col1rangesplit[0][1:])
indices=multiColIndicesFromHeaderMerged(headerFields,[p])
if(len(indices)==0):
print >> sys.stderr,"Error:",col1rangesplit[0][1:],"not matched in header"
sys.exit()
rangeL=min(indices)
elif col1rangesplit[0][0]=='_': #from the back added 12/10/2009
rangeL=len(headerFields)-int(col1rangesplit[0][1:])+1+transform
elif col1rangesplit[0][0]=='x':
#added 7/15/2010 for excel styled column id
rangeL=excelIndxToCol0(col1rangesplit[0][1:])+1+transform #converted back to 1-based and then do transform
else:
rangeL=int(col1rangesplit[0])+transform
#handle the right value of the bound
if col1rangesplit[1][0]=='.':
indices=multiColIndicesFromHeaderMerged(headerFields,[col1rangesplit[1][1:]])
if(len(indices)==0):
print >> sys.stderr,"Error:",col1rangesplit[1][1:],"not found in header"
sys.exit()
rangeH=max(indices)+2+transform
elif col1rangesplit[1][0]=='@':
p=re.compile(col1rangesplit[1][1:])
indices=multiColIndicesFromHeaderMerged(headerFields,[p])
if(len(indices)==0):
print >> sys.stderr,"Error:",col1rangesplit[1][1:],"not matched in header"
sys.exit()
rangeH=max(indices)+2+transform
elif col1rangesplit[1][0]=='_': #from the back added 12/10/2009
rangeH=len(headerFields)-int(col1rangesplit[1][1:])+2+transform
elif col1rangesplit[1][0]=='x':
#added 7/15/2010 for excel styled column id
rangeH=excelIndxToCol0(col1rangesplit[1][1:])+2+transform #converted back to 1-based and then do transform
else:
rangeH=int(col1rangesplit[1])+1+transform
if rangeL>=rangeH:
step=-1
rangeH-=2
else:
step=1
#print >> sys.stderr,rangeL,rangeH,step
for coltoAdd in range(rangeL,rangeH,step):
rangeValues.append(coltoAdd)
except ValueError:
pass
if sortMode==1:
rangeValues.sort()
elif sortMode==-1:
rangeValues.sort()
rangeValues.reverse()
return rangeValues
def meanOfList(L):
#print L
return float(sum(L))/len(L)
def removeOutliers(L):
if len(L)<3:
return
LB,UB=outlierBounds(L)
for i in range(len(L)-1,-1,-1):
if L[i]>UB or L[i]<LB:
del L[i]
def getCol0ListFromCol1ListString(col1ValueString):
return rangeListFromRangeString(col1ValueString,-1)
def getCol0ListFromCol1ListStringAdv(headerFields,col1ValueString):
return rangeListFromRangeStringAdv(headerFields,col1ValueString,-1)
def sortu(L):
return uniq(sortedList(L))
def uniq(L):
LKey=[]
UL=[]
for l in L:
if l not in LKey:
LKey.append(l)
UL.append(l)
return UL
MAX_INT=sys.maxint
MIN_INT=-MAX_INT-1
specialCharMap={ "\\t":"\t",
"\\n":"\n",
"\\r":"\r",
"^t":"\t",
"^b":" ",
"^s":"<",
"^g":">",
"^c":",",
"^d":".",
"^^":"^",
"^M":"$",
"^m":"-",
"^o":":",
"^S":"\\"
}
def replaceSpecialChar(S):
for k,v in specialCharMap.items():
#print >> sys.stderr, k#+"<"+S,
S=S.replace(k,v)
#print >> sys.stderr, S
return S
def mapSpecialChar(c,default):
try:
return specialCharMap[c]
except KeyError:
return default
def colIndicesFromHeader(headerFields,selectors):
colIndices0=[]
for selector in selectors:
try:
colIndices.append(headerFields.index(selector))
except ValueError: #not found!
colIndices.append(-1)
return colIndices0
def indexAll(L,x):
indices=[]
start=0
if type(x) is types.StringType:
for i in range(0,len(L)):
if x==L[i]:
indices.append(i)
else: # assume regex
for i in range(0,len(L)):
if x.search(L[i]) is not None:
indices.append(i)
return indices
def multiColIndicesFromHeaderWithSelector(headerFields,selectors):
colIndices0=[]
for selector in selectors:
colIndices0.append([selector,indexAll(headerFields,selector)])
return colIndices0
def multiColIndicesFromHeaderMerged(headerFields,selectors):
colIndices0=[]
for selector in selectors:
colIndices0.extend(indexAll(headerFields,selector))
return colIndices0
def getHeader(filename,headerRow,startRow,FS):
try:
fil=generic_istream(filename)
except IOError:
print >> sys.stderr,"Cannot open",filename
return
header=[]
prestartRows=[]
c=0;
for lin in fil:
c+=1;
if(c==headerRow):
lin=lin.rstrip()
if FS=="":
header=lin.split()
else:
header=lin.split(FS)
if(c<startRow):
lin=lin.rstrip()
if FS=="":
prestartRows.append(lin.split())
else:
prestartRows.append(lin.split(FS))
fil.close()
return header,prestartRows
def explainColumns(stream):
print >> stream,"cols format:"
print >> stream,"\t* inserts all columns"
print >> stream,"\tnumber: 1-5,3"
print >> stream,"\tnumber preceded by '_' means from the end. _1 means last row, _2 second last"
print >> stream,"\tfield name preceded by '.': .exp-5,.pvalue-.fdr"
print >> stream,"\texcel column preceded by 'x': xB-xAA means column 2 to column 27"
print >> stream,"\tregex preceded by '@': @FDR[a-z]"
print >> stream,"\t%filename%sep or just %filename: open file which contains the fields to include. default separator is [TAB]"
print >> stream,"\tif last field is a. Then following columns are added as ascending. If appears at end, all are sorted ascending"
print >> stream,"\tis last field is d. Then following columns are added as descending. If appears at end, all are sorted descending"
def getSubvector(D,I,sortIndex=False):
subv=[]
nI=I
if sortIndex:
nI=I[:]
nI.sort()
for i in nI:
subv.append(D[i])
return subv
def getSubVectorByBinarySelector(D,I):
subv=[]
for i,d in zip(I,D):
if i:
subv.append(d)
return subv
def getSubVectorByReverseBinarySelector(D,I):
subv=[]
for i,d in zip(I,D):
if not i:
subv.append(d)
return subv
def toStrVector(V):
sV=[]
for v in V:
sV.append(str(v))
return sV
def excelColIndex(idx0):
k,d=divMod(idx0,26)
D=[d]
while k>=1:
k,d=divMod(k-1,26)
D.append(d)
outString=""
for d in D:
outString=chr(ord('A')+(d))+outString
return outString
def excelIndxToCol0(alphaString):
idx=0
multiplier=1
for i in range(len(alphaString)-1,-1,-1):
idx+=(ord(alphaString[i])-ord('A')+1)*multiplier
multiplier*=26
return idx-1
def testExcelString():
for idx0 in range(0,20000):
print >> stderr,(idx0+1),
excelString=excelColIndex(idx0)
print >> stderr,"excel="+excelString,
rcol0=excelIndxToCol0(excelString)
print >> stderr,"reverted="+str(rcol0+1)
if rcol0!=idx0:
print >> stderr,"error"
exit()
def basename(filename):
return os.path.basename(filename)
def cleanfilename(filename):
bn=basename(filename)
bn=bn.split(".")
if len(bn)>1:
del bn[-1]
return ".".join(bn)
#from [ [1,2,3],[1],[1,2] ] => [ [1,1,1],[1,1,2],[2,1,1],[2,1,2],[3,1,1],[3,1,2] ]
def findAllCombinations(ListOfLists):
numberOfPositions=len(ListOfLists)
numberOfCombinations=1
bounds=[]
current=[]
for L in ListOfLists:
numInList=len(L)
numberOfCombinations*=numInList
bounds.append(numInList)
current.append(0)
combinations=[]
current[-1]=-1
for cI in range(0,numberOfCombinations):
thisCombination=[]
#thisIndex=[]
combinations.append(thisCombination)
OK=False
indexI=numberOfPositions-1
while not OK:
if current[indexI]==bounds[indexI]-1:
#reset this and keep status to not OK
current[indexI]=0
else:
current[indexI]+=1
OK=True
#push current element to front
#print >> stderr,current
thisCombination.insert(0,ListOfLists[indexI][current[indexI]])
#thisIndex.insert(0,current[indexI])
indexI-=1
while indexI>=0:
thisCombination.insert(0,ListOfLists[indexI][current[indexI]])
indexI-=1
#print >> thisIndex
return combinations
def preprocessFileLoadableArgs_inner_formkeystring(key,extraPrefix=""):
prefix=""
if key[0]!="-":
if len(key)==1:
prefix="-"
else:
prefix="--"
key=key.replace("_","-")
key=key.replace(" ","-")
return prefix+extraPrefix+key
def preprocessFileLoadableArgs_helpmsg():
return """import args from files
--@import-args filename
import args from filename formatted <key><TAB><value>[[<TAB><value>]...]
--@import-cfg filename
import args from filename formatted as config file format
[section_name]
<key>=<value>[[<TAB><value>]...]
if section_name is main,default,defaults,or ' ', or key is already prefixed by "-", then key is not (additionally) prefixed
else key is prefixed by <section_name>-
all " ",_ in key are replaced by "-" """
def preprocessFileLoadableArgs(args=sys.argv):
i=0
newArgs=[]
while i<len(args):
avalue=args[i]
if avalue=="--@import-args":
#--import-args filename
#consume [i:i+2]
try:
filename=args[i+1]
except:
print >> stderr,"not enough arg for --import-args. Need --import-args <filename>"
raise ValueError
fil=open(filename)
for lin in fil:
lin=lin.rstrip("\r\n")
fields=lin.split("\t")
key=fields[0]
values=fields[1:]
#now append!
key=preprocessFileLoadableArgs_inner_formkeystring(key)
newArgs.append(key)
newArgs.extend(values)
fil.close()
i+=1
elif avalue=="--@import-cfg":
#--import-cfg configfilename
#consume [i:i+2]
try:
filename=args[i+1]
except:
print >> stderr,"not enough arg for --import-args. Need --import-cfg <configfilename>"
raise ValueError
configparser=ConfigParser()
configparser.read(filename)
sections=configparser.sections()
for section in sections: