123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891 |
- <?php
- function tripal_feature_chado_schema_v1_11_feature() {
- $description = array();
- $referring_tables = array('analysisfeature',
- 'element',
- 'feature_cvterm',
- 'feature_dbxref',
- 'feature_expression',
- 'feature_genotype',
- 'feature_phenotype',
- 'feature_pub',
- 'feature_relationship',
- 'feature_synonym',
- 'featureloc',
- 'featurepos',
- 'featureprop',
- 'featurerange',
- 'library_feature',
- 'phylonode',
- 'wwwuser_feature'
- );
- $description['referring_tables'] = $referring_tables;
- return $description;
- }
- function tripal_feature_chado_schema_v1_11_featureprop() {
- $description = array();
- $referring_tables = array('analysisfeature',
- 'featureprop_pub',
- );
- $description['referring_tables'] = $referring_tables;
- return $description;
- }
- function tripal_feature_chado_schema_v1_11_featureloc() {
- $description = array();
- $referring_tables = array('analysisfeature',
- 'featureloc_pub',
- );
- $description['referring_tables'] = $referring_tables;
- return $description;
- }
- function tripal_feature_chado_schema_v1_2_featureloc() {
- $description = array();
- $referring_tables = array('analysisfeature',
- 'featureloc_pub',
- );
- $description['referring_tables'] = $referring_tables;
- return $description;
- }
- 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;
- }
-
- return tripal_core_insert_property('analysisfeature', $analysisfeature_id,
- $property, $cv_name, $value, $update_if_present);
- }
- 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()) {
-
-
- $connection = tripal_db_persistent_chado();
-
- if (!$upstream) {
- $upstream = 0;
- }
- if (!$downstream) {
- $downstream = 0;
- }
-
-
- 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
- FROM (
- SELECT
- OF.name srcname, FL.srcfeature_id, FL.strand,
- OCVT.name as srctypename, SCVT.name as typename,
- FL.fmin, FL.fmax,
- 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
- WHERE SF.feature_id = $3) as tbl1
- ';
- $status = chado_query($psql);
- if (!$status) {
- watchdog('tripal_views_handler_field_sequence',
- "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 = chado_query($psql);
- 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 = chado_query($psql);
- 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 (%d, %d, %d)";
- $parents = chado_query($sql, $upstream, $downstream, $feature_id);
- while ($parent = db_fetch_object($parents)) {
- $seq = '';
-
-
- if ($aggregate) {
-
-
- $sql = "EXECUTE sub_features (%d, %d)";
- $children = chado_query($sql, $feature_id, $parent->srcfeature_id);
- $sql = "EXECUTE count_sub_features (%d, %d)";
- $num_children = db_fetch_object(chado_query($sql, $feature_id, $parent->srcfeature_id));
-
-
-
- $types = array();
- $i = 0;
- while ($child = db_fetch_object($children)) {
-
-
-
- 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 (%d, %d, %d)";
-
- if ($i == 0 and $parent->strand >= 0) {
-
-
-
- $q = chado_query($sql, $upstream, 0, $child->feature_id);
- }
- elseif ($i == 0 and $parent->strand < 0) {
-
-
-
- $q = chado_query($sql, 0, $downstream, $child->feature_id);
- }
-
- elseif ($i == $num_children->num_children - 1 and $parent->strand >= 0) {
-
-
-
- $q = chado_query($sql, 0, $downstream, $child->feature_id);
- }
- elseif ($i == $num_children->num_children - 1 and $parent->strand < 0) {
-
-
-
- $q = chado_query($sql, $upstream, 0, $child->feature_id);
- }
-
-
- else {
- $sql = "EXECUTE sequence_by_parent (%d, %d, %d)";
- $q = chado_query($sql, 0, 0, $child->feature_id);
- }
-
- while ($subseq = db_fetch_object($q)) {
-
- 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, "\n", TRUE);
- }
- $residues .= ">$feature_name ($parent->typename) $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.\n";
- }
- }
- else {
- if ($output_format == 'fasta_html') {
- $residues .= "<br>";
- }
- $residues .= "\n" . $seq . "\n";
- if ($output_format == 'fasta_html') {
- $residues .= "<br>";
- }
- }
- }
- }
-
-
- else {
- $sql = "SELECT * FROM feature F WHERE feature_id = %d";
- $values = db_fetch_object(chado_query($sql, $feature_id));
- $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, "\n", TRUE);
- }
- $residues = ">$feature_name\n$residues\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_custom_tables($table = NULL) {
- if (!$table or strcmp($table, 'tripal_gff_temp')==0) {
- $schema['tripal_gff_temp'] = array(
- 'table' => 'tripal_gff_temp',
- 'fields' => array(
- 'feature_id' => array(
- 'type' => 'int',
- 'not null' => TRUE,
- ),
- 'organism_id' => array(
- 'type' => 'int',
- 'not null' => TRUE,
- ),
- 'uniquename' => array(
- 'type' => 'text',
- 'not null' => TRUE,
- ),
- 'type_name' => array(
- 'type' => 'varchar',
- 'length' => '1024',
- 'not null' => TRUE,
- ),
- ),
- 'indexes' => array(
- 'tripal_gff_temp_idx0' => array('feature_id'),
- 'tripal_gff_temp_idx0' => array('organism_id'),
- 'tripal_gff_temp_idx1' => array('uniquename'),
- ),
- 'unique keys' => array(
- 'tripal_gff_temp_uq0' => array('feature_id'),
- 'tripal_gff_temp_uq1' => array('uniquename', 'organism_id', 'type_name'),
- ),
- );
- }
- return $schema;
- }
|