-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_promoters.py
202 lines (190 loc) · 8.48 KB
/
create_promoters.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 22 16:02:07 2017
@author: ghovhannisyan
"""
import sys
import os
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.Seq import reverse_complement
prom_len=1000
with open(sys.argv[1], "r+") as gff_file:
gff_dict={}
for line in gff_file:
if not line.startswith("#"):
line=line.rstrip().split("\t")
if line[2]=="gene":
coords=[]
coords.extend([line[3],line[4]])
gene_name=line[8].split(";")[0][9:-1]
#print gene_name
gff_dict[line[0]+"*"+gene_name+"*"+line[6]]=coords
#print len(gff_dict)
#~ for k,v in gff_dict.iteritems():
#~ print k,v
#~ print len(gff_dict)
#gff_dict2=gff_dict
counter=0
n_short_genes=0
with open(sys.argv[2], "r") as genome, open(sys.argv[3], "w") as output:
for record in SeqIO.parse(genome, "fasta"):
for gene,coords in gff_dict.iteritems():
if gene.split("*")[0]==record.id:
if gene.split("*")[2]=="+":
if int(coords[0])>int(prom_len):
overlap_counter=0
max_coord=0
for k,v in gff_dict.iteritems():
if k.split("*")[0]!=gene.split("*")[1] and k.split("*")[0]==record.id and k.split("*")[2]=="+" and int(coords[0])-1000 <int(v[1])< int(coords[0]):
counter+=1
overlap_counter+=1
if int(v[1])>max_coord:
#print max_coord
max_coord=int(v[1])
#print max_coord
#else:
# print "blah" # int(v[1])
#~ print k,v, gene, coords
#~ print int(int(coords[0])-50)
#~ print int(v[1])
#~ print int(coords[0])
if overlap_counter>0:
#print overlap_counter #, max_coord
sequence=record.seq.tomutable()
sequence=sequence[int(max_coord)+1:int(coords[0])-1]
#print sequence, gene.split("*")[1], int(coords[0])-500, "this on +, normal"
if len(sequence)>100:
output.write(">%s=%s=%s=%s=%s=%s=%s\n%s\n"%(record.id,gene.split("*")[1],int(max_coord)+1,int(coords[0])-1,len(sequence),gene.split("*")[2],"OVERLAPPED",sequence))
print("%s\t%s\t%s\t%s\t%s"%(record.id,gene.split("*")[1],int(max_coord)+1,int(coords[0])-1,len(sequence)))
else:
n_short_genes+=1
elif overlap_counter==0:
sequence=record.seq.tomutable()
sequence=sequence[int(coords[0])-int(int(prom_len)+1):int(coords[0])-1]
#print sequence, gene.split("*")[1], int(coords[0])-500, "this on +, normal"
if len(sequence)>100:
output.write(">%s=%s=%s=%s=%s=%s\n%s\n"%(record.id,gene.split("*")[1],coords[0],coords[1],len(sequence),gene.split("*")[2],sequence))
print("%s\t%s\t%s\t%s\t%s"%(record.id,gene.split("*")[1],coords[0],coords[1],len(sequence)))
else:
n_short_genes=+1
elif int(coords[0])>50 and int(coords[0])<int(prom_len):
overlap_counter=0
max_coord=0
for k,v in gff_dict.iteritems():
if k.split("*")[0]!=gene.split("*")[1] and k.split("*")[0]==record.id and k.split("*")[2]=="+" and int(coords[0])-1000 <int(v[1])< int(coords[0]):
counter+=1
overlap_counter+=1
if int(v[1])>max_coord:
#print max_coord
max_coord=int(v[1])
#print max_coord
#else:
# print "blah" # int(v[1])
#~ print k,v, gene, coords
#~ print int(int(coords[0])-50)
#~ print int(v[1])
#~ print int(coords[0])
if overlap_counter>0:
#print overlap_counter #, max_coord
sequence=record.seq.tomutable()
sequence=sequence[int(max_coord)+1:int(coords[0])-1]
#print sequence, gene.split("*")[1], int(coords[0])-500, "this on +, normal"
if len(sequence)>100:
output.write(">%s=%s=%s=%s=%s=%s=%s\n%s\n"%(record.id,gene.split("*")[1],int(max_coord)+1,int(coords[0])-1,len(sequence),gene.split("*")[2],"OVERLAPPED_overflow",sequence))
print("%s\t%s\t%s\t%s\t%s"%(record.id,gene.split("*")[1],int(max_coord)+1,int(coords[0])-1,len(sequence)))
else:
n_genes_short=+1
elif overlap_counter==0:
sequence=record.seq.tomutable()
sequence=sequence[0:int(coords[0])-1]
#print len(sequence), gene.split("*")[1], int(coords[0])-500, "this on +, short"
if len(sequence)>100:
output.write(">%s=%s=%s=%s=%s=%s=%s\n%s\n"%(record.id,gene.split("*")[1],coords[0],coords[1],len(sequence),gene.split("*")[2],"overflow",sequence))
print("%s\t%s\t%s\t%s\t%s"%(record.id,gene.split("*")[1],coords[0],coords[1],len(sequence)))
else:
n_short_genes+=1
elif gene.split("*")[2]=="-":
if int(coords[1])+int(prom_len)<=len(record.seq):
overlap_counter_m=0
min_coord=1000000000000000000000000
for k,v in gff_dict.iteritems():
if k.split("*")[0]!=gene.split("*")[1] and k.split("*")[0]==record.id and k.split("*")[2]=="-" and int(coords[1])<int(v[0])< int(coords[1])+int(prom_len):
counter+=1
#print "OVERLAP"
overlap_counter_m+=1
if int(v[0])<=min_coord:
#print max_coord
min_coord=int(v[0])
#print max_coord
#else:
# print "blah" # int(v[1])
#~ print k,v, gene, coords
#~ print int(int(coords[0])-50)
#~ print int(v[1])
#~ print int(coords[0])
if overlap_counter_m>0:
#print overlap_counter_m,int(coords[1]),int(min_coord)
sequence=record.seq.tomutable()
sequence=sequence[int(coords[1]):int(min_coord)]
reverse_seq=reverse_complement(sequence)
#print sequence, gene.split("*")[1], int(coords[0])-500, "this on +, normal"
if len(sequence)>100:
output.write(">%s=%s=%s=%s=%s=%s=%s\n%s\n"%(record.id,gene.split("*")[1],int(coords[1]),int(min_coord),len(sequence),gene.split("*")[2],"OVERLAPPED",reverse_seq))
print("%s\t%s\t%s\t%s\t%s"%(record.id,gene.split("*")[1],int(coords[1]),int(min_coord),len(sequence)))
else:
n_short_genes+=1
elif overlap_counter_m==0:
sequence=record.seq.tomutable()
sequence=sequence[int(coords[1]):int(coords[1])+int(prom_len)]
reverse_seq=reverse_complement(sequence)
#print reverse_seq
#print reverse_seq, gene.split("*")[1],"this is on -, normal"
if len(sequence)>100:
output.write(">%s=%s=%s=%s=%s=%s\n%s\n"%(record.id,gene.split("*")[1],int(coords[1]),int(coords[1])+int(prom_len),len(reverse_seq),gene.split("*")[2],reverse_seq))
print("%s\t%s\t%s\t%s\t%s"%(record.id,gene.split("*")[1],int(coords[1]),int(coords[1])+int(prom_len),len(reverse_seq)))
else:
n_short_genes+=1
elif int(coords[1])+int(prom_len)>len(record.seq):
overlap_counter_m=0
min_coord=100000000000000000000
for k,v in gff_dict.iteritems():
if k.split("*")[0]!=gene.split("*")[1] and k.split("*")[0]==record.id and k.split("*")[2]=="-" and int(coords[1])<int(v[0])< int(coords[1])+int(prom_len):
counter+=1
overlap_counter_m+=1
if int(v[0])<min_coord:
#print max_coord
min_coord=int(v[0])
#print max_coord
#else:
# print "blah" # int(v[1])
#~ print k,v, gene, coords
#~ print int(int(coords[0])-50)
#~ print int(v[1])
#~ print int(coords[0])
if overlap_counter_m>0:
#print overlap_counter_m #, max_coord
sequence=record.seq.tomutable()
sequence=sequence[int(coords[1]):int(min_coord)]
reverse_seq=reverse_complement(sequence)
#print sequence, gene.split("*")[1], int(coords[0])-500, "this on +, normal"
if len(sequence)>100:
output.write(">%s=%s=%s=%s=%s=%s=%s\n%s\n"%(record.id,gene.split("*")[1],int(coords[1]),int(min_coord),len(sequence),gene.split("*")[2],"OVERLAPPED_overflow",reverse_seq))
print("%s\t%s\t%s\t%s\t%s"%(record.id,gene.split("*")[1],int(coords[1]),int(min_coord),len(sequence)))
else:
n_short_genes+=1
elif overlap_counter_m==0:
sequence=record.seq.tomutable()
sequence=sequence[int(coords[1]):]
reverse_seq=reverse_complement(sequence)
#print reverse_seq, gene.split("*")[1], "this is on -, short"
if len(reverse_seq)>100:
output.write(">%s=%s=%s=%s=%s=%s=%s\n%s\n"%(record.id,gene.split("*")[1],coords[1],len(record.seq),len(reverse_seq),gene.split("*")[2],"overflow",reverse_seq))
print("%s\t%s\t%s\t%s\t%s"%(record.id,gene.split("*")[1],coords[1],len(record.seq),len(reverse_seq)))
else:
n_short_genes+=1
else:
continue
#print n_short_genes
#print counter