SF.net SVN: gmod:[25253] schema/trunk/chado/bin/gmod_load_cvterms.pl

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

SF.net SVN: gmod:[25253] schema/trunk/chado/bin/gmod_load_cvterms.pl

nm249
Revision: 25253
          http://gmod.svn.sourceforge.net/gmod/?rev=25253&view=rev
Author:   nm249
Date:     2012-01-24 19:16:18 +0000 (Tue, 24 Jan 2012)
Log Message:
-----------
no need for dbh. Autocommit should be on, and use schema transaction

Modified Paths:
--------------
    schema/trunk/chado/bin/gmod_load_cvterms.pl

Modified: schema/trunk/chado/bin/gmod_load_cvterms.pl
===================================================================
--- schema/trunk/chado/bin/gmod_load_cvterms.pl 2011-12-01 19:46:55 UTC (rev 25252)
+++ schema/trunk/chado/bin/gmod_load_cvterms.pl 2012-01-24 19:16:18 UTC (rev 25253)
@@ -156,6 +156,7 @@
 use Bio::GMOD::DB::Config;
 
 use Bio::Chado::Schema;
+use Try::Tiny;
 
 our ($opt_d, $opt_h, $opt_H, $opt_F, $opt_n, $opt_D, $opt_v, $opt_t,
      $opt_u, $opt_o, $opt_p, $opt_r, $opt_g, $opt_s);
@@ -203,12 +204,10 @@
 $dsn .= ";host=$dbhost";
 $dsn .= ";port=$port";
 
-my $schema= Bio::Chado::Schema->connect($dsn, $user, $pass||'', { AutoCommit=>0 });
+my $schema= Bio::Chado::Schema->connect($dsn, $user, $pass||'');
 
-my $dbh=$schema->storage->dbh();
+if (!$schema) { die "No schema is avaiable! \n"; }
 
-if (!$schema || !$dbh) { die "No schema or dbh is avaiable! \n"; }
-
 #######################
 
 my $error = 0; # keep track of input errors (in command line switches).
@@ -251,42 +250,43 @@
     push @onts, $ont;
 }
 message("Default namespace is " . $default_ont->name . " \n" , 1);
