Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help: Reverse Letters

0 views
Skip to first unread message

Amy Lee

unread,
May 2, 2008, 10:44:04 AM5/2/08
to
Hello,

There's a problem while I'm processing sequences. My file content.

>seq1
ATGCCCTGACGTAAACGTAGGCTACG
>seq2
GCATGCCCTACGGGTACCCCAGTA

And I hope I can reverse the sequences to be this.

>seq1
GCATCGGATGCAAATGCAGTCCCGTA
>seq2
ATGACCCCATGGGCATCCCGTACG

I use "reverse" to do this, but it dose not work any more.
So could you tell me how to solve this problem?

Thank you very much~

Regards,

Amy Lee

Jürgen Exner

unread,
May 2, 2008, 10:48:28 AM5/2/08
to
Amy Lee <openlin...@gmail.com> wrote:
>There's a problem while I'm processing sequences. My file content.
>ATGCCCTGACGTAAACGTAGGCTACG

>And I hope I can reverse the sequences to be this.
>GCATCGGATGCAAATGCAGTCCCGTA

You asked a self answering question: The reverse() function does exactly
that.

>I use "reverse" to do this, but it dose not work any more.

?????
Are you saying that coming back into the office after yesterday's Labour
Day the functionality of reverse() in Perl has changed?

jue

Amy Lee

unread,
May 2, 2008, 10:53:08 AM5/2/08
to

