Wednesday, 1 June 2016

Transcribing DNA into RNA

In Rosalind problem 2 you are asked to transcribe a DNA sequence into an RNA sequence, i.e. changing the T's into U's. As with the first problem, this one was also pretty straightforward and I managed to solve it quickly.

Like the first program, I wanted this one to open a file and read each letter of each row individually, so this part of the program became pretty much the same as the previous program. I then wanted the program to change all the T's into U's and print the new string so I wrote the following code:


   with open('sampledata2.txt') as f:   
       for line in f:                   
           for nt in line:              
               if nt == 'T':            
                   print('U')           
               else:                    
                   print(nt)            

Before I even tried this code I knew it would not give me the formatting I wanted of the results, and upon trying it what I got was the correct letters out but only one letter per new row. So now I knew that the exchange of T to U was working, I just needed to append the letters to the same string and preferably print them to a file (mainly to facilitate uploading the answer to Rosalind).

To append each letter to a new string I started with defining an empty string called rna in the beginning of the program. Then, as the program iterates over each letter, it appends the letter to the string rna using the command += . When the iteration was done I wanted to save the string to a file that I could upload to Rosalind and to do this I used with open. Below is the final code:

   rna = ''                               
   with open('sampledata2.txt') as f:   
       for line in f:                   
           for nt in line:              
               if nt == 'T':            
                   rna+='U'               
               else:                      
                   rna+=nt                
   with open('newrna.txt','w') as answer: 
       answer.write(rna)                  

Each time I run this program the file newrna.txt gets overwritten with the new result. This is the result of using 'w' and what I wanted for this program. If I instead wanted to append the result to the existing file I could use 'a'.


No comments:

Post a Comment