#!/usr/bin/perl

use strict;
use DBI;
use FileHandle;
$|=1;
my $out = shift;
$out ||= "ara_prefix_info.txt";
open(WF, ">$out") or die("Cannot write to $out");
WF->autoflush(1);
my $HOST = "scopolamine";
my $DBNAME = "tair7";
my $DBUSER = "web_usr";
my $DBPASS = "";

print "\nEnter password for user '$DBUSER': ";
system("stty -echo");
my $pass = <STDIN>;
chomp $pass;
$DBPASS = $pass;
system("stty echo");

my $dbh = DBI->connect("dbi:Pg:dbname=$DBNAME;host=$HOST", $DBUSER, $DBPASS);

my $sth = $dbh->prepare("SELECT COUNT(*) FROM ara_annotation WHERE locus LIKE ?");

foreach my $chrom (qw/ 1 2 3 4 5 M C /){
	print "\nProcessing chromosome $chrom";
	for(my $area = 0; $area < 1000; $area++){
		if($area%100==0){ print "." }
		my $match = "AT" . $chrom . "G" . zeropad($area);
		$sth->execute($match . "%") 
			or print "\n$match: " . $dbh->errstr;
		my ($count) = $sth->fetchrow_array();		
		$count||=0;	
		print WF "$match\t$count\n";
	}
}
close WF;
print "\n";
sub zeropad {
	my $num = shift;
	if($num < 10) { return "00$num";}
	if($num < 100) { return "0$num";}
	if($num < 1000) { return "$num";}
	return;
}
