0

I am about halfway done with my python Pset6 code for DNA.

One problem I have been having is that without this particular line of code below:

lresults = [i for i in lresults if i != '1' and i != '2']

If I do not put the above code, my code gives a kind of list, as seen below, which won't be able to match with the number of DNA sequence found.

['1', '46', '1', '1', '1', '49', '1', '1', '1', '1', '1', '1', '1', '1', '48', '1', '1', '1', '1', '29', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '15', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '5', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '28', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '40', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1']

However, I think that this line of code is not good, as it would not be able to find for those sequences that are found to be repeated once.

Can I ask for some help with this? I hope that the above text can be easily understood.

As below, is my entire code for this whole process.

import csv
import os
import re
from sys import argv


while True:
    # for the missing command line argument
    if len(argv) < 2:
        print("Usage: python dna.py data.csv sequence.txt")
    else:
        num_of_rows = 0
        # Open the CSV file aka the databases file
        with open(argv[1]) as csvfile:
            reader = csv.DictReader(csvfile)

            data = []
            for row in reader:
                key_list = list(row.keys())
                val_list = list(row.values())
                data.append(val_list)
                num_of_rows += 1

        # Open the sequences file, and get the results as a string, and split the string into 4 
        characters each,
        # with each of the attribute as a dictionary key
        with open(argv[2]) as csvfile:
            seq_reader = csv.reader(csvfile)
            # print(seq_reader)
            for row in seq_reader:
                text_inside_seq_reader = "".join(row)

        # The below method is to find the max number of repeats inside the sequence
        lresults = []
        for seq in key_list[1:]:
            # print(seq)
            pattern = re.compile(r'({})+'.format(seq))
            matches = pattern.finditer(text_inside_seq_reader)
            for match in matches:
                value = match.end() - match.start()
                num_of_repeats = value / len(seq)
                lresults.append(str(round(num_of_repeats)))

        # This below is the code that I needed to prevent the [1,1,1] from happening
        lresults = [i for i in lresults if i != '1']

        # This below code is to ensure that the database file gives the name with the DNA values in a row
        n = 0
        while n < num_of_rows:
            if data[n][1:] == lresults:
                print(data[n][0])
                n +=1
            else:
                n +=1

        break
    break

You must log in to answer this question.

Browse other questions tagged .