I would like to extract sequences consecutively from a large fasta files and save in new fasta files. For example, I have two fasta files namely  species1.fasta and species2.fasta as given below,

    species1.fasta
    >tag1 
    TGCAAGAG
    >amylase 
    ATCGAGAT

    species2.fasta
    >prot8
    ATGTTTGCGA
    >lip1
    TGATATAGAT
and I need to extract first positioned sequences from both `species1.fasta` and `species2.fasta` and save in new file namely `group1.fasta`. Same manner, the second positioned sequences to be extracted and saved in `group2.fasta`, and the `expected output` should be like this,

    group1.fasta
    >tag1 
    TGCAAGAG
    >prot8
    ATGTTTGCGA

    group2.fasta
    >amylase 
    ATCGAGAT
    >lip1
    TGATATAGAT

Like this I have to do for 3000 sequences, manual process is impossible, therefore, I have tried the following script, wherein I could not loop the process. Therefore, please help me in this regard.

    my script
    for file in *.fasta do cat "$file" \   | awk '/^>/ {printf("%s%s\t",(N>0?"\n":""),$0);N++;next;} \  {printf("%s",$0);} \
     END {printf("\n");}' \   | awk '{if ( NR==1 ) print $0;}' \   | awk '{print $1"\n"$2}' | uniq > "new_${file}" 
Thank you.

 






A fairly naive approach (code not tested as I'm not at a computer to do so at the moment, so it will likely be wrong in some places, but the general approach should work)


    from Bio import SeqIO
    import sys

    for file in sys.argv[1:]:
        for i, rec in enumerate(SeqIO.parse(file, 'fasta')):
            file  = open('./sequences_' + str(i), 'a')
            file.write(rec.format('fasta'))
            file.close()

This should iterate all records in all files, and write a new file with the number. If the file doesnt yet exist, it will create it, if it does, it will append to it. 

Run as:

`python scriptname.py /path/to/files/*.fasta`