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.
11 1889 bvdet 2,851
Expert Mod 2GB
Is your question about Perl or Python? In Python, you will want to use str method replace(). Example: - >>> "ATTAC".replace("A", "B")
-
'BTTBC'
-
>>>
For online tutorials, use these search words: Python tutorial str replace
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?
bvdet 2,851
Expert Mod 2GB
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: - "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().
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 -
sequence.replace("A","W")
-
sequence.replace("C","X")
-
sequence.replace("G","Y")
-
sequence.replace("T","Z")
-
and then replace the intermediate form with the final form. -
sequence.replace("Z","A")
-
sequence.replace("Y","C")
-
sequence.replace("X","G")
-
sequence.replace("W","T")
-
BTW, in perl, this becomes one statement, if I recall correctly: -
sequence =~ s/ACGT/TGCA/g;
-
bvdet 2,851
Expert Mod 2GB
Actually, I prefer the dictionary solution below as a general solution: - >>> dd = {"T": "A", "A": "T", "G": "C", "C": "G"}
-
>>> seq = "ATTCGGCAACT"
-
>>> "".join([dd[s] for s in seq])
-
'TAAGCCGTTGA'
-
>>>
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.
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
bvdet 2,851
Expert Mod 2GB
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 - >>> seq = "ATTCGGCAACTxyz"
-
>>> "".join([dd.get(s, s) for s in seq])
-
'TAAGCCGTTGAxyz'
-
>>>
bvdet 2,851
Expert Mod 2GB
Oralloy,
Please come back anytime you want. The more viewpoints, the better!
BV
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
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Richard |
last post by:
In an attempt to try out some things with php, I have run into a new snag.
The output file is pure text.
Basically it's a simple photo gallery using <ul> links to show various
thumbnails.
In...
|
by: Bob |
last post by:
Everybody,
I've been doing a lot of on-line research and cannot find
any reference to the exact problem I'm having.
Let me preface this question with the fact that I'm coming
from an Oracle...
|
by: Rich |
last post by:
Does anyone know the correct way of opening an unbuffered output file
using STL?
I attempted:
ofstream myfile("fname.txt", ios::binary);
myfile.setbuf(NULL, 0);
and I was informed that...
|
by: tram |
last post by:
How do we pass on the step1 output file to step2 of the same job? I
would like to know the current job's output file programmetically,
instead of hard coding it. Any idaes would be appreciated.
...
|
by: Vikram |
last post by:
I have suddenly started getting this errorin all my ASP.NET applications when
I try to open any aspx page.
Compiler Error Message: CS0016: Could not write to output file...
|
by: Gary |
last post by:
I am getting this error randomly on a web farm for an .aspx:
BC31019: Unable to write to output file
'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET...
|
by: sp |
last post by:
hai
i have got
1. an xml file
2. an xsl file
and my xsl file filters the xml based on attribute value
and the output i receive is in the ordinary format
|
by: undshan |
last post by:
I am writing a code that needs to open a file, create an output file, run through my function, prints the results to the output file, and closes them within a loop. Here is my code:
#include...
|
by: John Bailo |
last post by:
This is a my solution to getting an Output parameter from a SqlDataSource.
I have seen a few scant articles but none of them take it all the way to
a solution. Hopefully this will help some...
|
by: jebbyleezer |
last post by:
Hello, I have source code that builds correctly, however, after the program terminates the output file produced is empty.
Here is my source code:
import java.io.*;
import java.util.Scanner;...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |