private function GFF3Importer::loadFeatureLoc

3.x GFF3Importer.inc private GFF3Importer::loadFeatureLoc($feature, $organism, $landmark, $fmin, $fmax, $strand, $phase, $is_fmin_partial, $is_fmax_partial, $residue_info, $locgroup, $landmark_type_id = '', $landmark_organism_id = '', $create_landmark = 0, $landmark_is_target = 0)

Insert the location of the feature

Parameters

$feature:

$organism:

$landmark:

$fmin:

$fmax:

$strand:

$phase:

$is_fmin_partial:

$is_fmax_partial:

$residue_info:

$locgroup: _type_id _organism_id

$create_landmark: _is_target

2 calls to GFF3Importer::loadFeatureLoc()
GFF3Importer::loadGFF3 in tripal_chado/includes/TripalImporter/GFF3Importer.inc
Actually load a GFF3 file. This is the function called by tripal jobs
GFF3Importer::loadTarget in tripal_chado/includes/TripalImporter/GFF3Importer.inc
Load the target attribute of a gff3 record

File

tripal_chado/includes/TripalImporter/GFF3Importer.inc, line 1715

Class

GFF3Importer

Code

private function loadFeatureLoc($feature, $organism, $landmark, $fmin, 
$fmax, $strand, $phase, $is_fmin_partial, $is_fmax_partial, $residue_info, $locgroup, 
$landmark_type_id = '', $landmark_organism_id = '', $create_landmark = 0, 
$landmark_is_target = 0) {

  $select = array(
    'organism_id' => $landmark_organism_id ? $landmark_organism_id : $organism->organism_id,
    'uniquename' => $landmark,
  );
  if ($landmark_type_id) {
    $select['type_id'] = $landmark_type_id;
  }
  $results = chado_select_record('feature', array('feature_id'), $select);

  $srcfeature = '';
  if (count($results) == 0) {
    // so we couldn't find the landmark using the uniquename. Let's try the 'name'.
    // if we return only a single result then we can proceed. Otherwise give an
    $select = array(
      'organism_id' => $landmark_organism_id ? $landmark_organism_id : $organism->organism_id,
      'name' => $landmark,
    );
    if ($landmark_type_id) {
      $select['type_id'] = $landmark_type_id;
    }
    $results = chado_select_record('feature', array('feature_id'), $select);
    if (count($results) == 0) {
      // if the landmark is the target feature in a matched alignment then try one more time to
      // find it by querying any feature with the same uniquename. If we find one then use it.
      if ($landmark_is_target) {
        $select = array('uniquename' => $landmark);
        $results = chado_select_record('feature', array('feature_id'), $select);
        if (count($results) == 1) {
          $srcfeature = $results[0];
        }
      }

      if (!$srcfeature) {
        // we couldn't find the landmark feature, so if the user has requested we create it then do so
        // but only if we have a type id
        if ($create_landmark and $landmark_type_id) {
          $values = array(
            'organism_id' => $landmark_organism_id ? $landmark_organism_id : $organism->organism_id,
            'name' => $landmark,
            'uniquename' => $landmark,
            'type_id' => $landmark_type_id
          );
          $results = chado_insert_record('feature', $values);
          if (!$results) {
            $this->logMessage("Cannot find landmark feature: '%landmark', nor could it be inserted.", 
            array('%landmark' => $landmark), TRIPAL_WARNING);
            return 0;
          }
          $srcfeature = new stdClass();
          $srcfeature->feature_id = $results['feature_id'];
        }
        else {
          $this->logMessage("Cannot find unique landmark feature: '%landmark'.", 
          array('%landmark' => $landmark), TRIPAL_WARNING);
          return 0;
        }
      }
    }
    elseif (count($results) > 1) {
      $this->logMessage("multiple landmarks exist with the name: '%landmark'.  Cannot " .
        "resolve which one to use. Cannot add the feature location record.", 
      array('%landmark' => $landmark), TRIPAL_WARNING);
      return 0;
    }
    else {
      $srcfeature = $results[0];
    }
  }
  elseif (count($results) > 1) {
    $this->logMessage("multiple landmarks exist with the name: '%landmark'.  Cannot " .
      "resolve which one to use. Cannot add the feature location record.", 
    array('%landmark' => $landmark, TRIPAL_WARNING));
    return 0;
  }
  else {
    $srcfeature = $results[0];
  }

  // TODO: create an attribute that recognizes the residue_info,locgroup,
  //  is_fmin_partial and is_fmax_partial, right now these are
  //  hardcoded to be false and 0 below.

  // check to see if this featureloc already exists, but also keep track of the
  // last rank value
  $rank = 0;
  $exists = 0;
  $select = array('feature_id' => $feature->feature_id);
  $options = array(
    'order_by' => array(
      'rank' => 'ASC'
    ),
  );

  $locrecs = chado_select_record('featureloc', array('*'), $select, $options);

  foreach ($locrecs as $featureloc) {
    // it is possible for the featureloc->srcfeature_id to be NULL. This can happen if the srcfeature
    // is not known (according to chado table field descriptions).  If it's null then just skip this entry
    if (!$featureloc->srcfeature_id) {
      continue;
    }
    $select = array('feature_id' => $featureloc->srcfeature_id);
    $columns = array('feature_id', 'name');
    $locsfeature = chado_select_record('feature', $columns, $select);

    // the source feature name and at least the fmin and fmax must be the same
    // for an update of the featureloc, otherwise we'll insert a new record.
    if (strcmp($locsfeature[0]->name, $landmark) == 0 and 
      ($featureloc->fmin == $fmin or $featureloc->fmax == $fmax)) {
      $match = array('featureloc_id' => $featureloc->featureloc_id);
      $values = array();
      $exists = 1;
      if ($featureloc->fmin != $fmin) {
        $values['fmin'] = $fmin;
      }
      if ($featureloc->fmax != $fmax) {
        $values['fmax'] = $fmax;
      }
      if ($featureloc->strand != $strand) {
        $values['strand'] = $strand;
      }
      if (count($values) > 0) {
        chado_update_record('featureloc', $match, $values);
      }
    }
    $rank = $featureloc->rank + 1;
  }
  if (!$exists) {

    // this feature location is new so add it
    if (strcmp($is_fmin_partial, 'f') == 0 or !$is_fmin_partial) {
      $is_fmin_partial = 'FALSE';
    }
    elseif (strcmp($is_fmin_partial, 't') == 0 or $is_fmin_partial = 1) {
      $is_fmin_partial = 'TRUE';
    }
    if (strcmp($is_fmax_partial, 'f') == 0 or !$is_fmax_partial) {
      $is_fmax_partial = 'FALSE';
    }
    elseif (strcmp($is_fmax_partial, 't') == 0 or $is_fmax_partial = 1) {
      $is_fmax_partial = 'TRUE';
    }
    $values = array(
      'feature_id' => $feature->feature_id,
      'srcfeature_id' => $srcfeature->feature_id,
      'fmin' => $fmin,
      'is_fmin_partial' => $is_fmin_partial,
      'fmax' => $fmax,
      'is_fmax_partial' => $is_fmax_partial,
      'strand' => $strand,
      'residue_info' => $residue_info,
      'locgroup' => $locgroup,
      'rank' => $rank
    );
    if ($phase) {
      $values['phase'] = $phase;
    }
    $success = chado_insert_record('featureloc', $values);
    if (!$success) {
      throw new Exception("Failed to insert featureloc.");
    }
  }
  return 1;
}