Well, I'd better paste my codes here. I'm still working :(
while (<$FILE_IN>)
{
unless (/^>.*$/)
{
reverse $_;
}
print $_;
}
Thank you very much~

Amy

RedGrittyBrick

unread,
May 2, 2008, 10:52:55 AM5/2/08
to
Amy Lee wrote:
> Hello,
>
> There's a problem while I'm processing sequences. My file content.
>
>> seq1
> ATGCCCTGACGTAAACGTAGGCTACG
>> seq2
> GCATGCCCTACGGGTACCCCAGTA
>
> And I hope I can reverse the sequences to be this.
>
>> seq1
> GCATCGGATGCAAATGCAGTCCCGTA
>> seq2
> ATGACCCCATGGGCATCCCGTACG
>
> I use "reverse" to do this, but it dose not work any more.
> So could you tell me how to solve this problem?
>

reverse needs a list

$ perl -e '$x="GCAT";$y=join"",reverse(split "",$x);print"$y\n"'
TACG

doubtless there are better ways to do this.

--
RGB

RedGrittyBrick

unread,
May 2, 2008, 10:55:40 AM5/2/08
to

Oh no it doesn't! How embarassing.

perl -e '$x="GCAT";$y=reverse($x);print"$y\n"'
TACG

--
RGB

Ben Bullock

unread,
May 2, 2008, 11:09:25 AM5/2/08
to
On Fri, 02 May 2008 22:53:08 +0800, Amy Lee wrote:

> reverse $_;

"Useless use of reverse in void context at ./usenet-2May2008-2.pl line 8."

As A. Sinan Unur told you previously, you should always

use warnings;
use strict;


Jürgen Exner

unread,
May 2, 2008, 11:15:32 AM5/2/08
to
Amy Lee <openlin...@gmail.com> wrote:
>> Amy Lee <openlin...@gmail.com> wrote:
>>>There's a problem while I'm processing sequences. My file content.
>>>ATGCCCTGACGTAAACGTAGGCTACG
>>>And I hope I can reverse the sequences to be this.
>>>GCATCGGATGCAAATGCAGTCCCGTA
>>
>> You asked a self answering question: The reverse() function does exactly
>> that.
>>
>>>I use "reverse" to do this, but it dose not work any more.
>>
>> ?????
>> Are you saying that coming back into the office after yesterday's Labour
>> Day the functionality of reverse() in Perl has changed?
> unless (/^>.*$/)
> {
> reverse $_;
> }

Had you used
use warnings; use strict;
as it strongly recommended then perl itself would have told you:

"Useless use of reverse in void context at [...] line [...]."

In other words: You reverse the content of $_ and then throw it away.

jue

Mirco Wahab

unread,
May 2, 2008, 11:04:27 AM5/2/08
to
Amy Lee wrote:
> while (<$FILE_IN>)
> {
> unless (/^>.*$/)
> {
> reverse $_;

You need to reverse the order of the reverse
operator, before reversing the text:

$_ =reverse;

Regards

M.

Glenn Jackman

unread,
May 2, 2008, 11:51:34 AM5/2/08
to

Also, the newline character will be reversed, so that should be removed
first:

while (<$FILE_IN>) {
chomp;
$_ = reverse unless /^>/;
print "$_\n";
}

--
Glenn Jackman
"If there is anything the nonconformist hates worse than a conformist,
it's another nonconformist who doesn't conform to the prevailing
standard of nonconformity." -- Bill Vaughan

Amy Lee

unread,
May 2, 2008, 12:04:29 PM5/2/08
to
On Fri, 02 May 2008 15:51:34 +0000, Glenn Jackman wrote:

> At 2008-05-02 11:04AM, "Mirco Wahab" wrote:
>> Amy Lee wrote:
>> > while (<$FILE_IN>)
>> > {
>> > unless (/^>.*$/)
>> > {
>> > reverse $_;
>>
>> You need to reverse the order of the reverse
>> operator, before reversing the text:
>>
>> $_ =reverse;
>
> Also, the newline character will be reversed, so that should be removed
> first:
>
> while (<$FILE_IN>) {
> chomp;
> $_ = reverse unless /^>/;
> print "$_\n";
> }

Thank you very much. However, I still have a problem which I can't
understand well. What if my file content is
>seq1
ACGGTC
ACTG
>seq2
CGATCC
ACCTC
In fact, the sequence is
>seq1
ACGGTCACTG
>seq2
CGATCCACCTC

So if I use the above codes to do that, I can't acquire the correct
sequences. I hope I can process the file that I mention.

Thank you very much~

Amy Lee

Mirco Wahab

unread,
May 2, 2008, 12:21:15 PM5/2/08
to
Amy Lee wrote:
> Thank you very much. However, I still have a problem which I can't
> understand well. What if my file content is
>> seq1
> ACGGTC
> ACTG
>> seq2
> CGATCC
> ACCTC
> In fact, the sequence is
>> seq1
> ACGGTCACTG
>> seq2
> CGATCCACCTC
>
> So if I use the above codes to do that, I can't acquire the correct
> sequences. I hope I can process the file that I mention.

Then you have to extract the sequences between
some bounds marker according to your problem.

You coult read the file w/different record
separator (eg. "\nseq") or create a regular
expression to extractz the sequences from
the file slurped into a buffer, like:

...
use warnings;
use strict;

# read the file into a buffer ($seq)
open my $fh, '<', 'sequence.dat' or die $!;
my $seq; do { local $/; $seq = <$fh> };
close $fh;

# extract sequence records
my $match = qr{>[^\n\r]+([^>]+|$)}s;
while($seq =~ /$match/g) {
(my $sequence = $1) =~ tr/\n\r//d;
print "'$sequence'=>" . reverse($sequence) . "\n"
}
...


Regards

M.

Jürgen Exner

unread,
May 2, 2008, 12:58:58 PM5/2/08
to
Amy Lee <openlin...@gmail.com> wrote:
[reversing strings]

>What if my file content is
>>seq1
>ACGGTC
>ACTG
>>seq2
>CGATCC
>ACCTC
>In fact, the sequence is
>>seq1
>ACGGTCACTG
>>seq2
>CGATCCACCTC

You have a gift of stating requirements in a complex way that is
ambigious at best.

Are you saying that your data is split among many lines and should be
reversed across the whole file instead of line by line?

If that is the case then just slurp in the whole file into a single
string and reverse that string. Depending on if you want to preserve the
line breaks you may either have to chomp()/remove them or adjust the
leading/trailing line break of the whole file.

jue

Jürgen Exner

unread,
May 2, 2008, 5:57:43 PM5/2/08
to
Jürgen Exner <jurg...@hotmail.com> wrote:
>Are you saying that your data is split among many lines and should be
>reversed across the whole file instead of line by line?
>
>If that is the case then just slurp in the whole file into a single
>string and reverse that string. Depending on if you want to preserve the
>line breaks you may either have to chomp()/remove them or adjust the
>leading/trailing line break of the whole file.

Oh, or of course you could just slurp the whole file into an array and
run reverse() twice, once on the array and once on each element of the
array. Details see the man page for reverse().

jue

Keith Keller

unread,
May 2, 2008, 11:40:29 PM5/2/08
to
On 2008-05-02, Amy Lee <openlin...@gmail.com> wrote:
>
> There's a problem while I'm processing sequences. My file content.

If you're processing FASTA files, why aren't you using BioPerl, as
others have already advised you to do? BioPerl would parse your file
for you, so that you could retrieve it as a scalar directly instead of
parsing the file yourself.

--keith

--
kkeller...@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information

0 new messages