0

I have written a program for DNA, but it's not printing anything. Code:

import csv
from sys import *

if len(argv) != 2:
    print("Usage: python dna.py data.csv sequence.txt")

def maxcount(n, main, seq):
    l = len(main)
    maxc = 0
    currc = 0
    for i in range(n):
        cp = i
        while seq[cp : cp + l]:
            currc += 1
            cp += l
        if currc > maxc:
            maxc =  currc
    return maxc

persons = []
strsn = {}
seq = ""

with open(argv[1], "r") as seqf:
    seq = seqf.read()
    seq.replace("\n", "")

dnaf = open(argv[0], "r")
dr = csv.dictReader(dnaf)
r = csv.reader(dnaf)
strs = next(r)[1 :]
for i in dr:
    persons.append(i)
for j in strs:
    strsn[j] = maxcount(len(j), j, seq)

for k in persons:
    d = []
    for z in strs:
        if strsn[z] == k[z]:
            d.append(True)
    if d.count(True) == len(strs):
        print(k["name"])
        exit(0)
print("No match")

EDIT: I have changed the code a little bit, on the advice of @Sentox. Code:

import csv
import sys

if len(sys.argv) != 3:
    print("Usage: python dna.py data.csv sequence.txt")
    sys.exit(1)

def maxcount(l, n, main, seq):
    maxc = 0
    currc = 0
    for i in range(n):
        p = i
        while p < n and seq[p : p + l] == main:
            p += l + 1
            currc += 1
        if currc > maxc:
            maxc = currc
        currc = 0
    return maxc

persons = []
strsn = {}
seq = ""

with open(sys.argv[2], "r") as seqf:
    seq = seqf.read()
    seq.replace("\n", "")

dnaf = open(sys.argv[1], "r")
dr = csv.DictReader(dnaf)
strs = dr.fieldnames[1:]
for i in dr:
    persons.append(i)
for j in strs:
    strsn[j] = maxcount(len(j), len(seq), j, seq)

for k in persons:
    d = []
    for z in strs:
        if strsn[z] == int(k[z]):
            d.append(True)
    if len(d) == len(strsn):
        print(k["name"])
        exit(0)
print("No match")

3 Answers 3

0

Some final tips:

  1. != 'name': is unnecessary because the fieldnames[1:] already drops 'name'
  2. strsn[z] == k[z] will never be true, because the data is read in from the CSV file as strings, while your sequence count is an integer. There's several ways to deal with this, but the most direct is to cast: int(k[z])
  3. Your core issue is that your algorithm to find the longest sequences doesn't currently work. You want to find the longest run of main, but you don't do anything with main except calculate its length (which is already passed to the function as n).
4
  • actually, n is the length of seq, and that's a mistake
    – user31128
    Commented Nov 18, 2020 at 7:56
  • still 'No Match'
    – user31128
    Commented Nov 19, 2020 at 6:15
  • On line 14, try changing p += l + 1 to just p += l. If your mini sequence is 5 chars, adding 5 + 1 will take you beyond the next character.
    – Sentox
    Commented Nov 19, 2020 at 6:43
  • I did that many days ago and completed week 6
    – user31128
    Commented Nov 21, 2020 at 13:28
0

Your code should most definitely be printing errors, not just nothing.

Your most pressing problem is that your indices for argv are wrong (you're trying to open the wrong parameters for the files you want). Once you sort that you can start debugging your code properly.

0

It can not print anything when your code does not even compile... First 2 bugs found:

  1. dr = csv.dictReader(dnaf)

should be dr = csv.DictReader(dnaf)

  1. argv[]

would be just an ordinary list, which is not defined in your case. In C, you would call int main( int argc, char *argv[] ) and it works just because compiler does it for you and main() function has its special meaning in C, however in Python you need not main() function for the program to run thus you need to obtain the command line arguments somehow. There is sys module to help you out and you can get them with sys.argv not only argv. You can also type from sys import argv but I prefer import sys and later explicitly use sys.argv so you know what you're working with...

Read Tracebacks errors, use https://docs.python.org/3/library/...

You may find useful regular expressions.

CSV module documentation

sys.argv documentation

5
  • argv will work because of the from sys import * line, which will literally important all names from sys (except those beginning with an underscore). Its use is generally frowned upon since it can cause confusion, but it will work here.
    – Sentox
    Commented Nov 17, 2020 at 10:02
  • why is there keyerror for 'AGATC'?
    – user31128
    Commented Nov 17, 2020 at 13:08
  • You pass your open file to two readers and call next() on the reader object. Then when you go to utilise the DictReader, it pulls the field names from the second row instead of the first. Scrap the reader and use strs = dr.fieldnames[1:].
    – Sentox
    Commented Nov 17, 2020 at 20:55
  • So, how can I access the names? By the way, I'm always getting 'no match'. And the errors have gone, so thanks for that
    – user31128
    Commented Nov 18, 2020 at 6:16
  • That's what I mean by using strs = dr.fieldnames[1:]. A DictReader has a property called fieldnames that will give you a list of just the field names. Slice it with [1:] to drop "name". Your algorithm currently isn't counting the sequences correctly so at this point you will need to use debug50 and start stepping through your code to figure out what's going wrong.
    – Sentox
    Commented Nov 18, 2020 at 6:54

You must log in to answer this question.