# NOTE: if no file is given then STDIN is used :-)
#
if(defined($file)){
open(GFF,"<$file")||die"Could not open $file for reading";
}
else{
open(GFF,"-")||die"Could not open STDIN for reading";;
}
my$dbi=dbi();
my$name_sql='select s.seq_region_id, s.name from seq_region s, seq_region_attrib sra, attrib_type at where at.attrib_type_id = sra.attrib_type_id and at.name like "top_level" and s.seq_region_id = sra.seq_region_id';
my$name_sth=$dbi->prepare($name_sql);
$name_sth->execute;
my($seq_region_id,$name);
my%name_to_seq_region_id;
$name_sth->bind_columns(\$seq_region_id,\$name);
while($name_sth->fetch()){
$name_to_seq_region_id{$name}=$seq_region_id;
}
$name_sth->finish;
my%stable_id_to_dbid;# use for gene,transcripts and exons
foreachmy$type(qw(gene transcript exon)){
my$sth=$dbi->prepare("SELECT ".$type."_id, stable_id FROM ".$type."_stable_id");
$sth->execute;
my($stable_id,$dbid);
$sth->bind_columns(\$dbid,\$stable_id);
while($sth->fetch){
$stable_id_to_dbid{$stable_id}=$dbid;
}
$sth->finish;
}
my%exon_to_transcripts;
my$et_sth=$dbi->prepare("select exon_id, transcript_id from exon_transcript");
$et_sth->execute();
my($e_id,$t_id);
$et_sth->bind_columns(\$e_id,\$t_id);
while($et_sth->fetch){
if(!defined($e_id)){
print"no exon_id?? $t_id\n";
next;
}
if(defined($exon_to_transcripts{$e_id})){
push@{$exon_to_transcripts{$e_id}},$t_id;
}
else{
$exon_to_transcripts{$e_id}=[$t_id];
}
}
$et_sth->finish;
# Remove the previous data
my$sth=$dbi->prepare("delete from splicing_event");
$sth->execute||die"Could not delete entries from table splicing_event";
$sth=$dbi->prepare("delete from splicing_event_feature");
$sth->execute||die"Could not delete entries from table splicing_event_feature";
$sth=$dbi->prepare("delete from splicing_transcript_pair");
$sth->execute||die"Could not delete entries from table splicing_transcript_pair";