5 find_accessions_for_seedlots.pl - give the accession for each seedlot name
9 find_accessions_for_seedlots.pl -H [dbhost] -D [dbname] -i [infile]
11 =head1 COMMAND-LINE OPTIONS
13 -H host name (required) e.g. "localhost"
14 -D database name (required) e.g. "cxgn_cassava"
15 -i path to infile (required)
23 Lukas Mueller <lam87@cornell.edu>, based on a script by Nick Morales
33 use Spreadsheet
::ParseExcel
;
34 use Spreadsheet
::ParseXLSX
;
35 use Bio
::Chado
::Schema
;
36 use CXGN
::DB
::InsertDBH
;
37 use SGN
::Model
::Cvterm
;
39 use CXGN
::Stock
::Seedlot
;
41 our ($opt_H, $opt_D, $opt_i);
45 if (!$opt_H || !$opt_D || !$opt_i) {
46 pod2usage
(-verbose
=> 2, -message
=> "Must provide options -H (hostname), -D (database name), -i (input file) \n");
52 # Match a dot, extension .xls / .xlsx
53 my ($extension) = $opt_i =~ /(\.[^.]+)$/;
56 print STDERR
"Figuring out file format... ($extension)...\n";
57 if ($extension eq '.xlsx') {
58 $parser = Spreadsheet
::ParseXLSX
->new();
61 $parser = Spreadsheet
::ParseExcel
->new();
64 #print STDERR "Parsing file... (please wait...)\n";
66 #my $excel_obj = $parser->parse($opt_i);
68 print STDERR
"Connecting to database $dbname on $dbhost ...\n";
70 my $dbh = CXGN
::DB
::InsertDBH
->new({
73 dbargs
=> {AutoCommit
=> 1, RaiseError
=> 1}
76 my $schema= Bio
::Chado
::Schema
->connect( sub { $dbh->get_actual_dbh() } );
77 $dbh->do('SET search_path TO public,sgn');
80 print STDERR
"Parsing file...\n";
82 #my $worksheet = ( $excel_obj->worksheets() )[0]; #support only one worksheet
83 #my ( $row_min, $row_max ) = $worksheet->row_range();
84 #my ( $col_min, $col_max ) = $worksheet->col_range();
86 my $seedlot_type_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'seedlot', 'stock_type')->cvterm_id();
87 my $stock_type_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'accession', 'stock_type')->cvterm_id();
89 print STDERR
"Seedlot type id = $seedlot_type_id\n";
91 open(my $F, "<", $opt_i) || die "Can't open file $opt_i";
98 my ($empty, $seedlot, @row) = split /\t/, $_;
100 print STDERR
"Reading row '$empty', $seedlot, and the rest is: ". join(",", @row)."\n";
102 my $seedlot_row = $schema->resultset("Stock::Stock")->find( { uniquename
=> $seedlot, type_id
=> $seedlot_type_id });
107 if (! defined($seedlot_row)) {
108 $name = "[SEEDLOT NOT IN DB]";
109 print STDERR
"ROW === ".Dumper
($seedlot_row);
113 print STDERR
"FOUND SEEDLOT!\n";
115 my $seedlot = CXGN
::Stock
::Seedlot
->new( schema
=> $schema, seedlot_id
=> $seedlot_row->stock_id() );
117 if (my $accession = $seedlot->accession()) {
118 $name = $accession->[1];
121 elsif (my $cross = $seedlot->cross()) {
127 print join("\t", $name, $seedlot, @row)."\n";
131 print STDERR
"Script Complete.\n";