Skip to main content
edited tags
Link
DinoCoderSaurus
  • 28.6k
  • 2
  • 12
  • 31
Source Link

MY code works with a hardcoded string, but not a string object

I'm working on pset6 2020 dna. I'm having trouble inputting the sequence file to be compared into my search function. If I just open the file and copy paste the sequence into the code as a string, it works and returns all the correct values in a timely fashion, but I can't do the same with the object into which I've read the file. I tried to look for a function that converts an FO into a string, but apperently the f.read() function returns a string object? I'm new to python and do not know if there is a diffrence between a string and a stringt object. If anyone would be willing to let me know what I'm doing wrong or point me in the direction of a good resource I'd be very grateful!!!

This works:

from cs50 import get_string
from sys import argv, exit
import csv


def main(): # main function
    if len(argv) != 3: # ensuring proper usage
        print("Wrong  number of command line arguments.")
        print("Usage: programname.py file.csv dna.txt")

    with open(argv[1], newline = '') as csvfile: #opening database
        reader0 = csv.reader(csvfile, delimiter=",")
        firstl = next(reader0) # isolating the first line to get the names of the STRs
        nos = len(firstl) - 1 #isolating the number of STRs

        STRs = []

        for i in range(1,nos + 1): #adding the names of the STRs to a list for later access
            STRn = firstl[i]

            STRs.append(STRn)

        reader = csv.DictReader(csvfile,firstl) #reading database into memory

        print(reader.fieldnames)
        print(STRs)
        VALUES = []


        for i in range(nos):
            value = search('AAGGTAAGTTTAGAATATAAAAGGTGAGTTAAATAGAATAGGTTAAAATTAAAGGAGATCAGATCAGATCAGATCTATCTATCTATCTATCTATCAGAAAAGAGTAAATAGTTAAAGAGTAAGATATTGAATTAATGGAAAATATTGTTGGGGAAAGGAGGGATAGAAGG', STR = STRs[i])

            VALUES.append(value)

        print(VALUES)


def search(seq, STR): #function to analyze the dna sequences for an individual STR
    val= 0
    start = 0
    STRlen = len(STR)
    EVAL = []
    while True:
        found = seq.find(STR, start)

        if found == -1:
            EVAL.append(val)
            break
        if start != found:
            EVAL.append(val)
            val = 0
            val += 1
            start = found + STRlen
        else :
            val += 1
            start = found + STRlen


    print(EVAL)
    endval = max(EVAL)
    return endval

main()

But this does not:

from cs50 import get_string
from sys import argv, exit
import csv


def main(): # main function
    if len(argv) != 3: # ensuring proper usage
        print("Wrong  number of command line arguments.")
        print("Usage: programname.py file.csv dna.txt")

    with open(argv[1], newline = '') as csvfile: #opening database
        reader0 = csv.reader(csvfile, delimiter=",")
        firstl = next(reader0) # isolating the first line to get the names of the STRs
        nos = len(firstl) - 1 #isolating the number of STRs

        STRs = []

        for i in range(1,nos + 1): #adding the names of the STRs to a list for later access
            STRn = firstl[i]

            STRs.append(STRn)

        reader = csv.DictReader(csvfile,firstl) #reading database into memory

        print(reader.fieldnames)
        print(STRs)
        VALUES = []

        sequence_o = open(argv[2], "r")
        sequence = sequence_o.read


        for i in range(nos):
            value = search(sequence, STR = STRs[i])

            VALUES.append(value)

        print(VALUES)


def search(seq, STR): #function to analyze the dna sequences for an individual STR
    val= 0
    start = 0
    STRlen = len(STR)
    EVAL = []
    while True:
        found = seq.find(STR, start)

        if found == -1:
            EVAL.append(val)
            break
        if start != found:
            EVAL.append(val)
            val = 0
            val += 1
            start = found + STRlen
        else :
            val += 1
            start = found + STRlen


    print(EVAL)
    endval = max(EVAL)
    return endval

main()

This generates the following error message:

Traceback (most recent call last): File "dna.py", line 66, in
main() File "dna.py", line 34, in main
value = search(sequence, STR = STRs[i]) File "dna.py", line 47, in search
found = seq.find(STR, start) AttributeError: 'builtin_function_or_method' object has no attribute 'find'