-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGeneList.py
1347 lines (1221 loc) · 54.5 KB
/
GeneList.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
#!/usr/bin/env python
import os
import sys
import os.path
import sqlite3 as sql
import Utils
## DB Utilities
def selectValue(conn, query, *args):
r = conn.execute(query.format(*args))
v = r.fetchone()
if v:
return v[0]
else:
return None
def selectColumn(conn, query, *args):
result = []
r = conn.execute(query.format(*args))
while True:
v = r.fetchone()
if v:
result.append(v[0])
else:
break
return result
def parseStartEnd(s):
"""Parse a range specification in Genbank format. It can contain complement and join operations."""
cl = len('complement')
jl = len('join')
strand = 1
if s[0:cl] == 'complement':
strand = -1
s = s[cl+1:-1]
if s[0:jl] == 'join':
s = s[jl+1:-1]
cp = s.find(",")
if cp > 0:
pairs = [ parseCoords(z) for z in s.split(",") ]
introns = []
for i in range(len(pairs)-1):
introns.append((pairs[i][1], pairs[i+1][0]))
return (pairs[0][0], pairs[-1][1], strand, introns)
else:
(st, en) = parseCoords(s)
return (st, en, strand, None)
def parseCoords(c):
"""Parse a pair of coordinates in the form X..Y and return them as ints."""
dp = c.find(".")
if dp > 0:
return (int(c[0:dp]), int(c[dp+2:]))
else:
return None
# Classification of positions
class Classif(object):
idx = 0
name = ""
subject = None
extra = ""
def __init__(self, subject=None, extra=""):
self.subject = subject
self.extra = extra
class CL_NONE(Classif):
idx = 0
name = 'o'
class CL_CODING(Classif):
idx = 90
name = 'E'
class CL_EXON(Classif):
idx = 80
name = 'e'
class CL_UPSTREAM(Classif):
idx = 70
name = 'u'
class CL_INTRON(Classif):
idx = 60
name = 'i'
class CL_DNSTREAM(Classif):
idx = 50
name = 'd'
class CL_ENHANCER(Classif):
idx = 65
name = 'h'
CLASSIFICATIONS = [CL_NONE, CL_CODING, CL_EXON, CL_UPSTREAM, CL_INTRON, CL_DNSTREAM, CL_ENHANCER]
def setClassificationOrder(keys):
idx = 100
for cl in CLASSIFICATIONS:
cl.idx = 0
for key in keys:
for cl in CLASSIFICATIONS:
if cl.name == key:
cl.idx = idx
idx += -1
def addClassification(cls, clist):
"""Add classification `cls' to list `clist' only if
it does not already appear."""
for x in clist:
if x.name == cls.name:
if cls.name in "Ee":
if cls.extra == x.extra:
return clist
else:
return clist
clist.append(cls)
return clist
def sortClassifications(cls):
cls.sort(key=lambda c: c.idx, reverse=True)
return cls
# Classes
class Genelist():
chroms = []
genes = {}
ngenes = 0
indexes = {}
btFlags = {}
currentChrom = ""
currentGenes = ""
source = ""
def __init__(self):
self.chroms = []
self.genes = {}
self.btFlags = {"": True, "*": True}
# Next two methods allow a genelist to be used in a with statement
# Useful for GenelistDB.
def __enter__(self):
return self
def __exit__(self, a, b, c):
pass
def saveAllToDB(self, filename):
"""Save all genes to the database represented by connection `conn'."""
tot = 0 # Total number of genes written
conn = sql.connect(filename)
with conn:
for chrom in self.chroms:
sys.stderr.write(" {}... ".format(chrom))
n = 0
for g in self.genes[chrom]:
g.saveToDB(conn)
n += 1
sys.stderr.write("{} genes written.\n".format(n))
tot += n
self.setTSS(conn)
self.setCanonical(conn)
self.setCounts(conn)
conn.execute("INSERT INTO Source (filename) values (?);", (self.source,))
return tot
def setTSS(self, conn):
sys.stderr.write("Setting TSS...\n")
conn.execute("UPDATE Genes SET tss=start WHERE strand='1';")
conn.execute("UPDATE Genes SET tss=end WHERE strand='-1';")
conn.execute("UPDATE Transcripts SET tss=txstart WHERE strand='1';")
conn.execute("UPDATE Transcripts SET tss=txend WHERE strand='-1';")
def setCanonical(self, conn):
sys.stderr.write("Setting Canonical...\n")
c = conn.cursor()
for r in conn.execute("SELECT ID FROM Genes;"):
gid = r[0]
maxtr = 0
best = ""
for tr in c.execute("SELECT ID, txstart, txend FROM Transcripts WHERE parentID=?;", (gid,)):
m = int(tr[2]) - int(tr[1])
if m > maxtr:
maxtr = m
best = tr[0]
c.execute("UPDATE Genes SET canonical=? WHERE ID=?;", (best, gid))
c.execute("UPDATE Transcripts SET canonical='Y' WHERE ID=?;", (best,))
def setCounts(self, conn):
sys.stderr.write("Updating Counts...\n")
ngenes = selectValue(conn, "SELECT count(*) from Genes;")
ntrans = selectValue(conn, "SELECT count(*) from Transcripts;")
nexons = selectValue(conn, "SELECT count(*) from Exons;")
conn.execute("INSERT INTO Counts (ngenes, ntranscripts, nexons) VALUES (?, ?, ?);", (ngenes, ntrans, nexons))
def getGenesTable(self):
table = {}
for chrom in self.chroms:
for gene in self.genes[chrom]:
table[gene.ID] = {'gene_id': gene.ID,
'gene_biotype': gene.biotype or "???",
'gene_name': gene.name}
return table
def getTranscriptsTable(self):
table = {}
for chrom in self.chroms:
for gene in self.genes[chrom]:
for tx in gene.transcripts:
table[tx.ID] = {'transcript_id': tx.ID,
'gene_biotype': gene.biotype or "???",
'transcript_name': tx.name,
'gene_name': gene.name}
return table
def setWanted(self, wanted):
"""Set all biotypes in `wanted' to True, all others to False."""
for w in wanted:
self.btFlags[w] = True
self.btFlags["*"] = False
def setNotWanted(self, notwanted):
"""Set all biotypes in `notwanted' to False, all others to True."""
for w in notwanted:
self.btFlags[w] = False
self.btFlags["*"] = True
def add(self, gene, chrom):
bt = gene.biotype
if bt in self.btFlags:
w = self.btFlags[bt]
else:
w = self.btFlags["*"]
if w:
if chrom not in self.chroms:
self.chroms.append(chrom)
self.genes[chrom] = []
self.genes[chrom].append(gene)
self.ngenes += 1
def selectChrom(self, chrom):
if chrom != self.currentChrom and chrom in self.chroms:
self.currentChrom = chrom
self.currentGenes = self.genes[chrom]
return self.currentGenes
def genesOnChrom(self, chrom):
if chrom in self.genes:
l = self.selectChrom(chrom)
return l
else:
return []
def allGeneNames(self):
result = []
for (chrom, cgenes) in Utils.get_iterator(self.genes):
for cg in cgenes:
result.append(cg.name)
return result
def findGene(self, name, chrom=None):
if chrom:
cgenes = self.genes[chrom]
for g in cgenes:
if g.ID == name or g.name == name:
return g
return None
else:
for ch in self.chroms:
g = self.findGene(name, chrom=ch)
if g:
return g
return None
def sortGenes(self):
for chrom in self.chroms:
self.genes[chrom].sort(key=lambda g:g.start)
def buildIndexes(self):
step = 100
idxs = {}
for chrom, genes in Utils.get_iterator(self.genes):
ng = len(genes)
d = []
# print("chr={}, genes={}".format(chrom, len(genes)))
for i in range(0, len(genes), step):
i2 = min(i+step, ng) - 1
# print("i1={}, i2={}".format(i, i+step-1))
gfirst = genes[i]
glast = genes[i2]
d.append([gfirst.start, glast.end, i, i2])
idxs[chrom] = d
# print(d)
# raw_input()
self.indexes = idxs
def positionsToRange(self, chrom, start, end):
"""Returns the first and last index for genes in the range `start' to `end'."""
first = last = 0
if chrom in self.indexes:
idxs = self.indexes[chrom]
for i in range(0, len(idxs)):
iblock = idxs[i]
if iblock[0] <= start <= iblock[1]:
sb = iblock
eb = iblock
if i > 0:
sb = idxs[i-1]
if i < len(idxs) - 1:
eb = idxs[i+1]
first = sb[2]
last = eb[3]
break
return (first, last)
def classifyIntersection(self, astart, aend, g):
"""A is mine, B is other."""
bstart = g.txstart
bend = g.txend
if astart < bstart:
if bstart <= aend <= bend:
how = 'left'
alen = aend-astart+1
blen = bend-bstart+1
isiz = aend-bstart+1
return (g, how, 1.0*isiz/alen, 1.0*isiz/blen)
elif aend > bend:
how = 'contains'
alen = aend-astart+1
isiz = bend-bstart+1
return (g, how, 1.0*isiz/alen, 1.0)
elif bstart <= astart <= bend:
if aend <= bend:
how = 'contained'
blen = bend-bstart+1
isiz = aend-astart+1
return (g, how, 1.0, 1.0*isiz/blen)
else:
how = 'right'
alen = aend-astart+1
blen = bend-bstart+1
isiz = bend-astart+1
return (g, how, 1.0*isiz/alen, 1.0*isiz/blen)
return None
def allIntersecting(self, chrom, start, end):
"""Returns all genes in `chrom' that intersect the `start-end' region."""
result = []
self.selectChrom(chrom)
genes = self.currentGenes
(first, last) = self.positionsToRange(chrom, start, end)
# print("Looking at {}-{}".format(first, last))
for i in range(first, last+1):
ix = self.classifyIntersection(start, end, genes[i])
if ix:
result.append(ix)
return result
def intersectFromBED(self, bedfile, outfile):
with open(outfile, "w") as out:
with open(bedfile, "r") as f:
f.readline()
for line in f:
parsed = line.rstrip("\n\r").split("\t")
allint = self.allIntersecting(parsed[0], int(parsed[1]), int(parsed[2]))
for a in allint:
g = a[0]
out.write("\t".join(parsed + [g.name, g.biotype, a[1], Utils.f2dd(a[2]*100), Utils.f2dd(a[3]*100)]) + "\n")
def notIntersectFromBED(self, bedfile, outfile):
with open(outfile, "w") as out:
with open(bedfile, "r") as f:
f.readline()
for line in f:
parsed = line.rstrip("\n\r").split("\t")
s = int(parsed[1])
e = int(parsed[2])
allint = self.allIntersecting(parsed[0], s, e)
if allint == []:
out.write("\t".join(parsed) + "\t{}\n".format(1+e-s))
def intronsToBED(self, bedfile):
with open(bedfile, "w") as out:
for chrom in self.chroms:
for gene in self.genes[chrom]:
intid = 1
prevexon = gene.exons[0]
for ex in gene.exons[1:]:
start = prevexon[1] + 1
end = ex[0] - 1
if gene.strand == 1:
out.write("{}\t{}\t{}\t{}_{}\t{}\t{}\n".format(chrom, start, end, gene.mrna, intid, gene.name, gene.strand))
else:
out.write("{}\t{}\t{}\t{}_{}\t{}\t{}\n".format(chrom, end, start, gene.mrna, intid, gene.name, gene.strand))
# print([chrom, end, start, gene.mrna, intid, gene.name, gene.strand])
# raw_input()
intid += 1
prevexon = ex
def junctionsToBED(self, bedfile, size=5):
with open(bedfile, "w") as out:
for chrom in self.chroms:
for gene in self.genes[chrom]:
intid = 1
for ex in gene.exons:
out.write("{}\t{}\t{}\t{}_{}_a\t{}\t{}\n".format(chrom, ex[0]-10, ex[0]+10, gene.mrna, intid-1, gene.name, gene.strand))
out.write("{}\t{}\t{}\t{}_{}_b\t{}\t{}\n".format(chrom, ex[1]-10, ex[1]+10, gene.mrna, intid, gene.name, gene.strand))
intid += 1
class GenelistDB(Genelist):
dbname = None
dbconn = None
_level = 0
dbcurs = None
preloaded = False # Did we preload all genes into the Genelist?
genesTable = {}
transcriptsTable = {}
def __enter__(self):
self._level += 1
if not self.dbconn:
# sys.stderr.write("Opening connection to db {}\n".format(self.dbname))
self.dbconn = sql.connect(self.dbname)
return self.dbconn
def __exit__(self, exc_type, exc_value, exc_tb):
if self.dbconn:
self._level -= 1
if self._level == 0:
# sys.stderr.write("Closing db connection.\n")
self.dbconn.close()
self.dbconn = None
def getGenesTable(self):
if not self.genesTable:
with self:
for row in self.dbconn.execute("SELECT ID, biotype, name FROM genes;"):
self.genesTable[row[0]] = {'gene_id': row[0],
'gene_biotype': row[1] or "???",
'gene_name': row[2]}
return self.genesTable
def getTranscriptsTable(self):
if not self.transcriptsTable:
with self:
for row in self.dbconn.execute("SELECT transcripts.ID, genes.biotype, transcripts.name, genes.name FROM transcripts, genes WHERE genes.ID=transcripts.parentID;"):
self.transcriptsTable[row[0]] = {'transcript_id': row[0],
'gene_biotype': row[1] or "???",
'transcript_name': row[2],
'gene_name': row[3]}
return self.transcriptsTable
def allGeneNames(self):
names = []
with self:
#conn = sql.connect(self.dbname)
# try:
curr = self.dbconn.execute("SELECT name FROM Genes ORDER BY name")
names = [ r[0] for r in curr.fetchall() ]
# finally:
# conn.close()
return names
def findGene(self, name, chrom=None):
"""Returns the gene called `name'. The name is matched against the ID, name, geneid, and ensg fields."""
with self:
row = self.dbconn.execute("SELECT ID, name, geneid, ensg, biotype, chrom, strand, start, end FROM Genes WHERE ID=? OR name=? OR geneid=? OR ensg=?",
(name, name, name, name)).fetchone()
if row:
gid = row[0]
g = Gene(gid, row[5], row[6])
for pair in zip(['ID', 'name', 'geneid', 'ensg', 'biotype', 'chrom', 'strand', 'start', 'end'], row):
setattr(g, pair[0], pair[1])
for trow in self.dbconn.execute("SELECT ID, name, accession, enst, chrom, strand, txstart, txend, cdsstart, cdsend FROM Transcripts WHERE parentID=?", (gid,)):
tid = trow[0]
tr = Transcript(tid, trow[4], trow[5], trow[6], trow[7])
tr.exons = []
for pair in zip(['ID', 'name', 'accession', 'enst', 'chrom', 'strand', 'txstart', 'txend', 'cdsstart', 'cdsend'], trow):
setattr(tr, pair[0], pair[1])
for erow in self.dbconn.execute("SELECT start, end FROM Exons WHERE ID=? ORDER BY idx", (tid,)):
tr.addExon(erow[0], erow[1])
g.addTranscript(tr)
return g
else:
return None
def allTranscriptNames(self):
names = []
with self:
curr = self.dbconn.execute("SELECT ID FROM Transcripts ORDER BY ID")
names = [ r[0] for r in curr.fetchall() ]
return names
def findTranscript(self, name, chrom=None):
"""Returns the transcript called `name'. The name is matched against the ID, name, accession, and enst fields."""
with self:
trow = self.dbconn.execute("SELECT ID, name, accession, enst, chrom, strand, txstart, txend, cdsstart, cdsend FROM Transcripts WHERE ID=? OR name=? OR accession=? OR enst=?",
(name, name, name, name)).fetchone()
if trow:
tid = trow[0]
tr = Transcript(tid, trow[4], trow[5], trow[6], trow[7])
tr.exons = []
for pair in zip(['ID', 'name', 'accession', 'enst', 'chrom', 'strand', 'txstart', 'txend', 'cdsstart', 'cdsend'], trow):
setattr(tr, pair[0], pair[1])
for erow in self.dbconn.execute("SELECT start, end FROM Exons WHERE ID=? ORDER BY idx", (tid,)):
tr.addExon(erow[0], erow[1])
return tr
else:
return None
def getAllTranscripts(self):
"""Returns an iterator that lopps over all transcripts."""
with self:
tcur = self.dbconn.cursor()
ecur = self.dbconn.cursor()
for trow in tcur.execute("SELECT t.ID, t.name, accession, enst, t.chrom, t.strand, txstart, txend, cdsstart, cdsend, g.name FROM Transcripts t, Genes g WHERE t.parentID = g.ID ORDER BY t.chrom, txstart"):
if trow:
tid = trow[0]
tr = Transcript(tid, trow[4], trow[5], trow[6], trow[7])
tr.gene = trow[10]
tr.exons = []
for pair in zip(['ID', 'name', 'accession', 'enst', 'chrom', 'strand', 'txstart', 'txend', 'cdsstart', 'cdsend'], trow):
setattr(tr, pair[0], pair[1])
for erow in ecur.execute("SELECT start, end FROM Exons WHERE ID=? ORDER BY idx", (tid,)):
tr.addExon(erow[0], erow[1])
yield tr
def findGenes(self, query, args=[]):
"""Returns the list of all genes that satisfy the `query'. `query' should be a SQL statement that returns
a single column of values from the ID field."""
result = []
with self:
gcur = self.dbconn.cursor()
for geneIDrow in self.dbconn.execute(query, args):
geneID = geneIDrow[0]
row = gcur.execute("SELECT ID, name, geneid, ensg, biotype, chrom, strand, start, end FROM Genes WHERE ID=?", (geneID,)).fetchone()
if row:
g = Gene(geneID, row[5], row[6])
for pair in zip(['ID', 'name', 'geneid', 'ensg', 'biotype', 'chrom', 'strand', 'start', 'end'], row):
setattr(g, pair[0], pair[1])
for trow in self.dbconn.execute("SELECT ID, name, accession, enst, chrom, strand, txstart, txend, cdsstart, cdsend, canonical FROM Transcripts WHERE parentID=?", (geneID,)):
tid = trow[0]
tr = Transcript(tid, trow[4], trow[5], trow[6], trow[7])
tr.exons = []
for pair in zip(['ID', 'name', 'accession', 'enst', 'chrom', 'strand', 'txstart', 'txend', 'cdsstart', 'cdsend'], trow):
setattr(tr, pair[0], pair[1])
tr.canonical = (trow[10] == 'Y')
for erow in self.dbconn.execute("SELECT start, end FROM Exons WHERE ID=? ORDER BY idx", (tid,)):
tr.addExon(erow[0], erow[1])
g.addTranscript(tr)
result.append(g)
return result
def allIntersecting(self, chrom, start, end):
"""Returns all genes in `chrom' that intersect the `start-end' region."""
return self.findGenes("SELECT ID from Genes where chrom=? and ((? <= start) and (start <= ?) or ((? <= end) and (end <= ?)) or ((start <= ?) and (end >= ?)))",
(chrom, start, end, start, end, start, end))
def findClosestGene(self, chrom, start, end, transcripts=True, biotype=None, tss=False, canonical=False):
"""Find the closest gene to the region chrom:start-end, in either direction. Returns a tuple: (gene, distance).
Distance can be positive (downstream of `end') or negative (upstream of `start'). If `transcripts' is True, look at
transcript instead of genes; in this case the return value is (transcript ID, distance). If `biotype' is specified,
restrict the search to genes with that biotype (e.g., protein_coding). If `tss' is True, distances are computed relative
to the gene's (or transcript's) TSS.
"""
g1 = None
g2 = None
p1 = 0
p2 = 0
d1 = 0
d2 = 0
args = {'table': "Transcripts" if transcripts else "Genes",
'chrom': chrom,
'start': start,
'end': end,
'fstart': "start",
'fend': "end",
'biotype': "AND biotype='{}' ".format(biotype) if biotype else "",
'canon': "AND canonical='Y' " if (transcripts and canonical) else ""}
if transcripts:
args['fstart'] = 'txstart'
args['fend'] = 'txend'
elif tss:
args['fstart'] = 'tss'
args['fend'] = 'tss'
with self:
query0 = "SELECT ID, {fstart}, {fend} FROM {table} WHERE chrom='{chrom}' AND {fstart} <= {start} AND {fend} >= {end} {biotype} {canon} ORDER BY {fstart} DESC LIMIT 1;".format(**args) # containing
query1 = "SELECT ID, {fend} FROM {table} WHERE chrom='{chrom}' AND {fend} <= {start} {biotype} {canon} ORDER BY {fend} DESC LIMIT 1;".format(**args) # gene is upstream of region
query2 = "SELECT ID, {fstart} FROM {table} WHERE chrom='{chrom}' AND {fstart} >= {end} {biotype} {canon} ORDER BY {fstart} LIMIT 1;".format(**args) # gene is downstream of region
r0 = self.dbconn.execute(query0).fetchone()
if r0:
g0 = r0[0]
p1 = r0[1]
p2 = r0[2]
d1 = start - p1
d2 = p2 - end
if d1 < d2:
return (g0, d1)
else:
return (g0, -d2)
r1 = self.dbconn.execute(query1).fetchone()
if r1:
g1 = r1[0]
p1 = r1[1]
d1 = start - p1
# print (r1, d1)
r2 = self.dbconn.execute(query2).fetchone()
if r2:
g2 = r2[0]
p2 = r2[1]
d2 = p2 - end
# print (r2, d2)
if p1 == 0 and p2 == 0: # No results?? This should not happen...
return (None, 0)
elif p1 == 0: # No upstream, we only have downstream
return (g2, -d2)
elif p2 == 0: # No downstream, we only have upstream
return (g1, d1)
else:
#print (d1, d2)
if d1 < d2: # We have both, but upstream is closer
return (g1, d1)
else:
return (g2, -d2)
def getGeneInfo(self, geneid, query):
with self:
gcur = self.dbconn.cursor()
row = gcur.execute(query, (geneid,)).fetchone()
return row
# Transcript class
class Transcript():
ID = ""
name = ""
chrom = ""
strand = ""
accession = ""
enst = ""
txstart = 0
txend = 0
cdsstart = None
cdsend = None
strand = None
canonical = False
exons = []
smallrects = []
largerects = []
gene = "" # Not normally used...
def __init__(self, ID, chrom, strand, txstart, txend):
self.ID = ID
self.chrom = chrom
self.strand = strand
self.txstart = txstart
self.txend = txend
self.exons = [(txstart, txend)] # We initially assume transcript has no introns
self.smallrects = []
self.largerects = []
def dump(self, prefix="", short=False):
if short:
print("{}{} {}:{}-{} {}".format(prefix, self.ID, self.chrom, self.txstart, self.txend, self.exons))
else:
print(prefix + "ID: " + self.ID)
print(prefix + "Chrom: " + self.chrom)
print("{}Strand: {}".format(prefix, self.strand))
print("{}Transcript: {}-{}".format(prefix, self.txstart, self.txend))
print("{}CDS: {}-{}".format(prefix, self.cdsstart, self.cdsend))
print("{}Exons: {}".format(prefix, self.exons))
def saveToDB(self, conn, parentID):
idx = 0
for ex in self.exons:
conn.execute("INSERT INTO Exons(ID, idx, chrom, start, end) VALUES (?, ?, ?, ?, ?);",
(self.ID, idx, self.chrom, ex[0], ex[1]))
idx += 1
try:
conn.execute("INSERT INTO Transcripts (ID, parentID, name, accession, enst, chrom, strand, txstart, txend, cdsstart, cdsend) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",
(self.ID, parentID, self.name, self.accession, self.enst, self.chrom, self.strand, self.txstart, self.txend, self.cdsstart, self.cdsend))
except sql.IntegrityError:
sys.stderr.write("Error: transcript ID {} is not unique.\n".format(self.ID))
def addExon(self, start, end):
self.exons.append((start, end))
def setIntrons(self, introns):
self.exons = [(self.txstart, introns[0][0])]
for i in range(len(introns)-1):
self.exons.append((introns[i][1], introns[i+1][0]))
self.exons.append((introns[-1][1], self.txend))
def updateCDS(self, cdsstart, cdsend):
if self.cdsstart:
self.cdsstart = min(self.cdsstart, cdsstart)
else:
self.cdsstart = cdsstart
if self.cdsend:
self.cdsend = max(self.cdsend, cdsend)
else:
self.cdsend = cdsend
def setCDS(self, cdsstart, cdsend):
"""Set the CDS of this transcript to `cdsstart' and `cdsend'. This also sets the
smallrects and largerects lists."""
if cdsstart and cdsend:
self.cdsstart = cdsstart
self.cdsend = cdsend
self.smallrects = [] # we're recomputing them
self.largerects = [] # from scratch
small = True
for e in self.exons:
if (e[0] <= self.cdsstart < self.cdsend <= e[1]): # CDS entirely contained in exon?
self.smallrects.append((e[0], self.cdsstart))
self.largerects.append((self.cdsstart, self.cdsend))
self.smallrects.append((self.cdsend, e[1]))
elif (e[0] <= self.cdsstart <= e[1]): # Exon contains start of CDS?
self.smallrects.append((e[0], self.cdsstart))
self.largerects.append((self.cdsstart, e[1]))
small = False
elif (e[0] <= self.cdsend <= e[1]): # Exon contains end of CDS?
self.largerects.append((e[0], self.cdsend))
self.smallrects.append((self.cdsend, e[1]))
small = True
elif small:
self.smallrects.append(e)
else:
self.largerects.append(e)
def posInExon(self, pos):
"""If `pos' belongs to an exon of this trancript, return the exon number, otherwise return the intron number (negative)."""
nexons = len(self.exons) + 1
ne = 1
for e in self.exons:
if pos < e[0]:
if self.strand == 1:
return -(ne-1)
else:
return -(nexons - (ne-1))
if e[0] <= pos <= e[1]:
if self.strand == 1:
return ne
else:
return nexons - ne
ne += 1
return False
def positionMatch(self, pos, mode, distance):
"""Return True if position `pos' matches transcript according to `mode'.
`mode' can be one of b, P, d, e, or i. `distance' is used when `mode' includes p or d."""
for m in mode:
if m == 'b':
if self.txstart <= pos <= self.txend:
return True
elif m == 'p':
if self.strand == 1:
if self.txstart - distance <= pos < self.txstart:
return True
else:
if self.txend < pos <= self.txend + distance:
return True
elif m == 'd':
if self.strand == -1:
if self.txstart - distance <= pos < self.txstart:
return True
else:
if self.txend < pos <= self.txend + distance:
return True
else:
ex = self.posInExon(pos)
if m == 'e' and ex:
return True
if m == 'i' and not ex:
return True
return False
def classifyPosition(self, pos, updistance, dndistance):
"""Returns a Classif instance that classifies position `pos' within this transcript,
including `updistance' bases upstream of the TSS and `dndistance' bases downstream of the TES.
If the class is CL_CODING, CL_EXON, or CL_INTRON, the `extra' attribute contains the exon or
intron number.
"""
# print((pos, self.txstart, self.txend, self.cdsstart, self.cdsend))
if self.txstart <= pos <= self.txend:
ne = self.posInExon(pos)
if ne>0:
if self.cdsstart and self.cdsend and (self.cdsstart <= pos <= self.cdsend):
return CL_CODING(subject=self, extra=ne)
else:
return CL_EXON(subject=self, extra=ne)
return CL_INTRON(subject=self, extra=ne)
elif self.strand == 1:
if self.txstart - updistance <= pos < self.txstart:
return CL_UPSTREAM(subject=self)
elif self.txend < pos <= self.txend + dndistance:
return CL_DNSTREAM(subject=self)
else:
return None
else:
if self.txstart - dndistance <= pos < self.txstart:
return CL_DNSTREAM(subject=self)
elif self.txend < pos <= self.txend + updistance:
return CL_UPSTREAM(subject=self)
else:
return None
def getRegion(self, params):
"""Returns the region for this transcript as (start, end) according to the 'regwanted',
'updistance, and 'dndistance' attributes in the `params' object."""
if self.txstart and self.txend:
if params.regwanted == 'b':
return (self.txstart, self.txend)
elif params.regwanted == 'u':
if self.strand == 1:
return (max(0, self.txstart - params.updistance), self.txstart + params.dndistance)
else:
return (max(0, self.txend - params.dndistance), self.txend + params.updistance)
elif params.regwanted == 'd':
if self.strand == 1:
return (max(0, self.txend - params.updistance), self.txend + params.dndistance)
else:
return (max(0, self.txstart - params.dndistance), self.txstart + params.updistance)
else:
return None
def geneLimits(self, upstream, downstream, ref='B'):
"""Returns the start and end coordinates of a region extending from `upstream' bases
upstream of the TSS to `downstream' bases downstream of the TSE. If `ref' is equal to S, both
coordinates are computed according to the TSS, and if it is 'E', both coordinates are computed
according to the TSE. Takes strand of transcript into account."""
if ref == 'B':
if self.strand == 1:
return (self.txstart - upstream, self.txend + downstream)
else:
return (self.txstart - downstream, self.txend + upstream)
elif ref == 'S':
if self.strand == 1:
return (self.txstart - upstream, self.txstart + downstream)
else:
return (self.txend - downstream, self.txend + upstream)
elif ref == 'E':
if self.strand == 1:
return (self.txend - upstream, self.txend + downstream)
else:
return (self.txend - downstream, self.txend + upstream)
def distanceFrom(self, position, ref='S'):
"""Returns the distance from `position' to the TSS (if ref=S) or the TSE (if ref=E).
The distance is negative if `position' is upstream of the reference point, positive if
downstream."""
if ref == 'S':
if self.strand == 1:
return position - self.txstart
else:
return self.txend - position
else:
if self.strand == 1:
return position - self.txend
else:
return self.txend - position
### Gene class
class Gene():
ID = ""
name = ""
geneid = ""
ensg = ""
biotype = ""
description = ""
chrom = ""
strand = ""
start = None # leftmost txstart
end = None # rightmost txend
transcripts = []
data = []
def __init__(self, ID, chrom, strand):
self.ID = ID
self.chrom = chrom
self.strand = strand
self.transcripts = []
self.data = []
def dump(self):
print("ID: " + self.ID)
print("Gene: " + self.name)
print("GeneID: " + self.geneid)
print("ENSG: " + self.ensg)
print("Biotype: " + self.biotype)
print("Region: {}:{}-{}".format(self.chrom, self.start, self.end))
print("Strand: {}".format(self.strand))
print("Transcripts:")
for t in self.transcripts:
t.dump(prefix=" ", short=True)
def saveToDB(self, conn):
for tr in self.transcripts:
tr.saveToDB(conn, self.ID)
conn.execute("INSERT INTO Genes (ID, name, geneid, ensg, biotype, chrom, strand, start, end) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);",
(self.ID, self.name, self.geneid, self.ensg, self.biotype, self.chrom, self.strand, self.start, self.end))
def addTranscript(self, transcript):
self.transcripts.append(transcript)
if self.start:
self.start = min(self.start, transcript.txstart)
else:
self.start = transcript.txstart
if self.end:
self.end = max(self.end, transcript.txend)
else:
self.end = transcript.txend
def classifyPosition(self, position, updistance, dndistance, best=False):
"""Returns all possible classifications of `position' for the transcript of this gene.
If `best' is True, only returns a single classification, ie the first one to appear in this list: """
result = []
for tr in self.transcripts:
c = tr.classifyPosition(position, updistance, dndistance)
#print((tr.ID, c))
if c:
c.subject = self
addClassification(c, result)
if len(result) == 0:
return [CL_NONE()]
else:
sortClassifications(result)
if best:
return [result[0]]
else:
return result
def getRegion(self, params):
"""Returns the region for this gene as (start, end) according to the 'regwanted',
'updistance, and 'dndistance' attributes in the `params' object."""
#print(self.name, self.start, self.end)
if self.start and self.end:
if params.regwanted == 'b':
return (self.start, self.end)
elif params.regwanted == 'B':
if self.strand == 1:
return (max(0, self.start - params.updistance), self.end + params.dndistance)
else:
return (max(0, self.start - params.dndistance), self.end + params.updistance)
elif params.regwanted == 'p':
if self.strand == 1:
return (max(0, self.start - params.updistance), self.start)
else:
return (self.end, self.end + params.dndistance)
elif params.regwanted == 'u':
if self.strand == 1:
return (max(0, self.start - params.updistance), self.start + params.dndistance)
else:
return (max(0, self.end - params.dndistance), self.end + params.updistance)
elif params.regwanted == 'd':
if self.strand == 1:
return (max(0, self.end - params.updistance), self.end + params.dndistance)
else:
return (max(0, self.start - params.dndistance), self.start + params.updistance)
else:
return None
def getLoader(filename):
"""Returns the name of the loader class to be used to load the gene database in `filename'.
Currently recognizes the following extensions: .gb (genbank), .gtf (GTF), .gff/.gff3 (GFF), .db
(sqlite3 database)."""
loadermap = {'.gb': GenbankLoader,
'.gtf': GTFloader,
'.gff': GFFloader,
'.gff3': GFFloader,
'.GTF': GTFloader,
'.GFF': GFFloader,
'.GFF3': GFFloader,
'.db': DBloader,
'.csv': refGeneLoader}
ext = os.path.splitext(filename)[1]
if ext in loadermap:
return loadermap[ext]
else:
return None
class GeneLoader():
gl = None
filename = ""
currGene = None
currTranscript = None
add_chr = not True