Search code examples
pythonpython-3.xbiopythonfasta

Read nucleotides in FASTA without using BioPython


I need to obtain the same output obtained with the following code, but without using BioPython. I'm stuck... Anyone could help me? Thanks!!!

from Bio import SeqIO
records = SeqIO.parse("data/assembledSeqs.fa", "fasta")
for i, seq_record in enumerate(records):
    print("Sequence %d:" % i)
    print("Number of A's: %d" % seq_record.seq.count("A"))
    print("Number of C's: %d" % seq_record.seq.count("C"))
    print("Number of G's: %d" % seq_record.seq.count("G"))
    print("Number of T's: %d" % seq_record.seq.count("T"))
    print()

The FASTA file looks like this:

>chr12_9180206_+:chr12_118582391_+:a1;2 total_counts: 115 Seed: 4 K:    20 length: 79
TTGGTTTCGTGGTTTTGCAAAGTATTGGCCTCCACCGCTATGTCTGGCTGGTTTACGAGC
AGGACAGGCCGCTAAAGTG
>chr12_9180206_+:chr12_118582391_+:a2;2 total_counts: 135 Seed: 4 K: 20 length: 80
CTAACCCCCTACTTCCCAGACAGCTGCTCGTACAGTTTGGGCACATAGTCATCCCACTCG
GCCTGGTAACACGTGCCAGC
>chr1_8969882_-:chr1_568670_-:a1;113 total_counts: 7600 Seed: 225 K: 20 length: 86
CACTCATGAGCTGTCCCCACATTAGGCTTAAAAACAGATGCAATTCCCGGACGTCTAAAC
CAAACCACTTTCACCGCCACACGACC
>chr1_8969882_-:chr1_568670_-:a2;69 total_counts: 6987 Seed: 197 K: 20   length: 120
TGAACCTACGACTACACCGACTACGGCGGACTAATCTTCAACTCCTACATACTTCCCCCA
TTATTCCTAGAACCAGGCGACCTGCGACTCCTTGACGTTGACAATCGAGTAGTACTCCCG

I've tried that, but doesn't work at all

def count_bases (fasta_file_name):
    with open(fasta_file_name) as file_content:
        for seqs in file_content:
            if seqs.startswith('>'):
                for i, seq in enumerate('>'):
                    print("Sequence %d:" % i)
            else:
                print("Number of A's: %d" % seqs.count("A"))
                print("Number of C's: %d" % seqs.count("C"))
                print("Number of G's: %d" % seqs.count("G"))
                print("Number of T's: %d" % seqs.count("T"))
                print()
    return bases

result = count_bases('data/assembledSeqs.fa')

Solution

  • This will work. First we want to parse your fasta file into headers and sequences, then flatten the list of lists of sequences to a list of strings, then count the number of each nucleotide in the string, then print:

    import sys
    
    fasta = sys.argv[1]
    
    def fastaParser(infile):
        seqs = []
        headers = []
        with open(infile) as f:
            sequence = ""
            header = None
            for line in f:
                if line.startswith('>'):
                    headers.append(line[1:-1])
                    if header:
                        seqs.append([sequence])
                    sequence = ""
                    header = line[1:]
                else:
                    sequence += line.rstrip()
            seqs.append([sequence])
        return headers, seqs
    
    headers, seqs = fastaParser(fasta)
    
    flat_seqs = [item for sublist in seqs for item in sublist]
    
    def countNucs(instring):
        # will count upper and lower case sequences, if do not want lower case remove .upper()
        g = instring.upper().count('G') 
        c = instring.upper().count('C')
        a = instring.upper().count('A')
        t = instring.upper().count('T')
        return 'G = {}, C = {}, A = {}, T = {}'.format(g, c, a, t)
    
    for header, seq in zip(headers, flat_seqs):
        print(header, countNucs(seq))
    

    using your example fasta to run on the command-line:

    [me]$ python3.5 fasta.py infasta.fa
    

    output:

    chr12_9180206_+:chr12_118582391_+:a1;2 total_counts: 115 Seed: 4 K:    20 length: 79 G = 24, C = 17, A = 14, T = 24
    chr12_9180206_+:chr12_118582391_+:a2;2 total_counts: 135 Seed: 4 K: 20 length: 80 G = 16, C = 30, A = 17, T = 17
    chr1_8969882_-:chr1_568670_-:a1;113 total_counts: 7600 Seed: 225 K: 20 length: 86 G = 12, C = 31, A = 27, T = 16
    chr1_8969882_-:chr1_568670_-:a2;69 total_counts: 6987 Seed: 197 K: 20   length: 120 G = 20, C = 41, A = 31, T = 28