Alternate DNA_complement:
def DNA_complement(str): str = str.replace('a','x') str = str.replace('t','a') str = str.replace('x','t') str = str.replace('g','y') str = str.replace('c','g') str = str.replace('y','c') return str
# CS190C: Spring 2009 # FirstName LastName # Problem Set 3, Problem 1 import string def DNA_complement(str): dna_list = list(str) # Convert the string to a list # Loop through all of the list indices and convert each letter at that # index in the list to its complement. for i in range(len(dna_list)): if dna_list[i] == 'a': dna_list[i] = 't' elif dna_list[i] == 't': dna_list[i] = 'a' elif dna_list[i] == 'c': dna_list[i] = 'g' elif dna_list[i] == 'g': dna_list[i] = 'c' # Convert the list back into a string, using a blank string as the # separator between each item in the list. return ''.join(dna_list) def DNA_heavy_max(str): # We store the counts of each letter in a list called counts. The count of # the heavier nucleotides come first (the count_letters list shows the # order). counts = [0, 0, 0, 0] count_letters = ['g', 'a', 't', 'c'] # Letters by weight # This loop can be accomplished with by doing str.count('g'), etc. But # that's no fun. It also requires going through the string four times # instead of one. for i in str: if i == 'g': counts[0] += 1 elif i == 'a': counts[1] += 1 elif i == 't': counts[2] += 1 elif i == 'c': counts[3] += 1 # At this point, we know how many times each nucleotide occurs, so we find # the index that has the biggest count. Note that since we search from # left to right, the count of the ith index must be strictly greater than # (not greater than or equal to) the count of max_index. max_index = 0 for i in range(1, len(counts)): if counts[i] > counts[max_index]: max_index = i # Here's where count_letters comes into play. Since it basically tells you # what letter is stored at what index, we use max_index to index into the # count_letters list to obtain what letter corresponds to the max count. return count_letters[max_index] if __name__ == '__main__': f = open(raw_input('Enter file name: '), 'r') # open a file containing DNA strings, one per line for line in f: print 'DNA sequence =', line print 'DNA complement =', DNA_complement(line) print 'DNA heavy max =', DNA_heavy_max(line) # call function DNA complement # call function DNA_heavy_max # print complemented string and most frequently occurring nucleotide
# CS190C: Spring 2009 # FirstName LastName # Problem Set 3, Problem 2 from ps3_09_p2_lib import * #import helper functions import math def distance(p, q): return math.sqrt((p[0]-q[0])**2 + (p[1]-q[1])**2) # Assumes points as at least one point in it. Also assumes that p is not in # points. This is important for nearest_neighbors. def closest(points, p): """ >>> closest([[1, 0], [0, 2]], [0, 0]) [1, 0] """ shortest_point = points[0] shortest_dist = distance(points[0], p) for i in points: i_dist = distance(i, p) if i_dist < shortest_dist: shortest_dist = i_dist shortest_point = i return shortest_point # Assumes there are at least two points in points. For every point up to the # second to last point, we use each point as the single point p in closest # and all points afterwards as the list of points p. Since every point is # compared to all points afterwards, we can show that all pairs of points are # compared. def nearest_neighbors(points): """ >>> nearest_neighbors([[0, 0], [3, 0], [0, 3], [1, 1]]) [[0, 0], [1, 1]] """ p1, p2 = points[0], points[1] dist = distance(p1, p2) for i in xrange(len(points) - 1): c1 = points[i] c2 = closest(points[i+1:], c1) c_dist = distance(c1, c2) if c_dist < dist: p1, p2, dist = c1, c2, c_dist return [p1, p2] if __name__ == '__main__': import doctest doctest.testmod() # Create a list of 100 random points myPoints = random_points(100, 2) # Save the points to a file save_points(myPoints, "points.txt") # Plot the points show_points(myPoints, (0,1,0)) # Create three extra points pointA = [1.0, 0.0] pointB = [1.0, 1.0] pointC = [0.0, 0.0] # Print the distance between two points print "The distance between", pointA, "and", pointB,"is:", distance(pointA, pointB) # Find the point in myPoints closest to point B closeToB = closest(myPoints, pointB); print "The point closes to", pointB, "is:", closeToB # Find the nearest neighbors in myPoints pointX, pointY = nearest_neighbors(myPoints) print "The nearest neighbors are:", pointX, "and", pointY