Deleted:Compare and Organize/Reorder Fasta Headers
0
0
Entering edit mode
18 months ago
jnora0625 • 0

I have a fasta file with headers that I want to compare with two other text files and if it's present in the first file, put it first on the header. If it's not on the first file, compare to the second file and if it's there put it first. Need help with writing the code how to compare the text files to the fasta header and if there's a match, organize/reorder the fasta header file so that that match name is the first entry on the header and the rest of the entries on the header stay the same.

seqs.fasta 
> ant| bee|fly
AGCT...
> bird| eagle| hawk| vulture
GATCG...

The text files look like these

file1.txt
mouse
hawk
cow

file2.txt
crane
fly

outputfile should look like this. (for the first one since fly is present in file2, place it as the first entry on the header. For the second header, hawk is present in file 1, place it as the first entry on the header.

> fly| ant| bee
AGCT... 
> hawk| bird| eagle| vulture 
GATCG...
fasta= open('seq.fasta')
one= open(file1.txt')
two= open('file2.txt')
output= open('newseq.fasta', 'w')

for line in fasta:
    if line.startswith('>'):
        header= line.strip().split("|")
        for name in one:
             if name in header: # looking to see if file1.txt has match in fasta file
                   header.insert(0,line.pop(header.index(name))) # insert the name from file1 as the first header entry
       else: # if no match in file1 look at file2
       for name in two:
              if name in header:
                    header.insert(0,line.pop(header.index(name))) # insert the name from file2 as first header entry   
    else:
        output.write(line)
fasta python • 441 views
ADD COMMENT
This thread is not open. No new answers may be added
Traffic: 2648 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