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
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
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 dofor line in my_file
directly.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.
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
So this is homework?
As pointed out by others, among other issues, the problem is that your function doesn't actually
return
anything.yes, an assignment
so i just add: return(sequence_names, sequences)
?? would you mind please helping me with the other issues as well?
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.
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.