This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Need Coding To Modify Multifasta File

Hi all i ahve a multifats file contiing 3 differnent sequence(its around 3000 sequences) i want to separate them accoding to name. I need three diffeent file which contaion 3 diffent sequence. Can any one help me out

    >pseudogenome_H131_84   Probable 9-O-acetyl-N-acetylneuraminic acid deacetylase
    ATGAACGCAATAATATCGCCCAATTATTACTATGTTCTTACCGTTGCTGGTCAGTCTAAT
    >pseudogenome_H134_4332   Probable 9-O-acetyl-N-acetylneuraminic acid deacetylase
    ATGAACGCAATAATATCGCCCGATTATTACTATGTTCTTACCGTTGCTGGTCAGTCTAAT
    GCCATGGCGTATGGCGAAGGACTGCCATTACCGGACAGGGAAGATGCGCCTCATCCCAGA
    >pseudogenome_H24a_4333   Probable 9-O-acetyl-N-acetylneuraminic acid deacetylase
    ATGAACGCAATAATATCGCCTGATTATTACTATATTCTTACCGTTGCTGGTCAGTCTAAT
    GCCATGGCGTATGGCGAAGGACTGCCATTACCGGACAGGGAAGATGCGCCTCATCCCAGA
perl awk

What have you tried? While many of us could simply write a script for you, you won't learn that way.

I can break file like this : awk '/^>/{file=++n".fasta"} {print > file}' three.fas now i need to sepatre accoding to name.

It would be great if you could correct mistakes in your post.

4 answers

Always search on the internet before asking...

Alternative methods to split a FASTA file by Paulo Nuin

Perl example works for me.

Not sure I got your Q, but in python:

from Bio import SeqIO
fh = open('file.fasta','r')
oh = open('result.fasta','a')
for key in SeqIO.parse(fh, 'fasta'):
name = key.name
if key.name in ['blablabla']:
    oh.write(str('>' + key.id)) + '\n')
    oh.write(str(key.seq[0:]) + '\n') 
oh.close()
fh.close()

Okay, here's a clean, straightforward way in python. The filenames will be enumeration on the number of sequences in your multi-fasta file.

This also requires that you have Biopython installed.

from Bio import SeqIO
fasta = SeqIO.parse("/tmp/fasta.fa","fasta") # Specify the format, should return an iterator to the multi-fasta entries
output_base = "/tmp/extracted_" # Base of your output: eg, /tmp/extracted_0, /tmp/extracted_1, ..., /tmp/extracted_N
for seqid, seq in enumerate(fasta):
    output_file = file(output_base+str(seqid)+".fa", "w") # You can of course decide on the appropriate file extension
    SeqIO.write(seq, output_file, "fasta") # See the docs at http://biopython.org/wiki/SeqIO#Sequence_Output
    output_file.close() # The loop should close and flush the sequence to the file automatically, but this call is just to be verbose

Best of luck! Paul

Found a script similar to your case here while Googling. Might be useful to you!

~Prakki Rama

Log in to answer this question.