function tripal_feature_load_featureloc_sequences

2.x tripal_feature.theme.inc tripal_feature_load_featureloc_sequences($feature_id, $featurelocs)
3.x tripal_feature.theme.inc tripal_feature_load_featureloc_sequences($feature_id, $featurelocs)
1.x tripal_feature.module tripal_feature_load_featureloc_sequences($feature_id, $featurelocs)

Get the sequence this feature is located on

Parameters

$feature_id:

$featurelocs:

1 call to tripal_feature_load_featureloc_sequences()

File

legacy/tripal_feature/theme/tripal_feature.theme.inc, line 47

Code

function tripal_feature_load_featureloc_sequences($feature_id, $featurelocs) {

  // if we don't have any featurelocs then no point in continuing
  if (!$featurelocs) {
    return array();
  }

  // get the list of relationships (including any aggregators) and iterate
  // through each one to find information needed to color-code the reference sequence
  $relationships = tripal_feature_get_aggregate_relationships($feature_id);
  if (!$relationships) {
    return array();
  }


  // iterate through each of the realtionships features and get their
  // locations
  foreach ($relationships as $rindex => $rel) {
    // get the featurelocs for each of the relationship features
    $rel_featurelocs = tripal_feature_load_featurelocs($rel->subject_id, 'as_child', 0);
    foreach ($rel_featurelocs as $rfindex => $rel_featureloc) {
      // keep track of this unique source feature
      $src = $rel_featureloc->src_feature_id . "-" . $rel_featureloc->src_cvterm_id;

      // copy over the results to the relationship object.  Since there can
      // be more than one feature location for each relationship feature we
      // use the '$src' variable to keep track of these.
      $rel->featurelocs = new stdClass();
      $rel->featurelocs->$src = new stdClass();
      $rel->featurelocs->$src->src_uniquename = $rel_featureloc->src_uniquename;
      $rel->featurelocs->$src->src_cvterm_id = $rel_featureloc->src_cvterm_id;
      $rel->featurelocs->$src->src_cvname = $rel_featureloc->src_cvname;
      $rel->featurelocs->$src->fmin = $rel_featureloc->fmin;
      $rel->featurelocs->$src->fmax = $rel_featureloc->fmax;
      $rel->featurelocs->$src->src_name = $rel_featureloc->src_name;

      // keep track of the individual parts for each relationship
      $start = $rel->featurelocs->$src->fmin;
      $end = $rel->featurelocs->$src->fmax;
      $type = $rel->subject_type;
      $rel_locs[$src]['parts'][$start][$type]['start'] = $start;
      $rel_locs[$src]['parts'][$start][$type]['end'] = $end;
      $rel_locs[$src]['parts'][$start][$type]['type'] = $type;
    }
  }

  // the featurelocs array provided to the function contains the locations
  // where this feature is found.   We want to get the sequence for each
  // location and then annotate it with the parts found from the relationships
  // locations determiend above.
  $floc_sequences = array();
  foreach ($featurelocs as $featureloc) {

    // build the src name so we can keep track of the different parts for each feature
    $src = $featureloc->srcfeature_id->feature_id . "-" . $featureloc->srcfeature_id->type_id->cvterm_id;

    // orient the parts to the beginning of the feature sequence
    if (!empty($rel_locs[$src]['parts'])) {
      $parts = $rel_locs[$src]['parts'];
      $rparts = array(); // we will fill this up if we're on the reverse strand

      foreach ($parts as $start => $types) {
        foreach ($types as $type_name => $type) {
          if ($featureloc->strand >= 0) {
            // this is on the forward strand.  We need to convert the start on the src feature to the
            // start on this feature's sequence
            $parts[$start][$type_name]['start'] = $parts[$start][$type_name]['start'] - $featureloc->fmin;
            $parts[$start][$type_name]['end'] = $parts[$start][$type_name]['end'] - $featureloc->fmin;
            $parts[$start][$type_name]['type'] = $type_name;
          }
          else {
            // this is on the reverse strand.  We need to swap the start and stop and calculate from the
            // begining of the reverse sequence
            $size = ($featureloc->fmax - $featureloc->fmin);
            $start_orig = $parts[$start][$type_name]['start'];
            $end_orig = $parts[$start][$type_name]['end'];
            $new_start = $size - ($end_orig - $featureloc->fmin);
            $new_end = $size - ($start_orig - $featureloc->fmin);

            $rparts[$new_start][$type_name]['start'] = $new_start;
            $rparts[$new_start][$type_name]['end'] = $new_end;
            $rparts[$new_start][$type_name]['type'] = $type_name;
          }
        }
      }

      // now sort the parts
      // if we're on the reverse strand we need to resort
      if ($featureloc->strand >= 0) {
        usort($parts, 'tripal_feature_sort_rel_parts_by_start');
      }
      else {
        usort($rparts, 'tripal_feature_sort_rel_parts_by_start');
        $parts = $rparts;
      }

      $floc_sequences[$src]['id'] = $src;
      $floc_sequences[$src]['type'] = $featureloc->feature_id->type_id->name;
      $args = array(':feature_id' => $featureloc->srcfeature_id->feature_id);
      $start = $featureloc->fmin + 1;
      $size = $featureloc->fmax - $featureloc->fmin;

      // TODO: fix the hard coded $start and $size
      // the $start and $size variables are hard-coded in the SQL statement
      // because the db_query function places quotes around all placeholders
      // (e.g. :start & :size) and screws up the substring function
      $sql = "
        SELECT substring(residues from $start for $size) as residues
        FROM {feature}
        WHERE feature_id = :feature_id
      ";
      $sequence = chado_query($sql, $args)->fetchObject();
      $residues = $sequence->residues;
      if ($featureloc->strand < 0) {
        $residues = tripal_reverse_compliment_sequence($residues);
      }
      $strand = '.';
      if ($featureloc->strand == 1) {
        $strand = '+';
      }
      elseif ($featureloc->strand == -1) {
        $strand = '-';
      }
      $floc_sequences[$src]['location'] = tripal_get_location_string($featureloc);
      $floc_sequences[$src]['defline'] = tripal_get_fasta_defline($featureloc->feature_id, '', $featureloc, '', strlen($residues));
      $floc_sequences[$src]['featureloc'] = $featureloc;
      $floc_sequences[$src]['formatted_seq'] = tripal_feature_color_sequence($residues, $parts, $floc_sequences[$src]['defline']);
    }
  }
  return $floc_sequences;
}