gravatar for Ionic_Bond

2 hours ago by


Hello everyone,

I am supposed to write a function that takes a name of a file (FASTA) as an argument. When passed the name of the file, the function should read the file, discard the header and return the sequence as a string. Now, I am being asked to rise a predefined (subclass?) (defined before my code) if the sequence part of the file contains characters that are not of the letters A,C,T,G,U. Also, all U nucleotides should be replaced by T in the returned string. I think I am on the right track but have no idea how to incorporate this subclass in my code if any of the letters are not A,C,T,G,U.
I am working with a small file before defining the function but this is what I have got:

This is defined before my code:

# Run this cell to define the exception
class BadSequenceException(Exception):


#my code:

file = open("sequence1.fasta")

all_lines = file.readlines()

sequences = []

with open('sequence1.fasta', 'r') as seq:

    sequence = ''

for line in seq:

    if line.startswith('>'):


        sequence = ''


        sequence += line.strip()

def check (sequence, code="ATGCU"):

for x in sequence:

    if x not in code:

        return False

return sequence.replace("U","T")


I presume that the subclasse must be raised where the RETURN FALSE is?
Also, BadSequenceException is a subclass of the class Exception and inherits all its functionalities right? Any guidance on this would be very much appreciated. Thank you so much.

Source link