-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget5ssFromGtf.py
49 lines (44 loc) · 1.75 KB
/
get5ssFromGtf.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
# no shebang line
import sys
import os
if len(sys.argv) != 2:
sys.exit('Please give the GTF filename in the command line.\n')
gtfName = sys.argv[1]
lineAbove = None
INGTF = open(gtfName, 'r')
OUT5SSREGION = open(gtfName + '.5ssRegion.bed', 'w')
#OUT5SSREGION.write("Pre exon\tNext exon\t5ssStart\t5ssEnd\tstrand\n")
for thisLine in INGTF:
thisLine = thisLine.strip()
if thisLine[0] == '#':
continue
if thisLine.split('\t')[2] == 'transcript':
#OUT5SSREGION.write(str(str(lineAbove).split('\t')[3:5]) + '\t' + str(thisLine.split('\t')[3:5]) + "\tLast exon no 5 ss\n")
lineAbove = None
thisLine = None
continue
if not thisLine.split('\t')[2] == 'exon':
continue
# exon in this line, parsing
if lineAbove is None:
lineAbove = thisLine
continue
else:
exonAbove = [int(x) for x in lineAbove.split('\t')[3:5]]
exonThis = [int(x) for x in thisLine.split('\t')[3:5]]
#OUT5SSREGION.write(str(exonAbove) + '\t' + str(exonThis) + '\t')
if exonAbove[1] < exonThis[0]:
ssStart = exonAbove[0] if exonAbove[1] - 200 < exonAbove[0] else exonAbove[1] - 200
#ssStart = exonAbove[1] - 1
ssEnd = exonThis[0] if exonAbove[1] + 200 > exonThis[0] else exonAbove[1] + 200
#ssEnd = exonAbove[1] + 1
OUT5SSREGION.write(lineAbove.split('\t')[0] + '\t' + str(ssStart) + '\t' + str(ssEnd) + '\t' + str(exonAbove[1]) + '\n')
else:
continue
ssStart = exonThis[0] if exonThis[1] - 200 < exonThis[0] else exonThis[1] - 200
ssEnd = exonAbove[0] if exonThis[1] + 200 > exonAbove[0] else exonThis[1] + 200
OUT5SSREGION.write(lineAbove.split('\t')[0] + '\t' + str(ssStart) + '\t' + str(ssEnd) + '\t' + str(exonThis[1]) + "\n")
lineAbove = thisLine
INGTF.close()
OUT5SSREGION.close()
print("Done!")