2

I am trying to read the file and put the two sequences (ie Spar and Sbay) into two variables seq1 and seq2. So far I only know how to pass everything to one sequence by using this code.

filename = 'AQY2.fasta'
readfile = open(filename, 'r')
seq1 = ''
seq2 = ''
for line in readfile.readlines():
     if ( line[0] == '>' ):
         header = line[1:]
     else:
         seq1 = seq1 + line

readfile.close() 
print(seq1)

Can anyone help me to read the two sequences into seq1 and seq2?

The image contains two sequences

Monica Li
  • 21
  • 1
  • 1
    I'm not sure this is what you mean, but you can add `seq2 = seq2 + line` just below `seq1 = seq1 + line` (with the same indentation, that's important!). You might also want to print `seq2` as well, to check it's ok, so add `print(seq2)` below `print(seq1)`. Finally, you should probably handle the file with `with`, but that's maybe for another time. – Orius Feb 13 '22 at 04:02
  • 1
    Please paste text not images of text or links to images of text – Mark Tolonen Feb 13 '22 at 04:05
  • 1
    FYI, there are libraries for parsing FASTA files in Python, you don't have to code it yourself. – Barmar Feb 13 '22 at 04:08
  • To follow up on what Barmar said, check out Biopython or [pyfaidx](https://github.com/mdshw5/pyfaidx). There's a lot of examples with Biopython in their documentation and at Biostars.org. Both of those separate each sequence record/entry without you needing to code it. – Wayne Feb 13 '22 at 04:36

1 Answers1

0

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'])
Udesh
  • 2,415
  • 2
  • 22
  • 32