+
 my $default_cv= $schema->resultset('Cv::Cv')->find_or_create( { name => $default_ont->name } , { key => 'cv_c1' }, );
 
 foreach my $new_ont(@onts) {
-    my $new_ont_name=$new_ont->name();
-    message("....found namespace  '$new_ont_name' \n", 1);
+    my $coderef = sub {
+ my $new_ont_name=$new_ont->name();
+ message("....found namespace  '$new_ont_name' \n", 1);
 
-    if ($opt_n && ( $opt_n ne $new_ont_name) ) {
- message ("$opt_n: skipping to next ontology..\n",1);
- next ();
-    }
-    my $rel_cv;
-    #check if relationship ontology is already loaded:
-    if ($new_ont_name ne 'relationship') {
- $rel_cv= $schema->resultset("Cv::Cv")->find_or_create( { name => 'relationship' } , { key => 'cv_c1' }, );
- my @rel= $schema->resultset("Cv::Cvterm")->search(
-    { cv_id => $rel_cv->get_column('cv_id'),
-      is_relationshiptype => 1,
-    });
- if (!@rel) {
-    warn "Relationship ontology must be loaded first!!\n" ;
-    exit(0);
+ if ($opt_n && ( $opt_n ne $new_ont_name) ) {
+    message ("$opt_n: skipping to next ontology..\n",1);
+    next ();
  }
-    }
-    ####add Typedef parsing to obo.pm!###
-    ####store a new cv if the ontology namespace does not exist
-    my $cv= $schema->resultset('Cv::Cv')->find_or_create( { name => $new_ont_name } , { key => 'cv_c1' }, );
+ my $rel_cv;
+ #check if relationship ontology is already loaded:
+ if ($new_ont_name ne 'relationship') {
+    $rel_cv= $schema->resultset("Cv::Cv")->find_or_create( { name => 'relationship' } , { key => 'cv_c1' }, );
+    my @rel= $schema->resultset("Cv::Cvterm")->search(
+ { cv_id => $rel_cv->get_column('cv_id'),
+  is_relationshiptype => 1,
+ });
+    if (!@rel) {
+ warn "Relationship ontology must be loaded first!!\n" ;
+ exit(0);
+    }
+ }
+ ####add Typedef parsing to obo.pm!###
+ ####store a new cv if the ontology namespace does not exist
+ my $cv= $schema->resultset('Cv::Cv')->find_or_create( { name => $new_ont_name } , { key => 'cv_c1' }, );
 
-    print STDERR "cv_id = ".($cv->get_column('cv_id') )."\n";
-    print STDERR "Updating an ontology in the database...\n";
-    my $db_ont = $cv;
-    my $ontology_name=$db_ont->get_column('name');
-    message("Ontology name: ".($db_ont->name())."\n", 1);
-    my %file_relationships = (); # relationships currently defined in the file
-    my %db_relationships = ();
-
-    eval {
+ print STDERR "cv_id = ".($cv->get_column('cv_id') )."\n";
+ print STDERR "Updating an ontology in the database...\n";
+ my $db_ont = $cv;
+ my $ontology_name=$db_ont->get_column('name');
+ message("Ontology name: ".($db_ont->name())."\n", 1);
+ my %file_relationships = (); # relationships currently defined in the file
+ my %db_relationships = ();
+ ######
  my $db = $schema->resultset("General::Db")->find_or_create(
     { name => $opt_s }, { key => 'db_c1' }, );
 
@@ -395,7 +395,7 @@
                   $db_index{$k}->set_column(name => $name );
                   $db_index{$k}->set_column( definition => $file_index{$k}->definition() );
                   $db_index{$k}->set_column(is_obsolete => $file_index{$k}->is_obsolete() );
-
+  
                   #changing the name of obsolete terms to "$name (obsolete $db:$accession)"
                   #to avoid violating the cvterm unique constaint (name, cv_id, is_obsolete)
                   if ($db_index{$k}->is_obsolete() ) {
@@ -476,9 +476,9 @@
 
                   my %file_secondary_ids = ();
                   foreach my $i ($file_index{$k}->get_secondary_ids()) {
+      $i = substr($i, 0, 255); #dbxref.accession is varchar(255) maybe it needs to be text?
                       $file_secondary_ids{uc($i)}=1;
                       message("adding secondary id $i to the database...\n");
-
                       $db_index{$k}->add_secondary_dbxref($i);
                   }
                   #########
@@ -638,7 +638,7 @@
  print STDERR "\n";
  #####################################
  my $r_count = 0;
- RELATIONSHIP: foreach my $r (keys(%file_relationships)) {
+      RELATIONSHIP: foreach my $r (keys(%file_relationships)) {
     $r_count++;
     if (!exists($db_relationships{$r})) {
  if ($opt_v) { print STDERR "Novel relationship: $r\n"; }
@@ -737,25 +737,23 @@
  );
  }
     }
- }
+      }
  message($ontology_name." : ". scalar(@novel_relationships)." novel relationships among ".(scalar(keys(%file_relationships)))." were found and stored.\n", 1);
-    };
 
-    if ($@ || ($opt_t)) {
- message( "Either running as trial mode (-t) or AN ERROR OCCURRED: $@\n",1);
- $dbh->rollback();
- exit(0);
-    }
-    else {
+ if ($opt_t) {
+    die "TEST RUN! rolling back\n";
+ }
+    };
+    
+    try {
+ $schema->txn_do($coderef);
  message("Committing! \n If you are using cvtermpath you should now run gmod_make_cvtermpath.pl . See the perldoc for more info. \n\n", 1);
-        $dbh->commit();
+    } catch {
+ # Transaction failed
+ die "An error occured! Rolling back! " . $_ . "\n";
     }
 }
 
-print STDERR "Done.\n";
-
-
-
 sub recursive_children {
     my $ont = shift;
     my $node = shift;

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.


------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d
_______________________________________________
Gmod-schema-cmts mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gmod-schema-cmts