12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115 |
- <?php
- function tripal_feature_analysis_get_property($analysis_id = NULL, $feature_id = NUll,
- $analysisfeature_id = NULL, $property, $cv_name = 'tripal') {
-
- if (($analysis_id and !$feature_id) or
- (!$analysis_id and $feature_id)) {
- watchdog('tripal_feature',
- 'tripal_feature_analysis_get_property: Both an analysis ID and feature ID should be specified',
- array(), WATCHDOG_WARNING);
- }
-
- if (!$analysisfeature_id) {
- $columns = array('analysisfeature_id');
- $values = array('analysis_id' => $analysis_id, 'feature_id' => $feature_id);
- $result = tripal_core_chado_select('analysisfeature', $columns, $values);
- $analysisfeature_id = $result[0]->analysisfeature_id;
- }
-
- return tripal_core_get_property('analysisfeature', $analysisfeature_id, $property, $cv_name);
- }
- function tripal_feature_analysis_insert_property($analysis_id = NULL, $feature_id = NUll,
- $analysisfeature_id = NULL, $property, $value, $update_if_present = 0, $cv_name = 'tripal') {
-
- if (($analysis_id and !$feature_id) or
- (!$analysis_id and $feature_id)) {
- watchdog('tripal_feature',
- 'tripal_feature_analysis_insert_property: Both an analysis ID and feature ID should be specified',
- array(), WATCHDOG_WARNING);
- }
-
- if (!$analysisfeature_id) {
- $columns = array('analysisfeature_id');
- $values = array('analysis_id' => $analysis_id, 'feature_id' => $feature_id);
- $result = tripal_core_chado_select('analysisfeature', $columns, $values);
- $analysisfeature_id = $result[0]->analysisfeature_id;
- }
-
- $success = tripal_core_insert_property('analysisfeature', $analysisfeature_id,
- $property, $cv_name, $value, $update_if_present);
- if (!$success) {
- watchdog('tripal_feature',
- 'tripal_feature_analysis_insert_property: Failed to insert analysis feature property',
- array(), WATCHDOG_WARNING);
- return FALSE;
- }
- return $success;
- }
- function tripal_feature_analysis_update_property($analysis_id = NULL, $feature_id = NUll,
- $analysisfeature_id = NULL, $property, $value, $insert_if_missing = 0,
- $cv_name = 'tripal') {
-
- if (($analysis_id and !$feature_id) or
- (!$analysis_id and $feature_id)) {
- watchdog('tripal_feature',
- 'tripal_feature_analysis_update_property: Both an analysis ID and feature ID should be specified',
- array(), WATCHDOG_WARNING);
- }
-
- if (!$analysisfeature_id) {
- $columns = array('analysisfeature_id');
- $values = array('analysis_id' => $analysis_id, 'feature_id' => $feature_id);
- $result = tripal_core_chado_select('analysisfeature', $columns, $values);
- $analysisfeature_id = $result[0]->analysisfeature_id;
- }
-
- return tripal_core_update_property('analysisfeature', $analysisfeature_id, $property, $cv_name, $value, $insert_if_missing);
- }
- function tripal_feature_analysis_update_property_by_id($analysisfeatureprop_id,
- $property, $value, $cv_name = 'tripal') {
-
- return tripal_core_update_property_by_id('analysisfeature',
- $analysisfeatureprop_id, $property, $cv_name, $value);
- }
- function tripal_feature_analysis_delete_property($analysis_id = NULL, $feature_id = NUll,
- $analysisfeature_id = NULL, $property, $cv_name = 'tripal') {
-
- if (($analysis_id and !$feature_id) or
- (!$analysis_id and $feature_id)) {
- watchdog('tripal_feature',
- 'tripal_feature_analysis_delete_property: Both an analysis ID and feature ID should be specified',
- array(), WATCHDOG_WARNING);
- }
-
- if (!$analysisfeature_id) {
- $columns = array('analysisfeature_id');
- $values = array('analysis_id' => $analysis_id, 'feature_id' => $feature_id);
- $result = tripal_core_chado_select('analysisfeature', $columns, $values);
- $analysisfeature_id = $result[0]->analysisfeature_id;
- }
-
- return tripal_core_delete_property('analysisfeature', $analysisfeature_id, $property, $cv_name);
- }
- function tripal_feature_analysis_delete_property_by_id($analysisfeatureprop_id) {
-
- return tripal_core_delete_property_by_id('analysisfeature', $analysisfeatureprop_id);
- }
- function tripal_feature_get_property($feature_id, $property, $cv_name='tripal') {
- return tripal_core_get_property('feature', $feature_id, $property, $cv_name);
- }
- function tripal_feature_insert_property($feature_id, $property, $value,
- $update_if_present = 0, $cv_name = 'tripal') {
- return tripal_core_insert_property('feature', $feature_id, $property,
- $cv_name, $value, $update_if_present);
- }
- function tripal_feature_update_property($feature_id, $property,
- $value, $insert_if_missing = 0, $cv_name = 'tripal') {
- return tripal_core_update_property('feature', $feature_id, $property, $cv_name, $value, $insert_if_missing);
- }
- function tripal_feature_update_property_by_id($featureprop_id, $property,
- $value, $cv_name = 'tripal') {
- return tripal_core_update_property_by_id('feature', $featureprop_id, $property, $cv_name, $value);
- }
- function tripal_feature_delete_property($feature_id, $property, $cv_name='tripal') {
- return tripal_core_delete_property('feature', $feature_id, $property, $cv_name);
- }
- function tripal_feature_delete_property_by_id($featureprop_id) {
- return tripal_core_delete_property_by_id('feature', $featureprop_id);
- }
- function tripal_feature_reverse_complement($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_feature_get_formatted_sequence($feature_id, $feature_name,
- $num_bases_per_line, $derive_from_parent, $aggregate, $output_format,
- $upstream, $downstream, $sub_features = array(), $relationship = '', $rel_part = '') {
-
-
- $connection = tripal_db_persistent_chado();
-
- if (!$upstream) {
- $upstream = 0;
- }
- if (!$downstream) {
- $downstream = 0;
- }
-
- if ($rel_part == "object" or $rel_part == "subject") {
- if ($rel_part == "subject") {
- $psql = '
- PREPARE feature_rel_get_object (int, text) AS
- 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 = $1 AND
- CVTFR.name = $2
- ';
- $status = tripal_core_chado_prepare('feature_rel_get_object', $psql, array('int', 'text'));
- if (!$status) {
- watchdog('tripal_feature', "init: not able to prepare SQL statement '%name'",
- array('%name' => 'feature_by_subject'), 'WATCHDOG ERROR');
- }
- $sql = "EXECUTE feature_rel_get_object(:feature_id, :relationship)";
- $features = chado_query($sql, array(':feature_id' => $feature_id, ':relationship' => $relationship));
- }
- if ($rel_part == "object") {
- $psql = '
- PREPARE feature_rel_get_subject (int, text) AS
- 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 = $1 AND
- CVTFR.name = $2
- ';
- $status = tripal_core_chado_prepare('feature_rel_get_subject', $psql, array('int', 'text'));
- if (!$status) {
- watchdog('tripal_feature', "init: not able to prepare SQL statement '%name'",
- array('%name' => 'feature_by_object'), 'WATCHDOG ERROR');
- }
- $sql = "EXECUTE feature_rel_get_subject(:feature_id, :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";
- }
- $sequences .= tripal_feature_get_formatted_sequence($feature->feature_id, $defline,
- $num_bases_per_line, $derive_from_parent, $aggregate, $output_format,
- $upstream, $downstream, $sub_features, '', '');
- }
- return $sequences;
- }
-
-
- if (!tripal_core_is_sql_prepared('sequence_by_parent')) {
-
-
-
-
-
- $psql ='
- PREPARE sequence_by_parent (int, int, int) AS
- SELECT srcname, srcfeature_id, strand, srctypename, typename,
- fmin, fmax, upstream, downstream, adjfmin, adjfmax,
- substring(residues from (adjfmin + 1) for (upstream + (fmax - fmin) + downstream)) as residues,
- genus, species
- FROM (
- SELECT
- 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 - $1 <= 0 THEN 0
- ELSE FL.fmin - $1
- END
- WHEN FL.strand < 0 THEN
- CASE
- WHEN FL.fmin - $2 <= 0 THEN 0
- ELSE FL.fmin - $2
- END
- END as adjfmin,
- CASE
- WHEN FL.strand >= 0 THEN
- CASE
- WHEN FL.fmax + $2 > OF.seqlen THEN OF.seqlen
- ELSE FL.fmax + $2
- END
- WHEN FL.strand < 0 THEN
- CASE
- WHEN FL.fmax + $1 > OF.seqlen THEN OF.seqlen
- ELSE FL.fmax + $1
- END
- END as adjfmax,
- CASE
- WHEN FL.strand >= 0 THEN
- CASE
- WHEN FL.fmin - $1 <= 0 THEN FL.fmin
- ELSE $1
- END
- ELSE
- CASE
- WHEN FL.fmax + $1 > OF.seqlen THEN OF.seqlen - FL.fmax
- ELSE $1
- END
- END as upstream,
- CASE
- WHEN FL.strand >= 0 THEN
- CASE
- WHEN FL.fmax + $2 > OF.seqlen THEN OF.seqlen - FL.fmax
- ELSE $2
- END
- ELSE
- CASE
- WHEN FL.fmin - $2 <= 0 THEN FL.fmin
- ELSE $2
- 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 = $3 and NOT (OF.residues = \'\' or OF.residues IS NULL)) as tbl1
- ';
- $status = tripal_core_chado_prepare('sequence_by_parent', $psql, array('int', 'int', 'int'));
- if (!$status) {
- watchdog('tripal_feature',
- "init: not able to prepare SQL statement '%name'",
- array('%name' => 'sequence_by_parent'), 'WATCHDOG ERROR');
- }
-
-
-
-
- $psql ='PREPARE sub_features (int, int) AS
- 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 = $1 and PF.feature_id = $2
- ORDER BY FL.fmin ASC';
- $status = tripal_core_chado_prepare('sub_features', $psql, array('int', 'int'));
- if (!$status) {
- watchdog('tripal_views_handler_field_sequence',
- "init: not able to prepare SQL statement '%name'",
- array('%name' => 'ssub_features'), 'WATCHDOG ERROR');
- }
- $psql ='PREPARE count_sub_features (int, int) AS
- 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 = $1 and PF.feature_id = $2';
- $status = tripal_core_chado_prepare('count_sub_features', $psql, array('int', 'int'));
- if (!$status) {
- watchdog('tripal_views_handler_field_sequence',
- "init: not able to prepare SQL statement '%name'",
- array('%name' => 'count_sub_features'), 'WATCHDOG ERROR');
- }
- }
-
-
- if ($derive_from_parent) {
-
-
- $sql = "EXECUTE sequence_by_parent (:upstream, :downstream, :feature_id)";
- $parents = chado_query($sql, array(':uptream' => $upstream, ':downstream' => $downstream, ':feature_id' => $feature_id));
- while ($parent = $parents->fetchObject()) {
- $seq = '';
-
-
- if ($aggregate) {
-
-
- $sql = "EXECUTE sub_features (:feature_id, :srcfeature_id)";
- $children = chado_query($sql, array(':feature_id' => $feature_id, ':srcfeature_id' => $parent->srcfeature_id));
- $sql = "EXECUTE count_sub_features (:feature_id, :srcfeature_id)";
- $sub_features = chado_query($sql, array(':feature_id' => $feature_id, ':srcfeature_id' => $parent->srcfeature_id));
- $num_children = $sub_features->fetchObject();
-
-
-
- $types = array();
- $i = 0;
- while ($child = $children->fetchObject()) {
-
-
-
- if (count($sub_features) > 0 and !in_array($child->type_name, $sub_features)) {
- continue;
- }
-
-
- if (!in_array($child->type_name, $types)) {
- $types[] = $child->type_name;
- }
-
- $sql = "EXECUTE sequence_by_parent (:upstream, %d, :feature_id)";
-
-
- if ($i == 0 and $parent->strand >= 0) {
-
-
-
- $q = chado_query($sql, array(':upstream' => $upstream, ':downstream' => 0, ':feature_id' => $child->feature_id));
- }
- elseif ($i == 0 and $parent->strand < 0) {
-
-
-
- $q = chado_query($sql, array(':upstream' => 0, ':downstream' => $downstream, ':feature_id' => $child->feature_id));
- }
-
-
-
- if ($i == $num_children->num_children - 1 and $parent->strand >= 0) {
-
-
-
- $q = chado_query($sql, array(':upstream' => 0, ':downstream' => $downstream, ':feature_id' => $child->feature_id));
- }
- elseif ($i == $num_children->num_children - 1 and $parent->strand < 0) {
-
-
-
- $q = chado_query($sql, array(':upstream' => $upstream, ':downstream' => 0, ':feature_id' => $child->feature_id));
- }
-
-
- else {
- $sql = "EXECUTE sequence_by_parent (%d, %d, %d)";
- $q = chado_query($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;
- }
- }
- $i++;
- }
- }
-
- else {
- $seq = $parent->residues;
- }
-
-
- $dir = 'forward';
- if ($parent->strand < 0) {
- $seq = tripal_feature_reverse_complement($seq);
- $dir = 'reverse';
- }
-
-
- if ($output_format == 'fasta_html') {
- $seq = wordwrap($seq, $num_bases_per_line, "<br>", TRUE);
- }
- elseif ($output_format == 'fasta_txt') {
- $seq = wordwrap($seq, $num_bases_per_line, "\r\n", TRUE);
- }
- $residues .= ">$feature_name. Sequence derived from feature of type, '$parent->srctypename', of $parent->genus $parent->species: $parent->srcname:" . ($parent->adjfmin + 1) . ".." . $parent->adjfmax . " ($dir). ";
- if (count($types) > 0) {
- $residues .= "Excludes all bases but those of type(s): " . implode(', ', $types) . ". " ;
- }
- if ($parent->upstream > 0) {
- $residues .= "Includes " . $parent->upstream . " bases upstream. ";
- }
- if ($parent->downstream > 0) {
- $residues .= "Includes " . $parent->downstream . " bases downstream. ";
- }
- if (!$seq) {
-
- if ($output_format == 'fasta_html') {
- $residues .= "No sequence available.</br>";
- }
- else {
- $residues .= "No sequence available.\r\n";
- }
- }
- else {
- if ($output_format == 'fasta_html') {
- $residues .= "<br>";
- }
- $residues .= "\r\n" . $seq . "\r\n";
- if ($output_format == 'fasta_html') {
- $residues .= "<br>";
- }
- }
- }
- }
-
-
- else {
- $sql = "SELECT * FROM {feature} F WHERE feature_id = :feature_id";
- $values = chado_query($sql, array(':feature_id' => $feature_id))->fetchObject();
- $residues = $values->residues;
- if ($output_format == 'fasta_html') {
- $residues = wordwrap($residues, $num_bases_per_line, "<br>", TRUE);
- }
- elseif ($output_format == 'fasta_txt') {
- $residues = wordwrap($residues, $num_bases_per_line, "\r\n", TRUE);
- }
- $residues = ">$feature_name\r\n$residues\r\n";
- }
-
-
- if ($residues and $num_bases_per_line) {
- if ($output_format == 'fasta_html') {
- $residues = '<span style="font-family: monospace;">' . $residues . '</span>';
- }
- }
- return $residues;
- }
- function tripal_feature_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 = tripal_core_expand_chado_vars($feature, 'table', 'feature_relationship', $options);
-
-
- $srelationships = $feature->feature_relationship->subject_id;
- $orelationships = $feature->feature_relationship->object_id;
-
-
-
-
- $options = array(
- 'include_fk' => array(
- 'srcfeature_id' => 1,
- 'feature_id' => 1,
- ),
- );
- $feature = tripal_core_expand_chado_vars($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;
-
-
- $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
- $n = db_query($sql, array(':feature_id' => $relationship->subject_id->feature_id))->fetchObject();
- if ($n) {
- $rel->record->nid = $n->nid;
- }
-
- 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;
-
-
- $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
- $n = db_query($sql, array(':feature_id' => $relationship->object_id->feature_id))->fetchObject();
- if ($n) {
- $rel->record->nid = $n->nid;
- }
-
- if (!array_key_exists($rel_type, $relationships['subject'])) {
- $relationships['subject'][$rel_type] = array();
- }
- if (!array_key_exists($child_type, $relationships['subject'][$rel_type])) {
- $relationships['subject'][$rel_type][$parent_type] = array();
- }
- $relationships['subject'][$rel_type][$parent_type][] = $rel;
- }
- }
- return $relationships;
- }
- function tripal_feature_add_dbxref($feature_id, $dbname, $accession) {
-
-
- $values = array('name' => $dbname);
- $options = array('statement_name' => 'sel_db_na');
- $db = tripal_core_chado_select('db', array('db_id'), $values, $options);
- if (!$db or count($db) == 0) {
- $options = array('statement_name' => 'ins_db_na');
- $success = tripal_core_chado_insert('db', $values, $options);
- if (!$success) {
- watchdog('tripal_feature', 'tripal_feature_add_dbxref: The feature dbxref entry for feature, %feature_id, " .
- "could not be added because the database, %dbname, does not exist and cannot be added.',
- array('%feature_id' => $feature_id, '%dbname' => $dbname), WATCHDOG_WARNING);
- return FALSE;
- }
- }
-
-
- $values = array(
- 'dbxref_id' => array(
- 'accession' => $accession,
- 'db_id' => array(
- 'name' => $dbname
- ),
- ),
- 'feature_id' => $feature_id,
- );
- $options = array('statement_name' => 'sel_featuredbxref_dbfe');
- $xref = tripal_core_chado_select('feature_dbxref', array('feature_dbxref_id'), $values, $options);
- if (count($xref) == 0) {
-
- $options = array('statement_name' => 'ins_featuredbxref_dbfe');
- $success = tripal_core_chado_insert('feature_dbxref', $values, $options);
- if (!$success) {
- watchdog('tripal_feature', 'tripal_feature_add_dbxref: The feature dbxref entry for feature, %feature_id, ' .
- 'could not be added: %db:%accession.', array('%feature_id' => $feature_id, '%db' => $dbname,
- '%accession' => $accession), WATCHDOG_WARNING);
- return FALSE;
- }
- }
- return TRUE;
- }
- function tripal_feature_add_cvterm($feature_id, $cvname, $cvterm) {
-
-
- $values = array('name' => $cvname);
- $options = array('statement_name' => 'sel_cv_na');
- $cv = tripal_core_chado_select('cv', array('cv_id'), $values, $options);
- if (!$cv or count($cv) == 0) {
- $options = array('statement_name' => 'ins_cv_na');
- $success = tripal_core_chado_insert('cv', $values, $options);
- if (!$success) {
- watchdog('tripal_feature', 'tripal_feature_add_cvterm: The feature cvterm entry for feature, %feature_id, " .
- "could not be added because the CV, %cvname, does not exist and cannot be added.',
- array('%feature_id' => $feature_id, '%cvname' => $cvname), WATCHDOG_WARNING);
- return FALSE;
- }
- }
-
-
- $values = array(
- 'cvterm_id' => array(
- 'name' => $cvterm,
- 'cv_id' => array(
- 'name' => $cvname
- ),
- ),
- 'feature_id' => $feature_id,
- 'pub_id' => 1,
- );
- $options = array('statement_name' => 'sel_featuredcvterm_cvfepu');
- $xref = tripal_core_chado_select('feature_cvterm', array('feature_cvterm_id'), $values, $options);
- if (count($xref) == 0) {
-
- $options = array('statement_name' => 'ins_featurecvterm_cvfepu');
- $success = tripal_core_chado_insert('feature_cvterm', $values, $options);
- if (!$success) {
- watchdog('tripal_feature', 'tripal_feature_add_cvterm: The feature cvterm entry for feature, %feature_id, ' .
- 'could not be added: %cvterm.', array('%feature_id' => $feature_id, '%cvterm' => $cvterm), WATCHDOG_WARNING);
- return FALSE;
- }
- }
- return TRUE;
- }
- function tripal_feature_return_fasta($feature, $desc) {
- $fasta = ">" . variable_get('chado_feature_accession_prefix', 'FID') . "$feature->feature_id|$feature->name";
- $fasta .= " $desc\n";
- $fasta .= wordwrap($feature->residues, 50, "\n", TRUE);
- $fasta .= "\n\n";
- return $fasta;
- }
|