9 $load_trait_props.pl -H [dbhost] -D [dbname] -I [input file] -o [ontology] -W
11 =head1 COMMAND-LINE OPTIONS
16 -t Test run . Rolling back at the end.
23 Jeremy D. Edwards (jde22@cornell.edu)
29 Add support for other spreadsheet formats
38 use Bio
::Chado
::Schema
;
39 use Spreadsheet
::ParseExcel
;
40 use CXGN
::DB
::InsertDBH
;
41 use CXGN
::DB
::Connection
;
42 use CXGN
::Fieldbook
::TraitProps
;
45 our ($opt_H, $opt_D, $opt_I, $opt_o, $opt_w, $opt_t);
46 getopts
('H:D:I:o:wt');
50 print STDERR
"A script to load trait properties\nUsage: load_trait_props.pl -D [database name] -H [database host, e.g., localhost] -I [input file] -o [ontology namespace, e.g., CO] -w\n\t-w\toverwrite existing trait properties if they exist (optional)\n\t-t\ttest run. roll back at the end\n";
54 if (!$opt_D || !$opt_H || !$opt_I || !$opt_o) {
56 die("Exiting: options missing\n");
60 #try to open the excel file and report any errors
61 my $parser = Spreadsheet
::ParseExcel
->new();
62 my $excel_obj = $parser->parse($opt_I);
65 die "Input file error: ".$parser->error()."\n";
68 my $worksheet = ( $excel_obj->worksheets() )[0]; #support only one worksheet
69 my ( $row_min, $row_max ) = $worksheet->row_range();
70 my ( $col_min, $col_max ) = $worksheet->col_range();
72 if (($col_max - $col_min) < 1 || ($row_max - $row_min) < 1 ) { #must have header and at least one row of phenotypes
73 die "Input file error: spreadsheet is missing header\n";
78 if ($worksheet->get_cell(0,0)) {
79 $trait_name_head = $worksheet->get_cell(0,0)->value();
82 if (!$trait_name_head || $trait_name_head ne 'trait_name') {
83 die "Input file error: no \"trait_name\" in header\n";
86 my @trait_property_names = qw(
95 #check header for property names
96 for (my $column_number = 1; $column_number <= scalar @trait_property_names; $column_number++) {
97 my $property_name = $trait_property_names[$column_number-1];
98 if ( !($worksheet->get_cell(0,$column_number)) || !($worksheet->get_cell(0,$column_number)->value() eq $property_name) ) {
99 die "Input file error: no \"$property_name\" in header\n";
103 my @trait_props_data;
106 for my $row ( 1 .. $row_max ) {
109 my $current_row = $row+1;
112 if ($worksheet->get_cell($row,0)) {
113 $trait_name = $worksheet->get_cell($row,0)->value();
114 $trait_props{'trait_name'}=$trait_name;
116 next; #skip blank lines
120 foreach my $property_name (@trait_property_names) {
121 if ($worksheet->get_cell($row,$prop_column)) {
122 if (($worksheet->get_cell($row,$prop_column)->value()) && ($worksheet->get_cell($row,$prop_column)->value() ne '')) {
123 $trait_props{$property_name}=$worksheet->get_cell($row,$prop_column)->value();
129 push @trait_props_data, \
%trait_props;
133 my $dbh = CXGN
::DB
::InsertDBH
137 dbargs
=> {AutoCommit
=> 1,
141 my $overwrite_existing_props = 0;
144 $overwrite_existing_props = 1;
153 my $chado_schema = Bio
::Chado
::Schema
->connect( sub { $dbh->get_actual_dbh() } );
155 my $db_name = $opt_o;
157 my $trait_props = CXGN
::Fieldbook
::TraitProps
->new({ chado_schema
=> $chado_schema, db_name
=> $db_name, trait_names_and_props
=> \
@trait_props_data, overwrite
=> $overwrite_existing_props, is_test_run
=> $is_test_run});
159 print STDERR
"Validating data...\t";
160 my $validate=$trait_props->validate();
163 die("input data is not valid\n");
165 print STDERR
"input data is valid\n";
168 print STDERR
"Storing data...\t\t";
169 my $store = $trait_props->store();
173 die("\n\nerror storing data\n");
176 print STDERR
"successfully stored data\n";