-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileUtility.py
More file actions
executable file
·1311 lines (1155 loc) · 33.9 KB
/
FileUtility.py
File metadata and controls
executable file
·1311 lines (1155 loc) · 33.9 KB
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
#/usr/bin/python
import os, sys, string
##
#
#
# 11/30,02
# The file_to_dict utility is modified so new line character is dealt with. But
# this action may impact some other utilities. Need to find out.
##
class file_util:
#
# param file1 group file with [group_id][common_id]
# param file2 [common_id][whatever]
#
def get_groups(self,file1,file2):
# gdict = {group_id:[common_id]}
gdict = {}
inp = open(file1)
inl = inp.readline()
while inl != "":
L = self.rmlb(inl).split("\t")
if L[0] not in gdict:
gdict[L[0]] = [L[1]]
else:
gdict[L[0]].append(L[1])
inl = inp.readline()
# cdict = {common_id:[whatever...]}
cdict = {}
inp = open(file2)
inl = inp.readline()
while inl != "":
L = self.rmlb(inl).split("\t")
if L[0] not in cdict:
cdict[L[0]] = [L[1]]
else:
cdict[L[0]].append(L[1])
inl = inp.readline()
# output by group
for i in gdict:
oup = open("group_%s" % i,"w")
# iterate common id
for j in gdict[i]:
# iterate whatever
for k in cdict[j]:
oup.write("%s\t%s\n" % (j,k))
print ("Done!")
def merge_list(self,file1,file2):
print ("Read:",file1)
inp = open(file1)
oup = open("%s.with.%s" % (file1,file2),"w")
mdict = {}
inl = inp.readline()
while inl != "":
mdict[self.rmlb(inl)] = 0
oup.write(inl)
inl = inp.readline()
print (" %i entries" % len(mdict.keys()))
print ("\nRead:",file2)
inp = open(file2)
inl = inp.readline()
add = 0
while inl != "":
if self.rmlb(inl) not in mdict:
add += 1
mdict[self.rmlb(inl)] = 0
oup.write(inl)
inl = inp.readline()
print (" %i added" % add)
print ("Done!")
def join(self,file1,file2):
print ("Generate dict:", file1)
inp = open(file1)
dict1 = {}
inl = inp.readline()
count1 = 0
while inl != "":
L = self.rmlb(inl).split("\t")
if not dict1.has_key(L[0]):
dict1[L[0]] = L[1]
count1 += 1
inl = inp.readline()
print ("Join with:", file2)
inp = open(file2)
oup = open("merged","w")
inl = inp.readline()
count2 = 0
countJ = 0
while inl != "":
L = self.rmlb(inl).split("\t")
count2 += 1
if dict1.has_key(L[0]):
try:
L.append(dict1[L[0]])
oup.write("%s\n" % string.joinfields(L,"\t"))
except TypeError:
print (L)
countJ += 1
inl = inp.readline()
print ("File1 entries :",count1)
print ("File2 entries :",count2)
print ("Joined entries:",countJ)
print ("Done!")
# swap the first two columns
def swap_col(self,target):
inp = open(target)
oup = open(target+".swap","w")
inl = inp.readline()
while inl != "":
L = self.rmlb(inl).split("\t")
L0 = L[0]
L1 = L[1]
L[1] = L0
L[0] = L1
oup.write("%s\n" % string.joinfields(L,"\t"))
inl = inp.readline()
#
# Delete columns
#
def del_col(self,inf,col):
inp = open(inf)
oup = open(inf+".dcol","w")
inl = inp.readline()
col = col.split(",")
# delete col with larger index first, so reverse col
tmp = []
for i in col:
tmp.append(int(i))
tmp.reverse()
col = tmp
while inl != "":
L = self.rmlb(inl).split("\t")
#print L
if L != [""]:
for j in col:
#print j,L
del(L[j])
oup.write("%s\n" % (string.joinfields(L,"\t")))
inl = inp.readline()
print ("Done!")
#
# Delete all lines containing the passed names
#
def del_line(self,target,name):
# generate a name dict
ndict = self.file_to_dict(name,0)
print ("Total %i names to delete" % len(ndict.keys()))
# go through lines in target file
inp = open(target,"r")
oup = open(target+".mod","w")
inl = inp.readline()
countT = 0
countR = 0
while inl != "":
countT += 1
L = self.rmlb(inl).split("\t")
found = 0
for j in L:
if ndict.has_key(j):
countR += 1
found = 1
break
if not found:
oup.write(inl)
inl = inp.readline()
print ("Total %i lines in target, %i deleted" % (countT,countR))
print ("Done!")
#
# Mark all lines containing the passed names
#
def mark_line(self,target,name):
# generate a name dict
ndict = self.file_to_dict(name,0)
print ("Total %i names to mark" % len(ndict.keys()))
# go through lines in target file
inp = open(target,"r")
oup = open(target+".mod","w")
inl = inp.readline()
countT = 0
countR = 0
while inl != "":
countT += 1
L = self.rmlb(inl).split("\t")
found = 0
for j in L:
# the following is for matching tokens in target containing
# xxx.x but name has only xxx, special case, disable after use
#if j.find(".") != -1:
# j = j.split(".")[0]
if ndict.has_key(j):
countR += 1
found = 1
break
if not found:
oup.write(inl)
else:
oup.write("%s\t*\n" % (self.rmlb(inl)))
inl = inp.readline()
print ("Total %i lines in target, %i marked" % (countT,countR))
print ("Done!")
#
# This function scan the first two tokens
#
# @param target id1\tid2\twhatever
# @param name id1\tid2
# @param oname output file name
# @param allT all matching target lines are taken, else take the 1st
# @param allN all names, even if they are redundant
#
def twinselect(self,target,name,outfile,allT=0,allN=0):
print ("Note that names not in the target will NOT have output")
if outfile == "":
outfile = "%s_%s" % (target,name)
# process name file
n = []
tmp = {}
inp = open(name)
inl = inp.readline()
while inl != "":
inl = self.rmlb(inl)
if allN:
n.append(inl)
else:
if inl not in tmp:
tmp[inl] = 1
n.append(inl)
inl = inp.readline()
# go through target
tmp = {}
inp = open(target)
oup = open(outfile,"w")
inl = inp.readline()
while inl != "":
L = self.rmlb(inl).split("\t")
if "%s\t%s" % (L[0],L[1]) in n:
if allT:
oup.write(inl)
else:
if "%s\t%s" % (L[0],L[1]) not in tmp:
tmp["%s\t%s" % (L[0],L[1])] = 1
oup.write(inl)
inl = inp.readline()
print ("Done!")
##
# Get specified columns from a tab-delimited file and order them based
# on the order specified.
##
def get_column(self,table,column,delim="\t"):
inp = open(table,"r")
if "," in column:
column = column.split(",")
oup = open(table+".col_%s" % "-".join(column),"w")
for i in range(len(column)):
column[i] = int(column[i])
else:
oup = open("%s.col%s" % (table,column),"w")
column = [int(column)]
print ("Get column(s):",column)
inl = inp.readline()
if len(inl.split("\t")) < max(column):
print ("Column number too large, there are only %i columns. Quit!"%\
len(inl.split("\t")))
sys.exit(0)
countL = 0
while inl != "":
inl = self.rmlb(inl)
llist = inl.split(delim)
tmp = []
try:
for j in column:
tmp.append(llist[j-1])
except:
print ("Funny line:",j-1)
print (">>",llist)
print ("skip!")
oup.write("%s\n" % string.joinfields(tmp,"\t"))
countL += 1
inl = inp.readline()
print ("Total %i lines." % countL)
print ("Done!")
#
# If multiple matches, just take the first
def dredun(self,target,col):
tdict = {}
inp = open(target,"r")
inl = inp.readline()
while inl != "":
ilist = inl.split("\t")
# THE TARGET COLUMN SHOULD NOT HAVE ".", this is for ensembl
# gene name
ilist[col] = ilist[col].split(".")[0]
key = ilist[col]
if not tdict.has_key(ilist[col]):
tdict[key] = [inl]
else:
tdict[key].append(inl)
inl = inp.readline()
tkeys = tdict.keys()
oup = open(target+".mredun","w")
for i in tkeys:
oup.write(tdict[i][0])
##
# Read a file and return a dict. The return style is:
# 0. 1st token as key, order as value
# 1. 1st token as key, the rest as value in a single string, omit new line
# 2. 1st token as key, the rest as a single string omitting new line, a
# list as value, mutiple match to the 1st token in the file,
# the later ones are appended to the list
# 3. 1st token as key, the rest in a list as value, omit new line
# 4. 1st token as key, the rest in a nested list, if multiple match to the
# key, later ones are appended to the list
# 5. 2nd token as key, the 1st token as value in a list. If the same key
# appear again, the 1st token will be appended.
# 6. Order as key, a list as value with [1st token, full line]
# 7. 1st token as key, the rest as value in a dict with 2nd token as key,
# the rest of the list as value.
#
# @param infile a tab-delimited file with at least two tokens
# @param style choose 1 or 2
# @return adict
##
def file_to_dict(self,infile,style=1):
if not style in [0,1,2,3,4,5,6,7]:
print ("Unknown parsing style, QUIT!")
sys.exit(0)
adict = {}
inp = open(infile)
inline = inp.readline()
order = 1
while inline != "":
if inline in ["\n","\r\n"]:
inline = inp.readline()
continue
if inline[-2:] == "\r\n":
inline = inline[:-2]
elif inline[-1] == "\n":
inline = inline[:-1]
llist = inline.split("\t")
if len(llist)<2 and style not in [0,6]:
print ("Only one token, can't be processed with specified style!")
return {}
if style == 5:
if not adict.has_key(llist[1]):
adict[llist[1]] = [inline[:inline.find("\t")]]
else:
#print "Redundant:",llist[1]
adict[llist[1]].append(inline[:inline.find("\t")])
elif style in [1,2,3,4]:
if not adict.has_key(llist[0]):
if style == 1:
adict[llist[0]] = inline[inline.find("\t")+1:]
elif style == 2:
adict[llist[0]] = [inline[inline.find("\t")+1:]]
elif style == 3:
adict[llist[0]] = llist[1:]
elif style == 4:
adict[llist[0]] = [llist[1:]]
else:
if style == 2:
adict[llist[0]].append(inline[inline.find("\t")+1:])
elif style == 4:
adict[llist[0]].append(llist[1:])
elif style == 0:
if not adict.has_key(llist[0]):
adict[llist[0]] = order
order += 1
#else:
# print " Redundant:",llist[0]
elif style == 6:
llist = inline.split("\t")
# assuming order will not be redundant
adict[order] = [llist[0],inline]
order += 1
elif style == 7:
L = inline.split("\t")
if L[0] not in adict:
adict[L[0]] = {L[1]:L[2:]}
elif L[1] not in adict[L[0]]:
adict[L[0]][L[1]] = L[2:]
else:
print ("Redun:",L)
inline = inp.readline()
return adict
##
# Convert file to a list or nested list, all WITHOUT "\n".
#
# @param infile input file
# @param style the output format, all without "\n":
# 0 - default, a list of lines
# 1 - a nested list with elements of a line in the nested ones
# @param delim delimiter for style = 1, default is tab.
##
def file_to_list(self,infile,style=0,delim="\t"):
inp = open(infile,"r")
inline = inp.readline()
flist = []
while inline != "":
# rid of empty line and newline char
if inline == "\n":
inline = inp.readline()
continue
if inline[-2:] == "\r\n":
inline = inline[:-2]
elif inline[-1] == "\n":
inline = inline[:-1]
if style == 0:
flist.append(inline)
elif style == 1:
if inline.find(delim) == -1:
print ("Delimiter not found in inline:",delim)
else:
flist.append(inline.split(delim))
else:
print ("Unknown style, file is not loaded to the list...")
inline = inp.readline()
return flist
def copy(self,t_dir,d_dir,name,ext):
if t_dir[:-1] != "/":
t_dir += "/"
if d_dir[:-1] != "/":
d_dir += "/"
nlist = self.file_to_list(name)
for i in nlist:
i = i + ext
os.system("cp %s%s %s" % (t_dir,i,d_dir))
print ("Done!")
##
# This function replace 1st tokens out of a file based on a passed name
# file. A complication is that sometimes names have appended other info
# already. To ensure names still can be found, the following convention
# need to be adhere to:
#
# <original name><space>whatever...<\t>other tokens...
#
# So whatever is before space is the name, between space and the first "\t"
# is the descirption that should be added back after.
#
# As of 08/23,2004, this script can only deal with non-redundant target name
# so is not useful when mulitple redundant target names are present. Modify
# this.
#
# @param target file with old names: at the first token only
# @param names file with new names: [new name][old name]
# @param F force output even name is not replaced [1], or [0,default]i
# @param tokens the token to replace, default 0 (the first)
##
def replace(self,target,names,outfile="",F=0,tokens="0"):
if outfile == "":
outfile = file1+".renamed"
oup = open(outfile,"w")
inp = open(names,"r")
inl = inp.readline()
ndict = {}
print ("Read names...")
while inl != "":
L = self.rmlb(inl).split("\t")
ndict[L[1]] = L[0]
inl = inp.readline()
if tokens.find(",") != -1:
tokens = tokens.split(",")
t = []
for i in tokens:
t.append(int(i))
tokens = t
else:
tokens = [int(tokens)]
inp = open(target,"r")
inl = inp.readline()
countT = countF = 0
print ("Replace target entries...")
while inl != "":
L = self.rmlb(inl).split("\t")
countT += 1
for j in tokens:
if ndict.has_key(L[j]):
L[j] = ndict[L[j]]
countF += 1
else:
if F:
L[0] = "<%s>" % L[0]
oup.write(string.joinfields(L,"\t")+"\n")
inl = inp.readline()
print ("Total %i entries in target file" % countT)
print (" %i with new names" % countF)
#
# Replace any token in a tab-delimited file that match the name dict
# @format name file format, 0:[new][old] (default), 1:[old][new]
#
def replace_all(self,target,names,format=0):
if format != 1:
ndict = self.file_to_dict(names,5)
else:
ndict = self.file_to_dict(names,3)
#print ndict
inp = open(target,"r")
oup = open(target+".replaced","w")
inl = inp.readline()
c = 0
while inl != "":
L = self.rmlb(inl).split("\t")
for j in range(len(L)):
if ndict.has_key(L[j]):
c += 1
L[j] = ndict[L[j]][0]
ostr = string.joinfields(L,"\t")
oup.write("%s\n" % ostr)
inl = inp.readline()
print ("Token replaced in %i instances." % c)
# Replace any string occurred, no requirement in format
def replace_any(self,target,names):
ndict = self.file_to_dict(names,1)
inp = open(target,"r")
oup = open(target+".replaced","w")
inl = inp.readline()
c = 0
while inl != "":
if c % 100 == 0:
print (" %i x 100" % (c/100))
#print [inl]
for j in ndict:
d = ndict[j]
L = inl.split(j)
if len(L) > 1:
inl = d.join(L)
#print j,d,len(L)
oup.write(inl)
inl = inp.readline()
c += 1
print ("Done!")
#
# Replace any string, VERY DANGEROUS, PROBABLY SHOULD NOT BE USED.
#
def replace_abs_all(self,target,names):
ndict = self.file_to_dict(names,5)
inp = open(target,"r")
oup = open(target+".replaced","w")
inl = inp.readline()
c = 0
while inl != "":
for j in ndict:
inl = inl.split(j)
inl = string.joinfields(inl,ndict[j])
oup.write(inl)
inl = inp.readline()
print ("Token replaced in %i instances." % c)
def merge_all(self,matrix1,matrix2,t1,t2,outfile):
uid = {}
inp = open(matrix1)
inl = inp.readline()
while inl != "":
L = self.rmlb(inl).split("\t")
if L[t1] not in uid:
uid[L[t1]] = [self.rmlb(inl),"-"]
else:
print ("Redun1:",L[t2])
inl = inp.readline()
inp = open(matrix2)
inl = inp.readline()
while inl != "":
L = self.rmlb(inl).split("\t")
if L[t2] not in uid:
uid[L[t2]] = ["-",self.rmlb(inl)]
elif uid[L[t2]][1] == "-":
uid[L[t2]][1] = self.rmlb(inl)
else:
print ("Redun2:",L[t2])
inl = inp.readline()
oup = open(outfile,"w")
for i in uid:
oup.write("%s\t%s\n" % (uid[i][0],uid[i][1]))
print ("Done!")
#
# Name can be redundant, suitable for small target file
#
def select3(self,target,names,outfile):
target = self.file_to_dict(target,1)
tokenI = 0 # assume 1st element if tab delimited
inp = open(names)
oup = open(outfile,"w")
inl = inp.readline()
countT = 0
countF = 0
while inl != "":
L = self.rmlb(inl).split("\t")
countT += 1
if L[tokenI] in target:
oup.write("%s\t%s\n" % (L[0],target[L[tokenI]]))
countF += 1
else:
oup.write("%s\t-\n" % L[0])
inl = inp.readline()
print ("Total %i, found %i" % (countT,countF))
print ("Done!")
#
# Similar to select. But much simpler and faster, only search first token.
#
# @param strStrip Deal with situation where there is empty space in the 1st
# col.
#
def select2(self,target,names,outfile,strStrip=0):
if outfile == "":
outfile = target+".selected"
print ("Target :",target)
print ("NameFile:",names)
print ("OutFile :",outfile)
print ("")
# construct name dict
print ("Construct name dict...")
names = self.file_to_dict(names,0)
print (" %i names" % len(names.keys()))
# go through target file
print ("Read target file...")
inp = open(target)
oup = open(outfile,"w")
inl = inp.readline()
countF = 0
#print "missing:"
while inl != "":
L = inl.strip().split("\t")
if strStrip:
L[0] = L[0].strip()
if names.has_key(L[0]):
oup.write("\t".join(L)+"\n")
countF += 1
if countF % 1e4 == 0:
print (" %i x 10k" % (countF/1e4))
names[L[0]] = 1
inl = inp.readline()
oup.close()
print (" %i found" % countF)
#for i in names:
# if names[i] != 1:
# print i
print ("\nDone!")
# get selected lines
#
# @param force whatever is in the name has an output line, even though it
# is not found in the target, default: 0, don't do this.
# @param M index for the column to match, default 0, if 999 is passed
# all column are checked.
# @param T target column to get, default "" (get all). Or has a string
# passed with "," as seperator. NOT IMPLEMENTED
# @param u inclusive, if this is set to [1], multiple matches to
# the same idx in the target file will be appended. Otherwise
# they will be ignored.
# @param p allow period in gene name or not.
#
def select(self,target,names,outfile,force=0,M=0,T="",u=1,p=0):
if outfile == "":
outfile = target+".selected"
# 1st token as key, rest as a member of a list
if M == 0:
tdict = self.file_to_dict(target,2)
if tdict == {}:
print ("Deal with file with only one token...")
tdict = self.file_to_dict(target,0)
tmp = {}
for i in tdict:
tmp[i] = [i]
tdict = tmp
print (len(tdict.keys()))
#for i in tdict:
# print i, tdict[i]
# or, based on the specified idx
else:
tdict = {}
inp = open(target,"r")
inl = inp.readline()
while inl != "":
i = self.rmlb(inl)
ilist = i.split("\t")
if p:
pass
else:
# THE TARGET COLUMN SHOULD NOT HAVE ".", this is for ensembl
# gene name
ilist[M] = ilist[M].split(".")[0]
key = ilist[M]
if not tdict.has_key(ilist[M]):
del ilist[M]
tstr = string.joinfields(ilist,"\t")
tdict[key] = [tstr]
else:
del ilist[M]
tstr = string.joinfields(ilist,"\t")
tdict[key].append(tstr)
inl = inp.readline()
countN = 0
countT = 0
print ("In name file but not in target file:")
inp = open(names,"r")
oup = open(outfile,"w")
inl = inp.readline()
while inl != "":
inl = self.rmlb(inl)
#print inl
if tdict.has_key(inl):
if u:
for j in tdict[inl]:
oup.write("%s\t%s\n" % (inl,j))
else:
oup.write("%s\t%s\n" % (inl,tdict[inl][0]))
else:
if force:
oup.write("%s\t-\n" % inl)
#print "",inl
countN += 1
countT += 1
inl = inp.readline()
print ("Name file: %i names, %i not in target" % (countT,countN))
#
# This is originally written for compressed chr sequences. This function
# decompress the sequence files then concatenate them.
#
def anneal(self,t_dir,outfile):
flist = os.listdir(t_dir)
fnames = []
print ("Decompress:")
for i in flist:
print ("",i)
if i[-3:] == ".gz":
fnames.append(i[:-3])
os.system("gunzip %s" % i)
fstr = string.joinfields(fnames," ")
print ("Concatenate files...")
os.system("cat %s > %s" % (fstr,outfile))
print ("Done!")
#
# delete files. It's up to the user to specify postfix, this is not very
# smart, so "." has to be added by user.
#
def delete(self,names,postfix=""):
ndict = self.file_to_dict(names,0)
for i in ndict:
os.system("rm %s" % (i+postfix))
def split(self,file,by):
inp = open(file)
lines = inp.readlines()
leng = len(lines)/by
C = 0
for i in range(by):
oup = open(file+"_%i" % (i+1),"w")
if C == by -1:
R = [leng*C,leng*(C+2)]
else:
R = [leng*C,leng*(C+1)]
oup.write("%s" % string.joinfields(lines[R[0]:R[1]],""))
C = C+1
print ("Done!")
def batch_move(self,names,src,dest,P):
print ("Source dir :",src)
print ("Destination:",dest)
print ("File ext :",P)
if src[-1] == "/":
src = src[:-1]
if dest[-1] == "/":
dest = dest[:-1]
nlist = futil.file_to_list(names,0,"\t")
print ("Move %i files:" % len(nlist))
countF = 1
for i in nlist:
if countF % 10 == 0:
print (" %i x 10" % (countF/10))
if P != "":
i = i + "." + P
os.system("mv %s/%s %s" % (src,i,dest))
countF += 1
print ("Done!")
#
# Exchange col in a two col file
#
def exchange(self,file):
inp = open(file,"r")
inl = inp.readline()
oup = open(file+".exchange","w")
while inl != "":
L = self.rmlb(inl).split("\t")
oup.write("%s\t%s\n" % (L[1],L[0]))
inl = inp.readline()
print ("Done!")
def join_tables(self):
flist = os.listdir("./")
flist.sort()
colH = []*len(flist) # col header
rowD = {} # row dict
print ("%i files in local dir" % len(flist))
print ("Go through files...")
countF = 0
for i in flist:
print (i)
inp = open(i)
# first line is header
inl = self.rmlb(inp.readline()).split("\t")
colH.append(inl[1])
print ("",inl[1])
inl = self.rmlb(inp.readline())
while inl != "":
L = self.rmlb(inl).split("\t")
if L[0] not in rowD:
rowD[L[0]] = ["-"]*len(flist)
rowD[L[0]][countF] = L[1]
inl = inp.readline()
countF += 1
print ("Generate output...")
rKeys = rowD.keys()
rKeys.sort()
oup = open("table_join","w")
oup.write("\t%s\n" % (string.joinfields(colH,"\t")))
for i in rKeys:
oup.write("%s\t%s\n" % (i,string.joinfields(rowD[i],"\t")))
print ("Done!")
def survey(self,infile,col):
inp = open(infile)
col = col.split(",")
for i in range(len(col)):
col[i] = int(col[i])
inl = inp.readline()
D = {}
while inl != "":
L = inl.split("\t")
for j in col:
if j not in D:
D[j] = {L[j]:1}
elif L[j] not in D[j]:
D[j][L[j]] = 1
else:
D[j][L[j]] += 1
inl = inp.readline()
for i in D:
print ("Col:",i)
for j in D[i]:
print ("",j)
print ("Done!")
def rmlb(self,astr):
if astr[-2:] == "\r\n":
astr = astr[:-2]
elif astr[-1] == "\n":
astr = astr[:-1]
return astr
#
# Covert a two column file into a matrix with 1st col as rows, 2nd col as col.
#
# @param infile A two column file
# @param orderr Row ordering, one row name per line.
# @param orderc Column ordering, one column name per line.
#
def list_to_matrix(self,infile,orderr,orderc):
inp = open(infile)
D = {} # {row:{col:1}}
col = {} # column name list
inl = inp.readlines()
for i in inl:
[r,c] = i.strip().split("\t")[:2] # row and col
if c not in col:
col[c] = 1
if r not in D:
D[r] = {c:1}
elif c not in D[r]:
D[r][c] = 1
else:
print ("Redun 2nd col for a given row name:",r,c)