| 1 | #!/usr/local/bin/perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Bio::GMOD::Config; | ||
| 6 | use Bio::GMOD::DB::Config; | ||
| 7 | use Getopt::Long; | ||
| 8 | |||
| 9 | my ($DBPROFILE,); | ||
| 10 | |||
| 11 | GetOptions( | ||
| 12 | 'dbprofile=s' => \$DBPROFILE, | ||
| 13 | ) or ( system( 'pod2text', $0 ), exit -1 ); | ||
| 14 | |||
| 15 | my $gmod_conf = Bio::GMOD::Config->new(); | ||
| 16 | my $db_conf = Bio::GMOD::DB::Config->new($gmod_conf, $DBPROFILE); | ||
| 17 | |||
| 18 | my $dbh = $db_conf->dbh; | ||
| 19 | my $schema = $db_conf->schema; | ||
| 20 | $schema ||= 'public'; | ||
| 21 | |||
| 22 | create_table($dbh, $schema); | ||
| 23 | create_functions($dbh); | ||
| 24 | populate_table($dbh); | ||
| 25 | |||
| 26 | exit(0); | ||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | sub populate_table { | ||
| 31 | my $dbh = shift; | ||
| 32 | |||
| 33 | $dbh->do("SELECT populate_gff_interval_stats()") or die $dbh->errstr; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub create_table { | ||
| 37 | my $dbh = shift; | ||
| 38 | my $schema = shift; | ||
| 39 | my $table_def =<<END | ||
| 40 | CREATE TABLE gff_interval_stats ( | ||
| 41 | typeid varchar(1024) not null, | ||
| 42 | srcfeature_id integer not null, | ||
| 43 | bin integer not null, | ||
| 44 | cum_count integer not null | ||
| 45 | ); | ||
| 46 | CREATE UNIQUE INDEX gff_interval_stats_idx1 ON gff_interval_stats(typeid,srcfeature_id,bin); | ||
| 47 | END | ||
| 48 | ; | ||
| 49 | |||
| 50 | #determine if the table already exists | ||
| 51 | my @table_exists = $dbh->selectrow_array("SELECT * FROM pg_tables WHERE tablename | ||
| 52 | = 'gff_interval_stats' AND schemaname = '$schema'"); | ||
| 53 | if (!scalar(@table_exists)) { | ||
| 54 | $dbh->do($table_def) or die $dbh->errstr; | ||
| 55 | } | ||
| 56 | else { #empty the table out | ||
| 57 | $dbh->do("DELETE FROM gff_interval_stats") or die $dbh->errstr; | ||
| 58 | } | ||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub create_functions { | ||
| 63 | my $dbh = shift; | ||
| 64 | |||
| 65 | my $load_bins =<<'END' | ||
| 66 | CREATE OR REPLACE FUNCTION gff_load_bins (varchar, int, int) RETURNS void AS $$ | ||
| 67 | DECLARE | ||
| 68 | current_type ALIAS FOR $1; | ||
| 69 | current_srcf ALIAS FOR $2; | ||
| 70 | current_bin ALIAS FOR $3; | ||
| 71 | |||
| 72 | i int; | ||
| 73 | cumcount int; | ||
| 74 | result gff_interval_stats%ROWTYPE; | ||
| 75 | BEGIN | ||
| 76 | cumcount = 0; | ||
| 77 | |||
| 78 | FOR result IN SELECT * FROM gff_interval_stats WHERE typeid is not null | ||
| 79 | AND typeid = current_type | ||
| 80 | AND srcfeature_id = current_srcf | ||
| 81 | AND bin <= current_bin | ||
| 82 | order by bin LOOP | ||
| 83 | |||
| 84 | cumcount = result.cum_count + cumcount; | ||
| 85 | UPDATE gff_interval_stats SET cum_count = cumcount | ||
| 86 | WHERE typeid = current_type | ||
| 87 | AND srcfeature_id = current_srcf | ||
| 88 | AND bin = result.bin; | ||
| 89 | END LOOP; | ||
| 90 | END; | ||
| 91 | $$ | ||
| 92 | LANGUAGE 'plpgsql'; | ||
| 93 | |||
| 94 | CREATE OR REPLACE FUNCTION gff_load_bins (varchar, int) RETURNS void AS $$ | ||
| 95 | DECLARE | ||
| 96 | current_type ALIAS FOR $1; | ||
| 97 | current_srcf ALIAS FOR $2; | ||
| 98 | |||
| 99 | i int; | ||
| 100 | cumcount int; | ||
| 101 | result gff_interval_stats%ROWTYPE; | ||
| 102 | BEGIN | ||
| 103 | cumcount = 0; | ||
| 104 | |||
| 105 | FOR result IN SELECT * FROM gff_interval_stats WHERE typeid is not null | ||
| 106 | AND typeid = current_type | ||
| 107 | AND srcfeature_id = current_srcf | ||
| 108 | order by bin LOOP | ||
| 109 | |||
| 110 | cumcount = result.cum_count + cumcount; | ||
| 111 | |||
| 112 | UPDATE gff_interval_stats SET cum_count = cumcount | ||
| 113 | WHERE typeid = current_type | ||
| 114 | AND srcfeature_id = current_srcf | ||
| 115 | AND bin = result.bin; | ||
| 116 | END LOOP; | ||
| 117 | END; | ||
| 118 | $$ | ||
| 119 | LANGUAGE 'plpgsql'; | ||
| 120 | END | ||
| 121 | ; | ||
| 122 | |||
| 123 | my $populate_gff_interval_stats =<<'END' | ||
| 124 | CREATE OR REPLACE FUNCTION populate_gff_interval_stats() RETURNS void AS $$ | ||
| 125 | DECLARE | ||
| 126 | binsize int; | ||
| 127 | resrow record; | ||
| 128 | |||
| 129 | current_bin int; | ||
| 130 | last_bin int; | ||
| 131 | current_srcf int; | ||
| 132 | current_type varchar; | ||
| 133 | ibin int; | ||
| 134 | |||
| 135 | i int; | ||
| 136 | tempvalue int; | ||
| 137 | BEGIN | ||
| 138 | binsize = 1000; | ||
| 139 | current_bin = -1; | ||
| 140 | current_srcf = -1; | ||
| 141 | current_type = ''; | ||
| 142 | |||
| 143 | |||
| 144 | FOR resrow IN SELECT cvterm.name ||':'|| dbxref.accession as typeid, | ||
| 145 | fl.srcfeature_id, fl.fmin, fl.fmax | ||
| 146 | FROM featureloc fl left join feature f using (feature_id) | ||
| 147 | left join cvterm on (f.type_id = cvterm.cvterm_id) | ||
| 148 | left join feature_dbxref fd on (f.feature_id = fd.feature_id) | ||
| 149 | left join dbxref on (fd.dbxref_id = dbxref.dbxref_id and dbxref.db_id = 2) | ||
| 150 | ORDER BY typeid, fl.srcfeature_id, fl.fmin LOOP | ||
| 151 | |||
| 152 | ibin = resrow.fmin/binsize; | ||
| 153 | |||
| 154 | IF (ibin != current_bin) THEN | ||
| 155 | IF ((resrow.srcfeature_id != current_srcf | ||
| 156 | OR resrow.typeid != current_type) AND current_srcf > 0) THEN | ||
| 157 | |||
| 158 | PERFORM gff_load_bins(current_type,current_srcf); | ||
| 159 | |||
| 160 | ELSE | ||
| 161 | --I don't think any thing needs to happen here | ||
| 162 | |||
| 163 | END IF; | ||
| 164 | END IF; | ||
| 165 | |||
| 166 | current_bin = ibin; | ||
| 167 | current_type = resrow.typeid; | ||
| 168 | current_srcf = resrow.srcfeature_id; | ||
| 169 | |||
| 170 | last_bin = (resrow.fmax-1)/binsize; | ||
| 171 | FOR i IN ibin..last_bin LOOP | ||
| 172 | SELECT INTO tempvalue COALESCE (cum_count,0) FROM gff_interval_stats | ||
| 173 | WHERE bin = i | ||
| 174 | AND srcfeature_id = resrow.srcfeature_id | ||
| 175 | AND typeid = resrow.typeid; | ||
| 176 | IF (tempvalue > 0) THEN | ||
| 177 | UPDATE gff_interval_stats SET cum_count = tempvalue + 1 | ||
| 178 | WHERE bin = i | ||
| 179 | AND srcfeature_id = resrow.srcfeature_id | ||
| 180 | AND typeid = resrow.typeid; | ||
| 181 | ELSEIF (resrow.typeid IS NOT NULL) THEN | ||
| 182 | INSERT INTO gff_interval_stats (typeid,srcfeature_id,bin,cum_count) | ||
| 183 | VALUES (resrow.typeid,resrow.srcfeature_id,i,1); | ||
| 184 | END IF; | ||
| 185 | END LOOP; | ||
| 186 | |||
| 187 | END LOOP; | ||
| 188 | |||
| 189 | PERFORM gff_load_bins(current_type,current_srcf); | ||
| 190 | |||
| 191 | END; | ||
| 192 | $$ | ||
| 193 | LANGUAGE 'plpgsql'; | ||
| 194 | END | ||
| 195 | ; | ||
| 196 | #check to see if the functions exist already | ||
| 197 | #if it does, don't do anything | ||
| 198 | # my @func_exists = $dbh->selectrow_array("SELECT * FROM pg_proc WHERE proname | ||
| 199 | # = 'populate_gff_interval_stats'"); | ||
| 200 | # if (!scalar(@func_exists)) { | ||
| 201 | $dbh->do($load_bins) or die $dbh->errstr; | ||
| 202 | $dbh->do($populate_gff_interval_stats) or die $dbh->errstr; | ||
| 203 | # } | ||
| 204 | return; | ||
| 205 | } |