0

DNA.py is nearly finished, but it is failing the tests with sequence/15.txt and 16.txt. This is the error message:

~/pset6/dna/ $ python dna.py databases/large.csv sequences/16.txt
Traceback (most recent call last):
  File "dna.py", line 81, in <module>
    n += (str_length * n_len)
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

Here is my code:

import csv
from csv import reader
import sys

# STR counter function. Records length of STR
def str_counter(name, length, location, sequence, seqlength):
    counter = 0
    # Iterate through loaded sequence for name of nucleotide
    # Does consecutive substring = current substring
    for i in range(location, seqlength, length):
        # Consecutive substring variable
        k = i + length
        if (sequence[i:k] == name):
            counter += 1
        if (sequence[i:k] != name):
            return counter


# Appends count to appropriate list
def append(str_length, nucleotide, str_dict, str_dict_length):
    for a in str_dict:
        if (a == nucleotide):
            str_dict[a].append(str_length)
    return str_dict


# PART 1 OPENING AND READING .csv FILE and .txt FILE INTO MEMORY

# Check command line arguements
if (len(sys.argv) < 3):
    print("Usage: python dna.py data.csv sequence.txt")
    exit(1)

# Read sequence txt file  into memory
with open(sys.argv[2]) as sequence:
    seq = sequence.read()

# length of seq
seqlength = len(seq)

# Reading .csv into memory
with open(sys.argv[1], 'r') as database:
    dtb_keys = reader(database)
    # Header row of .csv
    header = next(dtb_keys)


# PART 2 COUNT STRs

# STR dict. To store value of each STR
# Retrieve nucleotide names from .csv
keys = (header[1:])
# Generate dict using nucleotide names as the keys
str_dict = {key: [] for key in keys}
# Lenght of dict
str_dict_len = len(str_dict)

# Nested for loop, for each nucleotide in the dict, it will loop through sequence and append STR lengths into dict
# Iterate through strs.
# Loop for going through str_dict to select nucleotide

for nucleo in str_dict:
    # Length of nucleotide, for size of substring
    n_len = len(nucleo)
    n = 0
    while n < seqlength:
        if seq[n:n+n_len] == nucleo:
            # If nucleo found, start str counter
            str_length = str_counter(nucleo, n_len, n, seq, seqlength)
            # Append str lenght to dict
            append(str_length, nucleo, str_dict, str_dict_len)
            # Move n along to where counter finished
            **n += (str_length * n_len)**
        else:
            n += 1

# If there aren't any STRs = no match
empty_values = list(str_dict.values())
for val in empty_values:
    # Quit if values of str_dict are empty, this means no STRs
    if (val == []):
        print("No match")
        exit()

# List to store values of longest strs
max_str = []
for key in str_dict:
    max_str.append(max(str_dict[key]))

# Nested for loop for 2d array
str_database = open(sys.argv[1], 'r')
reader = csv.reader(str_database)
# list for holding str lengths to be converted to int
str_lines = []
# list for holding names, (chrs)
names = []
for row in reader:
    str_lines.append(row[1:])
    names.append(row[0])

# Scan through .csv for matching STRs
for x in range(1, len(str_lines)):
    # .csv is string by default, convert STRs to int
    test = list(map(int, str_lines[x]))
    # If match is found print name
    if (test == max_str):
        print(names[x])
        
# If no match, print No match
if (test != max_str):
    print("No match")

I suspect that this error is caused by 'n' in the 'while' loop going out of bounds. Both sequences,15.txt and 16.txt, end with a nucleotide so perhaps once 'n' has stepped passed this final nucleotide it is then out of bounds. I imagine that this would cause the 'NoneType' error?

I am not sure how to fix this. So far I have tried amending the 'while' loop statement. e.g. while n <**=** seqlength while n < seqlength + 1

But this has not changed anything. Perhaps I shouldn't use a 'while' loop?

Any tips, hints or advice please?

You must log in to answer this question.

Browse other questions tagged .