0

I have updated my code after making a number of amendments but am still a bit stuck! I don't think my STR_check_count is working properly as it always seems to return 0s and I'm also not sure if I'm using seq_dict correctly...

I've added a couple of 'FIX'(es) in the comments as to some of the bits that aren't working properly.

Any help is greatly appreciated!

Thanks a bunch!

import sys
import csv
from sys import argv, exit


STR_all = sys.argv[1]
DNA_identify = argv[2]


# If 2 command line arguments have not been given then print error message
if len(sys.argv) != 3:
    print("Error: enter CSV file name followed by Text file name")
    exit(1)


# Open CSV file containing the STR counts for a list of individuals and read entire contents
with open(STR_all, "r") as file:

    # reader is python dictionary containing csv info - name and STR counts
    reader = csv.DictReader(file)
    for row in reader:
        STRcount = ([int(v) for v in list(row.values()) [1:]])
        print(STRcount)


    # Remove "name" field from column headings
    ColHead = reader.fieldnames
    STRColHead = ColHead[1:]
    print(STRColHead)
    print(ColHead)


# Open Text file containing the DNA sequence to identify
DNA = open(DNA_identify , "r")

# Read entire contents of file named DNA and create list
DNA.read()
DNA_list = open(DNA_identify , "r").readlines()
print(DNA_list)

print(len(DNA_list[0]))

# Create dictionary to store STR_check_count values
seq_dict = {}
seq_dict = dict.fromkeys(STRColHead, 1)


# Search text file for longest run of consecutive STR repeats
for y in range(len(STRColHead)):
    STR_check_count = 0
    for i in range(len(DNA_list[0])):

        # Check for condition of matching fieldname
        if (DNA_list[i:i+len(STRColHead[y])] == STRColHead[y]) and (DNA_list[i:i+ 2*len(STRColHead[y])] == STRColHead[y]):
            STR_check_count += 1
        else:
            if STR_check_count > seq_dict[STRColHead[y]]:
                seq_dict[STRColHead[y]] = STR_check_count
                STR_check_count == 1


# Print count to check working correctly - prints 3 zeros at the moment - FIX
for y in range(len(STRColHead)):
    print(STR_check_count)

# Declare variable match
match = 0


# For row in text file, check if each STR count matches each row in dict created from CSV
for row in STRcount:
        total_tracker = 0
        while i <= len(STRColHead):
                if int(STR_check_count[y]) == int(STRcount):
                    total_tracker += 1
                else:
                    break

        # If all STR counts match then print name
        # prints word "name" at the moment - FIX
        if total_tracker == len(STRColHead):
            print(f"{ColHead[0]}")
            match == 1
            break


        # Else print no match - prints No Match 3 times at the moment - FIX
        elif match == 0:
            print("No Match")
3
  • You need to edit the question and properly format the code (it may be easier to delete the question and add a new one). To paste code into a question so that it is properly formatted, you can 1) paste the code 2) select the code 3) click the {} icon in the format bar which will do the formatting. (It looks like you might have clicked the "upside down quotes" icon) Jan 23, 2020 at 14:41
  • And BTW you can use debug50 on python code like this: debug50 python dna.py file1 file2 Jan 23, 2020 at 14:54
  • Thanks a bunch! Hopefully this is clearer now! Jan 23, 2020 at 16:58

1 Answer 1

0

Here's some things to get you started. First, the paste is funky, lots of newlines missing (just look at the first line!), therefore I did this analysis with my best estimate of the what the running code looks like. Forewarned....

  • This program doesn't run because of a missing : terminator on this line:
    if (DNA_list[i:i+len(STRs_list[y])] == STRs_list) and (DNA_list[i:i+ 2*len(STRs_list[y])] == STRs_list) I added it to continue analysis.

  • The program always fails on this line if int(STRs_list[i]) == int(row): because row is a list. Perhaps add an exit before the "match" routine to make it easier to concentrate on the "check count" routine.

  • DNA_identify is one line; this loop for line in DNA_identify: executes exactly once.

  • Does STRs_list need to be a list of lists? Since it will always contain one list? That adds an unnecessary level of complexity (and nesting). Suggest you "flatten" that.

  • Because of the above, the equalities tested here if (DNA_list[i:i+len(STRs_list[y])] == STRs_list) and (DNA_list[i:i+ 2*len(STRs_list[y])] == STRs_list) are always false.

You might have to rip out some code and start over. It won't be the last time :) You could use debug50; there would be some learning curve for sure, but it's time well spent, debuggers are your friend.

If you test with small.csv and 1.txt you already know what the repeats should be, because the answer is Bob.

Good luck!

2
  • Thanks so much for your help, massively appreciated! Will get working on it now :) Jan 24, 2020 at 9:04
  • I've had another good go and have updated the code if you happen to have any free time to look! :) Jan 27, 2020 at 13:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .