User Tools

Site Tools


assignment5key

Assignment 5 Answer Key

assignment5_key.py
 
'''
Tai Montgomery, Oct. 22, 2018
 
Functions for transposing matrices and counting miRNA reads in a fastq file.
 
Usage: import assignment5_key or python3 assigment5_key.py
 
'''
def transposer(matrix):
	'''
	Tranpsoses a matrix.
 
	Input:  a matrix
	Output: returns the matrix transposed
	'''
	new_matrix = [] 
	for i in range(len(matrix[0])):
		list_temp = []
		for n in range(len(matrix)):
			list_temp.append(matrix[n][i])
		new_matrix.append(list_temp)
	return new_matrix
 
 
def miRNA_counter(input_fastq_file, input_fasta_file, output_file):
	'''
	Identifies the numbers of reads in a fastq file  
	corresponding to known miRNAs.
 
	Input:  1) fastq file with adapters removed
	        2) fasta file containing miRNA sequences
	Output: tab delimited file (sequence\treads)
	'''
	try:
		infile1 = open(input_fastq_file)
		infile2 = open(input_fasta_file)
		outfile = open(output_file, 'w')
	except:
		print ("Error: one or more files couldn't be opened")
		return -1
 
	fastq_dictionary = {}
	line_number = 0
	with infile1, infile2, outfile:
		for line in infile1:
			line_number += 1
			if (line_number + 3) % 4 == 1:
				fastq_dictionary[line] = fastq_dictionary.get(line, 0) + 1
			else:
				continue
		for line in infile2:
			if line[0] == '>':
				name = line.rstrip()
			else:
				outfile.write(name[1:] + '\t' + str(fastq_dictionary.get(line, 0)) + '\n')
 
if __name__ == '__main__':
	function = input('Test transposer (t) or miRNA_counter (m) function: ')
	if function == 't':
		matrix = [[1,2,3], [4,5,6], [7,8,9], [10,11,12]]
		print(transposer(matrix))
	elif function == 'm':
		miRNA_counter('small_RNAs.fastq', 'cel_miRNA.fa', 'output_file.txt')
	else:
		print('Error: unrecongnized input')
assignment5key.txt · Last modified: 2018/10/30 09:53 by dokuroot