function tripal_feature_load_gff3_featureloc

2.x tripal_feature.gff_loader.inc tripal_feature_load_gff3_featureloc($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)
1.x gff_loader.inc tripal_feature_load_gff3_featureloc($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)

Related topics

2 calls to tripal_feature_load_gff3_featureloc()
tripal_feature_load_gff3 in tripal_feature/includes/gff_loader.inc
tripal_feature_load_gff3_target in tripal_feature/includes/gff_loader.inc

File

tripal_feature/includes/gff_loader.inc, line 1550
@todo Add file header description

Code

function tripal_feature_load_gff3_featureloc($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,
  );
  $options = array('statement_name' => 'sel_feature_orun');
  if ($landmark_type_id) {
    $select['type_id'] = $landmark_type_id;
    $options = array('statement_name' => 'sel_feature_orunty');
  }
  $results = tripal_core_chado_select('feature', array('feature_id'), $select, $options);

  $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,
    );
    $options = array('statement_name' => 'sel_feature_orna');
    if ($landmark_type_id) {
      $select['type_id'] = $landmark_type_id;
      $options = array('statement_name' => 'sel_feature_ornaty');
    }
    $results = tripal_core_chado_select('feature', array('feature_id'), $select, $options);
    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);
        $options = array('statement_name' => 'sel_feature_un');
        $results = tripal_core_chado_select('feature', array('feature_id'), $select, $options);
        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
          );
          $options = array('statement_name' => 'ins_feature_ornaunty');
          $results = tripal_core_chado_insert('feature', $values, $options);
          if (!$results) {
            watchdog("T_gff3_loader", "Cannot find landmark feature: '%landmark', nor could it be inserted", 
            array('%landmark' => $landmark), WATCHDOG_WARNING);
            return 0;
          }
          $srcfeature = new stdClass();
          $srcfeature->feature_id = $results['feature_id'];
        }
        else {
          watchdog("T_gff3_loader", "Cannot find unique landmark feature: '%landmark'.", 
          array('%landmark' => $landmark), WATCHDOG_WARNING);
          return 0;
        }
      }
    }
    elseif (count($results) > 1) {
      watchdog("T_gff3_loader", "multiple landmarks exist with the name: '%landmark'.  Cannot 
         resolve which one to use. Cannot add the feature location record", 
      array('%landmark' => $landmark), WATCHDOG_WARNING);
      return 0;
    }
    else {
      $srcfeature = $results[0];
    }
  }
  elseif (count($results) > 1) {
    watchdog("T_gff3_loader", "multiple landmarks exist with the name: '%landmark'.  Cannot 
      resolve which one to use. Cannot add the feature location record", 
    array('%landmark' => $landmark), WATCHDOG_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(
    'statement_name' => 'sel_featureloc_fe',
    'order_by' => array(
      'rank' => 'ASC'
    ),
  );
  $locrecs = tripal_core_chado_select('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);
    $options = array('statement_name' => 'sel_feature_fe');
    $columns = array('feature_id', 'name');
    $locsfeature = tripal_core_chado_select('feature', $columns, $select, $options);

    // 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) {
        $options = array('statement_name' => 'upd_featureloc_all');
        tripal_core_chado_update('featureloc', $match, $values, $options);
      }
    }
    $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
    );
    $options = array('statement_name' => 'ins_featureloc_all');
    if ($phase) {
      $values['phase'] = $phase;
      $options = array('statement_name' => 'ins_featureloc_allphase');
    }
    $success = tripal_core_chado_insert('featureloc', $values, $options);
    if (!$success) {
      watchdog("T_gff3_loader", "Failed to insert featureloc", array(), WATCHDOG_WARNING);
      exit;
      return 0;
    }
  }
  return 1;
}