Procházet zdrojové kódy

changes to cvtermpathupdate to allow for goslim to load and an update to the oboebi lookup functionality so that edam does not cause a problem and one small change to a tripal drush command to apply permissions to content types

Shawna Spoor před 7 roky
rodič
revize
10c1c605e9

+ 3 - 6
tripal/tripal.drush.inc

@@ -372,12 +372,9 @@ function drush_tripal_trp_set_permissions() {
 
   drush_tripal_set_user($username);
   $permissions = array();
-  $bundles =
-  db_select('tripal_bundle', 'TB')
-    ->fields('TB', array('name'))
-    ->execute()
-    ->fetchAll();
-  print_r($bundles);
+    
+  $bundles = tripal_get_content_types();
+
   foreach ($bundles as $bundles => $bundle) {
     array_push($permissions, ' view ' . $bundle->name, ' create ' . $bundle->name,
       ' edit ' . $bundle->name, ' delete ' . $bundle->name);

+ 56 - 38
tripal_chado/api/modules/tripal_chado.cv.api.inc

@@ -387,7 +387,8 @@ function tripal_update_cvtermpath($cv_id, $job_id = NULL){
 
     // Iterate through each root level term.
     $record = $result->fetchAll();
-    $roots = array();
+    $roots = [];
+
     foreach ($record as $item){
       tripal_update_cvtermpath_root_loop($item->cvterm_id, $item->cv_id, $roots);
     }
@@ -416,16 +417,24 @@ function tripal_update_cvtermpath($cv_id, $job_id = NULL){
  * @ingroup tripal_chado_cv_api
  */
 function tripal_update_cvtermpath_root_loop($rootid, $cvid, &$roots) {
-
   // Get's the cvterm record for this "root".
-  $ttype = db_select('cvterm', 'cv')
-          ->fields('cv', array('cvterm_id'));
-  $db_or = db_or();
-  $db_or->condition('cv.name', "isa", '=');
-  $db_or->condition('cv.name', "is_a", '=');
-  $ttype->condition($db_or);
-  $result = $ttype->execute()->fetchObject();
+  // $ttype = db_select('cvterm', 'cv')
+  //         ->fields('cv', array('cvterm_id'));
+  // $db_or = db_or();
+  // $db_or->condition('cv.name', "isa", '=');
+  // $db_or->condition('cv.name', "is_a", '=');
+  // $ttype->condition($db_or);
+  $ttype = db_query(
+    'SELECT cv.cvterm_id 
+    FROM cvterm cv
+    WHERE cv.name = :isa 
+          OR cv.name = :is_a
+    '
+    ,
+    array(':isa' => "isa", ':is_a' => "is_a")
+  );
 
+  $result = $ttype->fetchObject();
   $term_id = $rootid . '|' . $rootid . '|' . $cvid . '|' . $result->cvterm_id;
   // If the child_id matches any other id in the array then we've hit a loop.
   foreach ($roots as $element_id) {
@@ -437,9 +446,9 @@ function tripal_update_cvtermpath_root_loop($rootid, $cvid, &$roots) {
   $roots[] = $term_id;
 
   // Descends through the branch starting at this "root" term.
-  $tree_path = array();
-  $matched_rows = array();
-  $possible_start_of_loop = array();
+  $tree_path = [];
+  $matched_rows = [];
+  $possible_start_of_loop = [];
   $depth = 0;
   tripal_update_cvtermpath_loop($rootid, $rootid, $cvid, $result->cvterm_id, $depth,
                                 0, $tree_path, FALSE, $matched_rows, $possible_start_of_loop, FALSE);
@@ -451,7 +460,7 @@ function tripal_update_cvtermpath_root_loop($rootid, $cvid, &$roots) {
       FROM cvterm_relationship
       WHERE object_id = :rootid
     ',
-    array(':rootid' => $rootid)
+    [':rootid' => $rootid]
   );
   while ($cterm_result = $cterm->fetchAssoc()) {
     tripal_update_cvtermpath_root_loop($cterm_result['subject_id'], $cvid, $roots);
@@ -510,7 +519,7 @@ function tripal_update_cvtermpath_loop(
   $no_loop_skip_test) {
 
   // We have not detected a loop, so it's safe to insert the term.
-  $new_match_rows = array();
+  $new_match_rows = [];
   if (!empty($possible_start_of_loop)) {
     // Go through each matched_row.
     if (count($matched_rows) === 1) {
@@ -523,7 +532,7 @@ function tripal_update_cvtermpath_loop(
           FROM cvtermpath
           WHERE cvtermpath_id = :cvtermpath_id
         ',
-        array(':cvtermpath_id' => $cvtermpath_id)
+        [':cvtermpath_id' => $cvtermpath_id]
       );
       $next_row = $next_row->fetchObject();
 
@@ -541,7 +550,7 @@ function tripal_update_cvtermpath_loop(
         // The next_row is equal to start of loop, so we've reached the end
         // and confirmed that this is a loop.
         $possible_loop == FALSE;
-        $matched_rows = array();
+        $matched_rows = [];
         tripal_update_cvtermpath_loop_increment($origin, $child_id, $cv_id,
         $type_id, $depth + 1, $increment_of_depth, $tree_path, $possible_loop,
         $new_match_rows, $possible_start_of_loop, $no_loop_skip_test);
@@ -560,7 +569,7 @@ function tripal_update_cvtermpath_loop(
             FROM cvtermpath
             WHERE cvtermpath_id = :cvtermpath_id
           ',
-          array(':cvtermpath_id' => $cvtermpath_id)
+          [':cvtermpath_id' => $cvtermpath_id]
         );
         $next_row = $next_row->fetchObject();
 
@@ -578,7 +587,7 @@ function tripal_update_cvtermpath_loop(
           // The next_row is equal to start of loop, so we've reached the end
           // and confirmed that this is a loop.
           $possible_loop == FALSE;
-          $matched_rows = array();
+          $matched_rows = [];
           tripal_update_cvtermpath_loop_increment($origin, $child_id, $cv_id,
           $type_id, $depth + 1, $increment_of_depth, $tree_path, $possible_loop,
           $new_match_rows, $possible_start_of_loop, $no_loop_skip_test);
@@ -589,7 +598,7 @@ function tripal_update_cvtermpath_loop(
     // If $match_rows is empty there is no loop.
     if (empty($new_match_rows)) {
       $possible_loop == FALSE;
-      $matched_rows = array();
+      $matched_rows = [];
       unset($new_match_rows);
       $no_loop_skip_test = TRUE;
       // There is not loop so pass it back the possible_start_of_loop info
@@ -669,20 +678,21 @@ function tripal_update_cvtermpath_loop_increment(
   if ($possible_loop === FALSE && empty($possible_start_of_loop)) {
     chado_set_active('chado');
     $count = db_query(
-      'SELECT *
+      ' SELECT *
         FROM cvtermpath
-        WHERE cv_id = :cvid
-        AND object_id = :origin
+        WHERE object_id = :origin
         AND subject_id = :child_id
         AND pathdistance = :depth
+        AND type_id = :type_id
       ',
-      array(
-        ':cvid' => $cv_id,
+      [
         ':origin' => $origin,
         ':child_id' => $child_id,
-        ':depth' => $depth
-      )
+        ':depth' => $depth,
+        ':type_id' => $type_id
+      ]
     );
+    $count->fetchAll();
     $count_total = $count->rowCount();
     if ($count_total > 0) {
       return $count;
@@ -691,7 +701,8 @@ function tripal_update_cvtermpath_loop_increment(
     $term_id = $origin . '|' . $child_id . '|' . $cv_id . '|' . $type_id;
 
     if ($no_loop_skip_test === FALSE) {
-      // Now check if the most recent entry already exists in the array.
+      // If the increment of depth is 0 then it's the first time and we need to skip
+      // the test so we can build the tree_path which will be tested against.
       if ($increment_of_depth != 0) {
         // Search the $tree_path for the new $child_id in the build_id column.
         foreach ($tree_path as $parent) {
@@ -702,19 +713,19 @@ function tripal_update_cvtermpath_loop_increment(
             $possible_loop = TRUE;
             // Find all the results in the table that might be the start of the loop.
             $matching_rows = db_query(
-              'SELECT *
-              FROM cvtermpath
-              WHERE cv_id = :cvid
-              AND object_id = :origin
-              AND subject_id = :child_id
-              AND type_id = :type_id
+              ' SELECT *
+                FROM cvtermpath
+                WHERE cv_id = :cvid
+                AND object_id = :origin
+                AND subject_id = :child_id
+                AND type_id = :type_id
               ',
-              array(
+              [
                 ':cvid' => $cv_id,
                 ':origin' => $origin,
                 ':child_id' => $child_id,
                 ':type_id' => $type_id
-              )
+              ]
             );
             $matched_rows = $matching_rows->fetchAll();
             $possible_start_of_loop = array(
@@ -736,7 +747,14 @@ function tripal_update_cvtermpath_loop_increment(
           'type_id' => $type_id,
           'pathdistance' => $depth,
         ]);
-      $rows = $query->execute();
+
+      try {
+        $rows = $query->execute();
+      } catch (Exception $e) {
+        $error = $e->getMessage();
+        tripal_report_error('tripal_chado', TRIPAL_ERROR, "Could not fill cvtermpath term: @error", array('@error' => $error));
+        return false;
+      }
 
       // Then add that new entry to the $tree_path.
       $tree_path[$increment_of_depth] = [
@@ -746,10 +764,10 @@ function tripal_update_cvtermpath_loop_increment(
     }
     // Reset to FALSE  and empty variable if passed in as TRUE.
     $no_loop_skip_test == FALSE;
-    $possible_start_of_loop = array();
+    $possible_start_of_loop = [];
 
     // Get all of the relationships of this child term, and recursively
-    // call the tripal_update_cvtermpath_loop() function to continue
+    // call the tripal_update_cvtermpath_loop_increment() function to continue
     // descending down the tree.
     $query = db_select('cvterm_relationship', 'cvtr')
       ->fields('cvtr')

+ 27 - 20
tripal_chado/includes/TripalImporter/OBOImporter.inc

@@ -419,6 +419,8 @@ class OBOImporter extends TripalImporter {
     // Upate the cvtermpath table for each newly added CV.
     $this->logMessage("Updating cvtermpath table.  This may take a while...");
     foreach ($this->newcvs as $namespace => $cvid) {
+      print_r($cv_id);
+      print_r("\n");
       $this->logMessage("- Loading paths for @vocab", array('@vocab' => $namespace));
       tripal_update_cvtermpath($cvid);
     }
@@ -594,9 +596,12 @@ class OBOImporter extends TripalImporter {
       fclose($fh);
       // Check if the EBI ontology search has this ontology:
       try {
-        $results = $this->oboEbiLookup($short_name, 'ontology');  
+        $results = $this->oboEbiLookup($short_name, 'ontology');
         if (array_key_exists('default-namespace', $results['config']['annotations'])) {
           $results = $results['config']['annotations']['default-namespace'];
+          if (is_array($results)) {
+            $results = $results[0];
+          }
         }
         elseif (array_key_exists('namespace', $results['config'])) {
           $results = $results['config']['namespace'];
@@ -605,7 +610,7 @@ class OBOImporter extends TripalImporter {
           $results = $short_name;
         }
         $defaultcv = tripal_insert_cv(strtoupper($results), '');
-        $this->newcvs[strtoupper(strtoupper($header['ontology'][0]))] = $defaultcv->cv_id;
+        $this->newcvs[$defaultcv->name] = $defaultcv->cv_id;
       }
       catch (Exception $e) {
         watchdog_exception('OBOImporter no such accession found in EBI Ontology Search', $e);
@@ -738,6 +743,23 @@ class OBOImporter extends TripalImporter {
     if (!array_key_exists('namespace', $term) and !($defaultcv or $defaultcv == '')) {
       throw new Exception("Cannot add the term: no namespace defined. " . $term['id'][0]);
     }
+   // construct the term array for sending to the tripal_chado_add_cvterm function
+    // for adding a new cvterm
+    $t = array();
+    $t['id'] = $term['id'][0];
+    $t['name'] = $term['name'][0];
+    if (array_key_exists('def', $term)) {
+      $t['definition'] = $term['def'][0];
+    }
+    if (array_key_exists('subset', $term)) {
+      $t['subset'] = $term['subset'][0];
+    }
+    if (array_key_exists('namespace', $term)) {
+      $t['namespace'] = $term['namespace'][0];
+    }
+    if (array_key_exists('is_obsolete', $term)) {
+      $t['is_obsolete'] = $term['is_obsolete'][0];
+    }
 
     // Check that the default_cv is in the cv table.
     $sql =  "
@@ -757,28 +779,13 @@ class OBOImporter extends TripalImporter {
           $results = $ontology_info['config']['namespace'];
         }
         $cv_returned = tripal_insert_cv($results[0], '');
+        // If name && definition are both empty then look up the term from the ontology you just loaded.
         if($cv_returned) {
           $defaultcv = $cv_returned;
         }
       }
     }
-    // construct the term array for sending to the tripal_chado_add_cvterm function
-    // for adding a new cvterm
-    $t = array();
-    $t['id'] = $term['id'][0];
-    $t['name'] = $term['name'][0];
-    if (array_key_exists('def', $term)) {
-      $t['definition'] = $term['def'][0];
-    }
-    if (array_key_exists('subset', $term)) {
-      $t['subset'] = $term['subset'][0];
-    }
-    if (array_key_exists('namespace', $term)) {
-      $t['namespace'] = $term['namespace'][0];
-    }
-    if (array_key_exists('is_obsolete', $term)) {
-      $t['is_obsolete'] = $term['is_obsolete'][0];
-    }
+ 
 
     $t['cv_name'] = $defaultcv;
     $t['is_relationship'] = $is_relationship;
@@ -955,7 +962,7 @@ class OBOImporter extends TripalImporter {
       $pair = explode(":", $rel);    
       $ontology_id = $pair[0];
       $accession_num = $pair[1];
-      if (strlen($accession_num) > 6) {
+      if (is_numeric($accession_num)) {
         $results = $this->oboEbiLookup($rel, 'query');
         if (!empty($results)) {
           if (array_key_exists('docs', $results)){

+ 19 - 0
tripal_chado/includes/tripal_chado.cv.inc

@@ -822,4 +822,23 @@ function tripal_cv_cvtermpath_form_submit($form, &$form_state) {
     tripal_add_job("Update cvtermpath: $cv->name", 'tripal_cv',
         'tripal_update_cvtermpath', $job_args, $user->uid, 10);
   }
+}
+
+function tripal_cv_obo_ebi_lookup($accession) {
+  print_r("accession: $accession\n");
+  //The IRI of the terms, this value must be double URL encoded
+  $iri = urlencode("http://purl.obolibrary.org/obo/" . $accession);
+  //Grab just the ontology from the $accession.
+  $parts = explode(':', $accession);
+  $ontology = $parts[0];
+  print_r("ontology: $ontology\n");
+  $options = array();
+  $full_url = 'http://www.ebi.ac.uk/ols/api/ontologies/' . $ontology . '/' . 'terms/' . $iri;
+  $response = drupal_http_request($full_url, $options);
+  if(!empty($response)){
+    $response = drupal_json_decode($response->data);
+  }
+  print_r($response);
+  return $response;
+  // php-eval "module_load_include('inc', 'tripal_chado', 'sites/all/modules/tripal/tripal_chado/includes/tripal_chado.cv'); tripal_cv_obo_ebi_lookup('BFO:0000050')"
 }