I am using two vaiable matchSpar
and matchSbay
to read the file and seq a dictionary to store the corresponding result.
if matchSpar
is set to True
then lines added append to '>Spar'
key in seq
dictionary if matchSbay
is set to True
then lines added append to '>Sbay'
key in seq
dictionary.
filename = 'AQY2.fasta'
readfile = open(filename, 'r')
seq= {
'>Spar' : "",
'>Sbay' : ""
}
matchSpar = False
matchSbay = False
lines = []
for line in readfile.readlines():
line = line.replace('\n', '')
if line == '>Spar':
matchSpar = True
matchSbay = False
if line == '>Sbay':
matchSpar = False
matchSbay = True
if matchSpar :
seq[">Spar"] += line+"\n"
if matchSbay :
seq[">Sbay"] += line+"\n"
readfile.close()
print(seq['>Spar'])
print(seq['>Sbay'])
Its recommended to use with keyword
to file handling in python :
filename = 'AQY2.fasta'
seq= {
'>Spar' : "",
'>Sbay' : ""
}
matchSpar = False
matchSbay = False
with open(filename, 'r') as readfile:
for line in readfile.readlines():
if line == '>Spar\n':
matchSpar = True
matchSbay = False
if line == '>Sbay\n':
matchSpar = False
matchSbay = True
if matchSpar :
seq[">Spar"] += line
if matchSbay :
seq[">Sbay"] += line
print(seq['>Spar'])
print(seq['>Sbay'])