-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtrnasequtils.py
More file actions
executable file
·1332 lines (1190 loc) · 52.1 KB
/
trnasequtils.py
File metadata and controls
executable file
·1332 lines (1190 loc) · 52.1 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/env python3
import pysam
import sys
import tempfile
import re
import gzip
import subprocess
import os.path
from distutils.spawn import find_executable
from collections import defaultdict
allaminos = ('Ala','Arg','Asn','Asp','Cys','Gln','Glu','Gly','His','Ile','Ile2','Leu','Lys','Met','iMet','fMet','Phe','Pro','Ser','Thr','Trp','Tyr','Val','SeC','Sup','Undet')
if sys.version_info[0] < 3:
print("tRAX now requires python3, version " +str(sys.version_info[0]) +" too low",file=sys.stderr)
sys.exit(1)
def readmultifasta(fafile):
#print chrom+":"+ chromstart+"-"+ chromendre
if fafile == "stdin":
fafile = sys.stdin
elif fafile.endswith(".gz"):
fafile = gzip.open(fafile, "rt", encoding='utf-8')
else:
fafile = open(fafile, "r")
currloc = 0
currseq = None
sequence = ""
reheader = re.compile(r"\>([^\s\,]+)")
for line in fafile:
line = line.rstrip("\n")
currheader = reheader.match(line)
if currheader and sequence != "" and currseq is not None:
yield currseq, sequence
currseq = currheader.groups(1)[0]
sequence = ""
elif currheader:
currseq = currheader.groups(1)[0]
else:
sequence += line
if currseq is not None:
yield currseq, sequence
#returns location of the genome file
def readmultifastq(fqfile, getnameline = False):
#print chrom+":"+ chromstart+"-"+ chromend
if fqfile == "stdin":
fqfile = sys.stdin
elif fqfile.endswith(".gz"):
fqfile = gzip.open(fqfile, "rt", encoding='utf-8')
else:
fqfile = open(fqfile, "r")
currloc = 0
currseq = None
sequence = ""
quality = ""
if getnameline:
reheader = re.compile(r"\@(.+)")
else:
reheader = re.compile(r"\@([^\s\,]+)")
qualheader = re.compile(r"\+([^\s\,]*)")
readqual = False
for line in fqfile:
#print line
line = line.rstrip("\n")
seqheader = reheader.match(line)
qheader = qualheader.match(line)
if readqual:
quality += line
if len(quality) == len(sequence):
yield currseq, sequence, quality
readqual = False
elif seqheader:
currseq = seqheader.groups(1)[0]
sequence = ""
quality = ""
elif qheader and readqual == False:
readqual = True
pass
else:
sequence += line
def fastadict(fafile):
seqdict = dict()
for name, seq in readmultifasta(fafile):
seqdict[name] = seq
return seqdict
def tempmultifasta(allseqs):
fafile = tempfile.NamedTemporaryFile(suffix=".fa",mode="w+")
for seqname, seq in allseqs:
print(">"+seqname+"\n", file=fafile)
print(seq+"\n", file=fafile)
fafile.flush()
return fafile
def invertstrand(strand):
if strand == "+":
return "-"
elif strand == "-":
return "+"
class alignment:
def __init__(self, sequences):
if len(list(sequences.keys())) < 1:
raise EmptyAlignException()
self.aligns = sequences
if max(len(curr) for curr in self.aligns.values()) != min(len(curr) for curr in self.aligns.values()):
print("Non-matching sequence lengths in multiple alignment", file=sys.stderr)
#self.alignlength = len(self.aligns[self.aligns.keys()[0]])
self.alignlength = max(len(curr) for curr in self.aligns.values())
def getseqlength(self, seqname):
return len(self.aligns[seqname].replace("-",""))
def numseqs(self):
return len(list(self.aligns.keys()))
def getseqnames(self):
return list(self.aligns.keys())
def getalignseq(self ,seqname):
return self.aligns[seqname]
def toseqdict(self):
seqs = dict()
for currname, currseq in self.aligns.items():
seqs[currname] = string.translate(currseq, None, "-.~")
return seqs
def getsubset(self, subset):
newaligns = dict()
for curr in subset:
newaligns[curr] = ""
for i in range(self.alignlength):
if any(self.aligns[curr][i] not in gapchars for curr in subset):
for curr in subset:
newaligns[curr] += self.aligns[curr][i]
return alignment(newaligns)
def prottocodonalignment(self, nucseqs):
#I don't do any checking to make sure that the nucleotide sequence is equivalent here
newalign = defaultdict(str)
currpoint = defaultdict(int)
for i in range(self.alignlength):
for currgene in nucseqs.keys():
if self.aligns[currgene][i] != "-":
newalign[currgene] += nucseqs[currgene][currpoint[currgene]:currpoint[currgene] + 3]
currpoint[currgene] += 3
else:
newalign[currgene] += "---"
#print "|".join(str(curr) for curr in currpoint.values())
#print "|".join(str(len(curr)) for curr in nucseqs.values())
#print "|".join(curr for curr in newalign.values())
#print str(self.alignlength) pyroIsla1
return alignment(newalign, aligned = True)
def tempmultifasta(self):
return tempmultifasta(self.aligns)
def fastaformat(self, prefix = ""):
output = ""
for seqname, seq in self.aligns.items():
output += ">"+prefix+seqname+"\n"+seq+"\n"
return output
def getsegment(self,start, end):
newalign = dict()
for name, seq in self.aligns.items():
newalign[name] = seq[start:end]
return alignment(newalign)
def getsubsets(self,windowsize, stepsize):
for curr in range(0, self.alignlength - windowsize, stepsize):
start = curr
end = curr + windowsize
yield self.getsegment(start,end)
def removeemptyseqs(self):
newalign = dict()
for name, seq in self.aligns.items():
if len(string.translate(seq, None, "-.")) != 0:
newalign[name] = seq
return alignment(newalign)
#http://bioinformatics.oxfordjournals.org/content/25/5/668.full
def phylipformat(self):
#print ",".join(str(curr)for curr in self.aligns.keys())
output = str(len(list(self.aligns.keys())))+ " "+str(self.alignlength)+"\n"
for currgene in self.aligns.keys():
#sys.stderr.write("**"+str(currgene).ljust( 11)+"**\n")
output += str(currgene).ljust( 14)
output += self.aligns[currgene]+"\n"
#print output
return output
def nexusformat(self):
output = "#NEXUS\nBegin data;\nDimensions ntax="+str(len(list(self.aligns.keys())))+" nchar="+str(self.alignlength)+";\n"
output += "Format datatype=dna symbols=\""+"ATCG"+"\" missing=? gap=-;\n"
output += "Matrix\n"
for currgene in self.aligns.keys():
output += currgene.ljust( 11)
output += self.aligns[currgene]+"\n"
output+=";\nEnd;"
def getrealcoordinate(self, alignname, coord):
currreal = 0
#print self.aligns[alignname]
#print coord
for i in range(0, coord):
if self.aligns[alignname][i] not in set("-.~"):
currreal += 1
return min([currreal, len(list(curr for curr in self.aligns[alignname] if curr not in set("-.~"))) - 1])
def getseqrange(self, alignname, start, end):
return self.getsegment(self.getaligncoordinate(alignname, start),self.getaligncoordinate(alignname, end))
def printstk(self, name = None):
print("# STOCKHOLM 1.0")
if name is not None:
print("#=GF ID "+name)
padlength = max(len(curr) for curr in self.aligns.keys()) + 4
for currname, currseq in self.aligns.items():
print(string.ljust(currname,padlength ) +currseq)
print("//")
def printhtml(self, name = None):
print("<CODE>")
for currname, currseq in self.aligns.items():
print(currname + "\t"+currseq +"<BR/>")
print("</CODE>")
def clustalwformat(self):
output = "CLUSTAL W 2.1 multiple sequence alignment\n\n"
conservestring = ""
for i in range(0, self.alignlength):
conservestring += ":"
for currpos in range(0, self.alignlength, 60):
for seqname, seq in self.aligns.items():
output += seqname+"\t"+seq[currpos:min([currpos+60,self.alignlength])]+"\n"
output += "\t"+conservestring[currpos:min([currpos+60,self.alignlength])] + "\n\n"
return output
class RnaAlignment(alignment):
def __init__(self,alignseqs, structure, consensus = None,energies = None):
self.aligns = alignseqs
self.currstruct = structure
self.energies = energies
self.consensus = consensus
self.alignlength = max(len(curr) for curr in list(alignseqs.values()))
def addupstream(self, seqs, struct = None):
newseqs = dict()
#print >>sys.stderr, seqs.keys()
for curr in self.aligns.keys():
newseqs[curr] = seqs[curr] + self.aligns[curr]
if struct is None:
newstruct = (max(len(curr) for curr in seqs.values()) * ":") + self.currstruct
else:
newstruct = struct + self.currstruct
return RnaAlignment(newseqs, newstruct)
def adddownstream(self, seqs, struct = None):
newseqs = dict()
for curr in self.aligns.keys():
newseqs[curr] = self.aligns[curr]+ seqs[curr]
if struct is None:
newstruct = self.currstruct + (max(len(curr) for curr in seqs.values()) * ":")
else:
newstruct = self.currstruct + struct
return RnaAlignment(newseqs, newstruct)
def addmargin(self, length):
newseqs = dict()
for curr in self.aligns.keys():
newseqs[curr] = length*"N" + self.aligns[curr]+ length*"N"
newstruct = length * ":" + self.currstruct + length * ":"
if self.consensus is None:
newconsensus = None
else:
newconsensus = length*"N" + self.consensus+ length*"N"
return RnaAlignment(newseqs, newstruct, consensus = newconsensus)
def viennaformat(self):
output = ""
for currseq in self.aligns.keys():
output += ">"+currseq+"\n"
output += self.aligns[currseq]+"\n"
output += self.currstruct+"\n"
return output
def viennatempfile(self):
viennafile = tempfile.NamedTemporaryFile()
viennafile.write(self.viennaformat())
viennafile.flush()
return viennafile
def printstk(self, name = None):
print("# STOCKHOLM 1.0")
if name is not None:
print("#=GF ID "+name)
for currname, currseq in self.aligns.items():
print(currname + "\t"+currseq)
structline = ""
structpos = 0
secpos = 0
print("#=GC SS_cons\t"+self.currstruct)
print("//")
def printhtml(self, name = None):
for currname, currseq in self.aligns.items():
print(currname + "\t"+currseq +"</BR>")
structline = ""
structpos = 0
secpos = 0
print("#=GC SS_cons\t"+self.currstruct+"</BR>")
print("//"+"</BR>")
def convertmaturealign(rnaalign):
newseqs = dict()
newalign = rnaalign.struct
for name, seq in rnaalign.aligns.items():
newseqs[name]
def readrnastk(stk):
seqs = defaultdict(str)
struct = ""
consensus = ""
energyscore = None
for line in stk:
line = line.rstrip()
if line.startswith("//"):
#print >>sys.stderr, "||||||||||||||||||"
#print >>sys.stderr, consensus
if consensus == "":
consensus = None
#print >>sys.stderr, consensus
yield RnaAlignment(seqs, struct, consensus = consensus)
seqs = defaultdict(str)
struct = ""
consensus = ""
energyscore = None
elif not line.startswith("#") and len(line.split()) > 1:
currname = line.split()[0]
currseq = line.split()[1]
seqs[currname] += currseq
elif line.startswith("#=GC SS_cons"):
struct += line.split()[2]
elif line.startswith("#=GC RF"):
#print >>sys.stderr, "********************"
#
#print >>sys.stderr, line.split()[2]
#print >>sys.stderr, "#############################"
consensus += line.split()[2]
def uniqueorder(inp):
alldata = set()
for curr in inp:
if curr not in alldata:
yield curr
alldata.add(curr)
class transcriptfile:
def __init__(self, trnafilename):
trnafile = open(trnafilename)
locustranscript = dict()
trnatranscripts = list()
loci = list()
amino = dict()
anticodon = dict()
transcriptdict = defaultdict(set)
aminoorder = list()
anticodonorder = list()
for i, line in enumerate(trnafile):
fields = line.split()
if len(fields) < 2:
continue
trnatranscripts.append(fields[0])
amino[fields[0]] = fields[2]
anticodon[fields[0]] = fields[3]
aminoorder.append(fields[2])
anticodonorder.append(fields[3])
for currlocus in fields[1].split(','):
locustranscript[currlocus] = fields[0]
loci.append(currlocus)
transcriptdict[fields[0]].add(currlocus)
self.locustranscript = locustranscript
self.transcripts = trnatranscripts
self.amino = amino
self.anticodon = anticodon
self.transcriptdict = transcriptdict
self.loci = loci
self.aminoorder = tuple(uniqueorder(aminoorder))
self.anticodonorder = tuple(uniqueorder(anticodonorder))
def gettranscripts(self):
return set(self.transcripts)
def getlocustranscript(self, locus):
return self.locustranscript[locus]
def getloci(self):
return self.loci
def getamino(self, trna):
return self.amino[trna]
def getanticodon(self, trna):
return self.anticodon[trna]
def allaminos(self):
return self.aminoorder
def allanticodons(self):
return self.anticodonorder
def getaminotranscripts(self, trnaamino):
return set(curr for curr in self.transcripts if trnaamino == self.amino[curr])
def getanticodontranscripts(self, trnaanticodon):
return set(curr for curr in self.transcripts if trnaanticodon == self.anticodon[curr])
def getpairfile(pairfilename):
pairfile = open(pairfilename)
for currline in pairfile:
fields = currline.split()
if len(fields) > 1:
yield fields[0], fields[1]
class extraseqfile:
def __init__(self, extraseqfilename):
try:
extrafile = open(extraseqfilename)
seqlist = list()
seqfasta = dict()
seqbed = dict()
directory = os.path.dirname(extraseqfilename)
for i, line in enumerate(extrafile):
fields = line.split()
if len(fields) < 2:
continue
seqfasta[fields[0]] = directory+"/"+fields[1]
seqbed[fields[0]] = directory+"/"+fields[2]
seqlist.append(fields[0])
#bamlist = list(curr + "_sort.bam" for curr in samplefiles.iterkeys())
self.seqlist = seqlist
self.seqfasta = seqfasta
self.seqbed = seqbed
#self.bamlist = list(curr+ "_sort.bam" for curr in samplelist)
except IOError as e:
self.seqlist = list()
self.seqfasta = dict()
self.seqbed = dict()
#print >>sys.stderr, "**||**" + extraseqfilename
#print >>sys.stderr,"extraseqfile I/O error({0}): {1}".format(e.errno, e.strerror)
def getseqnames(self):
seqnamedict = defaultdict(set)
for curr in seqlist:
seqnamedict[currseq] += set(curr.name for curr in readbed(self.seqbed[currseq]))
return seqnamedict
def getseqbeds(self):
return self.seqbed
class samplefile:
def __init__(self, samplefilename, bamdir = "./"):
try:
samplefile = open(samplefilename)
samplelist = list()
samplefiles = dict()
replicatename = dict()
replicatelist = list()
for i, line in enumerate(samplefile):
fields = line.split()
if len(fields) < 2:
continue
samplefiles[fields[0]] = fields[2]
replicatename[fields[0]] = fields[1]
samplelist.append(fields[0])
if fields[1] not in set(replicatelist):
replicatelist.append(fields[1])
#bamlist = list(curr + "_sort.bam" for curr in samplefiles.iterkeys())
samplenames = list(curr for curr in samplefiles.keys())
if bamdir is None:
bamdir = "./"
self.bamdir = bamdir
self.samplelist = samplelist
self.samplefiles = samplefiles
self.replicatename = replicatename
self.replicatelist = replicatelist
#self.bamlist = list(curr+ "_sort.bam" for curr in samplelist)
except IOError:
print("Cannot read sample file "+samplefilename, file=sys.stderr)
print("exiting...", file=sys.stderr)
sys.exit(1)
def getsamples(self):
return self.samplelist
def getbamlist(self):
return list(curr+ ".bam" for curr in self.samplelist)
def getbam(self, sample):
return self.bamdir + "/" + sample + ".bam"
def getmergebam(self, sample):
return self.bamdir + "/" + sample + "-merge.bam"
def getfastq(self, sample):
return self.samplefiles[sample]
def getreplicatename(self, sample):
return self.replicatename[sample]
def allreplicates(self):
return self.replicatelist
def getrepsamples(self, replicate):
return list(currsample for currsample in self.samplelist if self.replicatename[currsample] == replicate)
def getfastqsample(self, fastqname):
for currsample in self.samplefiles.keys():
if self.samplefiles[currsample] == fastqname:
return currsample
def getsizefactors( sizefactorfilename):
sizefactorfile = None
try:
sizefactorfile = open(sizefactorfilename)
except IOError as err:
print("Cannot read size factor file "+sizefactorfilename, file=sys.stderr)
print("check Rlog.txt", file=sys.stderr)
sys.exit(1)
sizefactors = dict()
bamheaders =list()
sizes = list()
for i, line in enumerate(sizefactorfile):
if i == 0:
bamheaders = list(curr.strip("\"\n") for curr in line.split())
elif i == 1:
sizes = list(float(curr.strip("\"\n")) for curr in line.split())
for i in range(0, len(bamheaders)):
sizefactors[bamheaders[i]] = sizes[i]
#print >>sys.stderr, bamheaders[i]+":"+ str(sizes[i])
return sizefactors
#special class that uses the read indentifier for hashing in sets
class GenomeRange:
__slots__ = "dbname", "chrom", "strand","name", "fastafile", "start", "end", "data"
def __eq__(self, other):
return self.strand == other.strand and self.chrom == other.chrom and self.start == other.start and self.end == other.end
def __hash__(self):
return self.start + self.end + hash(self.chrom) + hash(self.strand)
def __init__(self, dbname, chrom, start, end, strand = None,name = None, orderstrand = False, data = None, fastafile = None):
self.dbname =dbname
self.chrom = chrom
self.start = int(start)
self.end = int(end)
self.strand = strand
self.fastafile = fastafile
if orderstrand and self.start > self.end:
temp = self.start
self.start = self.end
self.end = temp
self.strand = invertstrand(strand)
self.data = data
self.name = name
def coverage(self, other):
if self.strand == other.strand and self.chrom == other.chrom:
start = max([self.start,other.start])
end = min([self.end,other.end])
if end - start < 0:
return 0
else:
return end - start
else:
return 0
def bedstring(self, name = None,score = 1000):
if self.strand == None:
self.strand = "+"
if name is None and self.name is None:
name = "FEAT"
elif name is None:
name = self.name
return "\t".join([self.chrom,str(self.start),str(self.end),name,str(score), self.strand])
def length(self):
return self.end - self.start
def addmargin(self, dist = 50, name = None):
newname = name
if name is None:
newname = self.name
return GenomeRange(self.dbname, self.chrom, self.start - dist,self.end + dist,self.strand, name = newname, fastafile = self.fastafile)
def getupstream(self, dist = 50, name = None):
newname = name
if name is None:
newname = self.name
return GenomeRange(self.dbname, self.chrom, self.start - dist,self.start,self.strand, name = newname, fastafile = self.fastafile)
def getdownstream(self, dist = 50, name = None):
newname = name
if name is None:
newname = self.name
if self.strand != '-':
return GenomeRange(self.dbname, self.chrom, self.end,self.end + dist,self.strand, name = newname, fastafile = self.fastafile)
else:
return GenomeRange(self.dbname, self.chrom, self.start - dist,self.start,self.strand, name = newname, fastafile = self.fastafile)
def getfirst(self, dist = 50, name = None):
newname = name
if name is None:
newname = self.name
if self.strand == "-":
return GenomeRange(self.dbname, self.chrom, self.end - dist,self.end ,self.strand, name = newname, fastafile = self.fastafile)
else:
return GenomeRange(self.dbname, self.chrom, self.start,self.start + dist,self.strand, name = newname, fastafile = self.fastafile)
def getlast(self, dist = 50, name = None):
newname = name
if name is None:
newname = self.name
if self.strand == "-":
return GenomeRange(self.dbname, self.chrom, self.start,self.start + dist,self.strand, name = newname, fastafile = self.fastafile)
else:
return GenomeRange(self.dbname, self.chrom, self.end - dist,self.end ,self.strand, name = newname, fastafile = self.fastafile)
def getbase(self, basenum, name = None):
newname = name
if name is None:
newname = self.name
if self.strand == "-":
return GenomeRange(self.dbname, self.chrom, self.end - basenum - 1 ,self.end -basenum,self.strand, name = newname, fastafile = self.fastafile)
else:
return GenomeRange(self.dbname, self.chrom, self.start +basenum ,self.start + basenum + 1,self.strand, name = newname, fastafile = self.fastafile)
def antisense(self):
if self.strand == "+":
newstrand = "-"
else:
newstrand = "+"
return GenomeRange(self.dbname, self.chrom, self.start,self.end,newstrand, name = self.name, fastafile = self.fastafile)
def bamseq(self):
if self.strand == "+":
return self.data["seq"]
else:
return revcom(self.data["seq"])
def getgc(self):
seq = self.bamseq()
return sum(1 if currbase in set(["G","C"]) else 0 for currbase in seq)
class GenomeRead(GenomeRange):
def __eq__(self, other):
return self.name == other.name
def __hash__(self):
return hash(self.name)
def __init__(*args, **nargs):
GenomeRange.__init__(*args, **nargs)
'''
Still need to add trailer fragment code, both here and elsewhere
'''
def getfragtype(currfeat, currread, maxoffset = 10):
if currread.start < currfeat.start + maxoffset and currread.end > currfeat.end - maxoffset:
return "Whole"
elif currread.start < currfeat.start + maxoffset:
if currfeat.strand == "+":
return "Fiveprime"
else:
return "Threeprime"
elif currread.end > currfeat.end - maxoffset:
if currfeat.strand == "+":
return "Threeprime"
else:
return "Fiveprime"
def getendtype(currfeat, currread, maxoffset = 10):
endtype = None
if currread.end == currfeat.end:
endtype = "CCA"
elif currread.end == currfeat.end -1:
endtype = "CC"
elif currread.end == currfeat.end -2:
endtype = "C"
elif currread.end == currfeat.end -3:
endtype = ""
return endtype
smallrnatypes = set([])
def readfeatures(filename, orgdb="genome", seqfile= None, removepseudo = False):
if filename.endswith(".bed") or filename.endswith(".bed.gz"):
return readbed(filename, orgdb, seqfile)
elif filename.endswith(".gtf") or filename.endswith(".gtf.gz") or filename.endswith(".gff") or filename.endswith(".gff.gz"):
#print >>sys.stderr, removepseudo
return (curr for curr in readgtf(filename, orgdb, seqfile, filterpsuedo = removepseudo, filtertypes =set(['retained_intron','antisense','lincRNA']) ))
else:
print(filename+" not valid feature file", file=sys.stderr)
sys.exit()
def readgtf(filename, orgdb="genome", seqfile= None, filterpsuedo = False, replacename = False, filtertypes = set(['retained_intron','antisense','lincRNA'])):
bedfile = None
#print >>sys.stderr, "****"
if filename == "stdin":
bedfile = sys.stdin
elif filename.endswith(".gz"):
bedfile = gzip.open(filename, 'rt', encoding='utf-8')
else:
bedfile = open(filename, "r")
for currline in bedfile:
#print currline
if currline.startswith('track') or currline.startswith('#'):
continue
fields = currline.rstrip().split("\t")
if len(fields) > 2:
biotype = "Unknown"
featname = None
genename = None
#print >>sys.stderr, len(fields)
genesource = fields[1]
#retained introns are often other things as well, so I skip em
if fields[2] != "transcript" or genesource in filtertypes:
continue
for currattr in fields[8].rstrip(";").split(";"):
#print >>sys.stderr, currattr
currname = currattr.strip().split()[0]
currvalue = currattr.strip().split()[1]
if currname == "transcript_name":
featname = currvalue.strip('"')
elif currname == "name" or currname == "gene_id" and featname is None:
featname = currvalue.strip('"')
elif currname == "gene_biotype":
biotype = currvalue.strip('"')
elif currname == "gene_name":
genename = currvalue.strip('"')
#print >>sys.stderr, "***||"
if genename is None:
#print >>sys.stderr, "No name for gtf entry "+featname
genename = featname
pass
if filterpsuedo and biotype == "pseudogene":
#print >>sys.stderr, "*******"
continue
if genesource == 'ensembl':
#print >>sys.stderr, biotype
genesource = biotype
if not (fields[6] == "+" or fields[6] == "-"):
print("strand error in "+filename, file=sys.stderr)
skippedlines += 1
elif not (fields[3].isdigit() and fields[4].isdigit()):
print("non-number coordinates in "+filename, file=sys.stderr)
print(currline, file=sys.stderr)
skippedlines += 1
else:
if replacename:
featname = genename
yield GenomeRange( orgdb, fields[0],fields[3],fields[4],fields[6], name = featname, fastafile = seqfile, data = {"biotype":biotype, "source":genesource, "genename":genename,"feature":fields[2]})
def readbed(filename, orgdb="genome", seqfile= None, includeintrons = False):
bedfile = None
if filename == "stdin":
bedfile = sys.stdin
elif filename.endswith(".gz"):
bedfile = gzip.open(filename, 'rt', encoding='utf-8')
else:
bedfile = open(filename, "r")
skippedlines = 0
for currline in bedfile:
#print currline
data = dict()
if currline.startswith('track') or currline.startswith('#'):
continue
fields = currline.rstrip().split()
if len(fields) > 2:
if len(fields) < 5:
strand = "+"
else:
strand = fields[5]
if not (strand == "+" or strand == "-"):
print("strand error in "+filename, file=sys.stderr)
skippedlines += 1
elif not (fields[1].isdigit() and fields[2].isdigit()):
print("non-number coordinates in "+filename, file=sys.stderr)
print(currline, file=sys.stderr)
skippedlines += 1
else:
if includeintrons and len(fields) > 7:
data["blockcount"] = int(fields[9])
data["blocksizes"] = tuple(int(curr) for curr in fields[10].rstrip(",").split(","))
data["blockstarts"] = tuple(int(curr) for curr in fields[11].rstrip(",").split(","))
yield GenomeRange( orgdb, fields[0],fields[1],fields[2],strand, name = fields[3], fastafile = seqfile, data = data)
if skippedlines > 0:
print("skipped "+str(skippedlines)+" in "+filename, file=sys.stderr)
def ifelse(arg, trueres,falseres):
if arg:
return trueres
else:
return falseres
def isprimarymapping(mapping):
return not (mapping.flag & 0x0100 > 0)
def issinglemapping(mapping):
return mapping.mapq > 2
def mismatchnum(mapping):
return int(mapping.get_tag("XM"))
def getbamrangeshortseq(bamfile, chromrange = None, primaryonly = False, singleonly = False, maxmismatches = None, allowindels=True, skiptags = False):
try:
if chromrange is not None:
bamiter = bamfile.fetch(chromrange.chrom, chromrange.start, chromrange.end)
else:
bamiter = bamfile.fetch()
for currline in bamiter:
if primaryonly and not isprimarymapping(currline):
continue
if singleonly and not issinglemapping(currline):
continue
if not allowindels and len(currline.cigar) > 1:
continue
if maxmismatches is not None and mismatchnum(currline) > maxmismatches:
continue
rname = bamfile.getrname(currline.rname)
strand = "+"
strand = ifelse(currline.is_reverse, '-','+')
yield GenomeRead( "genome",rname,currline.pos,currline.aend,strand, name = currline.qname, data = {"seq":currline.seq} )
except ValueError as err:
#print>>sys.stderr, err
#print>>sys.stderr, bamfile.name
if chromrange is not None:
#print >>sys.stderr, chromrange.chrom+":"+ str(chromrange.start)+"-"+str(chromrange.end) +" failed"
pass
def getbamrangeshort(bamfile, chromrange = None, primaryonly = False, singleonly = False, maxmismatches = None, allowindels=True, skiptags = False):
bamiter = None
bamiter = None
try:
if chromrange is not None:
bamiter = bamfile.fetch(chromrange.chrom, chromrange.start, chromrange.end)
else:
bamiter = bamfile.fetch()
for currline in bamiter:
#if mismatchnum(currline) > maxmismatches:
# print >>sys.stderr, "*|*"
# print >>sys.stderr, maxmismatches
# print >>sys.stderr, mismatchnum(currline)
if primaryonly and not isprimarymapping(currline):
continue
if singleonly and not issinglemapping(currline):
continue
if not allowindels and len(currline.cigar) > 1:
continue
if maxmismatches is not None and mismatchnum(currline) > maxmismatches:
#print >>sys.stderr, "skipped"
continue
rname = bamfile.getrname(currline.rname)
strand = "+"
strand = ifelse(currline.is_reverse, '-','+')
yield GenomeRead( "genome",rname,currline.pos,currline.aend,strand, name = currline.qname)
except ValueError as err:
#print>>sys.stderr, err
#print>>sys.stderr, bamfile.name
if chromrange is not None:
#print >>sys.stderr, chromrange.chrom+":"+ str(chromrange.start)+"-"+str(chromrange.end) +" failed"
pass
def getbamrange(bamfile, chromrange = None, primaryonly = False, singleonly = False, maxmismatches = None, allowindels = True, skiptags = False):
bamiter = None
try:
if chromrange is not None:
bamiter = bamfile.fetch(chromrange.chrom, chromrange.start, chromrange.end)
else:
bamiter = bamfile.fetch()
for currline in bamiter:
#if mismatchnum(currline) > maxmismatches:
# print >>sys.stderr, "**"
# print >>sys.stderr, maxmismatches
# print >>sys.stderr, mismatchnum(currline)
if primaryonly and not isprimarymapping(currline):
continue
if singleonly and not issinglemapping(currline):
continue
if maxmismatches is not None and mismatchnum(currline) > maxmismatches:
#print >>sys.stderr, "skipped"
continue
rname = bamfile.getrname(currline.rname)
strand = "+"
strand = ifelse(currline.is_reverse, '-','+')
#yield GenomeRead( "genome",rname,currline.pos,currline.aend,strand, name = currline.qname)
#continue
#print rname
#need to fix this with cigar stuff
#len(currline.pos)
#not giving the reverse complement for now
seq = currline.seq
#print currline.cigar
#print >>sys.stderr, currline.qname
#[("YA",len(anticodons))] + [("YM",len(aminos))] + [("YR",len(trnamappings))]
uniqueac = True
uniqueamino = True
uniquetrna = True
#print >>sys.stderr, dir(currline)
mismatches = None
alignscore = None
secondbestscore = None
uniquemapping = False
if not skiptags:
if currline.has_tag("YA") and currline.get_tag("YA") > 1:
uniqueac = False
if currline.has_tag("YM") and currline.get_tag("YM") > 1:
uniqueamino = False
if currline.has_tag("YR") and currline.get_tag("YR") > 1:
uniquetrna = False
if currline.has_tag("XM"):
mismatches = currline.get_tag("XM")
if currline.has_tag("XS"):
secondbestscore = float(currline.get_tag("XS"))
if currline.has_tag("AS"):
alignscore = float(currline.get_tag("AS"))
if secondbestscore is None or alignscore > secondbestscore:
uniquemapping = True
#continue
if not allowindels and len(currline.cigar) > 1:
continue
#"flags": currline.flag,
yield GenomeRead( "genome",rname,currline.pos,currline.aend,strand, name = currline.qname , data = {"score":currline.mapq, "CIGAR":currline.cigar,"CIGARstring":currline.cigarstring, "seq":seq, "flags": currline.flag, "qual":currline.qual,"bamline":currline,'uniqueac':uniqueac,"uniqueamino":uniqueamino,"uniquetrna":uniquetrna,"uniquemapping":uniquemapping})
except ValueError as err:
#print>>sys.stderr, err
#print>>sys.stderr, bamfile.name
if chromrange is not None:
#print >>sys.stderr, chromrange.chrom+":"+ str(chromrange.start)+"-"+str(chromrange.end) +" failed"
pass
class BamRead(GenomeRange):
__slots__ = "bamline"
def __init__(self,bamline, dbname, chrom, start, end, strand = None,name = None, fastafile = None):
GenomeRange.__init__(self, dbname, chrom, start, end, strand = strand,name = name, fastafile = None)
self.bamline = bamline
def getmismatches(self):
if self.bamline.has_tag("XM"):
return int(self.bamline.get_tag("XM"))
else:
return None
def isuniqueaminomapping(self):
if self.bamline.has_tag("YM"):
return not self.bamline.get_tag("YM") > 1
else:
return None
def isuniqueacmapping(self):
if self.bamline.has_tag("YA"):
return not self.bamline.get_tag("YA") > 1
else:
return None
def isuniquetrnamapping(self):
if self.bamline.has_tag("YR"):
return not self.bamline.get_tag("YR") > 1
else:
return None
def hasindel(self):
return len(self.bamline.cigar) > 1
def getlength(self):
return len(self.bamline.seq)
def getseq(self):
if self.strand == '-':
return revcom(self.bamline.seq)
else:
return self.bamline.seq
def getgc(self):
seq = self.getseq()
return sum(1 if currbase in set(["G","C"]) else 0 for currbase in seq)
def issinglemapped(self):
return self.bamline.mapq >= 2
def getcigar(self):
return self.bamline.cigar
def getbam(bamfile, chromrange = None, primaryonly = False, singleonly = False, allowindels=True):
bamiter = None
try:
if chromrange is not None:
bamiter = bamfile.fetch(chromrange.chrom, chromrange.start, chromrange.end)
else:
bamiter = bamfile.fetch()
for currline in bamiter:
if primaryonly and not isprimarymapping(currline):
continue
if singleonly and not issinglemapping(currline):
continue