Waterdrake has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks I'm new here as you can see. I'm a student in bioinformatics and I'm starting to learn Perl hopping that one day I will become a monk :D . So the problem at hand I need to take sequences from a tabular txt document the format of the sequences is this (24 11 AAAGAAAGCTGAAGACTAAAGAAA) the first number is the number of the sequence the second number is the number i need it says how many times should the sequence be copied. I dont know how to make it I mean I have a base code but its full of holes . Here is a short example of what i need the code to do :
1 2 tt # the first two are the sequence from the doc 2 3 aa 1 tt -> 1-1 tt 2 tt -> 1-2 tt 3 aa -> 2-1 aa #and so on I will give you my code (but i dont think it will help :/ ) #!/usr/bin/perl\ open FH "<", "filename.txt" or die $!; open FHWRITE, ">>results.txt" or die $!; $seq1 = #dont know how for the $ to take the value of the first seq1 $countnumb = split (' ',); # dont know how to take the second number f +or count number but i think its whit split $count =0; while ($count ne $countnumb) { print "$seq1"; $count++; } # i dont know when this finishes how to make it go for the second seq
need help :D

Replies are listed 'Best First'.
Re: Code that copy's multiple times a $ from a document
by Laurent_R (Canon) on May 21, 2014 at 17:48 UTC
    I did not quite understand what you need to do with your data (your example does not seem to correspond with your textual description), but to get the values you need into variables, you might use this:
    #!/usr/bin/perl\ use strict; use warnings; open my $FH "<", "filename.txt" or die $!; while (my $line = <$FH>) { my ($id, number, $sequence) = split /\s+/, $line; # ... }
      i just need to copy the sequences what i mean by that i have a sequence and before it a number. this number corresponds to how many times i need the sequence to be copied. again an example but whit fruits because its easier
      2 apples i need it to write 1-1 -> apple 1-2 -> apple
      and then do the same whit other fruits in the list and save them in a txt document
        use strict; use warnings; my $sc = 1; while (<DATA>) { my (undef,$repeat,$seq) = split /\s+/,$_; my $i = 1; while ($repeat--) { printf "%d-%d %s\n",$sc,$i++,$seq; } $sc += 1; } __DATA__ 1 2 tt 2 3 aa 3 1 dd

        Output

        1-1 tt 1-2 tt 2-1 aa 2-2 aa 2-3 aa 3-1 dd
        1 Peter 4:10
        #!perl use strict; use warnings; open FH, '<', 'filename.txt' or die $!; open FHWRITE, '>>','results.txt' or die $!; my $count; while (my $line = <FH>){ print $line; my ($i,$repeat,$seq) = split /\s+/, $line; print FHWRITE "$i-$_ $seq\n" for (1..$repeat); $count += $repeat; } print "\n$count records written to results.txt\n";
        poj
Re: Code that copy's multiple times a $ from a document
by 2teez (Vicar) on May 21, 2014 at 21:53 UTC

    Using GotToBTru dataset, one can simply do this:

    use warnings; use strict; while (<DATA>) { my $val = [split]; print map { sprintf "%d - %d %s\n" => $val->[0], $_, $val->[2] } +( 1 .. $val->[1] ); } __DATA__ 1 2 tt 2 3 aa 3 1 dd
    Output
    1 - 1 tt 1 - 2 tt 2 - 1 aa 2 - 2 aa 2 - 3 aa 3 - 1 dd

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me