9 change_trait_ids.pl -H [dbhost] -D [dbname] -i [tsv file with old and new ids] [-t]
11 If an error caused by a duplicate key value is thrown, for example:
13 DBD::Pg::st execute failed: ERROR: duplicate key value violates unique constraint "dbxref_c1"
14 DETAIL: Key (db_id, accession, version)=(274, 1000004, ) already exists. at ./sgn/bin/change_trait_ids.pl line 69, <FILE> line 87.
16 You can manually alter the dbxref accession value in the db to something non-conflicting, for example:
17 "update dbxref set accession = '1444444' where accession = '1000004' and db_id = 274;"
18 then re-run this script. This process may have to be repeated for multiple ids
20 =head1 COMMAND-LINE OPTIONS
24 -i file with old and new ids
25 -t Test run. Rolls back at the end.
31 Bryan Ellerbrock (bje24@cornell.edu) - Sept 2016
40 use CXGN
::DB
::InsertDBH
;
42 our ($opt_H, $opt_D, $opt_i, $opt_t);
52 # store database handle and schema
54 $dbh = CXGN
::DB
::InsertDBH
->new( { dbhost
=>$dbhost,
56 dbargs
=> {AutoCommit
=> 0,
61 # prepare sql delete statements
63 my $update_query = "Update dbxref set accession = ? where accession = ? and db_id = ((SELECT dbxref_1.db_id FROM stock JOIN nd_experiment_stock USING (stock_id) JOIN nd_experiment_phenotype USING (nd_experiment_id) JOIN phenotype USING (phenotype_id) JOIN cvterm cvterm_1 ON phenotype.cvalue_id = cvterm_1.cvterm_id JOIN dbxref dbxref_1 ON cvterm_1.dbxref_id = dbxref_1.dbxref_id LIMIT 1 ));";
64 my $update_handle = $dbh->prepare($update_query);
67 # identify ids to be changed
69 open (FILE
, "<", $opt_i) || die "Can't open infile $opt_i \n";
72 my ($new_id, $old_id) = split('\t', $_);
76 print STDERR
"Updating old id $old_id to new id $new_id\n";
77 #chomp (my $ids = $_);
78 $update_handle->execute($new_id, $old_id);
81 my $list_fix = "update sgn_people.list_item set content = regexp_replace(content, ':.*', ':$new_id') where content like '$pattern';";
82 print STDERR
"updating list elements with query: $list_fix\n";
83 my $list_fix_handle = $dbh->prepare($list_fix);
84 $list_fix_handle->execute();
87 # Rollback if transaction failed
90 die "An error occured! Transaction rolled back!" . $_ . "\n";
94 # commit if this is not a test run
97 print "Id update succeeded! Commiting new ids! \n\n";
99 # Rolling back because test run
101 print "No errors occurred. Rolling back test run. \n\n";