function _chado_update_cvtermpath_loop_increment

3.x tripal_chado.cv.api.inc _chado_update_cvtermpath_loop_increment( $origin, $child_id, $cv_id, $type_id, $depth, $increment_of_depth, $tree_path, $possible_loop, $matched_rows, &$possible_start_of_loop, $no_loop_skip_test)

Parameters

$origin: The root terms cvterm_id.

$child_id: The cvterm_id of the current child term. The child term is a descendent of the origin.

$cv_id: The controlled vocabulary ID from the cv table of Chado (i.e. cv.cv_id).

$type_id: The relationship type between the origin term and the child.

$depth: The depth of the recursion.

$increment_of_depth.: An integer.

$tree_path.: The array of every term between the current child and the origin. Each element in the array is an associative array with the keys: -build_id: an string identifier for the child that combines the origin, child cvterm_id,cv_id, and the type_id. -depth: the depth that a child was inserted into the cvtermpath table.

$possible_loop: A boolean flag.

$matched_row: An array of rows that are currently in the cvtermpath table that match the build_id of the current term trying to be written to the table

$possible_start_of_ loop: The array of the possible loop item between the current child and the origin. Each element in the array is an associative array with the keys:

  • cvid : $cv_id
  • subject_id:
  • child_id : $child_id,
  • type_id : $type_id,
  • depth : $depth,

$no_loop_skip_test: A boolean used when the possible loop has been ruled out as a loop.

Return value

multitype: Either a number that represents the row count of existing rows that already match these specification or a Boolean false.

Related topics

1 call to _chado_update_cvtermpath_loop_increment()

File

tripal_chado/api/modules/tripal_chado.cv.api.inc, line 673
Provides API functions specificially for managing controlled vocabulary records in Chado.

Code

function _chado_update_cvtermpath_loop_increment(
$origin, 
$child_id, 
$cv_id, 
$type_id, 
$depth, 
$increment_of_depth, 
$tree_path, 
$possible_loop, 
$matched_rows, 
&$possible_start_of_loop, 
$no_loop_skip_test) {

  // Check to see if a row with these values already exists.
  if ($possible_loop === FALSE && empty($possible_start_of_loop)) {
    chado_set_active('chado');
    $count = db_query(
    ' SELECT *
        FROM cvtermpath
        WHERE object_id = :origin
        AND subject_id = :child_id
        AND pathdistance = :depth
        AND type_id = :type_id
      ', 
    [
    ':origin' $origin
      ':child_id' $child_id
      ':depth' $depth
      ':type_id' $type_id
      ]
    );
    $count->fetchAll();
    $count_total = $count->rowCount();
    if ($count_total > 0) {
      return $count;
    }
    // Build the ID.
    $term_id = $origin . '|' . $child_id . '|' . $cv_id . '|' . $type_id;

    if ($no_loop_skip_test === FALSE) {
      // 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) {
          // If this child is the same as a parent term that has already been
          // processed then we have a potential loop.
          if ($parent['build_id'] == $term_id) {
            // Tell the function this is a possible loop and to stop writing to 
            // the table.
            $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
              ', 
            [
            ':cvid' $cv_id
              ':origin' $origin
              ':child_id' $child_id
              ':type_id' $type_id
              ]
            );
            $matched_rows = $matching_rows->fetchAll();
            $possible_start_of_loop = array(
              'cvid' => $cv_id,
              'subject_id' => $origin,
              'child_id' => $child_id,
              'type_id' => $type_id,
              'depth' => $depth,
            );
          }
        }
      }

      $find_query = db_select('chado.cvtermpath', 'CVTP');
      $find_query->fields('CVTP', ['subject_id']);
      $find_query->condition('object_id', $origin);
      $find_query->condition('subject_id', $child_id);
      $find_query->condition('type_id', $type_id);
      $find_query->condition('pathdistance', $depth);
      $exists = $find_query->execute()->fetchObject();

      if (!$exists) {
        $query = db_insert('cvtermpath')
          ->fields([
          'object_id' $origin
            'subject_id' $child_id
            'cv_id' $cv_id
            'type_id' $type_id
            'pathdistance' $depth
            ]);
        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] =[
      'build_id' $term_id
        'depth' $depth
        ];
    }
    // Reset to FALSE  and empty variable if passed in as TRUE.
    $no_loop_skip_test == FALSE;
    $possible_start_of_loop =[];

    // Get all of the relationships of this child term, and recursively
    // call the _chado_update_cvtermpath_loop_increment() function to continue
    // descending down the tree.
    $query = db_select('cvterm_relationship', 'cvtr')
      ->fields('cvtr')
      ->condition('cvtr.object_id', $child_id, '=')
      ->execute();
    $cterm_relationships = $query->fetchAll();

    foreach ($cterm_relationships as $item) {
      $increment_of_depth++;
      _chado_update_cvtermpath_loop_increment($origin, $item->subject_id, $cv_id, 
      $item->type_id, $depth + 1, $increment_of_depth, $tree_path, $possible_loop, 
      $matched_rows, $possible_start_of_loop, $no_loop_skip_test);
    }
  }
  elseif ($possible_loop === FALSE && !empty($possible_start_of_loop)) {
    // This means a loop has been identified and the recursive call can move
    // on to the next item and skip the rest of this run.
    return $possible_start_of_loop;
  }
  elseif ($possible_loop === TRUE) {
    // Get all of the relationships of this child term, and recursively
    // call the _chado_update_cvtermpath_loop() function to continue
    // descending down the tree.
    $query = db_select('cvterm_relationship', 'cvtr')
      ->fields('cvtr')
      ->condition('cvtr.object_id', $child_id, '=')
      ->execute();
    $cterm_relationships = $query->fetchAll();
    foreach ($cterm_relationships as $item) {
      $increment_of_depth++;
      _chado_update_cvtermpath_loop($origin, $item->subject_id, $cv_id, 
      $item->type_id, $depth + 1, $increment_of_depth, $tree_path, $possible_loop, 
      $matched_rows, $possible_start_of_loop, $no_loop_skip_test);
    }
  }


}