This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How To Count Aminoacids From A Text File In Biopython?

Hi,

How to count aminoacids from a text file ? My file contains 1200 sequences. After getting amino acid composition, I have to create a Bar plot. How to do these things in Bio python?

I parsed the sequence file in to Biopython. I tried the following code to get amino acid composition. I have to get the composition for each sequence. But I got the composition only for the first sequence.

from Bio.SeqUtils.ProtParam import ProteinAnalysis

x = ProteinAnalysis("cc.fasta") // cc.fasta is my sequence file.I have already parsed this to biopython.

print x.count_amino_acids()

Please help to solve this problem.

biopython amino-acids

1 answer

Your updated code does not actually parse the sequence file. The Tutorial section I linked above is useful way to become more familiar with using SeqIO. Here is a short snippet which uses SeqIO and a loop to apply count_amino_acids to each sequence in a file:

import collections
from Bio import SeqIO
from Bio.SeqUtils.ProtParam import ProteinAnalysis

all_aas = collections.defaultdict(int)
for rec in SeqIO.parse("cc.fasta", "fasta"):
    x = ProteinAnalysis(str(rec.seq))
    print rec.id, x.count_amino_acids()
    for aa, count in x.count_amino_acids().iteritems():
        all_aas[aa] += count

Thank you very much for your answer.Do you know how to calculate the total number each amino acid from all sequences? My aim is to draw a bar chart.

Thank you very much for your answer. Do you know how to calculate the total number of each amino acid from all sequences? My aim is to create a bar chart.

You need a global dictionary where you increment the counts for each amino acid in each sequence. I edited the code with an example.

I tried your code. But I got the same output as that of previous code.I would like to get the output like this A=45(total number of alanine residues from my dataset), C=12 -----etc.

The change is collecting the total counts, that you want, in the 'all_aas' dictionary. Try a print all_aas line at the end. Depending on your Python experience, it might be worth becoming more familiar with Python data structures; this is a good book to get started with:http://learnpythonthehardway.org/book/

Thank you very much.

Log in to answer this question.