473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Getting a certain output from a file

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
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:
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
ah68
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
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:
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
dwblas
626 Expert 512MB
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
988 Expert 512MB
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
2,851 Expert Mod 2GB
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
dwblas
626 Expert 512MB
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
988 Expert 512MB
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
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
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
2,851 Expert Mod 2GB
Oralloy,

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

BV
Sep 13 '10 #11
Oralloy
988 Expert 512MB
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

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

Similar topics

2
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...
2
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...
5
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...
10
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. ...
1
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...
0
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...
3
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
3
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...
1
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...
0
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;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.