1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243 |
- <?php
- function tripal_autocomplete_feature($string = '') {
- $items = array();
- $sql = "
- SELECT
- F.feature_id, F.uniquename, F.name,
- O.genus, O,species,
- CVT.name as type
- FROM {feature} F
- INNER JOIN {organism} O ON O.organism_id = F.organism_id
- INNER JOIN {cvterm} CVT ON CVT.cvterm_id = F.type_id
- WHERE lower(F.uniquename) like lower(:str)
- ORDER by F.uniquename
- LIMIT 25 OFFSET 0
- ";
- $features = chado_query($sql, array(':str' => $string . '%'));
- while ($feature = $features->fetchObject()) {
- $key = "$feature->uniquename [id: $feature->feature_id]";
- $items[$key] = "$feature->uniquename ($feature->name, $feature->type, $feature->genus $feature->species)";
- }
- drupal_json_output($items);
- }
- function tripal_reverse_compliment_sequence($sequence) {
- $seq = strtoupper($sequence);
- $seq = strrev($seq);
- $seq = str_replace("A", "t", $seq);
- $seq = str_replace("T", "a", $seq);
- $seq = str_replace("G", "c", $seq);
- $seq = str_replace("C", "g", $seq);
- $seq = str_replace("Y", "r", $seq);
- $seq = str_replace("R", "y", $seq);
- $seq = str_replace("W", "w", $seq);
- $seq = str_replace("S", "s", $seq);
- $seq = str_replace("K", "m", $seq);
- $seq = str_replace("M", "k", $seq);
- $seq = str_replace("D", "h", $seq);
- $seq = str_replace("V", "b", $seq);
- $seq = str_replace("H", "d", $seq);
- $seq = str_replace("B", "v", $seq);
- return strtoupper($seq);
- }
- function tripal_get_feature_sequences($feature, $options) {
-
- $feature_id = array_key_exists('feature_id', $feature) ? $feature['feature_id'] : 0;
- $parent_id = array_key_exists('parent_id', $feature) ? $feature['parent_id'] : 0;
- $featureloc_id = array_key_exists('featureloc_id', $feature) ? $feature['featureloc_id'] : 0;
- $feature_name = array_key_exists('name', $feature) ? $feature['name'] : '';
-
- $num_bases_per_line = array_key_exists('width', $options) ? $options['width'] : 50;
- $derive_from_parent = array_key_exists('derive_from_parent', $options) ? $options['derive_from_parent'] : 0;
- $aggregate = array_key_exists('aggregate', $options) ? $options['aggregate'] : 0;
- $upstream = array_key_exists('upstream', $options) ? $options['upstream'] : 0;
- $downstream = array_key_exists('downstream', $options) ? $options['downstream'] : 0;
- $sub_features = array_key_exists('sub_feature_types', $options) ? $options['sub_feature_types'] : array();
- $relationship = array_key_exists('relationship_type', $options) ? $options['relationship_type'] : '';
- $rel_part = array_key_exists('relationship_part', $options) ? $options['relationship_part'] : '';
- $is_html = array_key_exists('is_html', $options) ? $options['is_html'] : 0;
- $is_txt = array_key_exists('is_txt', $options) ? $options['is_txt'] : 0;
- $is_raw = array_key_exists('is_raw', $options) ? $options['is_raw'] : 1;
- if (!$upstream) {
- $upstream = 0;
- }
- if (!$downstream) {
- $downstream = 0;
- }
-
- if (!is_array($sub_features)) {
- tripal_report_error('tripal_feature', TRIPAL_ERROR,
- "'sub_features' option must be an array for function tripal_get_feature_sequences().",
- array()
- );
- return array();
- }
-
-
-
- if ($rel_part == "object" or $rel_part == "subject") {
- if ($rel_part == "subject") {
- $sql = '
- SELECT FO.feature_id, FO.name, FO.uniquename, CVTO.name as feature_type, O.genus, O.species
- FROM {feature} FS
- INNER JOIN {feature_relationship} FR ON FR.subject_id = FS.feature_id
- INNER JOIN {cvterm} CVTFR ON CVTFR.cvterm_id = FR.type_id
- INNER JOIN {feature} FO ON FO.feature_id = FR.object_id
- INNER JOIN {cvterm} CVTO ON CVTO.cvterm_id = FO.type_id
- INNER JOIN {organism} O ON O.organism_id = FO.organism_id
- WHERE
- FS.feature_id = :feature_id AND
- CVTFR.name = :relationship
- ';
- $features = chado_query($sql, array(':feature_id' => $feature_id, ':relationship' => $relationship));
- }
- if ($rel_part == "object") {
- $sql = '
- SELECT FS.feature_id, FS.name, FS.uniquename, CVTO.name as feature_type, O.genus, O.species
- FROM {feature} FO
- INNER JOIN {feature_relationship} FR ON FR.object_id = FO.feature_id
- INNER JOIN {cvterm} CVTFR ON CVTFR.cvterm_id = FR.type_id
- INNER JOIN {feature} FS ON FS.feature_id = FR.subject_id
- INNER JOIN {cvterm} CVTO ON CVTO.cvterm_id = FS.type_id
- INNER JOIN {organism} O ON O.organism_id = FS.organism_id
- WHERE
- FO.feature_id = :feature_id AND
- CVTFR.name = :relationship
- ';
- $features = chado_query($sql, array(':feature_id' => $feature_id, ':relationship' => $relationship));
- }
- $sequences = '';
- while ($feature = $features->fetchObject()) {
-
- if ($rel_part == "subject") {
- $defline = "$feature_name, $relationship, $feature->uniquename $feature->feature_type ($feature->genus $feature->species)";
- }
- if ($rel_part == "object") {
- $defline = "$feature->uniquename $feature->feature_type ($feature->genus $feature->species), $relationship, $feature_name";
- }
- return tripal_get_feature_sequences(
- array(
- 'feature_id' => $feature->feature_id,
- 'name' => $defline,
- 'parent_id' => $parent_id,
- ),
- array(
- 'width' => $num_bases_per_line,
- 'derive_from_parent' => $derive_from_parent,
- 'aggregate' => $aggregate,
- 'upstream' => $upstream,
- 'downstream' => $downstream,
- 'sub_features' => $sub_features,
- )
- );
- }
- }
-
-
-
-
-
- $parent_sql ='
- SELECT featureloc_id, srcname, srcfeature_id, strand, srctypename, typename,
- fmin, fmax, upstream, downstream, adjfmin, adjfmax,
- substring(residues from (cast(adjfmin as int4) + 1) for cast((upstream + (fmax - fmin) + downstream) as int4)) as residues,
- genus, species
- FROM (
- SELECT
- FL.featureloc_id, OF.name srcname, FL.srcfeature_id, FL.strand,
- OCVT.name as srctypename, SCVT.name as typename,
- FL.fmin, FL.fmax, OO.genus, OO.species,
- CASE
- WHEN FL.strand >= 0 THEN
- CASE
- WHEN FL.fmin - :upstream <= 0 THEN 0
- ELSE FL.fmin - :upstream
- END
- WHEN FL.strand < 0 THEN
- CASE
- WHEN FL.fmin - :downstream <= 0 THEN 0
- ELSE FL.fmin - :downstream
- END
- END as adjfmin,
- CASE
- WHEN FL.strand >= 0 THEN
- CASE
- WHEN FL.fmax + :downstream > OF.seqlen THEN OF.seqlen
- ELSE FL.fmax + :downstream
- END
- WHEN FL.strand < 0 THEN
- CASE
- WHEN FL.fmax + :upstream > OF.seqlen THEN OF.seqlen
- ELSE FL.fmax + :upstream
- END
- END as adjfmax,
- CASE
- WHEN FL.strand >= 0 THEN
- CASE
- WHEN FL.fmin - :upstream <= 0 THEN FL.fmin
- ELSE :upstream
- END
- ELSE
- CASE
- WHEN FL.fmax + :upstream > OF.seqlen THEN OF.seqlen - FL.fmax
- ELSE :upstream
- END
- END as upstream,
- CASE
- WHEN FL.strand >= 0 THEN
- CASE
- WHEN FL.fmax + :downstream > OF.seqlen THEN OF.seqlen - FL.fmax
- ELSE :downstream
- END
- ELSE
- CASE
- WHEN FL.fmin - :downstream <= 0 THEN FL.fmin
- ELSE :downstream
- END
- END as downstream,
- OF.residues
- FROM {featureloc} FL
- INNER JOIN {feature} SF on FL.feature_id = SF.feature_id
- INNER JOIN {cvterm} SCVT on SF.type_id = SCVT.cvterm_id
- INNER JOIN {feature} OF on FL.srcfeature_id = OF.feature_id
- INNER JOIN {cvterm} OCVT on OF.type_id = OCVT.cvterm_id
- INNER JOIN {organism} OO on OF.organism_id = OO.organism_id
- WHERE SF.feature_id = :feature_id and NOT (OF.residues = \'\' or OF.residues IS NULL)) as tbl1
- ';
-
-
-
- $sfsql = '
- SELECT SF.feature_id, CVT.name as type_name, SF.type_id
- FROM {feature_relationship} FR
- INNER JOIN {feature} SF ON SF.feature_id = FR.subject_id
- INNER JOIN {cvterm} CVT ON CVT.cvterm_id = SF.type_id
- INNER JOIN {featureloc} FL ON FL.feature_id = FR.subject_id
- INNER JOIN {feature} PF ON PF.feature_id = FL.srcfeature_id
- WHERE FR.object_id = :feature_id and PF.feature_id = :srcfeature_id
- ORDER BY FL.fmin ASC
- ';
-
- $fsql ='
- SELECT count(*) as num_children
- FROM {feature_relationship} FR
- INNER JOIN {feature} SF ON SF.feature_id = FR.subject_id
- INNER JOIN {cvterm} CVT ON CVT.cvterm_id = SF.type_id
- INNER JOIN {featureloc} FL ON FL.feature_id = FR.subject_id
- INNER JOIN {feature} PF ON PF.feature_id = FL.srcfeature_id
- WHERE FR.object_id = :feature_id and PF.feature_id = :srcfeature_id
- ';
-
- $sequences = array();
-
- if ($derive_from_parent) {
-
- $parents = chado_query($parent_sql, array(':upstream' => $upstream, ':downstream' => $downstream, ':feature_id' => $feature_id));
- while ($parent = $parents->fetchObject()) {
-
-
- if ($parent_id and $parent_id != $parent->srcfeature_id) {
- continue;
- }
-
-
- if ($featureloc_id and $featureloc_id != $parent->featureloc_id) {
- continue;
- }
-
- $seq = '';
- $notes = '';
- $types = array();
-
-
- if ($aggregate) {
-
- $children = chado_query($sfsql, array(':feature_id' => $feature_id, ':srcfeature_id' => $parent->srcfeature_id));
- $num_children = chado_query($fsql, array(':feature_id' => $feature_id, ':srcfeature_id' => $parent->srcfeature_id))->fetchField();
-
-
- $i = 0;
- while ($child = $children->fetchObject()) {
-
-
-
- if (count($sub_features) > 0 and !in_array($child->type_name, $sub_features)) {
- $i++;
- continue;
- }
-
- if (!in_array($child->type_name, $types)) {
- $types[] = $child->type_name;
- }
-
-
- if ($i == 0 and $parent->strand >= 0) {
-
-
-
- $q = chado_query($parent_sql, array(':upstream' => $upstream, ':downstream' => 0, ':feature_id' => $child->feature_id));
- }
- elseif ($i == 0 and $parent->strand < 0) {
-
-
-
- $q = chado_query($parent_sql, array(':upstream' => 0, ':downstream' => $downstream, ':feature_id' => $child->feature_id));
- }
-
-
- elseif ($i == $num_children - 1 and $parent->strand >= 0) {
-
-
-
- $q = chado_query($parent_sql, array(':upstream' => 0, ':downstream' => $downstream, ':feature_id' => $child->feature_id));
- }
- elseif ($i == $num_children - 1 and $parent->strand < 0) {
-
-
-
- $q = chado_query($parent_sql, array(':upstream' => $upstream, ':downstream' => 0, ':feature_id' => $child->feature_id));
- }
-
- else {
- $q = chado_query($parent_sql, array(':upstream' => 0, ':downstream' => 0, ':feature_id' => $child->feature_id));
- }
- while ($subseq = $q->fetchObject()) {
-
- if ($subseq->srcfeature_id == $parent->srcfeature_id) {
- $seq .= $subseq->residues;
- }
- if ($subseq->upstream > 0 ) {
- $notes .= "Includes " . $subseq->upstream . " bases upstream. ";
- }
- if ($subseq->downstream > 0) {
- $notes .= "Includes " . $subseq->downstream . " bases downstream. ";
- }
- }
- $i++;
- }
- }
-
- else {
- $seq = $parent->residues;
- if ($parent->upstream > 0) {
- $notes .= "Includes " . $parent->upstream . " bases upstream. ";
- }
- if ($parent->downstream > 0) {
- $notes .= "Includes " . $parent->downstream . " bases downstream. ";
- }
- }
-
- $dir = 'forward';
- $length = strlen($seq);
- if ($parent->strand < 0) {
- $seq = tripal_reverse_compliment_sequence($seq);
- $dir = 'reverse';
- }
-
- if ($is_html) {
- $seq = wordwrap($seq, $num_bases_per_line, "<br>", TRUE);
- }
- if ($is_txt) {
- $seq = wordwrap($seq, $num_bases_per_line, "\r\n", TRUE);
- }
- if (!$seq) {
- $notes .= "No sequence available.";
- }
- if (count($types) > 0) {
- $notes .= "Excludes all bases but those of type(s): " . implode(', ', $types) . ". " ;
- }
-
-
-
- $featureloc = new stdClass;
- $featureloc->feature_id = $feature;
- $featureloc->fmin = $parent->adjfmin;
- $featureloc->fmax = $parent->adjfmax;
- $featureloc->strand = $parent->strand;
- $featureloc->srcfeature_id = new stdClass;
- $featureloc->srcfeature_id->name = $parent->srcname;
- $featureloc->srcfeature_id->type_id = $parent->srctypename;
- $featureloc->srcfeature_id->organism_id = new stdClass;
- $featureloc->srcfeature_id->organism_id->genus = $parent->genus;
- $featureloc->srcfeature_id->organism_id->species = $parent->species;
-
- $f = chado_generate_var('feature', array('feature_id' => $feature_id));
- $defline = tripal_get_fasta_defline($f, $notes, $featureloc, '', $length);
- $sequences[] = array(
- 'types' => $types,
- 'upstream' => $parent->upstream,
- 'downstream' => $parent->downstream,
- 'defline' => $defline,
- 'residues' => $seq,
- 'featureloc_id' => $parent->featureloc_id,
- 'length' => $length,
- );
- }
- }
-
-
- else {
- $f = chado_generate_var('feature', array('feature_id' => $feature_id));
- $f = chado_expand_var($f, 'field', 'feature.residues');
- $residues = $f->residues;
- $length = strlen($residues);
- if ($is_html) {
- $residues = wordwrap($residues, $num_bases_per_line, "<br>", TRUE);
- }
- else {
- $residues = wordwrap($residues, $num_bases_per_line, "\r\n", TRUE);
- }
-
- $defline = tripal_get_fasta_defline($f, '', NULL, '', $length);
-
- $sequences[] = array(
- 'types' => $f->type_id->name,
- 'upstream' => 0,
- 'downstream' => 0,
- 'defline' => $defline,
- 'residues' => $residues,
- 'length' => $length,
- );
- }
- return $sequences;
- }
- function tripal_get_bulk_feature_sequences($options) {
-
- $org_commonname = array_key_exists('org_commonname', $options) ? $options['org_commonname'] : '';
- $genus = array_key_exists('genus', $options) ? $options['genus'] : '';
- $species = array_key_exists('species', $options) ? $options['species'] : '';
- $analysis_name = array_key_exists('analysis_name', $options) ? $options['analysis_name'] : '';
- $type = array_key_exists('type', $options) ? $options['type'] : '';
- $feature_name = array_key_exists('feature_name', $options) ? $options['feature_name'] : '';
- $feature_uname = array_key_exists('feature_uname', $options) ? $options['feature_uname'] : '';
- $derive_from_parent = array_key_exists('derive_from_parent', $options) ? $options['derive_from_parent'] : 0;
- $aggregate = array_key_exists('aggregate', $options) ? $options['aggregate'] : 0;
- $sub_features = array_key_exists('sub_feature_types', $options) ? $options['sub_feature_types'] : array();
- $relationship = array_key_exists('relationship_type', $options) ? $options['relationship_type'] : '';
- $rel_part = array_key_exists('relationship_part', $options) ? $options['relationship_part'] : '';
- $num_bases_per_line = array_key_exists('width', $options) ? $options['width'] : 50;
- $upstream = array_key_exists('upstream', $options) ? $options['upstream'] : 0;
- $downstream = array_key_exists('downstream', $options) ? $options['downstream'] : 0;
- if (!$type and !$feature_name and !$genus) {
- print "Please provide a type, feature name or genus\n";
- return;
- }
-
- $vars = array();
- $sql = "
- SELECT DISTINCT F.feature_id, F.name, F.uniquename,
- O.genus, O.species, CVT.name as feature_type
- FROM {feature} F
- INNER JOIN {organism} O on O.organism_id = F.organism_id
- INNER JOIN {cvterm} CVT on CVT.cvterm_id = F.type_id
- ";
- if ($analysis_name) {
- $sql .= "
- INNER JOIN {analysisfeature} AF on AF.feature_id = F.feature_id
- INNER JOIN {analysis} A on AF.analysis_id = A.analysis_id
- ";
- }
- $sql .= "WHERE (1=1) ";
- if ($org_commonname) {
- $sql .= "AND O.common_name = :common_name ";
- $vars[':common_name'] = $org_commonname;
- }
- if ($genus) {
- $sql .= "AND O.genus = :genus ";
- $vars[':genus'] = $genus;
- }
- if ($species) {
- $sql .= "AND O.species = :species ";
- $vars[':species'] = $species;
- }
- if ($type) {
- $sql .= "AND CVT.name = :cvtname ";
- $vars[':cvtname'] = $type;
- }
- if ($feature_name) {
- if (is_array($feature_name)) {
- $sql .= "AND F.name IN (";
- foreach ($feature_name as $i => $fname) {
- $sql .= ":fname$i, ";
- $vars[":fname$i"] = $fname;
- }
-
- $sql = substr($sql, 0, -2) . ")";
- }
- else {
- $sql .= "AND F.name = :fname";
- $vars[':fname'] = $feature_name;
- }
- }
- if ($feature_uname) {
- if (is_array($feature_uname)) {
- $sql .= "AND F.uniquename IN (";
- foreach ($feature_uname as $i => $funame) {
- $sql .= ":funame$i, ";
- $vars[":funame$i"] = $funame;
- }
-
- $sql = substr($sql, 0, -2) . ")";
- }
- else {
- $sql .= "AND F.uniquename = :funame";
- $vars[':funame'] = $feature_uname;
- }
- }
- if ($analysis_name) {
- $sql .= "AND A.name = :aname";
- $vars[':aname'] = $analysis_name;
- }
- $num_bases_per_line = 50;
- $num_seqs = 0;
- $q = chado_query($sql, $vars);
- $sequences = array();
- while ($feature = $q->fetchObject()) {
-
- $seqs = tripal_get_feature_sequences(array('feature_id' => $feature->feature_id), $options);
- $sequences = array_merge($sequences, $seqs);
- $num_seqs++;
- }
- return $sequences;
- }
- function tripal_get_fasta_defline($feature, $notes = '', $featureloc = NULL, $type = '', $length = 0) {
-
- if ($featureloc) {
- if (!is_object($featureloc->srcfeature_id)) {
- $featureloc->srcfeature_id = chado_generate_var('feature', array('feature_id' => $featureloc->srcfeature_id));
- }
- if (!is_object($featureloc->srcfeature_id->organism_id)) {
- $featureloc->srcfeature_id->organism_id = chado_generate_var('organism', array('organism_id' => $featureloc->srcfeature_id->organism_id));
- }
- }
-
- if (!is_object($feature->organism_id)) {
- $feature->organism_id = chado_generate_var('organism', array('organism_id' => $feature->organism_id));
- }
-
- if (!$type) {
- $type = $feature->type_id->name;
- }
-
- $defline = $feature->uniquename . " " .
- 'ID=' . $feature->uniquename . "|" .
- 'Name=' . $feature->name . "|" .
- 'organism=' . $feature->organism_id->genus . " " . $feature->organism_id->species . "|" .
- 'type=' . $type . '|';
- if ($length > 0) {
- $defline .= "length=" . $length . "bp|";
- }
- if ($featureloc) {
- $defline .= "location=Sequence derived from alignment at " . tripal_get_location_string($featureloc);
- $defline .= " (" . $featureloc->srcfeature_id->organism_id->genus . " " . $featureloc->srcfeature_id->organism_id->species . ")|";
- }
- if ($notes) {
- $defline .= "Notes=$notes|";
- }
- $defline = substr($defline, 0, -1);
- return $defline;
- }
- function tripal_get_location_string($featureloc) {
- $feature = $featureloc->feature_id;
- $strand = '';
- if ($featureloc->strand == 1) {
- $strand = '+';
- }
- elseif ($featureloc->strand == -1) {
- $strand = '-';
- }
- return $featureloc->srcfeature_id->name . ":" . ($featureloc->fmin + 1) . ".." . $featureloc->fmax . $strand;
- }
- function tripal_get_feature_relationships($feature) {
-
- $options = array(
- 'return_array' => 1,
- 'order_by' => array('rank' => 'ASC'),
-
-
-
- 'include_fk' => array(
- 'type_id' => 1,
- 'object_id' => array(
- 'type_id' => 1,
- 'organism_id' => 1
- ),
- 'subject_id' => array(
- 'type_id' => 1,
- 'organism_id' => 1
- ),
- ),
- );
- $feature = chado_expand_var($feature, 'table', 'feature_relationship', $options);
-
- $srelationships = $feature->feature_relationship->subject_id;
- $orelationships = $feature->feature_relationship->object_id;
-
-
- $options = array(
- 'return_array' => 1,
- 'include_fk' => array(
- 'srcfeature_id' => 1,
- 'feature_id' => 1,
- ),
- );
- $feature = chado_expand_var($feature, 'table', 'featureloc', $options);
- $cfeaturelocs = $feature->featureloc->feature_id;
- if (!$cfeaturelocs) {
- $cfeaturelocs = array();
- }
- elseif (!is_array($cfeaturelocs)) {
- $cfeaturelocs = array($cfeaturelocs);
- }
-
-
- $flrels_sql = "
- SELECT
- FL.featureloc_id, F.name as srcfeature_name, FL.srcfeature_id,
- FL.feature_id, FL.fmin, FL.fmax, FL.strand, FL.phase
- FROM {featureloc} FL
- INNER JOIN {feature} F ON F.feature_id = FL.srcfeature_id
- WHERE FL.feature_id = :feature_id and FL.srcfeature_id = :srcfeature_id
- ";
-
- $relationships = array();
- $relationships['object'] = array();
- $relationships['subject'] = array();
-
- if ($orelationships) {
- foreach ($orelationships as $relationship) {
- $rel = new stdClass();
-
-
- $rel->child_featurelocs = array();
- foreach ($cfeaturelocs as $featureloc) {
- $res = chado_query($flrels_sql, array(':feature_id' => $relationship->subject_id->feature_id, ':srcfeature_id' => $featureloc->srcfeature_id->feature_id));
- while ($loc = $res->fetchObject()) {
-
- if (property_exists($featureloc->srcfeature_id, 'nid')) {
- $loc->nid = $featureloc->srcfeature_id->nid;
- }
- $rel->child_featurelocs[] = $loc;
- }
- }
- $rel->record = $relationship;
-
- $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
- $child_type = $relationship->subject_id->type_id->name;
-
- if (!array_key_exists($rel_type, $relationships['object'])) {
- $relationships['object'][$rel_type] = array();
- }
- if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
- $relationships['object'][$rel_type][$child_type] = array();
- }
- $relationships['object'][$rel_type][$child_type][] = $rel;
- }
- }
-
- if ($srelationships) {
- foreach ($srelationships as $relationship) {
- $rel = new stdClass();
-
- $rel->parent_featurelocs = array();
- foreach ($cfeaturelocs as $featureloc) {
- $res = chado_query($flrels_sql, array(':feature_id' => $relationship->object_id->feature_id, ':srcfeature_id' => $featureloc->srcfeature_id->feature_id));
- while ($loc = $res->fetchObject()) {
-
- if (property_exists($featureloc->srcfeature_id, 'nid')) {
- $loc->nid = $featureloc->srcfeature_id->nid;
- }
- $rel->parent_featurelocs[] = $loc;
- }
- }
- $rel->record = $relationship;
- $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
- $parent_type = $relationship->object_id->type_id->name;
- if (!array_key_exists($rel_type, $relationships['subject'])) {
- $relationships['subject'][$rel_type] = array();
- }
- if (!array_key_exists($parent_type, $relationships['subject'][$rel_type])) {
- $relationships['subject'][$rel_type][$parent_type] = array();
- }
- $relationships['subject'][$rel_type][$parent_type][] = $rel;
- }
- }
- return $relationships;
- }
- function chado_get_featureloc_sequences($feature_id, $featurelocs) {
-
- if (!$featurelocs) {
- return array();
- }
-
-
- $relationships = chado_get_feature_aggregate_relationships($feature_id);
- if (!$relationships) {
- return array();
- }
-
-
- foreach ($relationships as $rindex => $rel) {
-
- $rel_featurelocs = chado_get_featurelocs($rel->subject_id, 'as_child', 0);
- foreach ($rel_featurelocs as $rfindex => $rel_featureloc) {
-
- $src = $rel_featureloc->src_feature_id . "-" . $rel_featureloc->src_cvterm_id;
-
-
-
- $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;
-
- $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;
- }
- }
-
-
-
-
- $floc_sequences = array();
- foreach ($featurelocs as $featureloc) {
-
- $src = $featureloc->srcfeature_id->feature_id . "-" . $featureloc->srcfeature_id->type_id->cvterm_id;
-
- if (!empty($rel_locs[$src]['parts'])) {
- $parts = $rel_locs[$src]['parts'];
- $rparts = array();
- foreach ($parts as $start => $types) {
- foreach ($types as $type_name => $type) {
- if ($featureloc->strand >= 0) {
-
-
- $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 {
-
-
- $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;
- }
- }
- }
-
-
- if ($featureloc->strand >= 0) {
- usort($parts, 'chado_feature__residues_sort_rel_parts_by_start');
- }
- else {
- usort($rparts, 'chado_feature__residues_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;
-
-
-
-
- $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]['residues'] = $residues;
-
- }
- }
- return $floc_sequences;
- }
- function chado_get_aggregate_feature_relationships($feature_id, $substitute=1,
- $levels=0, $base_type_id=NULL, $depth=0) {
-
-
-
- if ($levels > 0 and $levels == $depth) {
- return NULL;
- }
-
- return chado_get_feature_relationships($feature_id, 'as_object');
- }
- function chado_get_feature_relationships($feature_id, $side = 'as_subject') {
-
-
- $sql = "
- SELECT
- FS.name as subject_name, FS.uniquename as subject_uniquename,
- CVTS.name as subject_type, CVTS.cvterm_id as subject_type_id,
- FR.subject_id, FR.type_id as relationship_type_id, FR.object_id, FR.rank,
- CVT.name as rel_type,
- FO.name as object_name, FO.uniquename as object_uniquename,
- CVTO.name as object_type, CVTO.cvterm_id as object_type_id
- FROM {feature_relationship} FR
- INNER JOIN {cvterm} CVT ON FR.type_id = CVT.cvterm_id
- INNER JOIN {feature} FS ON FS.feature_id = FR.subject_id
- INNER JOIN {feature} FO ON FO.feature_id = FR.object_id
- INNER JOIN {cvterm} CVTO ON FO.type_id = CVTO.cvterm_id
- INNER JOIN {cvterm} CVTS ON FS.type_id = CVTS.cvterm_id
- ";
- if (strcmp($side, 'as_object')==0) {
- $sql .= " WHERE FR.object_id = :feature_id";
- }
- if (strcmp($side, 'as_subject')==0) {
- $sql .= " WHERE FR.subject_id = :feature_id";
- }
- $sql .= " ORDER BY FR.rank";
-
- $results = chado_query($sql, array(':feature_id' => $feature_id));
-
-
- $i=0;
- $esql = "
- SELECT entity_id
- FROM {chado_entity}
- WHERE record_id = :feature_id";
- $relationships = array();
- while ($rel = $results->fetchObject()) {
- $entity = db_query($esql, array(':feature_id' => $rel->subject_id))->fetchObject();
- if ($entity) {
- $rel->subject_entity_id = $entity->entity_id;
- }
- $entity = db_query($esql, array(':feature_id' => $rel->object_id))->fetchObject();
- if ($entity) {
- $rel->object_entity_id = $entity->entity_id;
- }
- $relationships[$i++] = $rel;
- }
- return $relationships;
- }
- function chado_get_featurelocs($feature_id, $side = 'as_parent', $aggregate = 1) {
- $sql = "
- SELECT
- F.name, F.feature_id, F.uniquename,
- FS.name as src_name, FS.feature_id as src_feature_id, FS.uniquename as src_uniquename,
- CVT.name as cvname, CVT.cvterm_id,
- CVTS.name as src_cvname, CVTS.cvterm_id as src_cvterm_id,
- FL.fmin, FL.fmax, FL.is_fmin_partial, FL.is_fmax_partial,FL.strand, FL.phase
- FROM {featureloc} FL
- INNER JOIN {feature} F ON FL.feature_id = F.feature_id
- INNER JOIN {feature} FS ON FS.feature_id = FL.srcfeature_id
- INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
- INNER JOIN {cvterm} CVTS ON FS.type_id = CVTS.cvterm_id
- ";
- if (strcmp($side, 'as_parent')==0) {
- $sql .= "WHERE FL.srcfeature_id = :feature_id ";
- }
- if (strcmp($side, 'as_child')==0) {
- $sql .= "WHERE FL.feature_id = :feature_id ";
- }
- $flresults = chado_query($sql, array(':feature_id' => $feature_id));
-
- $i=0;
- $featurelocs = array();
- while ($loc = $flresults->fetchObject()) {
-
-
- $loc->feid = tripal_get_chado_entity_id('feature', $loc->feature_id);
- $loc->seid = tripal_get_chado_entity_id('feature', $loc->src_feature_id);
-
- $featurelocs[$i++] = $loc;
- }
-
- if ($aggregate and strcmp($side, 'as_parent')==0) {
-
-
- $relationships = tripal_feature_get_aggregate_relationships($feature_id, 0);
- foreach ($relationships as $rindex => $rel) {
-
- $rel_featurelocs = tripal_feature_load_featurelocs($rel->subject_id, 'as_child', 0);
- foreach ($rel_featurelocs as $findex => $rfloc) {
- $featurelocs[$i++] = $rfloc;
- }
- }
- }
- usort($featurelocs, 'chado_feature__residues_sort_locations');
- return $featurelocs;
- }
- function chado_feature__residues_sort_rel_parts_by_start($a, $b) {
- foreach ($a as $type_name => $details) {
- $astart = $a[$type_name]['start'];
- break;
- }
- foreach ($b as $type_name => $details) {
- $bstart = $b[$type_name]['start'];
- break;
- }
- return strnatcmp($astart, $bstart);
- }
- function chado_feature__residues_sort_locations($a, $b) {
- return strnatcmp($a->fmin, $b->fmin);
- }
|