-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanipulate_sequences.py
232 lines (178 loc) · 6.59 KB
/
manipulate_sequences.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from genomic_coordinates import *
# use this function to translate a coding sequence
def cds_translate(cds):
'''
(str) -> str
Translate a coding sequence into a protein sequence according to the standard genetic code
>>> cds_translate('ATGGCCATGGCGCCCAGAACTGAGATCAATAGTACCCGTATTAACGGGTGA')
MAMAPRTEINSTRING*
>>> cds_translate('ATGTACTAA')
MY*
'''
genetic_code = {'TTT': 'F', 'CTT': 'L', 'ATT': 'I', 'GTT': 'V',
'TTC': 'F', 'CTC': 'L', 'ATC': 'I', 'GTC': 'V',
'TTA': 'L', 'CTA': 'L', 'ATA': 'I', 'GTA': 'V',
'TTG': 'L', 'CTG': 'L', 'ATG': 'M', 'GTG': 'V',
'TCT': 'S', 'CCT': 'P', 'ACT': 'T', 'GCT': 'A',
'TCC': 'S', 'CCC': 'P', 'ACC': 'T', 'GCC': 'A',
'TCA': 'S', 'CCA': 'P', 'ACA': 'T', 'GCA': 'A',
'TCG': 'S', 'CCG': 'P', 'ACG': 'T', 'GCG': 'A',
'TAT': 'Y', 'CAT': 'H', 'AAT': 'N', 'GAT': 'D',
'TAC': 'Y', 'CAC': 'H', 'AAC': 'N', 'GAC': 'D',
'TAA': '*', 'CAA': 'Q', 'AAA': 'K', 'GAA': 'E',
'TAG': '*', 'CAG': 'Q', 'AAG': 'K', 'GAG': 'E',
'TGT': 'C', 'CGT': 'R', 'AGT': 'S', 'GGT': 'G',
'TGC': 'C', 'CGC': 'R', 'AGC': 'S', 'GGC': 'G',
'TGA': '*', 'CGA': 'R', 'AGA': 'R', 'GGA': 'G',
'TGG': 'W', 'CGG': 'R', 'AGG': 'R', 'GGG': 'G'}
CDS = cds.upper()
protein = ''
for i in range(0, len(CDS), 3):
codon = CDS[i:i+3]
if codon not in genetic_code:
protein += 'X'
else:
protein += genetic_code[codon]
return protein
# use this function to convert a fasta file to a dictionary
def convert_fasta(fasta):
'''
(file) -> dict
Take a file with fasta sequences and return a dictionnary with
sequence ID as key and single string sequence as value
'''
# convert nematode genomes into a dictionnary
genome = {}
infile = open(fasta, 'r')
for line in infile:
line = line.rstrip()
if line != '':
if line.startswith('>'):
genome[line[1:]] = ""
seq_name = line[1:]
else:
genome[seq_name] += line
infile.close
return genome
# use this function to reverse complement a DNA sequence
def reverse_complement(dna):
'''
(str) -> (str)
Return the reverse complementary sequence of string dna
>>> reverse_complement('atcg')
'cgat'
'''
valid_bases = {'A', 'T', 'C', 'G'}
dna2 = dna.upper()
dna_comp = ''
for i in dna2:
if i == 'A':
dna_comp += 'T'
elif i == 'T':
dna_comp += 'A'
elif i == 'C':
dna_comp += 'G'
elif i == 'G':
dna_comp += 'C'
elif i not in valid_bases:
dna_comp += 'N'
reverse_comp_dna = ''
for i in reversed(dna_comp):
reverse_comp_dna += i
if dna.islower():
reverse_comp_dna = reverse_comp_dna.lower()
return reverse_comp_dna
# use this function to get the complement of a DNA sequence
def seq_complement(dna):
'''
(str) -> str
Take a DNA sequence and return its complement in upper case
Precondition: accepts only N ambiguities, and treats any ambiguities as Ns
>>> seq_complement('atcg')
'TAGC'
>>> seq_complement('CTGagTg')
'GACTCAC'
>>> seq_complement('CTGARGTC')
'GACTNCAG'
'''
valid_bases = {'A', 'T', 'C', 'G'}
dna2 = dna.upper()
dna_comp = ''
for i in dna2:
if i == 'A':
dna_comp += 'T'
elif i == 'T':
dna_comp += 'A'
elif i == 'C':
dna_comp += 'G'
elif i == 'G':
dna_comp += 'C'
elif i not in valid_bases:
dna_comp += 'N'
return dna_comp
# use this function to reverse a sequence
def reverse_seq(seq):
'''
(str) -> str
Take a sequence and return its reverse sequence in upper case
>>> reverse_seq('actat')
'TATCA'
>>> reverse_seq('CGTCGTATCG')
'GCTATGCTGC'
'''
seq_rv = ''
for i in reversed(seq):
seq_rv += i
return seq_rv.upper()
# use this function to clean up the file of 1:1 orthologs from transcripts that belong to a same gene
def clean_cel_crem_orthologs_file(best_blast_hits, crem_gff, cel_gff, outputfile):
'''
(file, file, file) -> file
Clean up the file with best blat hits from transcripts that belong to a same
gene. Write to the outputfile the orthologous pairs between remanei and elegans
by keeping a single transcript per gene in each species
'''
# # create dicts with {gene: [transcript1, transcript2]} in remanei and elegans
# remanei = parent_gene(crem_gff)
# elegans = celegans_gene_to_transcripts(cel_gff)
#
# create sets of genes for which transcripts are already used in ortholog pairs
rem_already_used = set()
cel_already_used = set()
# create dicts of transcript : gene pairs {transcript : gene}
remanei_ts = transcript_to_gene(crem_gff)
elegans_ts = celegans_transcript_to_gene(cel_gff)
# create a dict to store the ortholog pairs {crem_TS : cel_TS}
orthologs = {}
# open best blast hits file
infile = open(best_blast_hits, 'r')
# skip header
infile.readline()
# loop over file
for line in infile:
line = line.rstrip()
line = line.split()
cel_TS = line[0]
crem_TS = line[1]
# check that the remanei gene has not been used
if remanei_ts[crem_TS] not in rem_already_used:
# check that the latens gene has not been used
if elegans_ts[cel_TS] not in cel_already_used:
# populate ortholog dict
orthologs[crem_TS] = cel_TS
# add corresponding genes to sets
rem_already_used.add(remanei_ts[crem_TS])
cel_already_used.add(elegans_ts[cel_TS])
# open file for writing
newfile = open(outputfile, 'w')
# write header
newfile.write('# 1:1 orthologs between remanei and elegans obtained by reciprocal BLAST hits\n')
newfile.write('# genes are represented by a single transcript\n')
newfile.write('# ambiguous sites in the reference genome are fixed\n')
newfile.write('Cremanei' + '\t' + 'Celegans' + '\n')
# write content to file
for gene in orthologs:
newfile.write(gene + '\t' + orthologs[gene] + '\n')
# close files
infile.close()
newfile.close()