By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

Getting a certain output from a file

P: 2
Hi, all.

I'm very new to python and it's a requirement for my biology major to take at least one class in bioinformatics.

I am having some difficulty with the homework my professor gave. The assignment is below:

Write a Perl script that computes the complement
of a DNA sequence. In other words, your script should convert all
A's to T's, C's to G's, G's to C's, and T's to A's.

The input is one sequence in FASTA format in a file called "dna.txt".

For example if the file contains

>human
ACCGT

then the output of the program should be TGGCA. Note that your program should work for
any sequence in this format and not just the given example.

________________________

How do I do this, because I have absolutely no idea. He wants a very basic code.

Also, where are good tutorials online for Python? I did order a book but until I receive it I need some thing to help me with homework. Thank you, I really appreciate it.
Sep 13 '10 #1
Share this Question
Share on Google+
11 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Is your question about Perl or Python? In Python, you will want to use str method replace(). Example:
Expand|Select|Wrap|Line Numbers
  1. >>> "ATTAC".replace("A", "B")
  2. 'BTTBC'
  3. >>> 
For online tutorials, use these search words: Python tutorial str replace
Sep 13 '10 #2

P: 2
It's Python. My university changed to using python this semester.

So I tried what you suggested and did a search. I tried to do it one way I found which was the following:

with open ("c:/temp/dna.fasta.txt", "r") as myfile:
lines = myfile.readlines()
print(lines)
['>human \n', 'ACCGT \n']

str = "ACCGT";
>>> print str.replace("A", "T")
SyntaxError: invalid syntax

^^ that way gave me an error & highlighted str

Then I tried your way by doing this:

"ACCGT".replace("A", "T")
'TCCGT'

^ This worked but it has to do it for each of the letters so I tried this:


"ACCGT".replace ("A", "T" + "C", "G" + "T", "A" + "G", "C")

Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
"ACCGT".replace ("A", "T" + "C", "G" + "T", "A" + "G", "C")
TypeError: replace() takes at most 3 arguments (5 given)


How can I use a code to do replace all the letters at once without each time copying the last output and then making a new str.replace line?
Sep 13 '10 #3

bvdet
Expert Mod 2.5K+
P: 2,851
Do not use str as a variable name, because the built-in function str() will be masked. Please use code tags when posting code.

To replace multiple letters:
Expand|Select|Wrap|Line Numbers
  1. "ATTACGGCC".replace("A", "T").replace("C", "G").replace("T", "A").replace("G", "C")
Doing it this way gives you the wrong answer though. There is a trick you can use to get around that. Replace "A" with "t" and "T" with "a", then convert the string to upper case with str method upper().
Sep 13 '10 #4

Expert 100+
P: 621
You can iterate over each letter individually and change it. If you know how to use a dictionary, you can store the "from" and "to" values in one dictionary, otherwise if/elif statements would probably be used.
Expand|Select|Wrap|Line Numbers
  1. with open ("c:/temp/dna.fasta.txt", "r") as myfile:
  2. lines = myfile.readlines()
  3. ##print(lines)
  4. for one_rec in lines:
  5.     print "-" * 50
  6.     print one_rec
  7.     new_rec = []
  8.     for each_char in one_rec:
  9.         if each_char == "A":
  10.             each_char = "T"
  11.         elif each_char == "C":
  12.             each_char = "G"
  13.         new_rec.append(each_char)
  14.     print "".join(new_rec) 
Also, where are good tutorials online for Python
http://www.freenetpages.co.uk/hp/alan.gauld/tutcont.htm
http://www.greenteapress.com/thinkpy...tml/index.html
http://hetland.org/writing/instant-python.html
Sep 13 '10 #5

Oralloy
Expert 100+
P: 983
Folks, I may be stoned or stupid, but it'll be quicker and more concise to make eight changes.

Change the original string to an intermediate form
Expand|Select|Wrap|Line Numbers
  1. sequence.replace("A","W")
  2. sequence.replace("C","X")
  3. sequence.replace("G","Y")
  4. sequence.replace("T","Z")
  5.  
and then replace the intermediate form with the final form.
Expand|Select|Wrap|Line Numbers
  1. sequence.replace("Z","A")
  2. sequence.replace("Y","C")
  3. sequence.replace("X","G")
  4. sequence.replace("W","T")
  5.  
BTW, in perl, this becomes one statement, if I recall correctly:
Expand|Select|Wrap|Line Numbers
  1. sequence =~ s/ACGT/TGCA/g;
  2.  
Sep 13 '10 #6

bvdet
Expert Mod 2.5K+
P: 2,851
Actually, I prefer the dictionary solution below as a general solution:
Expand|Select|Wrap|Line Numbers
  1. >>> dd = {"T": "A", "A": "T", "G": "C", "C": "G"}
  2. >>> seq = "ATTCGGCAACT"
  3. >>> "".join([dd[s] for s in seq])
  4. 'TAAGCCGTTGA'
  5. >>> 
Sep 13 '10 #7

Expert 100+
P: 621
Folks, I may be stoned or stupid, but it'll be quicker and more concise to make eight changes.
It won't be quicker, because you have to process each record 8 times, or however may replacements there are, and each replace requires 2 blocks of memory. The starting point should be to process the record once and append to a list so no intermediate blocks of memory are required (although, with short files the difference may be negligible).

Actually, I prefer the dictionary solution below as a general solution
Should we assume that all letters will be changed? I don't know. If all letters are not changed, then an if statement is required to test for membership in the dictionary, or first populate the dictionary with all letters, for ltr in string.uppercase: dd[ltr]=ltr, and then change the dictionary to reflect the letters that should be changed.
Sep 13 '10 #8

Oralloy
Expert 100+
P: 983
Thanks,

I was sticking my nose into a forum where I'm still learning. I appreciate your patience

I saw a few solutions that would have mapped two genomes into one. I was just trying to show how to avoid the problem without being rude.

The dictionary solution is much better, and now I know how to do it.

Thanks!
Oralloy
Sep 13 '10 #9

bvdet
Expert Mod 2.5K+
P: 2,851
Should we assume that all letters will be changed? I don't know. If all letters are not changed, then an if statement is required to test for membership in the dictionary, or first populate the dictionary with all letters, for ltr in string.uppercase: dd[ltr]=ltr, and then change the dictionary to reflect the letters that should be changed.
The dictionary would be defined for the letters that need to be changed. Dictionary method get() comes in handy if the dictionary does not contain all possible letters
Expand|Select|Wrap|Line Numbers
  1. >>> seq = "ATTCGGCAACTxyz"
  2. >>> "".join([dd.get(s, s) for s in seq])
  3. 'TAAGCCGTTGAxyz'
  4. >>> 
Sep 13 '10 #10

bvdet
Expert Mod 2.5K+
P: 2,851
Oralloy,

Please come back anytime you want. The more viewpoints, the better!

BV
Sep 13 '10 #11

Oralloy
Expert 100+
P: 983
Folks,

I think the point of ah68's assignment was to introduce him to using computers as a tool, not to write a general genome manipulation tool.

Let's not confuse the man. I know that in industry the good folks all try to anticipate problems and prepare for them, even when they don't solve them. And I think its a good practice. If anyone working for me didn't try to think things through, I'd be seriously worried.

Anyway, that's my current 2 cents value.

Cheer!
Oralloy
Sep 13 '10 #12

Post your reply

Sign in to post your reply or Sign up for a free account.