0

For the DNA assignment in pset6, I'm trying to print out the output of slicing the string that was read from one of the DNA sequence text files, which reads as follows:

AAGGTAAGTTTAGAATATAAAAGGTGAGTTAAATAGAATAGGTTAAAATTAAAGGAGATCAGATCAGATCAGATCTATCTATCTATCTATCTATCAGAAAAGAGTAAATAGTTAAAGAGTAAGATATTGAATTAATGGAAAATATTGTTGGGGAAAGGAGGGATAGAAGG

(Printing this string out is not exactly part of the assignment, rather it's just a way for me to understand what the outputs look like step by step)

Here is what my code looks like:

# Identifies a person based on their DNA
import csv
import sys

# Print error message if wrong number of arguments
if len(sys.argv) != 3:
    print("Usage: python dna.py csv txt")

else:
    # Open csv file and read contents into memory
    # https://docs.python.org/3/library/csv.html#id3
    csv = csv.DictReader(open(f"{sys.argv[1]}"))

    # Open txt file and read its contents into memory
    # https://docs.python.org/3/tutorial/inputoutput.html
    dna = open(f"{sys.argv[2]}").read()

    # Initialize counters for each sequence to 0
    AGATC_counter = 0
    AATG_counter = 0
    TATC_counter = 0
    counter = 0

    # Iterate i over all letters in string in increments of length of AGATC sequence
    # Start at [0] to assign i to first letter in the increment
    for i in range(0, len(dna), len("AGATC") + 1):
        print(dna[i:len("AGATC")])

When I run the program, I get this in my terminal:

AAGGT new line new line new line new line new line new line new line new line new line new line new line new line new line new line new line new line new line new line new line new line new line new line new line new line new line new line new line new line new line new line new line

(Except instead of literally saying "new line," there are actually that many new lines before it says ~/pset6/dna)

I'm not sure what I'm doing wrong here. I want the program to print sequences of 5 letters on each line until it reaches the end of the file. This is so that I know that my counters will be "reading" the correct sub-strings before counting up when I incorporate them later on.

2

I guess you meant print(dna[i:i+len("AGATC")]). In Python, if the end of the substring is before the beginning, the substring is empty, so you are printing a bunch of empty strings.

| improve this answer | |

You must log in to answer this question.

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