This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Python script for ATGC count

Hi, I am new to Python so if this query seems to you a piece of cake. Please I apologize in advance.

I have a python script to count ATGC

def readGenome(filename):
genome = ''
with open(filename, 'r') as f:
    for line in f:
        # ignore header line with genome information
        if not line[0] == '>':
            genome += line.rstrip()
return genome 

genome = readGenome(filename)
genome[:100]
# Count the number of occurences of each base
counts = {'A': 0, 'C': 0, 'G': 0, 'T': 0}
for base in genome:
    counts[base] += 1
print(counts)

import collections
print(collections.Counter(genome))

I cannot figure out the problem in this code. As I run it on command prompt such as

<command prompt=""> Python count_ATGC.py gene.fa

It gives me the error that

Traceback (most recent call last):

File "count_ATGC.py", line 9, in <module>

genome = readGenome()

TypeError: readGenome() missing 1 required positional argument: 'filename'

Could somebody help me with this error?

Thanks

genome sequence software error

Yes, you are not giving the fasta file string directory to the function readGenome(). So,

genome = readGenome(filename = "/path/to-the-genome-fasta-file.fasta")

filename is an argument that takes, I guess, a string specifying the genome in fasta format (I guess), in your computer. So the error is related with that, you're not giving input to the positional argument filename.

António

1 answer

Hi,

Try the following script applied in the same way you did:

import sys

filename = sys.argv[1]

def readGenome(filename):
    genome = ''
    with open(filename, 'r') as f:
        for line in f:
        # ignore header line with genome information
            if not line[0] == '>':
                genome += line.rstrip()
    return genome

genome = readGenome(filename)
genome[:100]
# Count the number of occurences of each base
counts = {'A': 0, 'C': 0, 'G': 0, 'T': 0}
for base in genome:
    counts[base] += 1
print(counts)

import collections
print(collections.Counter(genome))

The filename = sys.argv[1] will import the second input argument after python, i.e., python count_ATGC.py gene.fa, gene.fa and assign it to filename variable.

I hope this helps,

António

Log in to answer this question.