Issue
I have a problem with my program. This part of the code is problematic.
def revcmpl(self):
# TODO:convert sequence contained in the object
# to a list called seq
seq = list(self.seq)
# TODO: reverse the list in-place
seq.reverse()
# TODO: using string method join(), the class dictionary ALPH and a
# list comprehension, translate the reversed sequence and
# convert into a string
seq = list(seq)
seq_revcmpl = ''.join(DNASeq.ALPH[key] for key in self.seq.split())
seq_revcmpl = str(seq_revcmpl)
# TODO: create seqid variable and assign to it the object's seqid
# and the suffix '_revcmpl'
seqid = f'{self.seqid}_revcmpl'
# TODO: create a new object od DNASeq type using the new seqid,
# title contained in the object and
# reveresed and translated sequence,
# return the new object
obj1 = DNASeq(seqid, title, seq_revcmpl)
return obj1
I try to using string method join(), the class dictionary ALPH and a list comprehension, translate the reversed sequence and convert into a string. I try to run this:
# reload the sequences to have a collection of objects
# that are instances of the up-to-date DNASeq class
seqs = DNASeq.from_file('input/Staphylococcus_MLST_genes.fasta')
# select one of the sequences by its sequence id (seqid)
seq = seqs['yqiL']
new_seq = seq.revcmpl()
print( new_seq )
but I get an error
KeyError Traceback (most recent call last)
<ipython-input-57-a28b468b9cfe> in <module>
7 seq = seqs['yqiL']
8
----> 9 new_seq = seq.revcmpl()
10
11 print( new_seq )
<ipython-input-43-07d175957482> in revcmpl(self)
211
212 seq = list(seq)
--> 213 seq_revcmpl = ''.join(DNASeq.ALPH[key] for key in self.seq.split())
214 seq_revcmpl = str(seq_revcmpl)
215
<ipython-input-43-07d175957482> in <genexpr>(.0)
211
212 seq = list(seq)
--> 213 seq_revcmpl = ''.join(DNASeq.ALPH[key] for key in self.seq.split())
214 seq_revcmpl = str(seq_revcmpl)
215
KeyError: 'GCGTTTAAAGACGTGCCAGCCTATGATTTAGGTGCGACTTTAATAGAACATATTATTAAAGAGACGGGTTTGAATCCAAGTGAGATTGATGAAGTTATCATCGGTAACGTACTACAAGCAGGACAAGGACAAAATCCAGCACGAATTGCTGCTATGAAAGGTGGCTTGCCAGAAACAGTACCTGCATTTACAGTGAATAAAGTATGTGGTTCTGGGTTAAAGTCGATTCAATTAGCATATCAATCTATTGTGACTGGTGAAAATGACATCGTGCTAGCTGGCGGTATGGAGAATATGTCTCAGTCACCAATGCTTGTCAACAACAGTCGCTTCGGTTTTAAAATGGGACATCAATCAATGGTTGATAGCATGGTATATGATGGTTTAACAGATGTATTTAATCAATATCATATGGGTATTACTGCTGAAAATTTAGTGGAGCAATATGGTATTTCAAGAGAAGAACAAGATACATTTGCTGTAAACTCACAACAAAAAGCAGTACGTGCACAGCAA'
But why???? I split a sequence, seq_revcmpl = ''.join(DNASeq.ALPH[key] for key in self.seq.split())
Solution
The issue is here:
seq_revcmpl = ''.join(DNASeq.ALPH[key] for key in self.seq.split())
self.seq
will not contain any whitespace, so self.seq.split()
will return a list containing a single item - the sequence itself.
The generator-expression then only has one single iteration (because there's only one item in the list, a single big string), and key
will be the entire sequence.
I think what you want is:
seq_revcmpl = ''.join(DNASeq.ALPH[key] for key in self.seq)
Answered By - Paul M.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.