8 John Binns <zombieite@gmail.com>
12 Non-object-oriented tools for dealing with accession data.
14 =head2 all_accessions_extra_verbose
16 print CXGN::Accession::Tools::all_accessions_extra_verbose();
18 =head2 partial_name_to_ids
20 #get all accession ids for accessions with name like...
21 my @accession_ids=CXGN::Accession::Tools::partial_name_to_ids('LA7');
23 =head2 insert_accession
25 CXGN::Accession::Tools::insert_accession($dbh,$accname,$species,$common_name);
30 use CXGN
::DB
::Connection
;
33 package CXGN
::Accession
::Tools
;
39 my $accession_query=$dbh->prepare('select accession_id from accession');
40 $accession_query->execute();
41 while(my ($accession_id) = $accession_query->fetchrow_array())
43 #warn"$accession_id\n";
45 $hash{accession_id
}=$accession_id;
46 my $accession=CXGN
::Accession
->new($dbh,$accession_id);
47 $hash{verbose_name
}=$accession->verbose_name();
48 #warn"$hash{verbose_name}\n";
49 push(@accessions,\
%hash);
54 sub partial_name_to_ids
56 my($dbh,$partial_name)=@_;
57 $partial_name=$dbh->quote('%'.$partial_name.'%');
58 my $query="select accession_id from accession_names where accession_name ilike $partial_name";
59 my $id_query=$dbh->prepare($query);
62 while(my($id)=$id_query->fetchrow_array()){push(@ids,$id);}
66 sub insert_accession
{
68 my ($dbh, $accname, $species, $common) = @_;
70 # does the common name exist? if not, insert it.
72 ($common_id) = $dbh->selectrow_array("SELECT common_name_id FROM common_name WHERE common_name.common_name ilike '$common'");
75 $dbh->do("INSERT INTO common_name (common_name) VALUES ('$common')");
76 $common_id = $dbh->last_insert_id('common_name','sgn');
77 warn "inserting $common\n";
80 warn "using $common ($common_id)\n";
82 # does the species exist? if not, insert it.
84 ($organism_id) = $dbh->selectrow_array("SELECT organism_id FROM organism WHERE organism_name ilike '$species\%'");
87 $dbh->do("INSERT INTO organism (organism_name) VALUES ('$species')");
88 $organism_id = $dbh->last_insert_id('organism','sgn');
90 warn "inserting $species\n";
93 warn "using $species ($organism_id)\n";
95 # does there exist an accession with this name? If so, do nothing
97 my $accession_name_id;
98 ($accession_name_id) = $dbh->selectrow_array("SELECT accession_name_id FROM accession_names WHERE accession_name ILIKE '$accname'");
100 if($accession_name_id){
102 # the accession is already in there, so no problem!
103 warn "found $accname ($accession_name_id)\n";
107 # If not, insert the name and the accession.
110 $dbh->do("INSERT INTO accession_names (accession_name) VALUES ('$accname')");
111 $accession_name_id = $dbh->last_insert_id('accession_names','sgn');
112 warn "using $accname ($accession_name_id)\n";
113 $dbh->do("INSERT INTO accession (organism_id, accession_name_id) VALUES ($organism_id, $accession_name_id)");
114 my $accession_id = $dbh->last_insert_id('accession','sgn');
115 $dbh->do("UPDATE accession_names SET accession_id = $accession_id WHERE accession_name_id = $accession_name_id");