It does not work for multi lined fasta files also does not work if there are different words in fasta header like the one below:
>1:id bla bla | bla
ATTATTGTTG
ATTATTGT
ATGGAT
ATTAGGGAGGGGATTA
>2:id bla bla | bla
AGGATTGCGGCTAGG
My code so far:
temp_line = ''
out_lines = []
with open('dna.fasta.py', 'r') as f_in:
text = f_in.read()
text = text.split()
print(text)
for line in text:
line = line.strip('\n')
if line[0] == '>':
temp_line = line.strip('>')
out_lines.append(temp_line)
else:
out_lines.append(line)
print (out_lines)
print (temp_line)
with open('dna.phylip.py', 'w') as file_out:
file_out.write('\n'.join(out_lines))
My Input file: dna.fasta.py
>Human
AGCATGCATCGATCGATCGACTAGCTAGCG
>Chimp
GATATGTCGAGATCGTCAGCTCGATCAGCT
>Gorilla
TGTGTCGATCTCGAGCTGAGTCGTCTATCA
My output file so far: dna.phylip file
Human
AGCATGCATCGATCGATCGACTAGCTAGCG
Chimp
GATATGTCGAGATCGTCAGCTCGATCAGCT
Gorilla
TGTGTCGATCTCGAGCTGAGTCGTCTATCA
Correct Format of the output phylip file/ What it should look like:
Human AGCATGCATCGATCGATCGACTAGCTAGCG
Chimp GATATGTCGAGATCGTCAGCTCGATCAGCT
Gorilla TGTGTCGATCTCGAGCTGAGTCGTCTATCA
I have no idea how to remove the '/n' and add '/t', I've tried the strip and split function (to remove the new line), to add the tab, I've tried joining '/t' but my methods haven't worked. I don't what I'm doing wrong.
From your program, it looks like you have not properly understood the fasta format.
It could be
>1:id bla bla | bla
ATTATTGTTG
ATTATTGT
ATGGAT
ATTAGGGAGGGGATTA
>2:id bla bla | bla
AGGATTGCGGCTAGG
If you are not hesitant to use BioPython:
import sys
from Bio import SeqIO
for record in SeqIO.parse(sys.argv[1],"fasta"):
print strrecord.id) + "\t" + record.seq
The text = text.split() does not work with fasta. You need to look for line starts with ">" and then the following lines should be concatenated until you see another line that starts with ">". You can search for how to handle fasta with python in google or in this site.
You can do this with the BBMap package like this:
reformat.sh in=file.fasta out=file.oneline
If the task matters more than the code/language, here is a perl one liner:
perl -ne 'if($_=~s/^>//){chomp($_), print $_} else{print "\t".$_}' file.fa > out.fa
file.fa
>Human
AGCATGCATCGATCGATCGACTAGCTAGCG
>Chimp
GATATGTCGAGATCGTCAGCTCGATCAGCT
>Gorilla
TGTGTCGATCTCGAGCTGAGTCGTCTATCA
out.fa
Human AGCATGCATCGATCGATCGACTAGCTAGCG
Chimp GATATGTCGAGATCGTCAGCTCGATCAGCT
Gorilla TGTGTCGATCTCGAGCTGAGTCGTCTATCA
PS: Though, I love Python
It does not work for multi lined fasta files also does not work if there are different words in fasta header like the one below:
>1:id bla bla | bla
ATTATTGTTG
ATTATTGT
ATGGAT
ATTAGGGAGGGGATTA
>2:id bla bla | bla
AGGATTGCGGCTAGG
Hi Goutham,
Thanks for pointing that out :)
Fixed the problems, here is the new one liner:
perl -ne 'if($_=~s/^>//){chomp($_), print "\n".$_." "} else{chomp($_),print $_}' file.fa
I hope it is helpful for the person who posted the question.
with open('dna.fasta.py', 'r') as f, open('dna.phylip.py', 'w') as file_out:
for line in f:
line = line.strip()
print(line)
if line[0] == '>':
file_out.write('{}\t'.format(line.strip('>')))
else:
file_out.write('{}\n'.format(line))
Thank you! Why do you have to include the {} (brackets) when writing into the output file?
https://docs.python.org/3/library/string.html
You could just print the variable with a tab or newline.
Log in to answer this question.