Click here to get back home

Help: Replace Help

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Help: Replace Help Amy Lee 05-01-2008
Posted by Amy Lee on May 1, 2008, 8:29 am
Please log in for more thread options
Hello,

I'm going to process some RNA sequences files. And I make a small script
to reverse these sequences. However, I face a problem while it's running
because of order problem.

This is my file contents.

>seq1
ACGU
>seq2
GUACCGU

And I wanna replace A to C, C to A, G to U, U to G. So from my point the
reversed file should be viewed like this.

>seq1
CAUG
>seq2
UGCAAUG

This is my codes.

if (@ARGV == 1)
{
$file = $ARGV[0];
unless (-e $file)
{
print "***Error: $file dose not exist.\n";
next;
}
unless (open $FILE_IN, '<', $file)
{
print "***Error: Cannot read $file.\n";
next;
}
while (<$FILE_IN>)
{
unless (/^>.*$/)
{
s/A/C/g;
s/C/A/g;
s/G/U/g;
s/U/G/g;
}
print $_;
}
close $FILE_IN;
}

When I finished doing this task, the file is like this.

>seq1
AAGG
>seq2
GGAAAGG

And I don't wanna use BioPerl to solve this tiny problem, anyway I'm
trying to know how to do that.

So how to solve this kind of order problem? I suppose that the replacement
must process at the same time.

Thank you very much~

Regards,

Amy Lee

Posted by A. Sinan Unur on May 1, 2008, 8:46 am
Please log in for more thread options

> Re: Help: Replace Help

You have just wasted your subject line by repeating the word 'Help'.
Clearly, by posting a question here, you are asking for help. Repeating
the word 'help' does not serve any useful purpose.

> This is my file contents.
>
>>seq1
> ACGU
>>seq2
> GUACCGU
>
> And I wanna replace A to C, C to A, G to U, U to G. So from my point
> the reversed file should be viewed like this.
>
>>seq1
> CAUG
>>seq2
> UGCAAUG
>
> This is my codes.

You are missing

use strict;
use warnings;

> if (@ARGV == 1)
> {
> $file = $ARGV[0];
> unless (-e $file)
> {
> print "***Error: $file dose not exist.\n";
> next;
> }
> unless (open $FILE_IN, '<', $file)
> {
> print "***Error: Cannot read $file.\n";
> next;
> }

I do not understand what the 'next's are for. You should not send error
messages to STDOUT lest it also contain output you would like to use
further. You should show the reason for the error in your error messages
by including the $! variable. In short, replace all of the above with:

my ($file) = @ARGV;

open my $FILE_IN, '<', $file
or die "Cannot open '$file': $!";

Now, if you are processing files in a loop, replace die with warn.

Let's suppose $_ contains ACGU

> s/A/C/g;

Now it is CCGU.

> s/C/A/g;

Now it is AAGU.

> s/G/U/g;

Now it is AAUU.

> s/U/G/g;

Now it is AAGG.

I am assuming this is not what you wanted.

> And I don't wanna

s/wanna/want to/

wanna makes you sound childish.

> use BioPerl

Well, I do not know a thing about BioPerl so ...

#!/usr/bin/perl

use strict;
use warnings;

my %subst = qw( A C C A G U U G );
my @strings = qw( ACGU GUACCGU );

print "Before:\t@strings\n";

s/([ACGU])/$subst/g for @strings;

print "After\t@strings\n";

__END__

--
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/

Posted by szr on May 1, 2008, 9:21 pm
Please log in for more thread options
A. Sinan Unur wrote:
[...]
> Clearly, by posting a question here, you are asking for help.

I disagree that, in general, by simply posting, one is seeking help. One
could just as well be seek a discussion, or insight on something, but
not necessarily assistance. After all, this /is/ a *discussion* group
:-)

--
szr



Posted by Amy Lee on May 1, 2008, 8:50 am
Please log in for more thread options
Sorry, I did a principle mistake in my post.

I hope replace A to U, U to A, C to G, G to C.

Regards,

Amy

Posted by A. Sinan Unur on May 1, 2008, 8:55 am
Please log in for more thread options

> Sorry, I did a principle mistake in my post.
>
> I hope replace A to U, U to A, C to G, G to C.

You can easily adapt both Jurgen's (better for single character lookup
table driven substitutions) or mine to work with whatever you need.

Sinan

--
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/

Similar ThreadsPosted
s/$match/$replace/ fails when $replace has backreferences September 5, 2005, 7:05 pm
Replace with nothing May 12, 2005, 7:54 am
HOW TO replace ' but not ?' October 27, 2004, 4:30 pm
search & replace September 7, 2004, 12:27 pm
search and replace help April 22, 2005, 2:11 am
searc and replace April 22, 2005, 3:10 am
search and replace September 2, 2005, 6:12 am
match 1/2/3, replace with a/b/c October 15, 2005, 10:30 am
Search and Replace December 6, 2005, 3:41 am
Search and Replace May 24, 2006, 8:11 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap