Re: Array - Reading frame problem
by GrandFather (Saint) on Jul 16, 2005 at 13:24 UTC
|
0123456789
123456789
23456789
3456789
456789
56789
6789
789
89
9
Perl is Huffman encoded by design.
| [reply] [d/l] [select] |
|
| [reply] |
|
my @frame1 = @sequence;
my @frame2 = @sequence;
my @frame3 = @sequence;
shift @frame2;
shift @frame3; shift @frame3;
| [reply] [d/l] |
Re: Array - Reading frame problem
by jdporter (Paladin) on Jul 16, 2005 at 20:22 UTC
|
I've written a module to solve your problem directly; it uses tied strings.
See Tie::Scalar::Substring.
In your case, you could use it like this:
$sequence = 1234567890;
my( $frame1, $frame2, $frame3 );
tie $frame1, 'Tie::Scalar::Substring', \$sequence, 0;
tie $frame2, 'Tie::Scalar::Substring', \$sequence, 1;
tie $frame3, 'Tie::Scalar::Substring', \$sequence, 2;
| [reply] [d/l] |
Re: Array - Reading frame problem
by trammell (Priest) on Jul 16, 2005 at 15:59 UTC
|
Hard to say exactly what it is you want (you use the word "array" without using any arrays), but here's a shot:
#!perl -l
my @seq = qw/ 1 2 3 4 5 6 7 8 9 0 /;
for (1 .. 5) {
print "frame $_: ", getframe($_,@seq);
}
sub getframe {
my ($n,@in) = @_;
$n--;
return join '', @in[ $n .. $#in ];
}
__END__
frame 1: 1234567890
frame 2: 234567890
frame 3: 34567890
frame 4: 4567890
frame 5: 567890
| [reply] [d/l] |
Re: Array - Reading frame problem
by graff (Chancellor) on Jul 16, 2005 at 14:51 UTC
|
I'm not sure I understand you're question. Do you want to create an array so that each element in the array contains exactly one character, in sequence, from a string? That's pretty simple with split:
my $sequence = "1234567890";
my @chrsequence = split //, $sequence;
print "array has " . scalar @chrsequence . " elements: @chrsequence\n"
+;
The first argument to split in this case is an empty regex, which happens to have the effect of splitting the input string at every character. | [reply] [d/l] |
|
| [reply] |
|
You just need to learn more of the basics of perl syntax. There are lots of ways to do whatever you want with any subset of array elements:
print "$_\n" for ( @array[1..$#array] );
# which is the same as:
for my $i ( 1 .. $#array ) {
print "$array[$i]\n";
}
# and
$" = "\n";
print "@array[1..$#array]\n";
| [reply] [d/l] |
|
It is, using the slice notation already mentioned above.
my $sequence = "1234567890";
my @chrsequence = (split //, $sequence)[1..length($sequence)-1];
print "array has " . scalar @chrsequence . " elements: @chrsequence\n"
+;
| [reply] [d/l] |
Re: Array - Reading frame problem
by tlm (Prior) on Jul 16, 2005 at 13:12 UTC
|
which is wrong as the "2" in "234567890" will still be in position 1.. instead of 0
No, the "2" in "234567890" is in position 0.
| [reply] |
|
No.. what i meant was if i apply the code
$frame2 = substr($sequence, 1); to $sequence, the position of "2" will be in position "1" as i extract it using substring.
What i meant is if there's anyway for me to extract "234567890" by using an array, instead of substr because i wan "2" to be in position 0.
| [reply] |
|
If what you mean is you want the array equivalent of substr, it's array slices. (splice looks similar to substr syntactically, but is different in that it's destructive of the source array.)
@frame2 = @sequence[ 1 .. $#sequence ];
The module Tie::OffsetArray could probably be used cleverly for this kind of frame-related problem.
| [reply] [d/l] |
Are you doing DNA/RNA seq analysis?
by BerntB (Deacon) on Jul 18, 2005 at 04:54 UTC
|
I don't know what you are doing, but it almost sounds like you are looking for e.g. bioperl.org?
You might also want to look at Mastering Perl for Bioinformatics from O'Reilly. (That page have a free chapter on the net about Bioperl.)
Update:
Hmm... I looked at your other questions (should have done that before answering, I'm still a newbie).
You are working with DNA sequences. If you are going to do a lot of this (and isn't just trying to get help for a lab!), you probably want to read a book or two. There is a Beginning Perl for Bioinformatics. A good start is to look at O'Reilly.
| [reply] |
|
hey.. yea.. i'm doing Bioinformatics and working with DNA sequences etc. Thanks alot for the recommendations! :)
| [reply] |