User Tools

Site Tools


examkey

Exam Key


exam_key.py
"""
DSCI 511 Exam Answer Key
"""
 
def gc_content(sequence):
	"""
	Caclulates GC content in a sequence
	"""
	gc = 0
	for nt in sequence:
		if nt == 'G' or nt == 'C':
			gc += 1
		else:
			continue
	return gc/len(sequence)
 
 
def word_count(input_file):
	"""
	Counts the numbers of lines and characters in a file
	"""
	try:
		file_handle = open(input_file)
	except:
		return -1
	lines = 0
	chars = 0
	with file_handle:
		for line in file_handle:
			line = line.rstrip()
			chars += len(line)
			lines += 1
	return lines, chars
 
 
def matrix_mean(matrix):
	"""
	Calculates the mean of each row in a matrix
	"""
	l = []
	for i in range(len(matrix)):
		sum = 0
		for n in range(len(matrix[0])):
			sum += matrix[i][n]
		l.append(sum/len(matrix[0]))
	return l
 
 
def element_counter(some_list):
	"""
	Counts occurences of each element in a list
	"""
	d = {}
	for element in some_list:
		d[element] = d.get(element, 0) + 1
	return d
 
 
def motif_coordinates(sequence, motif):
	"""
	Identifies the coordinates of a motif within a sequence
	"""
	for i in range(len(sequence) - len(motif) + 1):
		if sequence[i:i+len(motif)] == motif:
			return i+1, i+len(motif)
 
 
if __name__ == '__main__':
	"""
	Code for testing functions
	"""
	print(gc_content('ATTGCC')) # 0.5
	print(word_count('test.txt')) # (2, 6)
	print(matrix_mean([[1,2,3],[5,6,7]])) # [2.0, 6.0]
	print(element_counter(['ATG', 'TAG', 'TTT', 'TAG', 'TTT'])) # {'TAG': 2, 'TTT': 2, 'ATG': 1}
	print(motif_coordinates('ATGCTGTTAGCGA', 'CGA')) # (11, 13)
 
examkey.txt · Last modified: 2018/12/10 10:59 by dokuroot