12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667 |
- <?php
- require_once "admin.php";
- require_once "syncFeatures.php";
- require_once "indexFeatures.php";
- require_once "fasta_loader.php";
- require_once "tripal_feature.api.inc";
- require_once "tripal_feature-secondary_tables.inc";
- require_once "tripal_feature-properties.inc";
- require_once "tripal_feature-relationships.inc";
- require_once "tripal_feature-db_references.inc";
- /*************************************************************************
- *
- */
- function tripal_feature_init(){
- // add the jGCharts JS and CSS
- drupal_add_js (drupal_get_path('theme', 'tripal').'/js/tripal_feature.js');
- drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jgcharts/jgcharts.js');
- drupal_add_css(drupal_get_path('theme', 'tripal').
- '/css/tripal_feature.css');
- }
- /*************************************************************************
- * Implements hook_views_api()
- * Purpose: Essentially this hook tells drupal that there is views support for
- * for this module which then includes tripal_db.views.inc where all the
- * views integration code is
- */
- function tripal_feature_views_api() {
- return array(
- 'api' => 2.0,
- );
- }
- /************************************************************************
- * Display help and module information
- * @param path which path of the site we're displaying help
- * @param arg array that holds the current path as would be returned from arg() function
- * @return help text for the path
- */
- function tripal_feature_help($path, $arg) {
- $output = '';
- switch ($path) {
- case "admin/help#tripal_feature":
- $output='<p>'.t("Displays links to nodes created on this date").'</p>';
- break;
- }
- return $output;
- }
- /************************************************************************
- * Provide information to drupal about the node types that we're creating
- * in this module
- */
- function tripal_feature_node_info() {
- $nodes = array();
- $nodes['chado_feature'] = array(
- 'name' => t('Feature'),
- 'module' => 'chado_feature',
- 'description' => t('A feature from the chado database'),
- 'has_title' => FALSE,
- 'title_label' => t('Feature'),
- 'has_body' => FALSE,
- 'body_label' => t('Feature Description'),
- 'locked' => TRUE
- );
- return $nodes;
- }
- /************************************************************************
- * Set the permission types that the chado module uses. Essentially we
- * want permissionis that protect creation, editing and deleting of chado
- * data objects
- */
- function tripal_feature_perm(){
- return array(
- 'access chado_feature content',
- 'create chado_feature content',
- 'delete chado_feature content',
- 'edit chado_feature content',
- );
- }
- /************************************************************************
- * Set the permission types that the module uses.
- */
- function chado_feature_access($op, $node, $account) {
- if ($op == 'create') {
- return user_access('create chado_feature content', $account);
- }
- if ($op == 'update') {
- if (user_access('edit chado_feature content', $account)) {
- return TRUE;
- }
- }
- if ($op == 'delete') {
- if (user_access('delete chado_feature content', $account)) {
- return TRUE;
- }
- }
- if ($op == 'view') {
- if (user_access('access chado_feature content', $account)) {
- return TRUE;
- }
- }
- return FALSE;
- }
- /************************************************************************
- * Menu items are automatically added for the new node types created
- * by this module to the 'Create Content' Navigation menu item. This function
- * adds more menu items needed for this module.
- */
- function tripal_feature_menu() {
- $items = array();
- // the administative settings menu
- $items['admin/tripal/tripal_feature'] = array(
- 'title' => 'Features',
- 'description' => 'Settings for Chado Features',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_feature_admin'),
- 'access arguments' => array('administer site configuration'),
- 'type' => MENU_NORMAL_ITEM,
- );
- $items['admin/tripal/fasta_loader'] = array(
- 'title' => 'Import a multi-FASTA file',
- 'description' => 'Load sequences from a multi-FASTA file into Chado',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_feature_fasta_load_form'),
- 'access arguments' => array('administer site configuration'),
- 'type' => MENU_NORMAL_ITEM,
- );
- // Adding Secondary Properties-----------------
- $items['node/%tripal_feature_node/properties'] = array(
- 'title' => t('Add Properties & Synonyms'),
- 'description' => t('Settings for Features'),
- 'page callback' => 'tripal_feature_add_ALL_property_page',
- 'page arguments' => array(1),
- 'access arguments' => array('create chado_feature content'),
- 'type' => MENU_CALLBACK
- );
- $items['node/%tripal_feature_node/db_references'] = array(
- 'title' => t('Add Database References'),
- 'description' => t('Settings for Features'),
- 'page callback' => 'tripal_feature_add_ALL_dbreferences_page',
- 'page arguments' => array(1),
- 'access arguments' => array('create chado_feature content'),
- 'type' => MENU_CALLBACK
- );
- $items['node/%tripal_feature_node/relationships'] = array(
- 'title' => t('Add Relationships'),
- 'description' => t('Settings for Features'),
- 'page callback' => 'tripal_feature_add_ALL_relationships_page',
- 'page arguments' => array(1),
- 'access arguments' => array('create chado_feature content'),
- 'type' => MENU_CALLBACK
- );
- //Edit/Deleting Secondary Properties-------------
- $items['node/%tripal_feature_node/edit_properties'] = array(
- 'title' => t('Edit Properties'),
- 'description' => t('Settings for Features'),
- 'page callback' => 'tripal_feature_edit_ALL_properties_page',
- 'page arguments' => array(1),
- 'access arguments' => array('edit chado_feature content'),
- 'type' => MENU_LOCAL_TASK,
- 'weight' => 8,
- );
- $items['node/%tripal_feature_node/edit_relationships'] = array(
- 'title' => t('Edit Relationships'),
- 'description' => t('Settings for Feature'),
- 'page callback' => 'tripal_feature_edit_ALL_relationships_page',
- 'page arguments' => array(1),
- 'access arguments' => array('edit chado_feature content'),
- 'type' => MENU_LOCAL_TASK,
- 'weight' => 9,
- );
- $items['node/%tripal_feature_node/edit_db_references'] = array(
- 'title' => t('Edit References'),
- 'description' => t('Settings for Feature'),
- 'page callback' => 'tripal_feature_edit_ALL_dbreferences_page',
- 'page arguments' => array(1),
- 'access arguments' => array('edit chado_feature content'),
- 'type' => MENU_LOCAL_TASK,
- 'weight' => 10,
- );
- return $items;
- }
- /*************************************************************************
- * Implements Menu wildcard_load hook
- * Purpose: Allows the node ID of a chado stock to be dynamically
- * pulled from the path. The node is loaded from this node ID
- * and supplied to the page as an arguement
- */
- function tripal_feature_node_load($nid) {
- if (is_numeric($nid)) {
- $node = node_load($nid);
- if ($node->type == 'chado_feature') {
- return $node;
- }
- }
- return FALSE;
- }
- /************************************************************************
- * When a new chado_feature node is created we also need to add information
- * to our chado_feature table. This function is called on insert of a new node
- * of type 'chado_feature' and inserts the necessary information.
- */
- function chado_feature_insert($node){
- // remove spaces, newlines from residues
- $residues = preg_replace("/[\n\r\s]/","",$node->residues);
- // If this feature already exists then don't recreate it in chado
- // TODO: the unique index in chado for this also includes the type_id. If the site
- // ever needs to have the same feature name for different types then this will break.
- $feature_sql = "SELECT *
- FROM {Feature} F
- INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
- WHERE uniquename = '%s' and organism_id = %d and CVT.name = '%s'";
- $previous_db = tripal_db_set_active('chado');
- $feature = db_fetch_object(db_query($feature_sql,$node->uniquename,$node->organism_id,$node->feature_type));
- tripal_db_set_active($previous_db);
- // if the feature doesn't exist then let's create it in chado.
- if(!$feature){
- $sql = "INSERT INTO {feature} (organism_id, name, uniquename, residues, seqlen,".
- " is_obsolete, type_id)".
- " VALUES(%d,'%s','%s','%s',%d, %s, ".
- " (SELECT cvterm_id ".
- " FROM {CVTerm} CVT ".
- " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
- " WHERE CV.name = 'sequence' and CVT.name = '%s'))";
- $obsolete = 'FALSE';
- if($node->is_obsolete){
- $obsolete = 'TRUE';
- }
- // use chado database
- $previous_db = tripal_db_set_active('chado');
- db_query($sql,$node->organism_id,$node->name,$node->uniquename,
- $residues,strlen($residues),$obsolete,$node->feature_type);
- // now that we've added the feature, get the feature id for this feature
- $feature = db_fetch_object(db_query($feature_sql,$node->uniquename,$node->organism_id,$node->feature_type));
- // now use drupal database
- tripal_db_set_active($previous_db);
- }
- // add the genbank accession and synonyms
- chado_feature_add_synonyms($node->synonyms,$feature->feature_id);
- // make sure the entry for this feature doesn't already exist in the chado_feature table
- // if it doesn't exist then we want to add it.
- $node_check_sql = "SELECT * FROM {chado_feature} ".
- "WHERE feature_id = '%s'";
- $node_check = db_fetch_object(db_query($node_check_sql,$feature->feature_id));
- if(!$node_check){
- // next add the item to the drupal table
- $sql = "INSERT INTO {chado_feature} (nid, vid, feature_id, sync_date) ".
- "VALUES (%d, %d, %d, " . time() . ")";
- db_query($sql,$node->nid,$node->vid,$feature->feature_id);
- }
- }
- /************************************************************************
- */
- function chado_feature_delete($node){
- // get feature_id so we can remove it from chado database
- $sql_drupal = "SELECT feature_id ".
- "FROM {chado_feature} ".
- "WHERE nid = %d AND vid = %d";
- $feature_id = db_result(db_query($sql_drupal, $node->nid, $node->vid));
- // remove the drupal content
- $sql_del = "DELETE FROM {chado_feature} ".
- "WHERE nid = %d ".
- "AND vid = %d";
- db_query($sql_del, $node->nid, $node->vid);
- $sql_del = "DELETE FROM {node} ".
- "WHERE nid = %d ".
- "AND vid = %d";
- db_query($sql_del, $node->nid, $node->vid);
- $sql_del = "DELETE FROM {node_revisions} ".
- "WHERE nid = %d ".
- "AND vid = %d";
- db_query($sql_del, $node->nid, $node->vid);
- // Remove data from feature tables of chado database. This will
- // cause a cascade delete and remove all data in referencing tables
- // for this feature
- $previous_db = tripal_db_set_active('chado');
- db_query("DELETE FROM {feature} WHERE feature_id = %d", $feature_id);
- tripal_db_set_active($previous_db);
-
- drupal_set_message("The feature and all associated data were removed from ".
- "chado");
- }
- /************************************************************************
- */
- function chado_feature_update($node){
- if($node->revision){
- // TODO -- decide what to do about revisions
- } else {
- // get the feature for this node:
- $sql = 'SELECT feature_id FROM {chado_feature} WHERE vid = %d';
- $feature = db_fetch_object(db_query($sql, $node->vid));
- // remove spaces, newlines from residues
- $residues = preg_replace("/[\n\r\s]/","",$node->residues);
- $sql = "UPDATE {feature} ".
- " SET residues = '%s', ".
- " name = '%s', ".
- " uniquename = '%s', ".
- " seqlen = %d, ".
- " organism_id = %d, ".
- " is_obsolete = %s, ".
- " type_id = (SELECT cvterm_id ".
- " FROM {CVTerm} CVT ".
- " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
- " WHERE CV.name = 'sequence' and CVT.name = '%s') ".
- "WHERE feature_id = %d ";
- $obsolete = 'FALSE';
- if($node->is_obsolete){
- $obsolete = 'TRUE';
- }
- $previous_db = tripal_db_set_active('chado'); // use chado database
- db_query($sql,$residues,$node->name,$node->uniquename,
- strlen($residues),$node->organism_id,$obsolete,$node->feature_type,
- $feature->feature_id);
- tripal_db_set_active($previous_db); // now use drupal database
- // add the genbank accession & synonyms
- // chado_feature_add_gbaccession($node->gbaccession,$feature->feature_id);
- chado_feature_add_synonyms($node->synonyms,$feature->feature_id);
- }
- }
- /************************************************************************
- *
- */
- function chado_feature_add_synonyms($synonyms,$feature_id){
- // make sure we only have a single space between each synonym
- $synonyms = preg_replace("/[\s\n\r]+/"," ",$synonyms);
- // split the synonyms into an array based on a space as the delimieter
- $syn_array = array();
- $syn_array = explode(" ",$synonyms);
- // use the chado database
- $previous_db = tripal_db_set_active('chado');
- // remove any old synonyms
- $feature_syn_dsql = "DELETE FROM {feature_synonym} WHERE feature_id = %d";
- if(!db_query($feature_syn_dsql,$feature_id)){
- $error .= "Could not remove synonyms from feature. ";
- }
- // return if we don't have any synonmys to add
- if(!$synonyms){
- tripal_db_set_active($previous_db);
- return;
- }
- // iterate through each synonym and add it to the database
- foreach($syn_array as $syn){
- // skip this item if it's empty
- if(!$syn){ break; }
- // check to see if we have this accession number already in the database
- // if so then don't add it again. it messes up drupal if the insert fails.
- // It is possible for the accession number to be present and not the feature
- $synonym_sql = "SELECT synonym_id FROM {synonym} ".
- "WHERE name = '%s'";
- $synonym = db_fetch_object(db_query($synonym_sql,$syn));
- if(!$synonym){
- $synonym_isql = "INSERT INTO {synonym} (name,synonym_sgml,type_id) ".
- "VALUES ('%s','%s', ".
- " (SELECT cvterm_id ".
- " FROM {CVTerm} CVT ".
- " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
- " WHERE CV.name = 'feature_property' and CVT.name = 'synonym'))";
- if(!db_query($synonym_isql,$syn,$syn)){
- $error .= "Could not add synonym. ";
- }
- // now get the synonym we just added
- $synonym_sql = "SELECT synonym_id FROM {synonym} ".
- "WHERE name = '%s'";
- $synonym = db_fetch_object(db_query($synonym_sql,$syn));
- }
- // now add in our new sysnonym
- $feature_syn_isql = "INSERT INTO {feature_synonym} (synonym_id,feature_id,pub_id) ".
- "VALUES (%d,%d,1)";
- if(!db_query($feature_syn_isql,$synonym->synonym_id,$feature_id)){
- $error .= "Could not add synonyms to feature. ";
- }
- }
- // return to the drupal database
- tripal_db_set_active($previous_db);
- return $error;
- }
- /************************************************************************
- *
- */
- function chado_feature_add_gbaccession($accession,$feature_id){
- // use chado database
- $previous_db = tripal_db_set_active('chado');
- // remove any old accession from genbank dbEST
- $fdbxref_dsql = "DELETE FROM {feature_dbxref} ".
- "WHERE feature_id = %d and dbxref_id IN ".
- " (SELECT DBX.dbxref_id FROM {dbxref} DBX ".
- " INNER JOIN DB ON DB.db_id = DBX.db_id ".
- " INNER JOIN feature_dbxref FDBX ON DBX.dbxref_id = FDBX.dbxref_id ".
- " WHERE DB.name = 'DB:Genbank' and FDBX.feature_id = %d)";
- if(!db_query($fdbxref_dsql,$feature_id,$feature_id)){
- $error .= "Could not remove accession from feature. ";
- }
- // if we don't have an accession number to add then just return
- if(!$accession){
- tripal_db_set_active($previous_db);
- return;
- }
- // get the db_id
- $db_sql = "SELECT db_id FROM {DB} ".
- "WHERE name = 'DB:Genbank_est'";
- $db = db_fetch_object(db_query($db_sql));
- // check to see if we have this accession number already in the database
- // if so then don't add it again. it messes up drupal if the insert fails.
- // It is possible for the accession number to be present and not the feature
- $dbxref_sql = "SELECT dbxref_id FROM {dbxref} ".
- "WHERE db_id = %d and accession = '%s'";
- $dbxref = db_fetch_object(db_query($dbxref_sql,$db->db_id,$accession));
- if(!$dbxref){
- // add the accession number
- $dbxref_isql = "INSERT INTO {dbxref} (db_id,accession) ".
- " VALUES (%d, '%s') ";
- if(!db_query($dbxref_isql,$db->db_id,$accession)){
- $error .= 'Could not add accession as a database reference ';
- }
- // get the dbxref_id for the just added accession number
- $dbxref_sql = "SELECT dbxref_id FROM {dbxref} ".
- "WHERE db_id = %d and accession = '%s'";
- $dbxref = db_fetch_object(db_query($dbxref_sql,$db->db_id,$accession));
- }
- // associate the accession number with the feature
- $feature_dbxref_isql = "INSERT INTO {feature_dbxref} (feature_id,dbxref_id) ".
- " VALUES (%d, %d) ";
- if(!db_query($feature_dbxref_isql,$feature_id,$dbxref->dbxref_id)){
- $error .= 'Could not add feature database reference. ';
- }
- tripal_db_set_active($previous_db);
- return $error;
- }
- /************************************************************************
- *
- */
- function chado_feature_form ($node,$param){
- $type = node_get_types('type', $node);
- $form = array();
- $feature = $node->feature;
- $synonyms = $node->synonyms;
- $analyses = $node->analyses;
- $references = $node->references;
- // We need to pass above variables for preview to show
- $form['feature'] = array(
- '#type' => 'value',
- '#value' => $feature
- );
- // This field is read when previewing a node
- $form['synonyms'] = array(
- '#type' => 'value',
- '#value' => $synonyms
- );
- // This field is read when previewing a node
- $form['analyses'] = array(
- '#type' => 'value',
- '#value' => $analyses
- );
- // This field is read when previewing a node
- $form['references'] = array(
- '#type' => 'value',
- '#value' => $references
- );
- // keep track of the feature id if we have one. If we do have one then
- // this would indicate an update as opposed to an insert.
- $form['feature_id'] = array(
- '#type' => 'value',
- '#value' => $feature->feature_id,
- );
- $form['title']= array(
- '#type' => 'textfield',
- '#title' => t('Title'),
- '#required' => TRUE,
- '#default_value' => $feature->featurename,
- '#description' => t('The title must be a unique identifier for this feature. It is recommended to use a combination of uniquename, organism and feature type in the title as this is guranteed to be unique.'),
- '#weight' => 1,
- '#maxlength' => 255
- );
- $form['uniquename']= array(
- '#type' => 'textfield',
- '#title' => t('Unique Feature Name'),
- '#required' => TRUE,
- '#default_value' => $feature->featurename,
- '#description' => t('Enter a unique name for this feature. This name must be unique for the organism and feature type.'),
- '#weight' => 1,
- '#maxlength' => 255
- );
- $form['name']= array(
- '#type' => 'textfield',
- '#title' => t('Feature Name'),
- '#required' => TRUE,
- '#default_value' => $feature->featurename,
- '#description' => t('Enter the name used by humans to refer to this feature.'),
- '#weight' => 1,
- '#maxlength' => 255
- );
- // get the list of supported feature types
- $ftypes = array();
- $ftypes[''] = '';
- $supported_ftypes = split("[ \n]",variable_get('chado_feature_types','EST contig'));
- foreach($supported_ftypes as $ftype){
- $ftypes["$ftype"] = $ftype;
- }
- $form['feature_type'] = array (
- '#title' => t('Feature Type'),
- '#type' => t('select'),
- '#description' => t("Choose the feature type."),
- '#required' => TRUE,
- '#default_value' => $feature->cvname,
- '#options' => $ftypes,
- '#weight' => 2
- );
- // get the list of organisms
- $sql = "SELECT * FROM {Organism} ORDER BY genus, species";
- $previous_db = tripal_db_set_active('chado'); // use chado database
- $org_rset = db_query($sql);
- tripal_db_set_active($previous_db); // now use drupal database
- //
- $organisms = array();
- $organisms[''] = '';
- while($organism = db_fetch_object($org_rset)){
- $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
- }
- $form['organism_id'] = array (
- '#title' => t('Organism'),
- '#type' => t('select'),
- '#description' => t("Choose the organism with which this feature is associated "),
- '#required' => TRUE,
- '#default_value' => $feature->organism_id,
- '#options' => $organisms,
- '#weight' => 3,
- );
- // Get synonyms
- if ($synonyms) {
- if (is_array($synonyms)) {
- foreach ($synonyms as $synonym){
- $syn_text .= "$synonym->name\n";
- }
- } else {
- $syn_text = $synonyms;
- }
- }
- $form['synonyms']= array(
- '#type' => 'textarea',
- '#title' => t('Synonyms'),
- '#required' => FALSE,
- '#default_value' => $syn_text,
- '#description' => t('Enter alternate names (synonmys) for this feature to help in searching and identification. You may enter as many alternate names as needed separated by spaces or on different lines.'),
- '#weight' => 5,
- );
- $form['residues']= array(
- '#type' => 'textarea',
- '#title' => t('Residues'),
- '#required' => FALSE,
- '#default_value' => $feature->residues,
- '#description' => t('Enter the nucelotide sequences for this feature'),
- '#weight' => 6
- );
- $checked = '';
- if($feature->is_obsolete == 't'){
- $checked = '1';
- }
- $form['is_obsolete']= array(
- '#type' => 'checkbox',
- '#title' => t('Is Obsolete'),
- '#required' => FALSE,
- '#default_value' => $checked,
- '#description' => t('Check this box if this sequence should be retired and no longer included in further analysis.'),
- '#weight' => 8
- );
- return $form;
- }
- /************************************************************************
- *
- */
- function chado_feature_validate($node){
- $result = 0;
- // if this is an update, we want to make sure that a different feature for
- // the organism doesn't already have this uniquename. We don't want to give
- // two sequences the same uniquename
- if($node->feature_id){
- $sql = "SELECT *
- FROM {Feature} F
- INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
- WHERE uniquename = '%s'
- AND organism_id = %d AND CVT.name = '%s' AND NOT feature_id = %d";
- $previous_db = tripal_db_set_active('chado');
- $result = db_fetch_object(db_query($sql, $node->uniquename,$node->organism_id,$node->feature_type,$node->feature_id));
- tripal_db_set_active($previous_db);
- if($result){
- form_set_error('uniquename',t("Feature update cannot proceed. The feature name '$node->uniquename' is not unique for this organism. Please provide a unique name for this feature. "));
- }
- }
- // if this is an insert then we just need to make sure this name doesn't
- // already exist for this organism if it does then we need to throw an error
- else {
- $sql = "SELECT *
- FROM {Feature} F
- INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
- WHERE uniquename = '%s'
- AND organism_id = %d AND CVT.name = '%s'";
- $previous_db = tripal_db_set_active('chado');
- $result = db_fetch_object(db_query($sql, $node->uniquename,$node->organism_id,$node->feature_type));
- tripal_db_set_active($previous_db);
- if($result){
- form_set_error('uniquename',t("Feature insert cannot proceed. The feature name '$node->uniquename' already exists for this organism. Please provide a unique name for this feature. "));
- }
- }
- // we want to remove all characters except IUPAC nucleotide characters from the
- // the residues. however, residues are not required so if blank then we'll skip
- // this step
- if($node->residues){
- $residues = preg_replace("/[^\w]/",'',$node->residues);
- if(!preg_match("/^[ACTGURYMKSWBDHVN]+$/i",$residues)){
- form_set_error('residues',t("The residues in feature $node->name contains more than the nucleotide IUPAC characters. Only the following characters are allowed: A,C,T,G,U,R,Y,M,K,S,W,B,D,H,V,N: '" . $residues ."'"));
- }
- }
- // we don't allow a genbank accession number for a contig
- if($node->feature_type == 'contig' and $node->gbaccession){
- form_set_error('gbaccession',t("Contigs cannot have a genbank accession number. Please change the feature type or remove the accession number"));
- }
- }
- /************************************************************************
- * When a node is requested by the user this function is called to allow us
- * to add auxiliary data to the node object.
- */
- function chado_feature_load($node){
- // add the feature_id for this node:
- $sql = 'SELECT feature_id FROM {chado_feature} WHERE vid = %d';
- $map = db_fetch_object(db_query($sql, $node->vid));
- // get information about this feature and add it to the items in this node
- $sql = "SELECT F.feature_id, F.name as featurename, F.uniquename, ".
- "F.residues, F.seqlen, O.genus, O.species, O.common_name, ".
- " CVT.name as cvname, O.organism_id, F.type_id, F.is_obsolete ".
- "FROM {Feature} F ".
- " INNER JOIN Organism O ON F.organism_id = O.organism_id ".
- " INNER JOIN CVterm CVT ON F.type_id = CVT.cvterm_id ".
- "WHERE F.feature_id = %d";
- $previous_db = tripal_db_set_active('chado'); // use chado database
- $feature = db_fetch_object(db_query($sql,$map->feature_id));
- tripal_db_set_active($previous_db); // now use drupal database
- $additions->feature = $feature;
- $additions->seqlen = $feature->seqlen;
- // add organism node nid
- $sql = "SELECT nid FROM {chado_organism} WHERE organism_id = %d";
- $org_nid = db_result(db_query($sql, $additions->feature->organism_id));
- $additions->org_nid = $org_nid;
- $additions->accession = variable_get('chado_feature_accession_prefix','ID') . $feature->feature_id;
- // add organism details
- $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
- $previous_db = tripal_db_set_active('chado'); // use chado database
- $organism = db_fetch_object(db_query($sql, $additions->feature->organism_id));
- tripal_db_set_active($previous_db); // now use drupal database
- $additions->organism = $organism;
-
- // add the feature synonyms
- $sql = "SELECT S.name ".
- "FROM {Feature_Synonym} FS ".
- " INNER JOIN Synonym S ".
- " ON FS.synonym_id = S.Synonym_id ".
- "WHERE FS.feature_id = %d";
- $previous_db = tripal_db_set_active('chado'); // use chado database
- $results = db_query($sql,$map->feature_id);
- tripal_db_set_active($previous_db); // now use drupal database
- $synonyms = array();
- $i=0;
- while($synonym = db_fetch_object($results)){
- $synonyms[$i++] = $synonym;
- }
- $additions->synonyms = $synonyms;
- // add feature references in external databases
- $sql = "SELECT F.uniquename,F.Feature_id,DBX.accession,DB.description as dbdesc, ".
- " DB.db_id, DB.name as db_name, DB.urlprefix,DBX.dbxref_id ".
- "FROM {Feature} F ".
- " INNER JOIN Feature_dbxref FDBX on F.feature_id = FDBX.feature_id ".
- " INNER JOIN Dbxref DBX on DBX.dbxref_id = FDBX.dbxref_id ".
- " INNER JOIN DB on DB.db_id = DBX.db_id ".
- "WHERE F.feature_id = %d";
- $previous_db = tripal_db_set_active('chado'); // use chado database
- $results = db_query($sql,$map->feature_id);
- tripal_db_set_active($previous_db); // now use drupal database
- $references = array();
- $i=0;
- while($accession = db_fetch_object($results)){
- $references[$i++] = $accession;
- }
- $additions->references = $references;
- // add the feature relationsips
- $sql = "SELECT
- FS.name as subject_name,
- FS.uniquename as subject_uniquename,
- FS.residues as subject_residues,
- CVTS.name as subject_type,
- FR.subject_id,
- FR.type_id as relationship_type_id,
- CVT.name as rel_type,
- FO.name as object_name,
- FO.uniquename as object_uniquename,
- CVTO.name as object_type,
- FR.object_id,
- FLS.name as src_name,
- FLS.uniquename as src_uniquename,
- FLS.feature_id as src_featureid,
- CVTR.name as src_cvname,
- CVTR.cvterm_id as src_type_id,
- FL.fmin,
- FL.fmax,
- FL.is_fmin_partial,
- FL.is_fmax_partial,
- FL.strand,
- FL.phase,
- FL.residue_info
- 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
- LEFT JOIN {featureloc} FL on FL.feature_id = FS.feature_id
- LEFT JOIN {feature} FLS on FLS.feature_id = FL.srcfeature_id
- LEFT JOIN {cvterm} CVTR on FLS.type_id = CVTR.cvterm_id
- ";
- $osql = "$sql WHERE FR.object_id = %d ORDER BY FLS.name, FL.fmin ASC";
- $ssql = "$sql WHERE FR.subject_id = %d ORDER BY FLS.name, FL.fmin ASC";
- // first get the relationships where this feature is the object
- // then where it is the subject
- $previous_db = tripal_db_set_active('chado'); // use chado database
- $oresults = db_query($osql, $map->feature_id);
- $sresults = db_query($ssql, $map->feature_id);
- tripal_db_set_active($previous_db); // now use drupal database
- // identify the drupal node for each feature and add that to the
- // relationship records
- $srelationships = array();
- $orelationships = array();
- $i=0;
- $nodesql = "SELECT nid FROM {chado_feature} WHERE feature_id = %d";
- $rel_locs; // holds relationship locations
- $rel_loc; // a single relationship location
- while($rel = db_fetch_object($oresults)){
- $node = db_fetch_object(db_query($nodesql,$rel->subject_id));
- if($node){
- $rel->subject_nid = $node->nid;
- }
- $node = db_fetch_object(db_query($nodesql,$rel->object_id));
- if($node){
- $rel->object_nid = $node->nid;
- }
- $orelationships[$i] = $rel;
- if($rel->src_uniquename){
- $rel_loc = $rel->src_uniquename ."|".$rel->src_type_id;
- $rel_locs[$rel_loc]['uname'] = $rel->src_uniquename;
- $rel_locs[$rel_loc]['type'] = $rel->src_type_id;
- if(!$rel_locs[$rel_loc]['source_min']){
- $rel_locs[$rel_loc]['source_min'] = 99999999999;
- }
- if($rel->fmin < $rel_locs[$rel_loc]['source_min']){
- $rel_locs[$rel_loc]['source_min'] = $rel->fmin;
- }
- if($rel->fmax > $rel_locs[$rel_loc]['source_max']){
- $rel_locs[$rel_loc]['source_max'] = $rel->fmax;
- }
- }
- $i++;
- }
- // build the parts array needed for coloring the relationsihps in the
- // reference sequence
- foreach($orelationships as $index => $rel){
- if($rel->src_uniquename){
- $rel_loc = $rel->src_uniquename ."|".$rel->src_type_id;
- $rel_locs[$rel_loc]['parts'][$index]['type'] = $rel->subject_type;
- $rel_locs[$rel_loc]['parts'][$index]['start'] = $rel->fmin - $rel_locs[$rel_loc]['source_min'];
- $rel_locs[$rel_loc]['parts'][$index]['end'] = $rel->fmax - $rel_locs[$rel_loc]['source_min'];
- }
- }
- $i=0;
- while($rel = db_fetch_object($sresults)){
- $node = db_fetch_object(db_query($nodesql,$rel->subject_id));
- if($node){
- $rel->subject_nid = $node->nid;
- }
- $node = db_fetch_object(db_query($nodesql,$rel->object_id));
- if($node){
- $rel->object_nid = $node->nid;
- }
- $srelationships[$i++] = $rel;
- }
- $additions->object_relationships = $orelationships;
- $additions->subject_relationships = $srelationships;
- // now get the sequence from the reference for those feature as a
- // subject relationship to this feature.
- $object_context = array();
- $i=0;
- foreach ($rel_locs as $rel_loc => $attrs){
- if($attrs['uname']){
- $sql = "SELECT residues
- FROM feature
- WHERE uniquename = '%s' AND organism_id = %d and type_id = %d";
- $context = db_fetch_object(db_query($sql,$attrs['uname'],$additions->feature->organism_id,$attrs['type']));
- $context->residues = substr($context->residues,$attrs['source_min'],$attrs['source_max'] - $attrs['source_min']);
- $context->source = $attrs['uname'];
- $context->fmin = $attrs['source_min'];
- $context->fmax = $attrs['source_max'];
- $context->residues = tripal_feature_color_sequence ($context->residues,$attrs['parts']);
- }
- $object_context[$i] = $context;
- }
- $additions->relationship_object_info = $object_context;
- // get the locations for this feature
- $sql = "SELECT F.name,F.feature_id, F.uniquename,CVT.name as cvname, CVT.cvterm_id,
- FL.fmin, FL.fmax, FL.is_fmin_partial, FL.is_fmax_partial,FL.strand,
- FL.phase, FL.residue_info
- FROM featureloc FL
- INNER JOIN feature F on FL.srcfeature_id = F.feature_id
- INNER JOIN cvterm CVT on F.type_id = CVT.cvterm_id
- WHERE FL.feature_id = %d";
- $previous_db = tripal_db_set_active('chado'); // use chado database
- $flresults = db_query($sql, $map->feature_id);
- tripal_db_set_active($previous_db); // now use drupal database
- $i=0;
- $featurelocs = array();
- while($loc = db_fetch_object($flresults)){
- $featurelocs[$i++] = $loc;
- }
- $additions->featurelocs = $featurelocs;
- // get features that are aligned to this feature
- $sql = "SELECT F.name,F.feature_id, F.uniquename,CVT.name as cvname, CVT.cvterm_id,
- FL.fmin, FL.fmax, FL.is_fmin_partial, FL.is_fmax_partial,FL.strand,
- FL.phase, FL.residue_info
- FROM featureloc FL
- INNER JOIN feature F on FL.feature_id = F.feature_id
- INNER JOIN cvterm CVT on F.type_id = CVT.cvterm_id
- WHERE FL.srcfeature_id = %d";
- $previous_db = tripal_db_set_active('chado'); // use chado database
- $flresults = db_query($sql, $map->feature_id);
- tripal_db_set_active($previous_db); // now use drupal database
- $i=0;
- $myfeaturelocs = array();
- while($loc = db_fetch_object($flresults)){
- $myfeaturelocs[$i++] = $loc;
- }
- $additions->myfeaturelocs = $myfeaturelocs;
- return $additions;
- }
- /************************************************************************
- *
- */
- function tripal_feature_color_sequence ($sequence,$parts){
- watchdog('tripal_feature',print_r($parts,1));
- $types = array();
- // first get the list of types so we can create a color legend
- foreach ($parts as $index => $child){
- $type = $child['type'];
- if(!in_array($type,$types)){
- $types[] = $type;
- }
- }
-
- $newseq .= "<div id=\"tripal_feature-sequence-legend\">Legend: ";
- foreach($types as $type){
- $newseq .= "<span class=\"tripal_feature-sequence-$type\">$type</span>";
- }
- $newseq .= "</div>";
-
- // set the background color of the rows based on the type
- $pos = 0;
- $newseq .= "<pre id=\"tripal_feature-sequence\">";
- foreach ($parts as $index => $child){
- $type = $child['type'];
- $start = $child['start'];
- $end = $child['end'];
-
- $class = "class=\"tripal_feature-sequence-$type\"";
- // iterate through the sequence up to the end of the child
- for ($i = $pos; $i < $end; $i++){
-
- // if we're at the beginning of the child sequence then set the
- // appropriate text color
- if($pos == $start){
- $newseq .= "<span $class>";
- $func = 'uc'; // nucleotides within the child should be uppercase
- }
- $newseq .= $sequence{$pos};
- $seqcount++;
- if($seqcount % 100 == 0){
- $newseq .= "\n";
- }
- $pos++;
- if($pos == $end){
- $newseq .= "</span>";
- $func = 'lc';
- }
- }
- }
- $newseq .= "</pre>";
- return $newseq;
- }
- /************************************************************************
- * This function customizes the view of the chado_feature node. It allows
- * us to generate the markup.
- */
- function chado_feature_view ($node, $teaser = FALSE, $page = FALSE) {
- if (!$teaser) {
- // use drupal's default node view:
- $node = node_prepare($node, $teaser);
-
- // if we're building the node for searching then
- // we want to handle this within the module and
- // not allow theme customization. We don't want to
- // index all items (such as DNA sequence).
- if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
- $node->content['index_version'] = array(
- '#value' => theme('tripal_feature_search_index',$node),
- '#weight' => 1,
- );
- }
- else if($node->build_mode == NODE_BUILD_SEARCH_RESULT){
- $node->content['index_version'] = array(
- '#value' => theme('tripal_feature_search_results',$node),
- '#weight' => 1,
- );
- }
- else {
- // do nothing here, let the theme derived template handle display
- }
- }
- return $node;
- }
- /*******************************************************************************
- * Display feature information for associated organisms. This function also
- * provides contents for indexing
- */
- function tripal_feature_nodeapi(&$node, $op, $teaser, $page) {
- switch ($op) {
- // Note that this function only adds feature view to an organism node.
- // The view of a feature node is controled by the theme *.tpl file
- case 'view':
- // Set the node types for showing feature information
- $types_to_show = array('chado_organism', 'chado_library');
- // Abort if this node is not one of the types we should show.
- if (!in_array($node->type, $types_to_show, TRUE)) {
- break;
- }
- // Add feature to the content item if it's not a teaser
- if (!$teaser) {
- // Show feature browser
- $node->content['tripal_feature_browser'] = array(
- '#value' => theme('tripal_feature_browser', $node),
- '#weight' => 5
- );
- $node->content['tripal_feature_org_counts'] = array(
- '#value' => theme('tripal_feature_counts', $node),
- '#weight' => 4
- );
- }
- }
- }
- /************************************************************************
- * We need to let drupal know about our theme functions and their arguments.
- * We create theme functions to allow users of the module to customize the
- * look and feel of the output generated in this module
- */
- function tripal_feature_theme () {
- return array(
- 'tripal_feature_search_index' => array (
- 'arguments' => array('node'),
- ),
- 'tripal_feature_search_results' => array (
- 'arguments' => array('node'),
- ),
- 'tripal_feature_browser' => array (
- 'arguments' => array('node'),
- ),
- 'tripal_feature_counts' => array (
- 'arguments' => array('node'),
- )
- );
- }
- /*******************************************************************************
- * create a list of features for the organism and pie chart
- */
- function theme_tripal_feature_counts($node){
- // don't show the summary if the settings in the admin page is turned off
- $show_browser = variable_get('tripal_feature_summary_setting',array('show_feature_summary'));
- if(strcmp($show_browser[0],'show_feature_summary')!=0){
- return;
- }
- // get the feature counts. This is dependent on a materialized view
- // installed with the organism module
- $content = '';
- if ($node->organism_id && $node->type == 'chado_organism') {
- $sql = "SELECT * FROM {organism_feature_count} ".
- "WHERE organism_id = %d AND NOT feature_type = 'EST_match' ".
- "ORDER BY num_features desc";
- $features = array();
- $previous_db = tripal_db_set_active('chado'); // use chado database
- $results = db_query($sql,$node->organism_id);
- tripal_db_set_active($previous_db); // now use drupal database
- $feature = db_fetch_object($results); // retrieve the first result
- if ($feature) {
- $content .= "<div class=\"tripal_feature_summary-info-box\"><br>
- <div class=\"tripal_expandableBox\">".
- "<h3>Feature Summary</h3>".
- "</div>";
- $content .= "<div class=\"tripal_expandableBoxContent\">";
- $content .= "<table class=\"tripal_table_horz\">";
- $content .= " <tr>";
- $content .= " <th class=\"dbfieldname\">Type</th>";
- $content .= " <th class=\"dbfieldname\">Number</th>";
- $content .= " </tr>";
- do {
- $content .= "<tr>";
- $content .= " <td>$feature->feature_type</td>";
- $content .= " <td>". number_format($feature->num_features) . "</td>";
- $content .= "</tr>";
- } while($feature = db_fetch_object($results));
- $content .= "</table>";
- $content .= "
- <img class=\"tripal_cv_chart\" id=\"tripal_feature_cv_chart_$node->organism_id\" src=\"\" border=\"0\">
- ";
- $content .= "</div></div>";
- }
- }
- return $content;
- }
- /************************************************************************
- *
- */
- function tripal_feature_cv_chart($chart_id){
- // The CV module will create the JSON array necessary for buillding a
- // pie chart using jgChart and Google Charts. We have to pass to it
- // a table that contains count information, tell it which column
- // contains the cvterm_id and provide a filter for getting the
- // results we want from the table.
- $organism_id = preg_replace("/^tripal_feature_cv_chart_(\d+)$/","$1",$chart_id);
- $options = array(
- count_mview => 'organism_feature_count',
- cvterm_id_column => 'cvterm_id',
- count_column => 'num_features',
- size => '650x200',
- filter => "CNT.organism_id = $organism_id AND NOT feature_type = 'EST_match' ",
- );
- return $options;
- }
- /************************************************************************
- *
- */
- function tripal_feature_cv_tree($tree_id){
- // The CV module will create the JSON array necessary for buillding a
- // pie chart using jgChart and Google Charts. We have to pass to it
- // a table that contains count information, tell it which column
- // contains the cvterm_id and provide a filter for getting the
- // results we want from the table.
- $organism_id = preg_replace("/^tripal_feature_cv_tree_(\d+)$/","$1",$tree_id);
- $options = array(
- cv_id => tripal_cv_get_cv_id('sequence'),
- count_mview => 'organism_feature_count',
- cvterm_id_column => 'cvterm_id',
- count_column => 'num_features',
- filter => "CNT.organism_id = $organism_id",
- label => 'Features',
- );
- return $options;
- }
- /*******************************************************************************
- * create a simple paged feature browser
- */
- function theme_tripal_feature_browser($node){
- // don't show the browser if the settings in the admin page is turned off
- $show_browser = variable_get('tripal_feature_browse_setting',array('show_feature_browser'));
- if(strcmp($show_browser[0],'show_feature_browser')!=0){
- return;
- }
- if ($node->organism_id && $node->type == 'chado_organism') {
- # get the list of available sequence ontology terms for which
- # we will build drupal pages from features in chado. If a feature
- # is not one of the specified typse we won't build a node for it.
- $allowed_types = variable_get('chado_feature_types','EST contig');
- $allowed_types = preg_replace("/[\s\n\r]+/"," ",$allowed_types);
- $so_terms = split(' ',$allowed_types);
- $where_cvt = "";
- foreach ($so_terms as $term){
- $where_cvt .= "CVT.name = '$term' OR ";
- }
- $where_cvt = substr($where_cvt,0,strlen($where_cvt)-3); # strip trailing 'OR'
- // get the features for this organism
- $sql = "SELECT F.name,F.feature_id,F.uniquename,CVT.name as cvname ".
- "FROM {feature} F ".
- " INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id ".
- "WHERE organism_id = $node->organism_id and ($where_cvt) ".
- "ORDER BY feature_id ASC";
- // the counting SQL
- $csql = "SELECT count(*) ".
- "FROM {feature} F".
- " INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id ".
- "WHERE organism_id = $node->organism_id and ($where_cvt) ".
- "GROUP BY organism_id ";
- $previous_db = tripal_db_set_active('chado'); // use chado database
- $features = pager_query($sql,10,0,$csql);
- tripal_db_set_active($previous_db); // now use drupal database
- $content = "<br><div id=\"tripal_feature_box\" class=\"feature-info-box\">";
- $content .= "<div class=\"tripal_expandableBox\">".
- "<h3>Browse Features</h3>".
- "</div>";
- $content .= "<div class=\"tripal_expandableBoxContent\">";
- $content .= "Below are the features associated with this organism.\n";
- $content .= "<table class=\"tripal_table_horz\">";
- $content .= " <tr>";
- $content .= " <th>Feature Name</th>";
- $content .= " <th>Type</th>";
- $content .= " </tr>";
- // prepare the query that will lookup node ids
- $sql = "SELECT nid FROM {chado_feature} ".
- "WHERE feature_id = %d";
- while($feature = db_fetch_object($features)){
- $node = db_fetch_object(db_query($sql,$feature->feature_id));
- if($node){
- $name= "<a href=\"" . url("node/$node->nid") . "\">$feature->name</a>";
- } else {
- $name= "$feature->name";
- }
- $content .= " <tr>";
- $content .= " <td>$name</td>";
- $content .= " <td>$feature->cvname</td>";
- $content .= " </tr>";
- }
- $content .= "</table>";
- $content .= theme('pager');
- $content .= "</div></div>";
- return $content;
- }
- }
- /************************************************************************
- * This function is an extension of the chado_feature_view by providing
- * the markup for the feature object THAT WILL BE INDEXED.
- */
- function theme_tripal_feature_search_index ($node) {
- $feature = $node->feature;
- $content = '';
- // get the accession prefix
- $aprefix = variable_get('chado_feature_accession_prefix','ID');
- $content .= "<h1>$feature->uniquename</h1>. ";
- $content .= "<strong>$aprefix$feature->feature_id.</strong> ";
- $content .= "$feature->cvname ";
- $content .= "$feature->common_name ";
- // add the synonyms of this feature to the text for searching
- $synonyms = $node->synonyms;
- if(count($synonyms) > 0){
- foreach ($synonyms as $result){
- $content .= "$result->name ";
- }
- }
- return $content;
- }
- /************************************************************************
- * This function is an extension of the chado_feature_view by providing
- * the markup for the feature object THAT WILL BE INDEXED.
- */
- function theme_tripal_feature_search_results ($node) {
- $feature = $node->feature;
- $content = '';
- // get the accession prefix
- $aprefix = variable_get('chado_feature_accession_prefix','ID');
- $content .= "Feature Name: <h1>$feature->uniquename</h1>. ";
- $content .= "<strong>Accession: $aprefix$feature->feature_id.</strong>";
- $content .= "Type: $feature->cvname. ";
- $content .= "Organism: $feature->common_name. ";
- // add the synonyms of this feature to the text for searching
- $synonyms = $node->synonyms;
- if(count($synonyms) > 0){
- $content .= "Synonyms: ";
- foreach ($synonyms as $result){
- $content .= "$result->name, ";
- }
- }
- return $content;
- }
- /************************************************************************
- *
- */
- function tripal_feature_set_vocabulary (){
- //include the file containing the required functions for adding taxonomy vocabs
- module_load_include('inc', 'taxonomy', 'taxonomy.admin');
- // get the vocabularies so that we make sure we don't recreate
- // the vocabs that already exist
- $vocabularies = taxonomy_get_vocabularies();
- $ft_vid = NULL;
- $op_vid = NULL;
- $lb_vid = NULL;
- $an_vid = NULL;
- // These taxonomic terms are hard coded because we
- // konw we have these relationships in the chado tables
- // through foreign key relationships. The tripal
- // modules that correspond to these chado "modules" don't
- // need to be installed for the taxonomy to work.
- foreach($vocabularies as $vocab){
- if($vocab->name == 'Feature Type'){
- $ft_vid = $vocab->vid;
- }
- if($vocab->name == 'Organism'){
- $op_vid = $vocab->vid;
- }
- if($vocab->name == 'Library'){
- $lb_vid = $vocab->vid;
- }
- if($vocab->name == 'Analysis'){
- $an_vid = $vocab->vid;
- }
- }
- if(!$ft_vid){
- $form_state = array();
- $values = array(
- 'name' => t('Feature Type'),
- 'nodes' => array('chado_feature' => 'chado_feature'),
- 'description' => t('The feature type (or SO cvterm for this feature).'),
- 'help' => t('Select the term that matches the feature '),
- 'tags' => 0,
- 'hierarchy' => 1,
- 'relations' => 1,
- 'multiple' => 0,
- 'required' => 0,
- 'weight' => 1,
- );
- drupal_execute('taxonomy_form_vocabulary', $form_state,$values);
- drupal_execute('taxonomy_form_vocabulary', $form_state);
- }
- if(!$op_vid){
- $form_state = array();
- $values = array(
- 'name' => t('Organism'),
- 'nodes' => array('chado_feature' => 'chado_feature'),
- 'description' => t('The organism to which this feature belongs.'),
- 'help' => t('Select the term that matches the feature '),
- 'tags' => 0,
- 'hierarchy' => 1,
- 'relations' => 1,
- 'multiple' => 0,
- 'required' => 0,
- 'weight' => 2,
- );
- drupal_execute('taxonomy_form_vocabulary', $form_state,$values);
- drupal_execute('taxonomy_form_vocabulary', $form_state);
- }
- if(!$lb_vid){
- $form_state = array();
- $values = array(
- 'name' => t('Library'),
- 'nodes' => array('chado_feature' => 'chado_feature'),
- 'description' => t('Chado features associated with a library are assigned the term associated with the library'),
- 'help' => t('Select the term that matches the feature '),
- 'tags' => 0,
- 'hierarchy' => 1,
- 'relations' => 1,
- 'multiple' => 0,
- 'required' => 0,
- 'weight' => 3,
- );
- drupal_execute('taxonomy_form_vocabulary', $form_state, $values);
- drupal_execute('taxonomy_form_vocabulary', $form_state);
- }
- if(!$an_vid){
- $form_state = array();
- $values = array(
- 'name' => t('Analysis'),
- 'nodes' => array('chado_feature' => 'chado_feature'),
- 'description' => t('Any analysis to which this feature belongs.'),
- 'help' => t('Select the term that matches the feature '),
- 'tags' => 0,
- 'hierarchy' => 1,
- 'relations' => 1,
- 'multiple' => 1,
- 'required' => 0,
- 'weight' => 4,
- );
- drupal_execute('taxonomy_form_vocabulary', $form_state,$values);
- drupal_execute('taxonomy_form_vocabulary', $form_state);
- }
- }
- /************************************************************************
- *
- */
- function tripal_feature_del_vocabulary(){
- //include the file containing the required functions for adding taxonomy vocabs
- module_load_include('inc', 'taxonomy', 'taxonomy.admin');
- // get the vocabularies
- $vocabularies = taxonomy_get_vocabularies();
- // These taxonomic terms are hard coded because we
- // know we have these relationships in the chado tables
- // through foreign key relationships. The tripal
- // modules that correspond to these chado "modules" don't
- // need to be installed for the taxonomy to work.
- foreach($vocabularies as $vocab){
- if($vocab->name == 'Feature Type'){
- taxonomy_del_vocabulary($vocab->vid);
- }
- if($vocab->name == 'Organism'){
- taxonomy_del_vocabulary($vocab->vid);
- }
- if($vocab->name == 'Library'){
- taxonomy_del_vocabulary($vocab->vid);
- }
- if($vocab->name == 'Analysis'){
- taxonomy_del_vocabulary($vocab->vid);
- }
- }
- }
- /************************************************************************
- *
- */
- function tripal_features_set_taxonomy($max_sync = 0,$job_id = NULL){
- // make sure our vocabularies are cleaned and reset before proceeding
- tripal_feature_del_vocabulary();
- tripal_feature_set_vocabulary();
- // iterate through all drupal feature nodes and set the taxonomy
- $results = db_query("SELECT * FROM {chado_feature}");
- $nsql = "SELECT * FROM {node} ".
- "WHERE nid = %d";
- $i = 0;
- // load into ids array
- $count = 0;
- $chado_features = array();
- while($chado_feature = db_fetch_object($results)){
- $chado_features[$count] = $chado_feature;
- $count++;
- }
- // Iterate through features that need to be synced
- $interval = intval($count * 0.01);
- foreach($chado_features as $chado_feature){
- // update the job status every 1% features
- if($job_id and $i % $interval == 0){
- tripal_job_set_progress($job_id,intval(($i/$count)*100));
- }
- $node = db_fetch_object(db_query($nsql,$chado_feature->nid));
- tripal_feature_set_taxonomy($node,$chado_feature->feature_id);
- $i++;
- }
- }
- /************************************************************************
- *
- */
- function tripal_feature_set_taxonomy ($node,$feature_id){
- // iterate through the taxonomy classes that have been
- // selected by the admin user and make sure we only set those
- $tax_classes = variable_get('tax_classes', '');
- $do_ft = 0;
- $do_op = 0;
- $do_lb = 0;
- $do_an = 0;
- foreach($tax_classes as $class){
- if(strcmp($class ,'organism')==0){
- $do_op = 1;
- }
- if(strcmp($class,'feature_type')==0){
- $do_ft = 1;
- }
- if(strcmp($class,'library')==0){
- $do_lb = 1;
- }
- if(strcmp($class,'analysis')==0){
- $do_an = 1;
- }
- }
- // get the list of vocabularies and find our two vocabularies of interest
- $vocabularies = taxonomy_get_vocabularies();
- $ft_vid = NULL;
- $op_vid = NULL;
- $lb_vid = NULL;
- $an_vid = NULL;
- foreach($vocabularies as $vocab){
- if($vocab->name == 'Feature Type'){
- $ft_vid = $vocab->vid;
- }
- if($vocab->name == 'Organism'){
- $op_vid = $vocab->vid;
- }
- if($vocab->name == 'Library'){
- $lb_vid = $vocab->vid;
- }
- if($vocab->name == 'Analysis'){
- $an_vid = $vocab->vid;
- }
- }
- // get the cvterm and the organism for this feature
- $sql = "SELECT CVT.name AS cvname, O.genus, O.species ".
- "FROM {CVTerm} CVT ".
- " INNER JOIN Feature F on F.type_id = CVT.cvterm_id ".
- " INNER JOIN Organism O ON F.organism_id = O.organism_id ".
- "WHERE F.feature_id = $feature_id";
- $previous_db = tripal_db_set_active('chado'); // use chado database
- $feature = db_fetch_object(db_query($sql));
- tripal_db_set_active($previous_db); // now use drupal database
- // Set the feature type for this feature
- if($do_ft && $ft_vid){
- $tags["$ft_vid"] = "$feature->cvname";
- }
- // Set the organism for this feature type
- if($do_op && $op_vid){
- $tags["$op_vid"] = "$feature->genus $feature->species";
- }
- // get the library that this feature may belong to and add it as taxonomy
- if($do_lb && $lb_vid){
- $sql = "SELECT L.name ".
- "FROM {Library} L ".
- " INNER JOIN Library_feature LF ON LF.library_id = L.library_id ".
- "WHERE LF.feature_id = %d ";
- $previous_db = tripal_db_set_active('chado'); // use chado database
- $library = db_fetch_object(db_query($sql,$feature_id));
- tripal_db_set_active($previous_db); // now use drupal database
- $tags["$lb_vid"] = "$library->name";
- }
- // now add the taxonomy to the node
- $terms['tags'] = $tags;
- taxonomy_node_save($node,$terms);
- // print "Setting $node->name: " . implode(", ",$tags) . "\n";
- // get the analysis that this feature may belong to and add it as taxonomy
- // We'll add each one individually since there may be more than one analysis
- if($do_an && $an_vid){
- $sql = "SELECT A.name ".
- "FROM {Analysis} A ".
- " INNER JOIN Analysisfeature AF ON AF.analysis_id = A.analysis_id ".
- "WHERE AF.feature_id = $feature_id ";
- $results = db_query($sql);
- $previous_db = tripal_db_set_active('chado'); // use chado database
- $analysis_terms = array();
- while($analysis=db_fetch_object($results)){
- $tags2["$an_vid"] = "$analysis->name";
- $terms['tags'] = $tags2;
- taxonomy_node_save($node,$terms);
- }
- tripal_db_set_active($previous_db); // now use drupal database
- }
- }
- /************************************************************************
- *
- */
- function tripal_features_cleanup($dummy = NULL, $job_id = NULL) {
- // build the SQL statments needed to check if nodes point to valid features
- $dsql = "SELECT * FROM {node} WHERE type = 'chado_feature' order by nid";
- $nsql = "SELECT * FROM {node} WHERE nid = %d";
- $csql = "SELECT * FROM {chado_feature} where nid = %d ";
- $cfsql= "SELECT * FROM {chado_feature}";
- $tsql = "SELECT * FROM {feature} F ".
- " INNER JOIN CVTerm CVT ON F.type_id = CVT.cvterm_id ".
- "WHERE feature_id = %d AND (";
- $supported_ftypes = split("[ \n]",variable_get('chado_feature_types','EST contig'));
- foreach($supported_ftypes as $ftype){
- $tsql .= " CVT.name = '$ftype' OR ";
- }
- $tsql .= " 0=1) "; // add a 0=1 just as a filler so we don't have to remove a trailing 'OR'
- // load into nodes array
- $results = db_query($dsql);
- $count = 0;
- $nodes = array();
- while($node = db_fetch_object($results)){
- $nodes[$count] = $node;
- $count++;
- }
- // load the chado_features into an array
- $results = db_query($cfsql);
- $cnodes = array();
- while($node = db_fetch_object($results)){
- $cnodes[$count] = $node;
- $count++;
- }
- $interval = intval($count * 0.01);
- // iterate through all of the chado_feature nodes and delete those that aren't valid
- foreach($nodes as $nid){
- // update the job status every 1% features
- if($job_id and $i % $interval == 0){
- tripal_job_set_progress($job_id,intval(($i/$count)*100));
- }
- // first check to see if the node has a corresponding entry
- // in the chado_feature table. If not then delete the node.
- $feature = db_fetch_object(db_query($csql,$nid->nid));
- if(!$feature){
- node_delete($nid->nid);
- $message = "Missing in chado_feature table.... DELETING: $nid->nid\n";
- watchdog('tripal_feature',$message,array(),WATCHDOG_WARNING);
- continue;
- }
- // second check to see if the node is for a feature of an allowed type.
- // if not, then delete the node. This check will also take care of the
- // case when a node exists and an entry in the chado_feature table exists
- // but no feature with a matching feature_id exists
- $previous_db = tripal_db_set_active('chado'); // use chado database
- $ftype = db_fetch_object(db_query($tsql,$feature->feature_id));
- tripal_db_set_active($previous_db); // now use drupal database
- if(!$ftype){
- node_delete($nid->nid);
- db_query("DELETE FROM {chado_feature} WHERE feature_id = $feature->feature_id");
- $message = "Node of the wrong feature type.... DELETING: $nid->nid\n";
- watchdog('tripal_feature',$message,array(),WATCHDOG_WARNING);
- }
- $i++;
- }
- // iterate through all of the chado_feature nodes and delete those that aren't valid
- foreach($cnodes as $nid){
- // update the job status every 1% features
- if($job_id and $i % $interval == 0){
- tripal_job_set_progress($job_id,intval(($i/$count)*100));
- }
- $node = db_fetch_object(db_query($nsql,$nid->nid));
- if(!$node){
- db_query("DELETE FROM {chado_feature} WHERE nid = $nid->nid");
- $message = "chado_feature missing node.... DELETING: $nid->nid\n";
- watchdog('tripal_feature',$message,array(),WATCHDOG_WARNING);
- }
- $i++;
- }
- return '';
- }
- /************************************************************************
- *
- */
- function tripal_feature_return_fasta($feature,$desc){
- $fasta = ">" . variable_get('chado_feature_accession_prefix','ID') . "$feature->feature_id|$feature->name";
- $fasta .= " $desc\n";
- $fasta .= wordwrap($feature->residues, 50, "\n", true);
- $fasta .= "\n\n";
- return $fasta;
- }
|