Tripal v1.0 (6.x-1.0)
trees.inc
Go to the documentation of this file.
00001 <?php
00002 
00013 function tripal_cv_show_browser() {
00014 
00015   $content = drupal_get_form('tripal_cv_list_form');
00016   $content .= "
00017     <div id=\"cv_browser\"></div>
00018   ";
00019 
00020   return $content;
00021 }
00022 
00023 
00118 function tripal_cv_tree($tree_id) {
00119   // parse out the tripal module name from the chart_id to find out
00120   // which Tripal "hook" to call:
00121   $tripal_mod = preg_replace("/^(tripal_.+?)_cv_tree_(.+)$/", "$1", $tree_id);
00122   if ($tripal_mod) {
00123     $callback = $tripal_mod . "_cv_tree";
00124 
00125     // now call the function in the module responsible for the tree.  This
00126     // should call the tripal_cv_init_cv with the proper parameters set for
00127     // getting the cv_id of the vocabulary to use
00128     $opt = call_user_func_array($callback, array($tree_id));
00129 
00130     // we only need to return the cv_id for this function call.
00131     $json_array[] = $opt['cv_id'];
00132   }
00133 
00134   $json_array[] = $tree_id;
00135   return drupal_json($json_array);
00136 }
00137 
00138 
00143 function tripal_cv_update_tree() {
00144   $content = array();
00145   $ontology = 'sequence';
00146 
00147   // get the id of the term to look up
00148   $cv = check_plain($_REQUEST['cv']);
00149   $term = check_plain($_REQUEST['term']);
00150   $tree_id = check_plain($_REQUEST['tree_id']);
00151 
00152   // get the options needed for this tree from the tripal module that
00153   // wants to create the tree
00154   $tripal_mod = preg_replace("/^(tripal_.+?)_cv_tree_(.+)$/", "$1", $tree_id);
00155   if ($tripal_mod) {
00156     $callback = $tripal_mod . "_cv_tree";
00157     $opt = call_user_func_array($callback, array($tree_id));
00158   }
00159 
00160   // get the CV root terms
00161   if (strcmp($term, 'root')==0) {
00162     if (!$cv) {
00163       $cv = $opt['cv_id'];
00164     }
00165     $content = tripal_cv_init_tree($cv, $opt['count_mview'],
00166       $opt['cvterm_id_column'], $opt['count_column'], $opt['filter'], $opt['label']);
00167 
00168   // get the children terms
00169   }
00170   else {
00171     $content = tripal_cv_get_term_children($term, $opt['count_mview'],
00172       $opt['cvterm_id_column'], $opt['count_column'], $opt['filter'], $opt['label']);
00173   }
00174 
00175   drupal_json($content);
00176 }
00177 
00187 function tripal_cv_init_tree($cv_id, $cnt_table = NULL, $fk_column = NULL,
00188   $cnt_column = NULL, $filter = NULL, $label = NULL) {
00189 
00190   // get the list of root terms for the provided CV
00191   $sql = "
00192     SELECT *
00193     FROM {cv_root_mview} CRM
00194     WHERE cv_id = %d
00195   ";
00196   $results = chado_query($sql, $cv_id);
00197 
00198   // prepare the SQL statement that will allow us to pull out count
00199   // information for each term in the tree.
00200   if ($cnt_table) {
00201     if (!$filter) {
00202       $filter = '(1=1)';
00203     }
00204     $cnt_sql = "
00205        SELECT CVT.name, CVT.cvterm_id, CNT.$cnt_column as num_items
00206        FROM {$cnt_table} CNT
00207         INNER JOIN cvterm CVT on CNT.$fk_column = CVT.cvterm_id
00208        WHERE $filter AND CVT.cvterm_id = %d
00209        ORDER BY $cnt_column desc
00210     ";
00211   }
00212 
00213   while ($term = db_fetch_object($results)) {
00214     $name = $term->name;
00215     $count = 0;
00216     if ($cnt_table) {
00217       $cnt_results = chado_query($cnt_sql, $term->cvterm_id);
00218       while ($cnt = db_fetch_object($cnt_results)) {
00219         $count += $cnt->cnt;
00220       }
00221       if ($count > 0) {
00222         $name .= " ($count $label(s))";
00223       }
00224     }
00225     $content[] = array(
00226       'attributes' => array(
00227         'id' => $term->cvterm_id,
00228       ),
00229       'state' => 'closed',
00230       'data' => $name,
00231       'children' => array(),
00232     );
00233   }
00234 
00235   return $content;
00236 
00237 }
00238 
00247 function tripal_cv_get_term_children($cvterm_id, $cnt_table = NULL,
00248   $fk_column = NULL, $cnt_column = NULL, $filter = NULL, $label = NULL) {
00249 
00250   // get the children for the term provided
00251   $sql = "
00252     SELECT CVTR.cvterm_relationship_id,CVTR.subject_id,
00253        CVT1.name as subject_name, CVT3.name as type_name, CVTR.type_id,
00254        CVT2.name as object_name,CVTR.object_id
00255     FROM {cvterm_relationship} CVTR
00256        INNER JOIN CVTerm CVT1 on CVTR.subject_id = CVT1.cvterm_id
00257        INNER JOIN CVTerm CVT2 on CVTR.object_id = CVT2.cvterm_id
00258        INNER JOIN CVTerm CVT3 on CVTR.type_id = CVT3.cvterm_id
00259        INNER JOIN CV on CV.cv_id = CVT1.cv_id
00260     WHERE CVTR.object_id = %d
00261     ORDER BY CVT1.name
00262   ";
00263   $results = chado_query($sql, $cvterm_id);
00264 
00265   // prepare the SQL statement that will allow us to pull out count
00266   // information for each term in the tree.
00267   if ($cnt_table) {
00268     if (!$filter) {
00269       $filter = '(1=1)';
00270     }
00271     $cnt_sql = "
00272        SELECT CVT.name, CVT.cvterm_id, CNT.$cnt_column as num_items
00273        FROM {$cnt_table} CNT
00274         INNER JOIN cvterm CVT on CNT.$fk_column = CVT.cvterm_id
00275        WHERE $filter AND CVT.cvterm_id = %d
00276        ORDER BY $cnt_column desc
00277     ";
00278   }
00279 
00280   // populate the JSON content array
00281   while ($term = db_fetch_object($results)) {
00282     // count the number of items per term if requested
00283     $name = $term->subject_name;
00284     $count = 0;
00285     if ($cnt_table) {
00286       $cnt_results = chado_query($cnt_sql, $term->subject_id);
00287       while ($cnt = db_fetch_object($cnt_results)) {
00288         $count += $cnt->num_items;
00289       }
00290       if ($count > 0) {
00291         $name .= " (" . number_format($count) . " $label)";
00292 
00293         // check if we have any children if so then set the value
00294         $children = db_fetch_object(chado_query($sql, $term->subject_id));
00295         $state = 'leaf';
00296         if ($children) {
00297           $state = 'closed';
00298         }
00299         $content[] = array(
00300           'attributes' => array(
00301             'id' => $term->subject_id,
00302           ),
00303           'state' => $state,
00304           'data' => $name,
00305           'children' => array(),
00306         );
00307       }
00308     }
00309     else {
00310       // check if we have any children if so then set the value
00311       $children = db_fetch_object(chado_query($sql, $term->subject_id));
00312       $state = 'leaf';
00313       if ($children) {
00314         $state = 'closed';
00315       }
00316       $content[] = array(
00317         'attributes' => array(
00318            'id' => $term->subject_id,
00319         ),
00320         'state' => $state,
00321         'data' => $name,
00322         'children' => array(),
00323       );
00324     }
00325   }
00326   $content[] = $cnt_sql;
00327 
00328   return $content;
00329 }
00330 
00335 function tripal_cv_init_browser($cv_id) {
00336 
00337   $content = "
00338     <div id=\"tripal_cv_cvterm_info_box\">
00339       <a href=\"#\" onclick=\"$('#tripal_cv_cvterm_info_box').hide()\" style=\"float: right\">Close [X]</a>
00340       <h3>Term Information</h3>
00341       <div id=\"tripal_cv_cvterm_info\"></div>
00342     </div>
00343     <div id=\"tripal_ajaxLoading\" style=\"display:none\">
00344       <div id=\"loadingText\">Loading...</div>
00345       <img src=\"$url\">
00346     </div>
00347     <h3>Tree Browser</h3>
00348     <div id=\"browser\"</div>
00349     </div>
00350   ";
00351 
00352   drupal_json(array('update' => "$content"));
00353 
00354 }
00355 
00361 function tripal_cv_cvterm_info($cvterm_id) {
00362 
00363   // get the id of the term to look up
00364   $cv = check_plain($_REQUEST['cv']);
00365   $tree_id = check_plain($_REQUEST['tree_id']);
00366 
00367   // first get any additional information to add to the cvterm
00368   if (strcmp($tree_id, 'undefined') != 0) {
00369     $tripal_mod = preg_replace("/^(tripal_.+?)_cv_tree_(.+)$/", "$1", $tree_id);
00370     if ($tripal_mod) {
00371       $callback = $tripal_mod . "_cvterm_add";
00372       $opt = call_user_func_array($callback, array($cvterm_id, $tree_id));
00373     }
00374   }
00375 
00376   $sql = "
00377     SELECT CVT.name as cvtermname, CVT.definition, CV.name as cvname,
00378        DBX.accession,DB.urlprefix,DB.db_id,DB.name as dbname
00379     FROM {CVTerm} CVT
00380       INNER JOIN CV on CVT.cv_id = CV.cv_id
00381       INNER JOIN dbxref DBX on CVT.dbxref_id = DBX.dbxref_id
00382       INNER JOIN DB on DBX.db_id = DB.db_id
00383     WHERE CVT.cvterm_id = %d
00384   ";
00385   $cvterm = db_fetch_object(chado_query($sql, $cvterm_id));
00386 
00387   $sql = "
00388     SELECT CVTS.synonym, CVT.name as cvname
00389     FROM {cvtermsynonym} CVTS
00390       INNER JOIN cvterm CVT on CVTS.type_id = CVT.cvterm_id
00391     WHERE CVTS.cvterm_id = %d
00392 
00393   ";
00394   $results = chado_query($sql, $cvterm_id);
00395   while ($synonym = db_fetch_object($results)) {
00396     $synonym_rows .= "<b>$synonym->cvname:</b>  $synonym->synonym<br />";
00397   }
00398 
00399   $accession = $cvterm->accession;
00400   if ($cvterm->urlprefix) {
00401     $accession = "<a href=\"$cvterm->urlprefix$cvterm->accession\">$cvterm->accession</a>";
00402   }
00403 
00404   $content = "
00405     <div id=\"cvterm\">
00406     <table>
00407       <tr><th>Term</th><td>$cvterm->cvtermname</td></tr>
00408       <tr><th>Accession</th><td>$accession</td></tr>
00409       <tr><th>Ontology</th><td>$cvterm->cvname</td></tr>
00410       <tr><th>Definition</th><td>$cvterm->definition</td></tr>
00411       <tr><th>Synonyms</th><td>$synonym_rows</td></tr>
00412       <tr><th>Internal ID</th><td>$cvterm_id</td></tr>
00413   ";
00414 
00415   // now add in any additional options from a hook
00416   if ($opt) {
00417     foreach ($opt as $key => $value) {
00418       $content .= "<tr><th>$key</th><td>$value</td>";
00419     }
00420   }
00421 
00422   // close out the information table
00423   $content .= "
00424     </table>
00425     </div>
00426   ";
00427   drupal_json(array('update' => $content));
00428 }
00429 
00435 function tripal_cv_list_form($form_state) {
00436 
00437   // get a list of db from chado for user to choose
00438   $sql = "
00439     SELECT DISTINCT CV.name,CV.cv_id
00440     FROM {cvterm_relationship} CVTR
00441        INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
00442        INNER JOIN CV on CV.cv_id = CVT.cv_id
00443   ";
00444   $results = chado_query($sql);
00445 
00446   $blastdbs = array();
00447   $cvs[''] = '';
00448   while ($cv = db_fetch_object($results)) {
00449     $cvs[$cv->cv_id] = $cv->name;
00450   }
00451 
00452   $form['db_options'] = array(
00453     '#type' => 'value',
00454     '#value' => $cvs
00455   );
00456 
00457   $form['cv_list'] = array(
00458     '#title' => t('CVs with relationships'),
00459     '#type' => 'select',
00460     '#description' => t('Choose the controlled vocabulary to browse'),
00461     '#options' => $form['db_options']['#value'],
00462     '#attributes' => array(
00463       'onChange' => "return tripal_cv_init_browser(this)",
00464     )
00465   );
00466 
00467   return $form;
00468 }
 All Classes Files Functions Variables