-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_LexA_regulated_genes.py
197 lines (148 loc) · 8.2 KB
/
get_LexA_regulated_genes.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
# -*- coding: utf-8 -*-
# Load the necessary modules
from Bio import Entrez, SeqIO
import urllib2
from datetime import datetime
startTime=datetime.now()
# Tell to NCBI who I am
Entrez.email = ""
# FIMO output
ids = [ ]
strand = [ ]
positions = [ ]
motif = [ ]
def get_regulated_genes(species=None):
for i in range(0,len(ids)):
while True:
try:
if strand[i] == "1":
handle = Entrez.efetch(db="nucleotide", id=ids[i], rettype="gb", seq_start=int(positions[i])-66)
elif strand[i] == "-1":
handle = Entrez.efetch(db="nucleotide", id=ids[i], rettype="gb", seq_start=0, seq_stop=int(positions[i])+50)
break
except urllib2.HTTPError:
print "Oops! HTTP Error...Trying again"
pass
records = SeqIO.parse(handle, "gb")
current_genes, current_position, current_id , intergenic_distance = [], [], [], []
ATG_end, organism = None, None
for record in records:
organism = record.annotations['organism']
if species == None or species == organism:
for f in record.features:
if f.type == "CDS":
if str(f.strand) == strand[i]:
if f.strand == 1 and "<" in str(f.location) or f.strand == -1 and ">" in str(f.location):
continue
else:
try:
protein, protein_id = f.qualifiers["product"], f.qualifiers["protein_id"]
current_genes.append(protein[0])
current_id.append(protein_id[0])
if strand[i] == "1":
current_position.append(int(str(f.location).split("[")[1].split(":")[0].strip("<").strip(">")))
if strand[i] == "-1":
current_position.append(int(str(f.location).split("[")[1].split(":")[1].split("]")[0].strip("<").strip(">")))
if ATG_end != None:
intergenic = int(str(f.location).split("[")[1].split(":")[0].strip("<").strip(">")) - ATG_end
intergenic_distance.append(intergenic)
ATG_end = int(str(f.location).split("[")[1].split(":")[1].split("]")[0].strip("<").strip(">"))
except KeyError:
continue
handle.close()
if len(current_position) > 0:
if strand[i] == "1" and current_position[0] <= 350:
c = 0
for j in intergenic_distance:
if len(intergenic_distance) == 0:
break
else:
if j > 150:
break
else:
c+=1
for k in range(0,c+1):
print organism+'\t'+ids[i]+'\t'+current_id[k]+'\t'+current_genes[k]
elif strand[i] == "-1" and int(positions[i]) - current_position[::-1][0] <= 266: # 266 = 250 + 16 (motif size)
current_genes, current_position, current_id , intergenic_distance = current_genes[::-1], current_position[::-1], current_id[::-1], intergenic_distance[::-1]
c = 0
for j in intergenic_distance:
if len(intergenic_distance) == 0:
break
else:
if j > 150:
break
else:
c+=1
for k in range(0,c+1):
print organism+'\t'+ids[i]+'\t'+current_id[k]+'\t'+current_genes[k]
def get_reverse_regulated_genes(species=None):
for i in range(0,len(ids)):
while True:
try:
if strand[i] == "-1":
handle = Entrez.efetch(db="nucleotide", id=ids[i], rettype="gb", seq_start=int(positions[i])-66)
elif strand[i] == "1":
handle = Entrez.efetch(db="nucleotide", id=ids[i], rettype="gb", seq_start=0, seq_stop=int(positions[i])+50)
break
except urllib2.HTTPError:
print "Oops! HTTP Error...Trying again"
pass
records = SeqIO.parse(handle, "gb")
current_genes, current_position, current_id , intergenic_distance = [], [], [], []
ATG_end, organism = None, None
for record in records:
organism = record.annotations['organism']
if species == None or species == organism:
for f in record.features:
if f.type == "CDS":
if str(f.strand) != strand[i]:
if f.strand == 1 and "<" in str(f.location) or f.strand == -1 and ">" in str(f.location):
continue
else:
try:
protein, protein_id = f.qualifiers["product"], f.qualifiers["protein_id"]
current_genes.append(protein[0])
current_id.append(protein_id[0])
if strand[i] == "-1":
current_position.append(int(str(f.location).split("[")[1].split(":")[0].strip("<").strip(">")))
if strand[i] == "1":
current_position.append(int(str(f.location).split("[")[1].split(":")[1].split("]")[0].strip("<").strip(">")))
if ATG_end != None:
intergenic = int(str(f.location).split("[")[1].split(":")[0].strip("<").strip(">")) - ATG_end
intergenic_distance.append(intergenic)
ATG_end = int(str(f.location).split("[")[1].split(":")[1].split("]")[0].strip("<").strip(">"))
except KeyError:
continue
handle.close()
if len(current_position) > 0:
if strand[i] == "-1" and current_position[0] <= 350:
c = 0
for j in intergenic_distance:
if len(intergenic_distance) == 0:
break
else:
if j > 150:
break
else:
c+=1
for k in range(0,c+1):
print organism+'\t'+ids[i]+'\t'+current_id[k]+'\t'+current_genes[k]
elif strand[i] == "1" and int(positions[i]) - current_position[::-1][0] <= 266: # 266 = 250 + 16 (motif size)
current_genes, current_position, current_id , intergenic_distance = current_genes[::-1], current_position[::-1], current_id[::-1], intergenic_distance[::-1]
c = 0
for j in intergenic_distance:
if len(intergenic_distance) == 0:
break
else:
if j > 150:
break
else:
c+=1
for k in range(0,c+1):
print organism+'\t'+ids[i]+'\t'+current_id[k]+'\t'+current_genes[k]
# Run the functions
get_regulated_genes()
get_reverse_regulated_genes()
# Print the datetime
print datetime.now() - startTime