This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How To Have Only Id And Sequence Printed In Output After Parse Sequences From A Fasta File

I used this piece of biopython codes written by Eric Normandeau to parse several sequences from a Fasta file. The codes worked nicely. I would like to have my parsed output from this format

>DR179241 similar to UniRef100_A5HIY3 Cluster: Thaumatin-like protein; n=1; ...... ATAATCTTTAGATCAGTCATCAATCTCAACAGTATCGCTTTCAATTCTCTTTCATATTGC ATGGAAGTGTGTAAATACAATTAGGGCATTCATTGAGTTGACTTCATTTAAGCGCT......

exactly like this format:

>DR179241
ATAATCTTTAGATCAGTCATCAATCTCAACAGTATCGCTTTCAATTCTCTTTCATATTGC
ATGGAAGTGTGTAAATACAATTAGGGCATTCATTGAGTTGACTTCATTTAAGCGCT......

Could anyone kindly please light me for example how to modify this piece of biopython codes written by Eric Normandeau in order to suit my purpose?

Thank you very much and have a nice day.

biopython parsing fasta

3 answers

You could do this in R, using seqinR:

library(seqinr)
x=read.fasta("yourfile.fasta")
write.fasta(x,file="newfile.fasta",names=names(x))

Oohh, good to know. Thanks Leonor.

How about this (untested):

#!/usr/bin/env python
import sys
from Bio import SeqIO

fasta_file = sys.argv[1]  # Input fasta file
result_file = sys.argv[2] # Output fasta file

def modified(records):
    for record in records:
        #Clear the description
        record.description=""
        yield record

records = modified(SeqIO.parse(fasta_file),"fasta")
count = SeqIO.write(records, result_file, "fasta")
print "Converted %i records" % count

This used a generator function to modify the records (memory efficient - you should avoid loading everything into memory). The key point for your request was to set the description to an empty string.

Thanks Peter. Thanks for the suggestion.

If you are not bound to (bio)python I would just use sed:

sed 's/\s.*//' seq.fa

Explanation: From each line remove everything following and including the first blank space.

Sequence lines remain unchanged as they should not contain spaces (they get truncated otherwise!)

Log in to answer this question.