Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sys import argv, exit
- import csv
- import re
- if len(argv) < 3:
- print("mising command-line argument")
- exit(1)
- #INSTRUCTIONS
- #1. Read the sequence file into a string (you've got this).
- #2. Open the csv file and get each sub string ("AGATC", "TCTG", etc) from the first row into a list (you've essentially done this too).
- #3. For each item in the list of sub strings, look through the sequence you read in step 1 and see what the longest sequential run is. Store this number in a list so you have the longest runs of all sub strings.
- #4. Iterate through the rest of the CSV file, comparing the numbers in each row to the list of numbers you created in step 3. When all numbers match, the name from this row is the one to print.
- with open(argv[1],"r") as file, open(argv[2],"r") as csvfile: # you may need to swap incdices
- count = 0
- contents = file.read() #1. Read the sequence file into a string (you've got this).
- csvcontents = csv.reader(csvfile)
- #2. Open the csv file and get each sub string
- #("AGATC", "TCTG", etc) from the first row
- #into a list (you've essentially done this too).
- header = next(csvcontents) # this is just a way to get the next row one time
- # see what next header does
- print("header prints:")
- print(header)
- #SENTOX ADVICE:
- # when you loop through anything that iterates in python
- # objects usually have a next method that provides the next
- # when you call something like
- # for row in csvcontents:
- # internally python will keep calling next() from csvcontents to get each row one at a time
- # since the file has just been opened, it is the first row by definintion
- # with csv reader, the returns a lot of values
- # the header is already a list of values from the first row. appending these values into another list is reduntant
- print("attempting to print rows of csv file")
- for row in csvcontents:
- print(row)
- #SENTOX ADVICE:
- #you can use the subscript [1:] which means:
- #make a copy of this list starting from element 1 (which is the second element)
- #to the end of the list... in other words, drop the first element
- # this subscript cn be used directly on the list returned by next
- #complist = next(csvcontents)[1:]
- #3. For each item in the list of sub strings,
- #look through the sequence you read
- #in step 1 and see what the longest
- #sequential run is. Store this number
- #in a list so you have the longest runs of all sub strings.
- #for item in sublist[1:]:
- #while contents[beg:end]:
- for item in contents: # look at each item in the list
- beg = 0 # beginning index
- end = len(item) # ending index
- seqrun = 0 # number of times the sequence runs/repeats
- longest = 0
- while contents[beg:beg+len(item)]: # while the substring of contents from beginning to end have values
- #while contents[beg:end]: # while the substring of contents from beginning to end have values
- if contents[beg:beg+len(item)] == item: # if the span of contents from beg to beg plus the length of the item
- seqrun = 1 # it occurs at least once
- while contents[beg + len(item):end + len(item)] == item: # as long as the contents span from beginning to end matches the contents span when incremented by end
- seqrun += 1
- beg += len(item)
- end += len(item)
- if seqrun > longest:
- longest = seqrun
- if seqrun > 1:
- print(item + " repeats " + seqrun + " times")
- beg += 1
- end += 1
- else:
- beg += 1
- end += 1
- # SENTOX ADVICE
- # you are adding end to the beginning and itself, treating it as a proxy for len(item)
- # this would work, except end is no longer len(item)
- # it has been incremented through string over time
- # meaning we could be at any number... it could be index 500 of the string
- # so instead of 500 + 4 (if the item were 4 characters long)
- # once that is fixed, a variable is needed to hold the largest
- # seqrun found, otherwise you will just have the most recent seqrun instead
Advertisement
Add Comment
Please, Sign In to add comment