loader for solcap images
[phenome.git] / bin / loading_scripts / solcap / load_solcap_images.pl
blob8481eaf5fcebf3322894141361244b18134ab0dd
1 #!/usr/bin/perl
3 =head1 NAME
5 load_solcap_images.pl
7 =head1 SYNOPSYS
9 load_solcap_images.pl -D [ sandbox | cxgn | trial ] -H hostname -i dirname
11 =head1 DESCRIPTION
13 Loads images into the SGN database, using the SGN::Image framework.
14 Then link the loaded image with the relevant stock (see phenome.stock_image table)
16 Requires the following parameters:
18 =over 8
20 =item -D
22 a database parameter, which can either be "cxgn", "sandbox", or "trial". "cxgn" and "sandbox" will cause the script to connect to the respective databases; "trial" will connect to sandbox, but not perform any of the database modifications.
24 =item -H
26 host name
28 =item -i
30 a dirname that contains image filenames or subdirectories named after database accessions, containing one or more images (see option -d) .
32 =item -u
34 use name - from sgn_people.sp_person.
36 =item -l
38 location, as stored in stockprop (when plots are loaded with load_solcap_plots.pl)
40 =item -d
42 files are stored in sub directories named after database accessions
44 =item -e
46 image file extension . Defaults to 'jpg'
49 =item -t
51 trial mode . Nothing will be stored.
54 =back
56 The script will generate an error file, named like the filename supplied, with the extension .err.
58 =head1 AUTHOR(S)
60 Naama Menda (nm249@cornell.edu) September 2011.
62 =cut
64 use strict;
66 use CXGN::Metadata::Schema;
67 use CXGN::Metadata::Metadbdata;
68 use CXGN::DB::InsertDBH;
69 use SGN::Image;
70 use Bio::Chado::Schema;
71 use CXGN::People::Person;
72 use Carp qw /croak/;
74 use File::Basename;
75 use SGN::Context;
76 use Getopt::Std;
79 our ($opt_H, $opt_D, $opt_t, $opt_i, $opt_u, $opt_d, $opt_e, $opt_f, $opt_l);
80 getopts('H:D:u:i:e:f:tdl:');
82 my $dbhost = $opt_H;
83 my $dbname = $opt_D;
84 my $dirname = $opt_i;
85 my $sp_person = $opt_u;
86 my $location = $opt_l;
88 my $ext = $opt_e || 'jpg';
90 if (!$dbhost && !$dbname) {
91 print "dbhost = $dbhost , dbname = $dbname\n";
92 print "opt_t = $opt_t, opt_u = $opt_u, opt_d= $opt_d opt_i = $dirname\n";
93 usage();
96 if (!$dirname) { print "dirname = $dirname\n" ; usage(); }
98 my $dbh = CXGN::DB::InsertDBH->new( { dbhost=>$dbhost,
99 dbname=>$dbname,
100 } );
102 my $schema= Bio::Chado::Schema->connect( sub { $dbh->get_actual_dbh() } , { on_connect_do => ['SET search_path TO public;'] }
104 my $metadata_schema = CXGN::Metadata::Schema->connect(
105 sub { $dbh->get_actual_dbh },
106 { on_connect_do => ['SET search_path TO metadata;'] },
108 my $sp_person_id= CXGN::People::Person->get_person_by_username($dbh, $sp_person);
109 my %name2id = ();
112 my $ch = SGN::Context->new();
113 my $image_dir = $opt_f || $ch->get_conf("image_dir");
115 print "PLEASE VERIFY:\n";
116 print "Using dbhost: $dbhost. DB name: $dbname. \n";
117 print "Path to image is: $image_dir\n";
118 print "CONTINUE? ";
119 my $a = (<STDIN>);
120 if ($a !~ /[yY]/) { exit(); }
122 if (($dbname eq "sandbox") && ($image_dir !~ /sandbox/)) {
123 die "The image directory needs to be set to image_files_sandbox if running on rubisco/sandbox. Please change the image_dir parameter in SGN.conf\n\n";
125 if (($dbname eq "cxgn") && ($image_dir =~ /sandbox/)) {
126 warn "The image directory needs to be set to image_files when the script is running on the production database. Please change the image_dir parameter in SGN.conf\n\n";
129 my %image_hash = (); # used to retrieve images that are already loaded
130 my %connections = (); # keep track of object -- image connections that have already been made.
132 my $object_rs = $schema->resultset("Stock::Stock")->search( { } ) ;
133 while (my $object = $object_rs->next ) {
134 my $id = $object->stock_id;
135 my $name = $object->name;
136 $name2id{lc($name)} = $id;
139 # cache image chado object - image links to prevent reloading of the
140 # same data
142 print "Caching image-stock links...\n";
144 my $q = "SELECT * FROM phenome.stock_image";
145 my $sth = $dbh->prepare($q);
146 $sth->execute();
147 while ( my $hashref = $sth->fetchrow_hashref() ) {
148 my $image_id = $hashref->{image_id};
149 my $chado_table_id = $hashref->{stock_id}; ##### table specific
150 my $i = SGN::Image->new($dbh, $image_id);
151 my $original_filename = $i->get_original_filename();
152 $image_hash{$original_filename} = $i; # this doesn't have the file extension
153 $connections{$image_id."-".$chado_table_id}++;
156 open (ERR, ">load_solcap_images.err") || die "Can't open error file\n";
158 my @files = glob "$dirname/*.$ext";
159 @files = glob "$dirname/*" if $opt_d ;
160 my @sub_files;
162 my $new_image_count = 0;
165 foreach my $file (@files) {
166 eval {
167 chomp($file);
168 @sub_files = ($file);
169 @sub_files = glob "$file/*.$ext" if $opt_d;
171 my $object_name = basename($file, ".$ext" );
172 print "object_name = '".$object_name."' \n";
173 #$individual_name =~s/(W\d{3,4}).*\.JPG/$1/i if $individual_name =~m/^W\d{3}/;
174 #2009_oh_8902_fruit-t
175 # solcap images:
176 #my ($year, $place, $plot, undef) = split /_/ , $object_name;
177 #2009_ca_2138_fruit_c.jpg
178 my ($year, $place, $name, undef,undef) = split /_/ , $object_name;
180 #my $stock = $schema->resultset("Stock::Stock")->find( {
181 # stock_id => $name2id{ lc($name) } } );
182 my ($stock) = $schema->resultset("Stock::Stockprop")->search(
183 { value => $location, })->search_related('stock', { name=> $name } );
185 if (!$stock) {
186 warn "no stock found for plot # $name ! Skipping !!\n\n";
187 next();
190 foreach my $filename (@sub_files) {
191 chomp $filename;
192 print STDOUT "Processing file $file...\n";
193 print STDOUT "Loading $name, image $filename\n";
194 print ERR "Loading $name, image $filename\n";
195 my $image_id; # this will be set later, depending if the image is new or not
196 if (! -e $filename) {
197 warn "The specified file $filename does not exist! Skipping...\n";
198 next();
201 if (!exists($name2id{lc($name)})) {
202 message ("$name does not exist in the database...\n");
205 else {
206 print ERR "Adding $filename...\n";
207 if (exists($image_hash{$filename})) {
208 print ERR "$filename is already loaded into the database...\n";
209 $image_id = $image_hash{$filename}->get_image_id();
210 $connections{$image_id."-".$name2id{lc($name)}}++;
211 if ($connections{$image_id."-".$name2id{lc($name)}} > 1) {
212 print ERR "The connection between $name and image $filename has already been made. Skipping...\n";
214 elsif ($image_hash{$filename}) {
215 print ERR qq { Associating stock $name2id{lc($name)} with already loaded image $filename...\n };
216 ################################
219 else {
220 print ERR qq { Generating new image object for image $filename and associating it with stock $name, id $name2id{lc($name) } ...\n };
221 my $caption = $name;
223 if ($opt_t) {
224 print STDOUT qq { Would associate file $filename to stock $name, id $name2id{lc($name)}\n };
225 $new_image_count++;
227 else {
228 my $image = SGN::Image->new($dbh);
229 $image_hash{$filename}=$image;
231 $image->process_image("$filename", undef, undef);
232 $image->set_description("$caption");
233 $image->set_name(basename($filename , ".$ext"));
234 $image->set_sp_person_id($sp_person_id);
235 $image->set_obsolete("f");
236 $image_id = $image->store();
237 $new_image_count++;
241 my $metadata = CXGN::Metadata::Metadbdata->new($metadata_schema, $sp_person);
242 my $metadata_id = $metadata->store()->get_metadata_id();
243 die "NO METADATA ID FOUND\n" if !$metadata_id;
244 #store the image_id - stock_id link
245 my $q = "INSERT INTO phenome.stock_image (stock_id, image_id, metadata_id) VALUES (?,?,?)";
246 my $sth = $dbh->prepare($q);
247 $sth->execute($stock->stock_id, $image_id, $metadata_id);
250 if ($@) {
251 print STDOUT "ERROR OCCURRED WHILE SAVING NEW INFORMATION. $@\n";
252 $dbh->rollback();
254 else {
255 $dbh->commit();
260 close(ERR);
261 close(F);
264 print STDOUT "Inserted $new_image_count images.\n";
265 print STDOUT "Done. \n";
267 sub usage {
268 print "Usage: load_images.pl -D dbname [ cxgn | sandbox ] -H dbhost -t [trial mode ] -i input dir -l location of the plots (as loaded into stockprop). \n Images will be stored in phenome.stock_image \n";
269 exit();
272 sub message {
273 my $message=shift;
274 print STDOUT $message;
275 print ERR $message;