User Tools

Site Tools


assignment4key

Assignment 4 Answer Key

fastq_fasta.py
'''
Tai Montgomery, Oct. 10, 2018
 
Functions for processing fastq files in various ways.
 
Usage: import fastq_fasta or python3 fastq_fasta.py 
 
'''
 
def fastq_fasta(input_file, output_file):
 
	'''
	Converts a fastq formatted file to fasta formatted file.
 
	Input: fastq file
	Output: fasta file
	'''
	try:
		infile = open(input_file)
		outfile = open(output_file, 'w')	
	except:
		return -1
 
	with infile, outfile:
		line_number = 0
		for line in infile:
			line_number += 1
			if (line_number + 3) % 4 == 0:
				outfile.write('>'+line[1:])
			elif (line_number + 3) % 4 == 1:
				outfile.write(line)
			else:
				continue
		print("\nDone......\nReads processed:", line_number/4, "\n")	
 
 
def fastq_trimmer(input_file, output_file, trim_5p = 0, trim_3p = 0):
	'''
	Trims a user-defined number of nucleotides from the
	3' and 5' ends of reads in a fastq file.
 
	Input: fastq file
	Output: fastq file
	'''
	try:
		infile = open(input_file)
		outfile = open(output_file, 'w')
	except:
		return -1
 
	with infile, outfile:
		line_number = 0
		for line in infile:
			line_number += 1
			if (line_number + 3) % 4 == 1:
				outfile.write(line[trim_5p:len(line)-trim_3p-1] + '\n')
			elif line_number % 4 == 0:
				outfile.write(line[trim_5p:len(line)-trim_3p-1] + '\n')
			else:
				outfile.write(line)
		print("\nDone......\nReads processed:", line_number/4, "\n")
 
 
if __name__ == '__main__':
	input_file = input('Enter input file name: ')
	output_file = input('Enter output file output name: ')
 
	# for testing fastq_trimmer, uncomment the next two lines.
	#trim_5p = int(input("Number of nts to trim from 5' end of sequences: ")) 
	#trim_3p = int(input("Number of nts to trim from 3' end of sequences: ")) 
 
 
	# for testing, uncomment the function to test.
	#fastq_fasta(input_file, output_file)
	#fastq_trimmer(input_file, output_file, trim_5p, trim_3p)
assignment4key.txt · Last modified: 2018/10/16 21:34 by dokuroot