add image file for sgn project
[sgn.git] / bin / load_trait_props.pl
blob899ea0c1df70eb62aa10aca7ffda47039017c4cc
1 #!/usr/bin/env perl
3 =head1
5 load_trait_props.pl
7 =head1 SYNOPSIS
9 $load_trait_props.pl -H [dbhost] -D [dbname] -I [input file] -o [ontology] -W
11 =head1 COMMAND-LINE OPTIONS
13 -H host name
14 -D database name
15 -w overwrite
16 -t Test run . Rolling back at the end.
18 =head2 DESCRIPTION
21 =head2 AUTHOR
23 Jeremy D. Edwards (jde22@cornell.edu)
25 April 2014
27 =head2 TODO
29 Add support for other spreadsheet formats
31 =cut
33 use strict;
34 use warnings;
36 use lib 'lib';
37 use Getopt::Std;
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');
49 sub print_help {
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) {
55 print_help();
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);
64 if ( !$excel_obj ) {
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";
76 my $trait_name_head;
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(
87 trait_format
88 trait_default_value
89 trait_minimum
90 trait_maximum
91 trait_categories
92 trait_details
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 ) {
107 my %trait_props;
108 my $trait_name;
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;
115 } else {
116 next; #skip blank lines
119 my $prop_column = 1;
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();
126 $prop_column++;
129 push @trait_props_data, \%trait_props;
133 my $dbh = CXGN::DB::InsertDBH
134 ->new({
135 dbname => $opt_D,
136 dbhost => $opt_H,
137 dbargs => {AutoCommit => 1,
138 RaiseError => 1},
141 my $overwrite_existing_props = 0;
143 if ($opt_w){
144 $overwrite_existing_props = 1;
147 my $is_test_run = 0;
149 if ($opt_t){
150 $is_test_run = 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();
162 if (!$validate) {
163 die("input data is not valid\n");
164 } else {
165 print STDERR "input data is valid\n";
168 print STDERR "Storing data...\t\t";
169 my $store = $trait_props->store();
171 if (!$store){
172 if (!$is_test_run) {
173 die("\n\nerror storing data\n");
175 } else {
176 print STDERR "successfully stored data\n";