def ReadFastaFile(filename):
fileObj = open(filename, 'r')
sequences = []
seqFragments = []
for line in fileObj:
if line.startswith('>'):
if seqFragments:
sequence = ''.join(seqFragments)
sequences.append(sequence)
seqFragments = []
else:
seq = line.rstrip()
seqFragments.append(seq)
if seqFragments:
sequence = ''.join(seqFragments)
sequences.append(sequence)
fileObj.close()
return sequences
I want to get list with name and sequence This code gives me a list with only the sequence, because i first thought i would not need the name for what I want to do. But now i realized that it would be good to also include the names. Maybe if possible also in a dictionary form, so that it is like: dict = {'name':sequence}. Does somebody have I idea how to alter the code to achieve this?
https://stackoverflow.com/questions/29333077/reading-a-fasta-file-format-into-python-dictionary
can you post the example output format you want?
example output (would be happy about one of both): list = [['name1','sequence1'],['name2'],['sequence2'].....]
or
dictionary = {'name1':'sequence1','name2':['sequence2'.....} (but i am not sure right no if you can have both elements as strings in a dictionary?)
not sure about python...but in perl i would create a hash
list = [['name1','sequence1'],['name2'],['sequence2'].....] hash = [['key1','value1'],['key2'],['value2'].....]