cool has asked for the wisdom of the Perl Monks concerning the following question:
Recently I have started working with SQL and so with DBI. I have a few small queries. I have given the code, input file and output.
It would be great if someone can spare time for these questions.1- Why While loop over fetchrow_array is running one extra iteration (?, is this the actual prob)
2- Is this a good method to check 'NULL'? or there is some better alternative available.3- For firing different queries, is the way used in code appropriate? Or there is another method.
Thank you in advance (further will be given as rep ++ :-))Database relationuse strict; use DBI; print "Please enter the Nucleotide position \n"; my $in=<STDIN>; chomp $in; my $flankingregion=3000; my $startposition=$in-$flankingregion; my $endposition=$in+$flankingregion; my $dbh = DBI->connect('DBI:Pg:dbname=trial') or die "Couldn't open database: $DBI::errstr; stopped"; my $sth = $dbh->prepare(<<End_SQL) or die "Couldn't prepare statement: +$DBI::errstr; stopped"; SELECT * FROM wholegene where $startposition<=gstart and $endpositio +n>=gend; End_SQL $sth->execute() or die "Couldn't execute statement: $DBI::errstr; stop +ped"; my @midstore; my $midflag=1; { my $ctrmid=0; while ( ($midstore[$ctrmid][0], $midstore[$ctrmid][1], $midstore[$ct +rmid][2],$midstore[$ctrmid][3],$midstore[$ctrmid][4])= my @arr = $st +h->fetchrow_array() ) { $midflag=2; my ($field1, $field2, $field3,$field4,$field5) =@arr; ++$ctrmid; print STDOUT "Field 1: $field1 Field 2: $field2 Field 3: $ +field3 Field 4:$field4 Field5: $field5\n"; } } if ($midflag==1){print "NULL\n";} print "#################################$startposition\t:: $endpositio +n\n"; my $sth1 = $dbh->prepare(<<End_SQL) or die "Couldn't prepare statement +:$DBI::errstr; stopped"; SELECT * FROM wholegene where gstart<=$startposition and $startposit +ion<=gend; End_SQL my $startflag=1; my @startstore; $sth1->execute() or die "Couldn't execute statement: $DBI::errstr; sto +pped"; { my $ctrstart=0; while (($startstore[$ctrstart][0],$startstore[$ctrstart][1],$startst +ore[$ctrstart][2],$startstore[$ctrstart][3],$startstore[$ctrstart][4] +)= my @arr = $sth1->fetchrow_array() ) { $startflag=2; + my ($field1, $field2, $field3,$field4,$field5) = @arr; print STDOUT "Field 1: $field1 Field 2: $field2 Field 3: $field3 F +ield 4:$field4 Field5: $field5\n"; ++$ctrstart; } } if ($startflag==1){print "NULL start\n";} print "#################################$startposition\t:: $endpositio +n\n"; my $sth2 = $dbh->prepare(<<End_SQL) or die "Couldn't prepare statement +:$DBI::errstr; stopped"; SELECT * FROM wholegene where gstart<=$endposition and $endposition< +=gend; End_SQL my $endflag=1; my @endstore; $sth2->execute() or die "Couldn't execute statement: $DBI::errstr; sto +pped"; { my $ctrend=0; while (($endstore[$ctrend][0],$endstore[$ctrend][1],$endstore[$ctren +d][2],$endstore[$ctrend][3],$endstore[$ctrend][4]= my @arr)= $sth2->f +etchrow_array() ) { my ($field1, $field2, $field3,$field4,$field5)=@arr; $endflag=2; print STDOUT "Field 1: $field1 Field 2: $field2 Field 3: $field3 F +ield 4:$field4 Field5: $field5\n"; ++$ctrend; } } if ($endflag==1){print "NULL end\n";} $dbh->disconnect(); { my $x=0; foreach (@midstore) { print "\nMID POINT@@@@@@@@@@@@@@\n$midstore[$x][0]\t$midstore[$x][1 +]\t$midstore[$x][2]\t$midstore[$x][3]\t$midstore[$x][4]\n"; $x++; } } { my $x=0; foreach (@startstore) { print "\nSTARTING PT@@@@@@@@@@@@\n$startstore[$x][0]\t$startstore[ +$x][1]\t$startstore[$x][2]\t$startstore[$x][3]\t$startstore[$x][4]\n" +; $x++; } } { my $x=0; foreach (@endstore) { print "\nEND POINT@@@@@@@@@@@@@@\n$endstore[$x][0]\t$endstore[$x][ +1]\t$endstore[$x][2]\t$endstore[$x][3]\t$endstore[$x][4]\n"; ++$x; } } my $y=scalar(@midstore); print "\n$y";
Outputgeneid | genename | gstart | gend | strand ----------+----------+---------+---------+-------- EG11277 | thrL | 190 | 255 | + EG10998 | thrA | 337 | 2799 | + EG10999 | thrB | 2801 | 3733 | + EG11000 | thrC | 3734 | 5020 | + G6081 | yaaX | 5234 | 5530 | + EG10011 | yaaA | 5683 | 6459 | - EG11555 | yaaJ | 6529 | 7959 | - EG11556 | talB | 8238 | 9191 | + EG11511 | mog | 9306 | 9893 | + EG11512 | yaaH | 9928 | 10494 | - EG11509 | htgA | 10830 | 11315 | + G6082 | yaaW | 10643 | 11356 | - G8202 | yaaI | 11382 | 11786 | - EG10241 | dnaK | 12163 | 14079 | + G0-8893 | tpke11 | 14080 | 14168 | + EG10240 | dnaJ | 14168 | 15298 | + G6083 | insL-1 | 15445 | 16557 | + G0-9563 | hokC | 16751 | 16903 | - EG10373 | mokC | 16751 | 16960 | - G0-9581 | sokC | 16952 | 17006 | + EG10652 | nhaA | 17489 | 18655 | + EG11078 | nhaR | 18715 | 19620 | + G6085 | insB-1 | 19811 | 20314 | - G6086 | insA-1 | 20233 | 20508 | - G0-10535 | insAB-1 | 19810 | 20508 | - EG10919 | rpsT | 20815 | 21078 | -
Please enter the Nucleotide position 4000 Field 1: EG10999 Field 2: thrB Field 3: 2801 Field 4:3733 Field5: + Field 1: EG11000 Field 2: thrC Field 3: 3734 Field 4:5020 Field5: + Field 1: G6081 Field 2: yaaX Field 3: 5234 Field 4:5530 Field5: + Field 1: EG10011 Field 2: yaaA Field 3: 5683 Field 4:6459 Field5: - #################################1000 :: 7000 Field 1: EG10998 Field 2: thrA Field 3: 337 Field 4:2799 Field5: + #################################1000 :: 7000 Field 1: Field 2: Field 3: Field 4: Field5: MID POINT@@@@@@@@@@@@@@ EG10999 thrB 2801 3733 + MID POINT@@@@@@@@@@@@@@ EG11000 thrC 3734 5020 + MID POINT@@@@@@@@@@@@@@ G6081 yaaX 5234 5530 + MID POINT@@@@@@@@@@@@@@ EG10011 yaaA 5683 6459 - MID POINT@@@@@@@@@@@@@@ STARTING PT@@@@@@@@@@@@ EG10998 thrA 337 2799 + STARTING PT@@@@@@@@@@@@ END POINT@@@@@@@@@@@@@@ EG11555 yaaJ 6529 7959 - END POINT@@@@@@@@@@@@@@ 5
|
|---|