Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Selecting a random number, and back calculating to chromosome and contig!

by Sameet (Beadle)
on Apr 30, 2004 at 12:06 UTC ( [id://349376]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,
I have written a really long code, with a lot of repeatition, but using  use strict; use warnings;, many errors are generated. If these are commented out, there are no errors but the output is not what i am expecting. All help is highly solicitated. Following is the code. I have written it right now only for chromosome 1, but i need to do it for all the 23 chromosomes

I am eagerly awaiting all the suggestions!Thank you for your answers. Acutally i did ahve a typo, but due to lack to readibility i missed it altogether! Now the code is working fine. I want to finally download a specific sequnece generated by the random number generator from the NCBI website or Human Genome Resource website!
Sameet

#!/usr/bin/perl use strict; use warnings; my $number = 50; my $chromosome; my $contig; my $start; my $end; my @sequences; my $accession; my @numbers; my %chromosome_contig; # This part i got from the PerlMonks my $max = 3.34166e+09; for ( 1 .. $number ) { my $rnd = ""; my $div = "1"; for ( 0 .. length($max)/3 ) { $rnd .= sprintf( "%03d", int( rand( 1000 ) ) ); $div .= "000"; } my $return = int( $max * $rnd / $div ); push (@numbers, $return); print "@numbers"; # For later dubugging print "\n"; # print "$return\n"; } # This part onwards is my code foreach my $numbers (@numbers) { if ($numbers < 246047941){ # each number is lenght of the given ch +romosome or contig my($contig) = ''; $chromosome = 1 ; print "Chromosome $chromosome\n"; if ($numbers < 167280 ) { my $contig = 'NT_077402.1'; print "Chromosome $chromosome, contig my $contig\n"; } elsif ($numbers < 217280){ my $contig = 'gap'; print "Chromosome $chromosome but sequence in \"Gap\"\n"; } elsif ($numbers < 257582){ my $contig = 'NT_077911.1'; print "Chromosome $chromosome, contig my $contig\n"; } elsif ($numbers < 357582){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig\"\n"; } elsif ($numbers < 511231){ my $contig = 'NT_077912.1'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 561231){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; }elsif ($numbers < 852347){ my $contig = 'NT_034471.3'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 902347){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.22593e+06){ my $contig = 'NT_077913.2'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.27593e+06){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.46795e+06){ my $contig = 'NT_077914.2'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.56795e+06){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.84175e+06){ my $contig = 'NT_077915.1'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.89175e+06){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 2.42621e+06){ my $contig = 'NT_004350.16'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 2.47621e+06){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 3.60656e+06){ my $contig = 'NT_004321.15'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 3.66656e+06){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 5.09672e+06){ my $contig = 'NT_004547.16'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 5.15672e+06){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 5.53249e+06){ my $contig = 'NT_077919.2'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 5.58249e+06){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.27009e+07){ my $contig = 'NT_021937.16'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.27509e+07){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.29355e+07){ my $contig = 'NT_077382.2'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.29855e+07){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.57673e+07){ my $contig = 'NT_004873.15'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.58173e+07){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.65067e+07){ my $contig = 'NT_077920.2'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.65567e+07){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.73573e+07){ my $contig = 'NT_030584.10'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.74073e+07){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 2.24061e+07){ my $contig = 'NT_077921.1'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 2.24561e+07){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 2.74548e+07){ my $contig = 'NT_004610.16'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 2.75048e+07){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 2.88694e+07){ my $contig = 'NT_077383.3'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 2.89194e+07){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 2.90985e+07){ my $contig = 'NT_077922.2'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 2.91485e+07){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 2.92333e+07){ my $contig = 'NT_077384.1'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 2.92833e+07){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 3.2138e+07){ my $contig = 'NT_037485.3'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 3.2288e+07){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 3.33489e+07){ my $contig = 'NT_004538.15'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 3.33539e+07){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 3.84657e+07){ my $contig = 'NT_004511.16'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 3.85157e+07){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 3.88138e+07){ my $contig = 'NT_079482.1'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 3.89138e+07){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 7.57044e+07){ my $contig = 'NT_032977.6'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 7.57094e+07){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 8.86492e+07){ my $contig = 'NT_026943.13'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 8.86992e+07){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 9.23759e+07){ my $contig = 'NT_004686.16'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 9.23809e+07){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.04335e+08){ my $contig = 'NT_028050.13'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.04385e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.0936e+08){ my $contig = 'NT_029860.11'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.0941e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.16488e+08){ my $contig = 'NT_019273.16'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.16538e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.2115e+08){ my $contig = 'NT_004754.15'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.4144e+08){ my $contig = 'GAP'; print "Chromosome $chromosome, contig \"my $contig\"\n"; }elsif ($numbers < 1.41636e+08){ my $contig = 'NT_077387.2'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.41686e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.41852e+08){ my $contig = 'NT_022052.2'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.41902e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.42081e+08){ my $contig = 'NT_077988.2'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.42131e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.42242e+08){ my $contig = 'NT_022071.12'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.42292e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.42494e+08){ my $contig = 'NT_077930.1'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.42644e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.43046e+08){ my $contig = 'NT_077389.2'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.43096e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.43222e+08){ my $contig = 'NT_077931.2'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.43272e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.434e+08){ my $contig = 'NT_077932.2'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.4345e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.4362e+08){ my $contig = 'NT_077933.1'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.4367e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.44669e+08){ my $contig = 'NT_004434.16'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.44719e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.45001e+08){ my $contig = 'NT_034398.4'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.45051e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.4604e+08){ my $contig = 'NT_034400.2'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.4649e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.46831e+08){ my $contig = 'NT_077936.2'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.46981e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.47153e+08){ my $contig = 'NT_079483.1'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.47203e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.47424e+08){ my $contig = 'NT_034401.5'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.47474e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.47783e+08){ my $contig = 'NT_034403.3'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.47833e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.51824e+08){ my $contig = 'NT_032962.5'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.51874e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.59739e+08){ my $contig = 'NT_079484.1'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.59789e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.69248e+08){ my $contig = 'NT_004668.16'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.69298e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.8699e+08){ my $contig = 'NT_004487.16'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.8704e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.88508e+08){ my $contig = 'NT_004671.15'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.88558e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.90908e+08){ my $contig = 'NT_034410.5'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.91358e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 1.91378e+08){ my $contig = 'NT_079485.1'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 1.91428e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 2.08083e+08){ my $contig = 'NT_021877.16'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 2.08133e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 2.08556e+08){ my $contig = 'NT_077939.1'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 2.08606e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 2.14861e+08){ my $contig = 'NT_004559.11'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 2.14911e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 2.17166e+08){ my $contig = 'NT_021973.16'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 2.17216e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 2.20041e+08){ my $contig = 'NT_004433.16'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 2.20091e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 2.32545e+08){ my $contig = 'NT_004836.15'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 2.3255e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 2.3256e+08){ my $contig = 'NT_077941.1'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 2.32565e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 2.3338e+08){ my $contig = 'NT_031730.8'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 2.3343e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } elsif ($numbers < 2.33634e+08){ my $contig = 'NT_077390.2'; print "Chromosome $chromosome, contig my $cont +ig\n"; } elsif ($numbers < 2.33684e+08){ my $contig = "gap"; print "Chromosome $chromosome, contig \"$conig +\"\n"; } else { my $contig = 'NT_032968.6'; print "Chromosome $chromosome, contig my $cont +ig\n"; } } elsif ( $numbers < 4.89464e+08 ){ print $numbers; print "belongs to chromosome 2\n"; } elsif ( $numbers < 6.90124e+08) { print "Belongs to Chromosome 3\n"; } elsif ( $numbers < 8.81846e+08) { print "Belongs to Chromosome4\n"; }elsif ( $numbers < 1.06286e+09) { print "Belongs to Chromosome 5\n"; } elsif ( $numbers < 1.23377e+09) { print "Belongs to Chromosome 6\n"; } elsif ( $numbers < 1.39232e+09) { print "Belongs to chromosome 7\n"; } elsif ( $numbers < 1.53863e+09) { print "Belongs to chromosome 8\n"; } elsif ( $numbers < 1.675e+09) { print "Belongs to Chromosome 9\n"; } elsif ( $numbers < 1.81004e+09) { print "Belongs to Chromosome 10\n"; } elsif ( $numbers < 1.94452e+09) { print "Belongs to Chromosome 11\n"; } elsif ( $numbers < 2.07654e+09) { print "Belongs to Chromosome 12\n"; } elsif ( $numbers < 2.18957e+09) { print "Belongs to Chromosome 13\n"; } elsif ( $numbers < 2.29483e+09) { print "Belongs to Chromosome 14\n"; } elsif ( $numbers < 2.39508e+09) { print "Belongs to Chromosome 15\n"; } elsif ( $numbers < 2.48512e+092) { print "Belongs to Chromosome 16\n"; } elsif ( $numbers < 2.56686e+09) { print "Belongs to Chromosome 17\n"; } elsif ( $numbers < 2.64298e+09) { print "Belongs to Chromosome 18\n"; } elsif ( $numbers < 2.70679e+09) { print "Belongs to Chromosome 19\n"; } elsif ( $numbers < 2.77048e+09) { print "Belongs to Chromosome 20\n"; } elsif ( $numbers < 2.81745e+09) { print "Belongs to Chromosme 21\n"; } elsif ( $numbers < 2.86683e+09) { print "Belongs to Chromosome 22\n"; } elsif ( $numbers < 3.02052e+09) { print "Belongs to Chromosome X\n"; } elsif ( $numbers < 3.07081e+09){ print "Belongs to Chromosome Y\n"; } }

I am eagerly awaiting all the suggestions!Thank you for your answers. Acutally i did ahve a typo, but due to lack to readibility i missed it altogether! Now the code is working fine. I want to finally download a specific sequnece generated by the random number generator from the NCBI website or Human Genome Resource website!
Sameet

Replies are listed 'Best First'.
Re: Selecting a random number, and back calculating to chromosome and contig!
by thor (Priest) on Apr 30, 2004 at 12:32 UTC
    You're a victim of typos. When I ran your code through perl -c, I got a log of messages like:
    Global symbol "$conig" requires explicit package name at 349376.pl line 555.
    You spell that variable as "$contig" in some places and "$conig" in others. However, that's not the entire scope of your problem. You declare $contig as my in different scopes, so I'd expect that your code wouldn't run as you expect. A short demonstration of what I'm talking about:
    my $contig = 3; my $foo = 6; if ($foo < 8) { my $contig = 5; } print "contig = $contig\n"; __END__ contig = 3

    As a matter of style, you should probably avoid the whole "if..elsif..elsif..elsif..." routine for this. As I'm sure you found out, it's very tedious to type. A more visually appealing (and easier to maintain) to do something like the following:

    my %master = ( 167280 => (chromosome => 1, contig => 'NT_077402.1'), 217280 => (chromosome => 1, contig => 'gap'), 257582 => (chromosome => 1, contig => 'NT_077911.1'), # etc ); NUMBER: foreach my $number (@numbers) { foreach my $key (sort { $a <=> $b } keys %master) { if ($number < $key) { my $chromosome = $master{$key}{chromosome}; my $contig = $master{$key}{contig}; #do whatever processing here; I'll just print print "chromosome = $chromosome, contig = $contig\n"; next NUMBER; } } }
    This style has the advantage of maintainability. Should you find an error in your processing logic (for example, if I found that I had misspelled 'chromosome' in my print statement'), you have exactly one place to fix it.

    Hope this helps,
    thor

    Update: I just thought of another reason to avoid the "if..elsif..." paradigm. Once, someone came to me for help in trying to run a tool that had such a thing in it. perl core dumped because the script was too large. I converted the solution to a hash, and it ran in no time at all.
      I will try to do this. Actually this hash business sort of tricky. i actually tried to avoid it!! but i can see that using hashes is probably the best option i have got. I will look into it and get back as soon as possible
      Thank you for all the help
      Sameet


      Update
      Hi,
      I did a lookup table as you had suggested but it still has become a huge file (now huge is subjective i know :-) ), but it is something like 650+ lines of code for the look-up hash itself. I will test it and again get back if i have more doubts or need more help.
      Thank you again for all the help!


      Update 2
      Acutally the file from which i am downloading these values come from a standard data format an html file, where length of each contig is given. What i am trying to do it is make those contigs into periods and then map the given random number where it would fall.
      . What i want to know are there any HTML parsers avialable, if yes, (I am sure they are but I am at a loss which one to use! :-( ), how to go about using them.
      Regards


      UPDATE3
      I have done what you had asked. But it gives me following error
      Argument "NT_077988.2" isn't numeric in sort at newhumangenomelookup.p +l line 736. Argument "contig" isn't numeric in sort at newhumangenomelookup.pl lin +e 736. Argument "NT_024498.12" isn't numeric in sort at newhumangenomelookup. +pl line 736. Argument "NT_030737.8" isn't numeric in sort at newhumangenomelookup.p +l line 736. Argument "NT_077627.2" isn't numeric in sort at newhumangenomelookup.p +l line 736. Argument "NT_079485.1" isn't numeric in sort at newhumangenomelookup.p +l line 736. Argument "NT_035608.1" isn't numeric in sort at newhumangenomelookup.p +l line 736. Argument "NT_077911.1" isn't numeric in sort at newhumangenomelookup.p +l line 736. Argument "NT_023736.16" isn't numeric in sort at newhumangenomelookup. +pl line 736. Argument "Y" isn't numeric in sort at newhumangenomelookup.pl line 736 +. Argument "NT_077931.2" isn't numeric in sort at newhumangenomelookup.p +l line 736. Argument "NT_011903.10" isn't numeric in sort at newhumangenomelookup. +pl line 736. Argument "NT_011295.10" isn't numeric in sort at newhumangenomelookup. +pl line 736.
      I have given here just a part of the error that i got as output. Can you give me some hints as to what is going wrong
      I am giving my new code here
      Sameet
        It looks like you wrote your if/else statements by cutting and pasting each statement, and then modifying each individual statement. While this may feel like real work, it's really not an efficient use of your time. If you ever wanted to modify this program, it would be very difficult to do because of its length. Developing with repetitive code is also prone to typos and other sorts of little errors, as you can see by the conig/contig typos that are in your code. By using a better data structure like thor proposes, you can avoid making minute modifications to repetitive pieces of code (like your if/else statement) and concentrate more on developing your program.

        Another idea that you may look into as you learn more is by representing your data and operations on this data in the form of objects. For a reference, check out this short example on OO programming in Perl.

        As you develop your code more and more, you may want to begin abstracting your useful code into subroutines. Here's an example that relies on thor's hash-based data structure shown above:

        sub print_contig_information { my $number = shift; foreach my $key (sort { $a <=> $b } keys %master) { if ($number < $key) { my $chromosome = $master{$key}{chromosome}; my $contig = $master{$key}{contig}; #do whatever processing here; I'll just print print "chromosome = $chromosome, contig = $contig\n"; } } }
        By modularizing your code every way, you avoid having to change the methodology of changing your code in many places every time you'd like to modify your program.

        Hope that this helps. :)

Re: Selecting a random number, and back calculating to chromosome and contig!
by BrowserUk (Patriarch) on Apr 30, 2004 at 12:43 UTC

    The first thing you should do is put the use strict and use warnings back and go through line by line and correct each of the many errors.

    For a start, try searching for "$conig" and correct all the places where you have mis-spelt "$contig".

    That said, why bother setting a variable to the value if the only thing you are going to do with that value is interpolate it into a string and print it out?

    The value "2.48512e+092" looks suspicious relative to the other values around it which are all of the order e+9?

    Once you ve corrected all those errors, you may get closer to your desired results, but asking for help before is unlikely to get anyone to do this work for you.

    This kind of mapping problem would lend itself neatly to some kind of table lookup, which would be easily implemented in perl and would be much more compact, easier to maintain and possibly more efficient, but the starting point is a table showing what number maps to what values.

    It would be possible to extract this from this behemoth of a if/elsif cascade, but you probably already have the table written down somewhere, so why shoudl we go through all that work to extract it.

    If you posted a simple text table showing the required mapping, then you would probably get a reply that converst that into an nicely implemented lookup table.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
Re: Selecting a random number, and back calculating to chromosome and contig!
by mutated (Monk) on Apr 30, 2004 at 13:42 UTC
    Have you concidered replacing your considerably large elsif structure with something like this?
    use strict; ... ... my contig; SWITCH: { if ($numbers < 561231) {$contig = 'whatever';last SWITCH;} if ($numbers < 700000) {$contig = 'whatevr1';last SWITCH;} if ($numbers < 720000) {$contig = 'whatevr2';last SWITCH;} .. }
    it will make your code much more readable and much easier to find the typos and context errors that other people have noted in your code.


    daN.
      Or a lookup table?
      my %table = ( 561231 => 'whatever', 700000 => 'whatevr2', ... ); my @thresh = sort { $a <=> $b } keys %table; $_ = pop(@thresh) while $thresh[0] < $numbers; my $contig = $table{$_};
      Even this can be improved if there's more than ~200 thresholds by using a binary search method instead. I wouldn't bother with that unless it proves to be a bottleneck, though.

      --
      [ e d @ h a l l e y . c c ]

Re: Selecting a random number, and back calculating to chromosome and contig!
by Anneq (Vicar) on Apr 30, 2004 at 12:29 UTC

    The errors/warnings you get when using strict and warnings are your clues as to what the problem is. Post them here for more rapid help with your problem. To get an even better response, describe what you were expecting the script to do.

    As well, when posts to this site are long, people generally surround the large chunks, code for example, with ‹readmore› tags.

    Good luck,

    Anne

Re: Selecting a random number, and back calculating to chromosome and contig!
by pbeckingham (Parson) on Apr 30, 2004 at 12:39 UTC

    I am also seeing the following, which to me looks like a copy/paste error, where the my is inadvertently included, and the quotes are omitted:

    print "Chromosome $chromosome, contig my $contig\n";
    I also cannot agree more with thor, who has given you an excellent suggestion on refactoring that code.

Re: Selecting a random number, and back calculating to chromosome and contig!
by freddo411 (Chaplain) on Apr 30, 2004 at 17:14 UTC
    I suggest you add a "read more" tag to your post, so that it would be digestable by mere mortals.

    Read up on it here: Approved PM Tags

    Thanks in advance for making PM a friendly site to use.

    -------------------------------------
    Nothing is too wonderful to be true
    -- Michael Faraday

Re: Selecting a random number, and back calculating to chromosome and contig!
by fluxion (Monk) on Apr 30, 2004 at 12:33 UTC
    I dont see any obvious errors, perhaps you should post a sample of the warnings/errors you're getting so others can narrow down the possiblities a bit.


    Roses are red, violets are blue. All my base, are belong to you.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://349376]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 07:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found