python type error
0
0
Entering edit mode
4.8 years ago
yuri • 0

what does the type error: cannot unpack non-iterable NoneType object mean? i am trying to read fastA names and sequences from a filepath. see my code below:

def Read_FastA_Names_And_Sequences(filepath):
    sequence_names = []
    sequences = []
    current_sequence = ""

my_file = open(filepath, "r")
my_data = my_file.readlines()
for line in my_data:
    if line[0] == ">":
        x = line[1:].rstrip("\r\n")
        sequence_names.append(x)
        new_sequence = True
        if len(current_sequence) == 0:
            pass
        else:
            sequences.append(current_sequence)
    else:
        if new_sequence:
            current_sequence = ""
            current_sequence = current_sequence + line.strip("\n")
            new_sequence = False
        else:
            current_sequence = current_sequence + line.strip("\n")

sequence_names, sequences = Read_FastA_Names_And_Sequences(filepath) doesnt return anything

sequencing • 1.4k views
ADD COMMENT
1
Entering edit mode

you are trying to do something that can only be done to an iterable to something that is not an iterable. Use type() on the object that is throwing the error or print the object to see for yourself

ADD REPLY
1
Entering edit mode

You're not returning anything from the function, i.e. there is no return statement.

Also, don't do my_data = my_file.readlines(), it is sufficient (and more efficient and memory-saving) to do for line in my_file directly.

ADD REPLY
0
Entering edit mode

Can I ask a broader question of why are you writing a fasta parser at all?

There's quite a lot wrong with that code, and you are reinventing the wheel.

ADD REPLY
0
Entering edit mode

the briefing i was given says that the function Read_FastA_Names_And_Sequences(filepath) must return two lists: sequence_names and sequences. The sequence_names list must have the names (titles) of the various sequences in the fastA file as string elements. The sequences list must have the corresponding sequences as string elements

ADD REPLY
0
Entering edit mode

So this is homework?

As pointed out by others, among other issues, the problem is that your function doesn't actually return anything.

ADD REPLY
0
Entering edit mode

yes, an assignment

so i just add: return(sequence_names, sequences)

?? would you mind please helping me with the other issues as well?

ADD REPLY
0
Entering edit mode

I think you need to start over, as you've over-complicated this. Write the code iteratively, testing with print statements as you go so that you understand how the information is flowing through the code.

Your function currently does nothing at all, except create 2 empty lists and an empty string. You pass it the file path, but then you do nothing with that variable. All of your parsing behaviour is in the main script body, which is fine, but renders that function pointless. I'd suggest looking for a beginners guide to writing python functions.

ADD REPLY
0
Entering edit mode

Indent the parsing logic (or more specifically, everything starting from my_file = open(filepath, "r"))*, so that it happens inside the function, then add the return at the end of the function.

*maybe that is already the case in your actual code and it is just a formatting issue here, in that case just add the return.

ADD REPLY

Login before adding your answer.

Traffic: 1955 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6