Help: Reverse Letters

A

Amy Lee

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

Jürgen Exner

Amy Lee said:
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
 
R

RedGrittyBrick

Amy said:
Hello,

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

GCATGCCCTACGGGTACCCCAGTA

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

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.
 
A

Amy Lee

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


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

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

Amy
 
R

RedGrittyBrick

RedGrittyBrick said:
reverse needs a list

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

Oh no it doesn't! How embarassing.

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

Mirco Wahab

Amy said:
while (<$FILE_IN>)
{
unless (/^>.*$/)
{
reverse $_;

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

$_ =reverse;

Regards

M.
 
B

Ben Bullock

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

Jürgen Exner

Amy Lee said:
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
 
A

Amy Lee

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
 
M

Mirco Wahab

Amy said:
Thank you very much. However, I still have a problem which I can't
understand well. What if my file content is
CGATCC
ACCTC
In fact, the sequence is
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

Jürgen Exner

Amy Lee said:
What if my file content is
CGATCC
ACCTC
In fact, the sequence is
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

Jürgen Exner

Jürgen Exner said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top