/
lab10.py
204 lines (170 loc) · 5.89 KB
/
lab10.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
from Bio import SeqIO
from Bio.Alphabet import IUPAC
from Bio import Entrez
from Bio.SeqFeature import SeqFeature
from Bio.SeqFeature import FeatureLocation
from Bio.Data import CodonTable
def SearchInDB(Entrez, id, db):
handle = Entrez.esearch(db=db,term=id)
rec = Entrez.read(handle)
print (rec["Count"])
print (rec["IdList"])
return rec
def GetInfo(Entrez, rec, db, format, useIDForEfetch = False, id = ""):
if useIDForEfetch:
print ("Used ", id, " for efetch!")
handle = Entrez.efetch(db=db, id=id, rettype=format, retmode='text') # Special use case for Exercise 3
else:
handle = Entrez.efetch(db=db, id=rec["IdList"][0], rettype=format, retmode='text')
return handle
def GetSeqRecord(handle, format):
secrec = SeqIO.read(handle, format)
return secrec
def SaveToFile(id, secrec, format):
handle = open(id + "." + format, "w")
SeqIO.write([secrec], handle, format)
handle.close()
def SetEmail (mail):
Entrez.email = mail
return Entrez
def SearchInDBAndSaveToFasta(mail, id, db, format):
SearchInDBAndSaveToFastaAndAlterAlphabet(mail, id, db, format)
def SearchInDBAndSaveToFastaUsingID(mail, id, db, format):
SearchInDBAndSaveToFastaAndAlterAlphabet(mail, id, db, format, True)
def SearchInDBAndSaveToFastaAndAlterAlphabet(mail, id, db, format, useIDForEfetch = False, alterAlphabet = False, newAplhabet = ""):
Entrez = SetEmail (mail)
rec = SearchInDB(Entrez, id, db)
handle = GetInfo(Entrez, rec, db, format, useIDForEfetch, id)
secrec = GetSeqRecord(handle, format)
print("Alphabet type: ", secrec.seq.alphabet)
if alterAlphabet:
secrec.seq.alphabet = newAplhabet
print("Alphabet altered to ",newAplhabet ," !!!")
handle.close()
SaveToFile(id, secrec, format)
def complInv(seq):
hash_map = {'A': 'T', 'T':'A', 'C':'G', 'G':'C'}
result = ''
for i in range(0, len(seq)):
c = hash_map[seq[i]]
result += c
return result[::-1]
def compl(str):
tb = str.maketrans('ATCG', 'TAGC')
return str.translate(tb)[::-1]
def GetFrame(seqOriginal, table, frame, codoaneDeStartADN, codoaneDeStopADN):
if frame not in (1,2,3,-1,-2,-3):
print("Error!!!")
return
if frame in (1,2,3):
seq = seqOriginal[frame-1:]
if frame in (-1,-2,-3):
complement = compl(str(seqOriginal))
seq = complement[-frame-1:]
i = 0
start = -1
list = []
while i < len(seq)-2:
codon = seq[i:i+3]
if codon in codoaneDeStartADN:
start = i
if codon in codoaneDeStopADN and start >= 0:
stop = i
tuple = (start, stop)
list.append(tuple)
start = -1
i += 3
return list
def find_orfs(seq, table):
codoaneDeStartADN = ["ATG"]
codoaneDeStopADN = ["TAG", "TAA", "TGA" ]
list = []
result = []
for frame in (1,2,3,-1,-2,-3):
list = GetFrame(seq, table, frame, codoaneDeStartADN, codoaneDeStopADN)
result.append(list)
return result
# MAIN
_RunExercise = [1,2,3,4,5,6]
if 1 in _RunExercise:
print('\n---Exercise 1---')
mail = 'cosminel93@yahoo.com'
id = "PAX-6.1"
db = "nucleotide"
format = "fasta"
SearchInDBAndSaveToFasta(mail, id, db, format)
id = "PAX-6.5"
SearchInDBAndSaveToFasta(mail, id, db, format)
if 2 in _RunExercise:
print('\n---Exercise 2---')
mail = 'cosminel93@yahoo.com'
id = "NC_009084"
db = "nuccore"
format = "fasta"
SearchInDBAndSaveToFastaAndAlterAlphabet(mail, id, db, format, False, True, IUPAC.unambiguous_dna)
if 3 in _RunExercise:
print('\n---Exercise 3---')
mail = 'cosminel93@yahoo.com'
id = "NC_016438"
db = "nuccore"
format = "gb"
SearchInDBAndSaveToFastaUsingID(mail, id, db, format)
if 4 in _RunExercise:
print('\n---Exercise 4---')
record = SeqIO.read(open("NC_016438.gb"), "genbank")
elementsToFind = [4, "gene", "CDS"]
numberOfElements = len(record.features)
i = 0
nbOfFoundElements = 0
while i < numberOfElements and nbOfFoundElements < elementsToFind[0]:
if record.features[i].type in elementsToFind:
nbOfFoundElements += 1
print ("Element " + str(nbOfFoundElements) + ":")
print ("Type: ", record.features[i].type)
print ("Location: ", record.features[i].location)
print ("Strand: ", record.features[i].strand)
print ("\n")
i += 1
if 5 in _RunExercise:
print('\n---Exercise 5---')
id = "PAX-6.5"
format = "fasta"
record = SeqIO.read(open(id + "." + format), format)
print ("Record:\n", record)
record.seq.alphabet = IUPAC.unambiguous_dna
print("\nAlphabet altered to IUPAC.unambiguous_dna !!!")
accNb= record.id.split("|")[3]
print ("Access number: ", accNb)
record.name = accNb
record.id = accNb
print("record.name and record.id have been altered !!!")
feature = SeqFeature()
feature.type = "gene"
feature.location = FeatureLocation(18,200)
feature.strand = -1
record.features.append(feature)
print ("record.features: ", record.features)
print ("\nRecord:\n", record)
count = SeqIO.write(record, open(id + ".gb", "w"), "genbank")
print ("Converted %i records" % count)
if 6 in _RunExercise:
print('\n---ORF---')
mail = ''
id = "NC_009926"
db = "nuccore"
format = "fasta"
Entrez = SetEmail (mail)
rec = SearchInDB(Entrez, id, db)
handle = GetInfo(Entrez, rec, db, format)
secrec = GetSeqRecord(handle, format)
list = []
list = find_orfs(secrec.seq, CodonTable.standard_dna_table)
handle = open(id + "_ORF.txt", "w")
listToString = ""
i = 1
for elem in list:
listToString += "Frame " + str(i) + " : " + str(elem) + "\n"
i += 1
handle.write(listToString)
handle.close()
print("Done. Check file!")