-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_distance_to_ss.py
195 lines (179 loc) · 8.7 KB
/
get_distance_to_ss.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
"""
@authors: Juan L. Trincado
@email: juanluis.trincado@upf.edu
get_distance_to_ss.py: Given the ORFs per transcript (take the longest per transcript),
the sequences of each transcript and the position of the ss, get the relative distance to this ss
If the distance returned is 0, that means the stop codon is falling in the final exon and the transcript
is not going into NMD. If the distance is between 1 and 50, the stop codon is not falling in the last
exon but is not going to NMD. If its greater than 50, thn it goes to NMD
"""
import logging, sys, os, re
from copy import deepcopy
from argparse import ArgumentParser, RawTextHelpFormatter
# create logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# create console handler and set level to info
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
def main():
try:
orfs_path = sys.argv[1]
transcripts_paths = sys.argv[2]
output_path = sys.argv[3]
#
# orfs_path = "/home/shinoda/Desktop/Florida/annotation/MBNL1_TEST/MBNL3_possible_ORFs_refseq.fa"
# transcripts_paths = "/home/shinoda/Desktop/Florida/annotation/MBNL1_TEST/MBNL3_possible_transcripts_refseq.fa.paths"
# output_path = "/home/shinoda/Desktop/Florida/annotation/MBNL1_TEST/MBNL3_evaluated_refseq.paths"
# 1. Extract just the first ORF (the longest) from each transcript
outFile = open(orfs_path+".unique", 'w')
with open(orfs_path) as f:
logger.info("Processing ORFs file...")
transcript_id, transcript_id_old = "Transcript_0", ""
for line in f:
# If its header
if(re.search(">", line)):
#If the transcript id changes, output the next 2 lines
if(not(re.search(transcript_id, line))):
tokens = line.rstrip().split(":")
transcript_id = tokens[0][1:]
pass_flag = False
outFile.write(line)
else:
pass_flag = True
# If its sequence
else:
if(pass_flag):
pass
else:
outFile.write(line)
outFile.close()
# 2. Using the info in the header of the ORFs unique file, we can obtain the information of
# whether our stop codon is falling
results = {}
with open(orfs_path+".unique") as f:
logger.info("Processing ORFs unique file...")
for line in f:
# If its header
if (re.search(">", line)):
# Two things: obtain the differences for each exon associated (with the cordinates)
# and obtain the length of the ORF
tokens = line.rstrip().split(":")
strand = tokens[1]
transcript_id = tokens[0][1:]
exons = tokens[2:-1]
ORF = tokens[-1].split("-")
# Get the difference for each exon. Go substracting the sequences according to the distance
# to the ORF end
first_pos = int(ORF[0])
second_pos = int(ORF[1])
pos = 0
flag_1, flag_2, flag_3 = True, False, False
i = 0
for x in exons:
coords = x.split("_")
length_exon = int(coords[1]) - int(coords[0])
if (flag_1):
# coords = x.split("_")
# length_exon = int(coords[1]) - int(coords[0])
if((pos + length_exon) < first_pos):
pass
# pos += length_exon
# Reached start codon
else:
offset = first_pos - pos
if(strand=="+"):
start_codon = int(coords[0]) + offset
else:
start_codon = int(coords[1]) - offset
flag_1 = False
flag_2 = True
if ((pos + length_exon) < second_pos):
pass
# pos += length_exon
# Reached stop codon
else:
# offset = second_pos - (pos + length_exon)
offset = second_pos - pos
if (strand == "+"):
stop_codon = int(coords[0]) + offset
else:
stop_codon = int(coords[1]) - offset
flag_2 = False
flag_3 = True
# pos += length_exon
# if (i == len(exons) - 1):
# distance = pos - second_pos
# else:
# pass
if (flag_2):
# coords = x.split("_")
# length_exon = int(coords[1]) - int(coords[0])
if ((pos + length_exon) < second_pos):
pass
# pos += length_exon
# Reached stop codon
else:
offset = second_pos - pos
if (strand == "+"):
stop_codon = int(coords[0]) + offset
else:
stop_codon = int(coords[1]) - offset
flag_2 = False
flag_3 = True
# pos += length_exon
# If reached last exon, the stop codon is in the last exon. Therefore, there won't be NMD
# if (i==len(exons)-1):
# # distance = (pos + length_exon) - second_pos
# distance = 0
# break
# else:
# pass
if (flag_3):
# If not reached last exon, get the distance to the 3'ss
if(i!=len(exons)-1):
# coords = x.split("_")
# length_exon = int(coords[1]) - int(coords[0])
# pos += length_exon
distance = (pos + length_exon) - second_pos
break
# If reached last exon, the stop codon is in the last exon. Therefore, there won't be NMD
else:
distance = 0
break
pos += length_exon
i += 1
results[transcript_id] = [start_codon,stop_codon,distance]
else:
pass
# 3. Associate the info to the transcripts_paths file
outFile = open(output_path, 'w')
with open(transcripts_paths) as f:
logger.info("Processing transcripts_paths file...")
for line in f:
tokens = line.rstrip().split(":")
transcript_id = tokens[0][1:]
#Get the information from the previous results
if(transcript_id in results):
info = results[transcript_id]
outFile.write(":".join(tokens)+"\tStart_codon: "+str(info[0])+
"\tStop_codon: "+str(info[1])+
"\tDistance: "+str(info[2])+"\n")
else:
outFile.write(":".join(tokens)+"\tNo ORF\n")
outFile.close()
logger.info("Saved " + output_path)
logger.info("Done. Exiting program.")
exit(0)
except Exception as error:
logger.error('ERROR: ' + repr(error))
logger.error("Aborting execution")
sys.exit(1)
if __name__ == '__main__':
main()