Skip to content

Instantly share code, notes, and snippets.

@fomightez
Created January 14, 2015 04:39
Show Gist options
  • Save fomightez/f73300b7be3a982d6b09 to your computer and use it in GitHub Desktop.
Save fomightez/f73300b7be3a982d6b09 to your computer and use it in GitHub Desktop.
basic version of DNA FASTA records converted to RNA, see https://github.com/fomightez/sequencework/blob/master/ConvertSeq/ConvertFASTAdnaSEQtoRNA.py for a fancier version
#! /usr/bin/env python
# DNA_to_RNA.py
# basic version of DNA FASTA records converted to RNA, see https://github.com/fomightez/sequencework/blob/master/ConvertSeq/ConvertFASTAdnaSEQtoRNA.py for a fancier version
# adapted from start and end of latlon_3.py - from Chapter 10 PCfB
# Read in each line of the example file
# Set the input file name
# (The program must be run from within the directory
# that contains this data file)
InFileName = 'dna.faa'
# Open the input file for reading
InFile = open(InFileName, 'r')
# Initialize the counter used to keep track of records
FASTA_records_counter = 0
# Open the output file for writing
# Do this *before* the loop, not inside it
OutFileName=InFileName + "_rna.faa"
OutFile=open(OutFileName,'w') # You can append instead with 'a'
# Loop through each line in the file
for line in InFile:
line = line.strip ();#this way I have better knowledge of first character and control of ends ultimately becaue I know there isn't any unless I add
if line.startswith('>'): # only need those that start with the greater-than symbol
FASTA_records_counter += 1 #<-- short way to write `FASTA_records_counter = FASTA_records_counter + 1`
output_line = line
else:
output_line = (line.upper()).replace('T','U')
OutFile.write(output_line +"\n")
# After the loop is completed, close the files
InFile.close()
OutFile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment