Source code

Download source archive ↓

upstream-extended/community/tools/biopython/base_distribution.py

1#!/usr/bin/env python
2"""
3    Calculate the frequency of the nucleotides, the length and the GC-Content of a fastafile
4    Bjoern Gruening
5"""
6
7
8import sys
9
10from Bio import SeqIO
11
12
13def __main__():
14    input_file = open(sys.argv[1], "r")
15    output_file = open(sys.argv[2], "w")
16
17    output_file.write("Gene\tA\tC\tG\tT\tLength\tCG%\n")
18
19    for record in SeqIO.parse(input_file, "fasta"):
20        gene_name = record.name
21
22        Acount = record.seq.count("A") + record.seq.count("a")
23        Ccount = record.seq.count("C") + record.seq.count("c")
24        Gcount = record.seq.count("G") + record.seq.count("g")
25        Tcount = record.seq.count("T") + record.seq.count("t")
26
27        seq_length = len(record.seq)
28
29        GCcontent = float(Ccount + Gcount) / seq_length
30
31        output_line = "%s\t%i\t%i\t%i\t%i\t%i\t%f\n" % (
32            gene_name,
33            Acount,
34            Ccount,
35            Gcount,
36            Tcount,
37            seq_length,
38            GCcontent,
39        )
40
41        output_file.write(output_line)
42
43    output_file.close()
44    input_file.close()
45
46
47if __name__ == "__main__":
48    __main__()