tripal_analysis_unigene.module 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. <?php
  2. function tripal_analysis_unigene_init(){
  3. // Add style sheet
  4. drupal_add_css(drupal_get_path('theme', 'tripal').'/css/tripal_analysis_unigene.css');
  5. }
  6. /**
  7. *
  8. */
  9. function tripal_analysis_unigene_node_info() {
  10. $nodes = array();
  11. $nodes['chado_analysis_unigene'] = array(
  12. 'name' => t('Analysis: Unigene'),
  13. 'module' => 'chado_analysis_unigene',
  14. 'description' => t('A unigene assembly constructed from transcriptomic reads.'),
  15. 'has_title' => FALSE,
  16. 'title_label' => t('Analysis: Unigene'),
  17. 'has_body' => FALSE,
  18. 'body_label' => t('Unigene Analysis Description'),
  19. 'locked' => TRUE
  20. );
  21. return $nodes;
  22. }
  23. /*******************************************************************************
  24. * Set the permission types that the chado module uses. Essentially we
  25. * want permissionis that protect creation, editing and deleting of chado
  26. * data objects
  27. */
  28. function tripal_analysis_unigene_perm(){
  29. return array(
  30. 'access chado_analysis_unigene content',
  31. 'create chado_analysis_unigene content',
  32. 'delete chado_analysis_unigene content',
  33. 'edit chado_analysis_unigene content',
  34. );
  35. }
  36. /*******************************************************************************
  37. * The following function proves access control for users trying to
  38. * perform actions on data managed by this module
  39. */
  40. function chado_analysis_unigene_access($op, $node, $account){
  41. if ($op == 'create') {
  42. return user_access('create chado_analysis_unigene content', $account);
  43. }
  44. if ($op == 'update') {
  45. if (user_access('edit chado_analysis_unigene content', $account)) {
  46. return TRUE;
  47. }
  48. }
  49. if ($op == 'delete') {
  50. if (user_access('delete chado_analysis_unigene content', $account)) {
  51. return TRUE;
  52. }
  53. }
  54. if ($op == 'view') {
  55. if (user_access('access chado_analysis_unigene content', $account)) {
  56. return TRUE;
  57. }
  58. }
  59. return FALSE;
  60. }
  61. /**
  62. *
  63. */
  64. function chado_analysis_unigene_form ($node){
  65. $unigene = $node->analysis->tripal_analysis_unigene;
  66. // set the form defaults
  67. $unigene_name = $node->unigene_name;
  68. if(!$unigene_name){
  69. $unigene_name = $unigene->unigene_name;
  70. }
  71. $num_reads = $node->num_reads;
  72. if(!$num_reads){
  73. $num_reads = $unigene->num_reads;
  74. }
  75. $avg_length = $node->avg_length;
  76. if(!$avg_length){
  77. $avg_length = $unigene->avg_length;
  78. }
  79. $num_clusters = $node->num_clusters;
  80. if(!$num_clusters){
  81. $num_clusters = $unigene->num_clusters;
  82. }
  83. $num_contigs = $node->num_contigs;
  84. if(!$num_contigs){
  85. $num_contigs = $unigene->num_contigs;
  86. }
  87. $num_singlets = $node->num_singlets;
  88. if(!$num_singlets){
  89. $num_singlets = $unigene->num_singlets;
  90. }
  91. $contig_min_len= $node->contig_min_len;
  92. if(!$contig_min_len){
  93. $contig_min_len = $unigene->contig_min_len;
  94. }
  95. $contig_max_len= $node->contig_max_len;
  96. if(!$contig_max_len){
  97. $contig_max_len = $unigene->contig_max_len;
  98. }
  99. $contig_avg_len= $node->contig_avg_len;
  100. if(!$contig_avg_len){
  101. $contig_avg_len = $unigene->contig_avg_len;
  102. }
  103. $singlet_min_len= $node->singlet_min_len;
  104. if(!$singlet_min_len){
  105. $singlet_min_len = $unigene->singlet_min_len;
  106. }
  107. $singlet_max_len= $node->singlet_max_len;
  108. if(!$singlet_max_len){
  109. $singlet_max_len = $unigene->singlet_max_len;
  110. }
  111. $singlet_avg_len= $node->singlet_avg_len;
  112. if(!$singlet_avg_len){
  113. $singlet_avg_len = $unigene->singlet_avg_len;
  114. }
  115. // add in the default fields for an analysis
  116. $form = chado_analysis_form($node);
  117. $form['unigene_name'] = array(
  118. '#title' => t('Unigene Name'),
  119. '#type' => 'textfield',
  120. '#required' => TRUE,
  121. '#description' => t('A distinct name used to identify this unigene'),
  122. '#default_value' => $unigene_name,
  123. );
  124. $form['unigene_stats'] = array(
  125. '#title' => t('Unigene Stats'),
  126. '#type' => 'fieldset',
  127. '#description' => t('Unigene, contig and singlet statistics'),
  128. '#collapsible' => TRUE,
  129. );
  130. $form['unigene_stats']['num_reads'] = array(
  131. '#title' => t('Number of Reads'),
  132. '#type' => 'textfield',
  133. '#required' => FALSE,
  134. '#description' => t('Provide the number of reads, after filtering that were used for input into the assembly'),
  135. '#default_value' => $num_reads,
  136. );
  137. $form['unigene_stats']['avg_length'] = array(
  138. '#title' => t('Average Contig Length'),
  139. '#type' => 'textfield',
  140. '#required' => FALSE,
  141. '#description' => t('Provide the average sequence length in the ungiene (average of contigs and signlets).'),
  142. '#default_value' => $avg_length,
  143. );
  144. $form['unigene_stats']['num_clusters'] = array(
  145. '#title' => t('Number of Clusters'),
  146. '#type' => 'textfield',
  147. '#required' => FALSE,
  148. '#description' => t('Provide the number of clusters generated by the asssembly if a clustering mechanism was used for unigene constructions'),
  149. '#default_value' => $num_clusters,
  150. );
  151. $form['unigene_stats']['num_contigs'] = array(
  152. '#title' => t('Number of Contigs'),
  153. '#type' => 'textfield',
  154. '#required' => FALSE,
  155. '#description' => t('Provide the number of contigs generated by the assembly'),
  156. '#default_value' => $num_contigs,
  157. );
  158. $form['unigene_stats']['num_singlets'] = array(
  159. '#title' => t('Number of Singlets'),
  160. '#type' => 'textfield',
  161. '#required' => FALSE,
  162. '#description' => t('Provide the number of singlets remaining in the assembly'),
  163. '#default_value' => $num_singlets,
  164. );
  165. $form['contig_stats'] = array(
  166. '#title' => t('Contig Stats'),
  167. '#type' => 'fieldset',
  168. '#description' => t('Unigene Contig statistics'),
  169. '#collapsible' => TRUE,
  170. );
  171. $form['contig_stats']['contig_min_len'] = array(
  172. '#title' => t('Minimum Contig Length'),
  173. '#type' => 'textfield',
  174. '#required' => FALSE,
  175. '#description' => t('Provide the length of the smallest contig'),
  176. '#default_value' => $contig_min_len,
  177. );
  178. $form['contig_stats']['contig_max_len'] = array(
  179. '#title' => t('Maximum Contig Length'),
  180. '#type' => 'textfield',
  181. '#required' => FALSE,
  182. '#description' => t('Provide the length of the largest contig'),
  183. '#default_value' => $contig_max_len,
  184. );
  185. $form['contig_stats']['contig_avg_len'] = array(
  186. '#title' => t('Average Contig Length'),
  187. '#type' => 'textfield',
  188. '#required' => FALSE,
  189. '#description' => t('Provide the average contig length'),
  190. '#default_value' => $contig_avg_len,
  191. );
  192. $form['singlet_stats'] = array(
  193. '#title' => t('Singlet Stats'),
  194. '#type' => 'fieldset',
  195. '#description' => t('Unigene Singlet statistics'),
  196. '#collapsible' => TRUE,
  197. );
  198. $form['singlet_stats']['singlet_min_len'] = array(
  199. '#title' => t('Minimum Singlet Length'),
  200. '#type' => 'textfield',
  201. '#required' => FALSE,
  202. '#description' => t('Provide the length of the smallest singlet'),
  203. '#default_value' => $singlet_min_len,
  204. );
  205. $form['singlet_stats']['singlet_max_len'] = array(
  206. '#title' => t('Maximum Singlet Length'),
  207. '#type' => 'textfield',
  208. '#required' => FALSE,
  209. '#description' => t('Provide the length of the largest singlet'),
  210. '#default_value' => $singlet_max_len,
  211. );
  212. $form['singlet_stats']['singlet_avg_len'] = array(
  213. '#title' => t('Average Singlet Length'),
  214. '#type' => 'textfield',
  215. '#required' => FALSE,
  216. '#description' => t('Provide the average singlet length'),
  217. '#default_value' => $singlet_avg_len,
  218. );
  219. return $form;
  220. }
  221. /**
  222. *
  223. */
  224. function chado_analysis_unigene_validate($node, &$form){
  225. // use the analysis parent to validate the node
  226. tripal_analysis_validate($node, $form);
  227. }
  228. /**
  229. *
  230. */
  231. function chado_analysis_unigene_insert($node){
  232. // insert the analysis
  233. chado_analysis_insert($node);
  234. // add the unigene name as a property of the anslysis
  235. tripal_analysis_insert_property($node->analysis->analysis_id,'analysis_type','tripal_analysis_unigene');
  236. tripal_analysis_insert_property($node->analysis->analysis_id,'analysis_unigene_name',$node->unigene_name);
  237. tripal_analysis_insert_property($node->analysis->analysis_id,'analysis_unigene_num_contigs',$node->num_contigs);
  238. tripal_analysis_insert_property($node->analysis->analysis_id,'analysis_unigene_num_reads',$node->num_reads);
  239. tripal_analysis_insert_property($node->analysis->analysis_id,'analysis_unigene_avg_length',$node->avg_length);
  240. tripal_analysis_insert_property($node->analysis->analysis_id,'analysis_unigene_num_clusters',$node->num_clusters);
  241. tripal_analysis_insert_property($node->analysis->analysis_id,'analysis_unigene_num_singlets',$node->num_clusters);
  242. }
  243. /**
  244. *
  245. */
  246. function chado_analysis_unigene_update($node){
  247. chado_analysis_update($node);
  248. tripal_analysis_update_property($node->analysis_id,'analysis_unigene_name',$node->unigene_name,1);
  249. tripal_analysis_update_property($node->analysis_id,'analysis_unigene_num_contigs',$node->num_contigs,1);
  250. tripal_analysis_update_property($node->analysis_id,'analysis_unigene_num_reads',$node->num_reads,1);
  251. tripal_analysis_update_property($node->analysis_id,'analysis_unigene_avg_length',$node->avg_length,1);
  252. tripal_analysis_update_property($node->analysis_id,'analysis_unigene_num_clusters',$node->num_clusters,1);
  253. tripal_analysis_update_property($node->analysis_id,'analysis_unigene_num_singlets',$node->num_singlets,1);
  254. }
  255. /**
  256. *
  257. */
  258. function chado_analysis_unigene_delete($node){
  259. chado_analysis_delete($node);
  260. }
  261. /**
  262. *
  263. */
  264. function chado_analysis_unigene_view ($node, $teaser = FALSE, $page = FALSE) {
  265. // use drupal's default node view:
  266. $node = node_prepare($node, $teaser);
  267. return $node;
  268. }
  269. /**
  270. *
  271. */
  272. function chado_analysis_unigene_load($node){
  273. // load the default set of analysis fields
  274. $additions = chado_analysis_load($node);
  275. // create some variables for easier lookup
  276. $analysis = $additions->analysis;
  277. $analysis_id = $analysis->analysis_id;
  278. // add in the properties
  279. $unigene_name = tripal_analysis_get_property($analysis_id,'analysis_unigene_name');
  280. $num_contigs = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_contigs');
  281. $num_reads = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_reads');
  282. $avg_length = tripal_analysis_get_property($analysis_id,'analysis_unigene_avg_length');
  283. $num_clusters = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_clusters');
  284. $num_singlets = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_singlets');
  285. $analysis->tripal_analysis_unigene->unigene_name = $unigene_name->value;
  286. $analysis->tripal_analysis_unigene->num_contigs = $num_contigs->value;
  287. $analysis->tripal_analysis_unigene->num_reads = $num_reads->value;
  288. $analysis->tripal_analysis_unigene->avg_length = $avg_length->value;
  289. $analysis->tripal_analysis_unigene->num_clusters = $num_clusters->value;
  290. $analysis->tripal_analysis_unigene->num_singlets = $num_singlets->value;
  291. // add in organism information using the materialized view
  292. $sql = "SELECT * FROM {organism_unigene_mview} OUM ".
  293. " INNER JOIN {organism} O on OUM.organism_id = O.organism_id ".
  294. "WHERE OUM.analysis_id = %d ".
  295. "ORDER BY O.genus, O.species";
  296. $previous_db = tripal_db_set_active('chado'); // use chado database
  297. $organisms = db_query($sql,$analysis_id);
  298. tripal_db_set_active($previous_db); // now use drupal database
  299. while($organism = db_fetch_object($organisms)){
  300. $sql = "SELECT nid FROM {chado_organism} WHERE organism_id = %d";
  301. $c_org = db_fetch_object(db_query($sql,$organism->organism_id));
  302. $organism->nid = $c_org->nid;
  303. $analysis->tripal_analysis_unigene->organisms[] = $organism;
  304. }
  305. return $additions;
  306. }
  307. /**
  308. *
  309. *
  310. * @ingroup tripal_analysis_unigene
  311. */
  312. function tripal_analysis_unigene_block($op = 'list', $delta = 0, $edit=array()){
  313. switch($op) {
  314. case 'list':
  315. $blocks['base_ugene']['info'] = t('Analysis: Unigene Details');
  316. $blocks['base_ugene']['cache'] = BLOCK_NO_CACHE;
  317. $blocks['feature_ugene']['info'] = t('Tripal Feature Unigene');
  318. $blocks['feature_ugene']['cache'] = BLOCK_NO_CACHE;
  319. $blocks['org_ugene']['info'] = t('Tripal Organism Unigene');
  320. $blocks['org_ugene']['cache'] = BLOCK_NO_CACHE;
  321. return $blocks;
  322. case 'view':
  323. if(user_access('access chado_analysis_unigene content') and arg(0) == 'node' and is_numeric(arg(1))) {
  324. $nid = arg(1);
  325. $node = node_load($nid);
  326. $block = array();
  327. switch($delta){
  328. case 'base_ugene':
  329. $block['subject'] = t('Unigene Details');
  330. $block['content'] = theme('tripal_analysis_unigene_base',$node);
  331. break;
  332. case 'feature_ugene':
  333. $block['subject'] = t('Unigene');
  334. $block['content'] = theme('tripal_feature_unigenes',$node);
  335. break;
  336. case 'org_ugene':
  337. $block['subject'] = t('Unigene');
  338. $block['content'] = theme('tripal_organism_unigenes',$node);
  339. break;
  340. default :
  341. }
  342. return $block;
  343. }
  344. }
  345. }
  346. /*******************************************************************************
  347. * tripal_analysis_unigene_nodeapi()
  348. * HOOK: Implementation of hook_nodeapi()
  349. * Display unigene results for allowed node types
  350. */
  351. function tripal_analysis_unigene_nodeapi(&$node, $op, $teaser, $page) {
  352. switch ($op) {
  353. case 'view':
  354. // Find out which node types for showing the unigene
  355. $types_to_show = variable_get('tripal_analysis_unigene_setting',
  356. array('chado_feature','chado_organism'));
  357. // Abort if this node is not one of the types we should show.
  358. if (in_array($node->type, $types_to_show, TRUE)) {
  359. // Add unigene to the content item if it's not a teaser
  360. if ($teaser) {
  361. return '';
  362. }
  363. // add the alignment to the feature search indexing
  364. if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
  365. $node->content['tripal_analysis_unigene_index_version'] = array(
  366. '#value' => theme('tripal_analysis_unigene_search_index',$node),
  367. '#weight' => 4,
  368. );
  369. } else if ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
  370. $node->content['tripal_analysis_unigene_index_version'] = array(
  371. '#value' => theme('tripal_analysis_unigene_search_result',$node),
  372. '#weight' => 4,
  373. );
  374. } else {
  375. if(strcmp($node->type,'chado_organism')==0){
  376. $node->content['tripal_organism_unigenes'] = array(
  377. '#value' => theme('tripal_organism_unigenes', $node),
  378. '#weight' => 4
  379. );
  380. }
  381. if(strcmp($node->type,'chado_feature')==0){
  382. $node->content['tripal_feature_unigenes'] = array(
  383. '#value' => theme('tripal_feature_unigenes', $node),
  384. '#weight' => 4
  385. );
  386. }
  387. }
  388. }
  389. break;
  390. }
  391. }
  392. /************************************************************************
  393. * We need to let drupal know about our theme functions and their arguments.
  394. * We create theme functions to allow users of the module to customize the
  395. * look and feel of the output generated in this module
  396. */
  397. function tripal_analysis_unigene_theme () {
  398. return array(
  399. 'tripal_analysis_unigene_search_index' => array (
  400. 'arguments' => array('node'),
  401. ),
  402. 'tripal_analysis_unigene_search_result' => array (
  403. 'arguments' => array('node'),
  404. ),
  405. 'tripal_organism_unigenes' => array (
  406. 'arguments' => array('node'=> null),
  407. 'template' => 'tripal_organism_unigenes',
  408. ),
  409. 'tripal_feature_unigenes' => array (
  410. 'arguments' => array('node'=> null),
  411. 'template' => 'tripal_feature_unigenes',
  412. ),
  413. 'tripal_analysis_unigene_base' => array (
  414. 'arguments' => array('node'=> null),
  415. 'template' => 'tripal_analysis_unigene_base',
  416. ),
  417. );
  418. }
  419. /*******************************************************************************
  420. *
  421. */
  422. function tripal_analysis_unigene_preprocess_tripal_organism_unigenes(&$variables){
  423. $node = $variables['node'];
  424. $organism = $node->organism;
  425. $unigenes = tripal_analysis_unigene_load_organism_unigenes($organism);
  426. $node->organism->tripal_analysis_unigene->unigenes = $unigenes;
  427. }
  428. /*******************************************************************************
  429. *
  430. */
  431. function tripal_analysis_unigene_preprocess_tripal_feature_unigenes(&$variables){
  432. $node = $variables['node'];
  433. $feature = $node->feature;
  434. $unigenes = tripal_analysis_unigene_load_feature_unigenes($feature);
  435. $node->feature->tripal_analysis_unigene->unigenes = $unigenes;
  436. }
  437. /************************************************************************
  438. * This function is an extension of the chado_feature_view by providing
  439. * the markup for the feature object THAT WILL BE INDEXED.
  440. */
  441. function theme_tripal_analysis_unigene_search_index ($node) {
  442. $feature = $node->feature;
  443. $obj_feature = tripal_analysis_unigene_get_alignments($feature);
  444. $alignments = $obj_feature->alignments;
  445. $content = "<strong>";
  446. if(count($alignments) > 0){
  447. // iterate through each alignment
  448. foreach ($alignments as $result){
  449. // EST alignments in chado use an EST_match type to map ESTs to
  450. // contigs and a rank to indicate the major srcfeature.
  451. // We don't want to show EST_matches on the alignment view
  452. // since that doesn't make much sense to the end user. If this
  453. // is an EST_match and the feature is an EST then we want to show
  454. // the contig in the alignments. The contig name is part of the
  455. // uniquename in the EST_match
  456. if($node->feature->cvname == 'EST' && $result->cvname == 'EST_match'){
  457. $sql = "SELECT srcfeature_id ".
  458. "FROM {featureloc} ".
  459. "WHERE feature_id = $result->feature_id ".
  460. "ORDER BY rank";
  461. $previous_db = tripal_db_set_active ('chado');
  462. $contig_fid = db_result(db_query($sql));
  463. $sql = "SELECT name FROM {feature} WHERE feature_id = $contig_fid";
  464. $contig_name = db_result(db_query($sql));
  465. tripal_db_set_active($previous_db);
  466. $sql = "SELECT nid ".
  467. "FROM {chado_feature} ".
  468. "WHERE feature_id = $contig_fid";
  469. $contig_nid = db_result(db_query($sql));
  470. // Check if the EST exists as a drupal node. If yes, add a link to
  471. // it. If no, just show the name
  472. if ($contig_nid != 0) {
  473. $sql = "SELECT nid FROM {node} WHERE nid=$contig_nid";
  474. $node_exists = db_result(db_query($sql));
  475. }
  476. $content .= "$contig_name " ;
  477. }
  478. elseif($node->feature->cvname == 'contig' && $result->cvname == 'EST_match'){
  479. $sql = "SELECT vid ".
  480. "FROM {node} ".
  481. "WHERE title = '$result->feature_name'".
  482. "ORDER BY vid DESC";
  483. // since the feature name is also the node title we can look it up
  484. $est_node = db_fetch_object(db_query($sql));
  485. $content .= "$result->feature_name ";
  486. }
  487. else {
  488. $content .= "$result->cvname $result->feature_name ";
  489. }
  490. }
  491. }
  492. $content .= "</strong>";
  493. return $content;
  494. }
  495. /************************************************************************
  496. * This function is an extension of the chado_feature_view by providing
  497. * the markup for the feature object to show on a search result page.
  498. */
  499. function theme_tripal_analysis_unigene_search_result ($node) {
  500. $feature = $node->feature;
  501. $obj_feature = tripal_analysis_unigene_get_alignments($feature);
  502. $alignments = $obj_feature->alignments;
  503. $content = "<strong>";
  504. if(count($alignments) > 0){
  505. // iterate through each alignment
  506. foreach ($alignments as $result){
  507. // EST alignments in chado use an EST_match type to map ESTs to
  508. // contigs and a rank to indicate the major srcfeature.
  509. // We don't want to show EST_matches on the alignment view
  510. // since that doesn't make much sense to the end user. If this
  511. // is an EST_match and the feature is an EST then we want to show
  512. // the contig in the alignments. The contig name is part of the
  513. // uniquename in the EST_match
  514. if($node->feature->cvname == 'EST' && $result->cvname == 'EST_match'){
  515. $sql = "SELECT srcfeature_id ".
  516. "FROM {featureloc} ".
  517. "WHERE feature_id = $result->feature_id ".
  518. "ORDER BY rank";
  519. $previous_db = tripal_db_set_active ('chado');
  520. $contig_fid = db_result(db_query($sql));
  521. $sql = "SELECT name FROM {feature} WHERE feature_id = $contig_fid";
  522. $contig_name = db_result(db_query($sql));
  523. tripal_db_set_active($previous_db);
  524. $sql = "SELECT nid ".
  525. "FROM {chado_feature} ".
  526. "WHERE feature_id = $contig_fid";
  527. $contig_nid = db_result(db_query($sql));
  528. // Check if the EST exists as a drupal node. If yes, add a link to
  529. // it. If no, just show the name
  530. if ($contig_nid != 0) {
  531. $sql = "SELECT nid FROM {node} WHERE nid=$contig_nid";
  532. $node_exists = db_result(db_query($sql));
  533. }
  534. $content .= "Alignment to contig $contig_name. " ;
  535. }
  536. elseif($node->feature->cvname == 'contig' && $result->cvname == 'EST_match'){
  537. $sql = "SELECT vid ".
  538. "FROM {node} ".
  539. "WHERE title = '$result->feature_name'".
  540. "ORDER BY vid DESC";
  541. // since the feature name is also the node title we can look it up
  542. $est_node = db_fetch_object(db_query($sql));
  543. $content .= "Aligned EST: $result->feature_name ";
  544. }
  545. else {
  546. $content .= "Aligned $result->cvname: $result->feature_name ";
  547. }
  548. }
  549. }
  550. $content .= "</strong>";
  551. return $content;
  552. }
  553. /*******************************************************************************
  554. * tripal_analysis_unigene_results ()
  555. * Prepare unigene result for the feature shown on the page
  556. */
  557. function theme_tripal_analysis_unigene_feature_alignments($node) {
  558. $feature = $node->feature;
  559. $obj_feature = tripal_analysis_unigene_get_alignments($feature);
  560. $alignments = $obj_feature->alignments;
  561. if(count($alignments) > 0){
  562. $content = "<div id=\"tripal_analysis_unigene_box\" class=\"tripal_unigene-info-box\">";
  563. // we're showing contig alignments in GBrowse so create a link here for
  564. // that if this feature is a contig
  565. if($node->feature->cvname == 'contig'){
  566. $content .= "<div class=\"tripal_expandableBox\">".
  567. " <h3>ESTs in this contig</h3>".
  568. "</div>";
  569. $content .= "<div class=\"tripal_expandableBoxContent\">";
  570. } else {
  571. $content .= "<div class=\"tripal_expandableBox\">".
  572. " <h3>Alignments</h3>".
  573. "</div>";
  574. $content .= "<div class=\"tripal_expandableBoxContent\">";
  575. }
  576. $content .= "".
  577. "<table class=\"tripal_table_horz\">".
  578. " <tr>".
  579. " <th>Type</th>".
  580. " <th>Feature</th>".
  581. " <th align=\"right\">Position</th>".
  582. " </tr>";
  583. // iterate through each alignment
  584. foreach ($alignments as $result){
  585. // EST alignments in chado use an EST_match type to map ESTs to
  586. // contigs and a rank to indicate the major srcfeature.
  587. // We don't want to show EST_matches on the alignment view
  588. // since that doesn't make much sense to the end user. If this
  589. // is an EST_match and the feature is an EST then we want to show
  590. // the contig in the alignments. The contig name is part of the
  591. // uniquename in the EST_match
  592. if($node->feature->cvname == 'EST' && $result->cvname == 'EST_match'){
  593. $sql = "SELECT srcfeature_id ".
  594. "FROM {featureloc} ".
  595. "WHERE feature_id = $result->feature_id ".
  596. "ORDER BY rank";
  597. $previous_db = tripal_db_set_active ('chado');
  598. $contig_fid = db_result(db_query($sql));
  599. $sql = "SELECT name FROM {feature} WHERE feature_id = $contig_fid";
  600. $contig_name = db_result(db_query($sql));
  601. tripal_db_set_active($previous_db);
  602. $sql = "SELECT nid ".
  603. "FROM {chado_feature} ".
  604. "WHERE feature_id = $contig_fid";
  605. $contig_nid = db_result(db_query($sql));
  606. // Check if the EST exists as a drupal node. If yes, add a link to
  607. // it. If no, just show the name
  608. if ($contig_nid != 0) {
  609. $sql = "SELECT nid FROM {node} WHERE nid=$contig_nid";
  610. $node_exists = db_result(db_query($sql));
  611. }
  612. $content .=
  613. "<tr>".
  614. " <td>Contig</td>".
  615. " <td>";
  616. if ($node_exists != 0) {
  617. $content .= "<a href=\"".url("node/$contig_nid")."\">".
  618. "$contig_name</a>";
  619. } else {
  620. $content .= $contig_name;
  621. }
  622. $content .=
  623. " </td>".
  624. " <td align=\"right\">".
  625. number_format($result->fmin).
  626. "-".
  627. number_format($result->fmax).
  628. " </td>".
  629. "</tr>";
  630. }
  631. elseif($node->feature->cvname == 'contig' &&
  632. $result->cvname == 'EST_match'){
  633. $sql = "SELECT vid ".
  634. "FROM {node} ".
  635. "WHERE title = '$result->feature_name'".
  636. "ORDER BY vid DESC";
  637. // since the feature name is also the node title we can look it up
  638. $est_node = db_fetch_object(db_query($sql));
  639. $content .=
  640. "<tr>".
  641. " <td>EST</td>".
  642. " <td>";
  643. //Check if the EST exists as a drupal node before adding a hyperlink
  644. if ($est_node->vid != 0) {
  645. $content .=
  646. "<a href=\"".url("node/$est_node->vid")."\">".
  647. $result->feature_name.
  648. "</a>";
  649. } else {
  650. $content .= $result->feature_name;
  651. }
  652. $content .=
  653. " </td>".
  654. " <td align=\"right\">".
  655. number_format($result->fmin).
  656. "-".
  657. number_format($result->fmax).
  658. " </td>".
  659. "</tr>";
  660. }
  661. else {
  662. $content .= "".
  663. "<tr>".
  664. " <td>$result->cvname</td>".
  665. " <td>$result->feature_name</td>".
  666. " <td align=\"right\">$result->fmin</td>".
  667. " <td align=\"right\">$result->fmax</td>".
  668. " <td align=\"right\">$result->strand</td>".
  669. "</tr>";
  670. }
  671. }
  672. $content .= "</table>";
  673. /* if this is a contig then get the alignment
  674. if($node->feature->cvname == 'contig'){
  675. // get the directory prefix
  676. $prefix = preg_replace("/^(\d*)\d{3}$/","$1",$node->feature_id);
  677. if(!$prefix){
  678. $prefix = '0';
  679. }
  680. $data_url = variable_get('chado_feature_data_url','sites/default/files/data');
  681. $fh = fopen("$data_url/misc/$prefix/$node->feature->feature_id/alignment.txt", 'r');
  682. if($fh){
  683. $content .= "<b>Alignment:</b><div class=\"tripal_feature_assembly_alignment\"><pre>";
  684. while(!feof($fh)){
  685. $content .= fgets($fh);
  686. }
  687. $content .="</pre></div>";
  688. }
  689. fclose($fh);
  690. }
  691. */
  692. $content .= "</div></div>";
  693. }
  694. return $content;
  695. }
  696. /*******************************************************************************
  697. *
  698. */
  699. function tripal_analysis_unigene_get_alignments($map) {
  700. // get the alignments for this feature
  701. $sql = "SELECT F.name as feature_name, FL.fmin, FL.fmax, FL.strand, ".
  702. " FL.phase, CVT.name as cvname, F.feature_id, F.uniquename, ".
  703. " FL.featureloc_id ".
  704. "FROM {featureloc} FL ".
  705. " INNER JOIN Feature F ON F.feature_id = FL.feature_id ".
  706. " INNER JOIN Cvterm CVT ON CVT.cvterm_id = F.type_id ".
  707. "WHERE srcfeature_id = %d AND ".
  708. " NOT(CVT.name = 'match' or CVT.name = 'match_part') ".
  709. "ORDER BY FL.fmin, FL.fmax";
  710. $previous_db = tripal_db_set_active('chado');
  711. $results = db_query($sql,$map->feature_id);
  712. $alignments = array();
  713. $i=0;
  714. while($subfeature = db_fetch_object($results)){
  715. $alignments[$i++] = $subfeature;
  716. }
  717. $additions->alignments = $alignments;
  718. /* get the GO Terms
  719. $sql = "SELECT DISTINCT * FROM {go_results_mview} ".
  720. "WHERE feature_id = %d";
  721. $results = db_query($sql,$map->feature_id);
  722. $go_terms = array();
  723. $i=0;
  724. while($term = db_fetch_object($results)){
  725. $go_terms[$i++] = $term;
  726. }
  727. $additions->go_terms = $go_terms;
  728. // get the feature properties
  729. $sql = "SELECT FP.value,FP.rank,CVT.name,CVT.definition ".
  730. "FROM {featureprop} FP".
  731. " INNER JOIN Cvterm CVT ".
  732. " ON FP.type_id = CVT.cvterm_id ".
  733. "WHERE feature_id = %d";
  734. $results = db_query($sql,$map->feature_id);
  735. $properties = array();
  736. $i=0;
  737. while($property = db_fetch_object($results)){
  738. $properties[$i++] = $property;
  739. }
  740. $additions->properties = $properties;
  741. */
  742. tripal_db_set_active($previous_db);
  743. return $additions;
  744. }
  745. /************************************************************************
  746. *
  747. */
  748. function tripal_analysis_unigene_load_organism_unigenes($organism){
  749. // get information about this assemblies and add it to the items in this node
  750. $sql = "SELECT * FROM {organism_unigene_mview} OUM ".
  751. " INNER JOIN {analysis} A ON A.analysis_id = OUM.analysis_id ".
  752. "WHERE OUM.organism_id = %d ".
  753. "ORDER BY A.timeexecuted DESC";
  754. $previous_db = tripal_db_set_active('chado'); // use chado database
  755. $results = db_query($sql,$organism->organism_id);
  756. tripal_db_set_active($previous_db); // now use drupal database
  757. $unigenes = array();
  758. $i=0;
  759. $sql = "SELECT nid FROM {chado_analysis} WHERE analysis_id = %d";
  760. while($unigene = db_fetch_object($results)){
  761. $analysis_id = $unigene->analysis_id;
  762. $c_node = db_fetch_object(db_query($sql,$analysis_id));
  763. if($c_node){
  764. $unigene->nid = $c_node->nid;
  765. }
  766. // add in the properties
  767. $unigene_name = tripal_analysis_get_property($analysis_id,'analysis_unigene_name');
  768. $num_contigs = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_contigs');
  769. $num_reads = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_reads');
  770. $num_clusters = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_clusters');
  771. $num_singlets = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_singlets');
  772. $unigene->unigene_name = $unigene_name->value;
  773. $unigene->num_reads = $num_reads->value;
  774. $unigene->num_clusters = $num_clusters->value;
  775. $unigene->num_contigs = $num_contigs->value;
  776. $unigene->num_singlets = $num_singlets->value;
  777. $unigenes[$i++] = $unigene;
  778. }
  779. return $unigenes;
  780. }
  781. /************************************************************************
  782. *
  783. */
  784. function tripal_analysis_unigene_load_feature_unigenes($feature){
  785. // first get all the unigene analyses for this organism
  786. $sql = "SELECT * FROM {organism_unigene_mview} OUM ".
  787. " INNER JOIN {analysis} A ON A.analysis_id = OUM.analysis_id ".
  788. "WHERE OUM.organism_id = %d ".
  789. "ORDER BY A.timeexecuted DESC";
  790. $previous_db = tripal_db_set_active('chado'); // use chado database
  791. $results = db_query($sql,$feature->organism_id->organism_id);
  792. tripal_db_set_active($previous_db); // now use drupal database
  793. // iterate through the unigenes and find those that use this feature
  794. $unigenes = array();
  795. $i=0;
  796. $sql = "SELECT nid FROM {chado_analysis} WHERE analysis_id = %d";
  797. while($unigene = db_fetch_object($results)){
  798. $analysis_id = $unigene->analysis_id;
  799. // check if this feature is present in the unigene
  800. $values = array(
  801. 'feature_id' => $feature->feature_id,
  802. 'analysis_id' => $analysis_id,
  803. );
  804. $hasFeature = tripal_core_chado_select('analysisfeature',array('*'),$values);
  805. // if the feature is present then get information about it
  806. if(sizeof($hasFeature) > 0){
  807. // see if there is a drupal node for this unigene
  808. $c_node = db_fetch_object(db_query($sql,$analysis_id));
  809. if($c_node){
  810. $unigene->nid = $c_node->nid;
  811. }
  812. // add in the properties
  813. $unigene_name = tripal_analysis_get_property($analysis_id,'analysis_unigene_name');
  814. $singlet = tripal_core_get_property('analysisfeature',$analysis_id,'singlet','tripal');
  815. $unigene->unigene_name = $unigene_name->value;
  816. $unigene->singlet = $num_singlets->value;
  817. $unigenes[$i++] = $unigene;
  818. }
  819. }
  820. return $unigenes;
  821. }
  822. /*******************************************************************************
  823. * Tripal Unigene administrative setting form. This function is called by
  824. * tripal_analysis module which asks for an admin form to show on the page
  825. */
  826. function tripal_analysis_unigene_get_settings() {
  827. // Get an array of node types with internal names as keys
  828. $options = node_get_types('names');
  829. // Add 'chado_feature' to allowed content types for showing unigene results
  830. $allowedoptions ['chado_feature'] = "Show 'ESTs in this contig' on feature pages";
  831. $allowedoptions ['chado_organism'] = "Show assemblies on organism pages";
  832. $form['description'] = array(
  833. '#type' => 'item',
  834. '#value' => t("This option allows user to display the unigene assembly ".
  835. "information. For contigs, this would include an alignment and for ".
  836. "organisms this would be a list of assemblies. Check the box to ".
  837. "enable the display of unigene information. Uncheck to disable."),
  838. '#weight' => 0,
  839. );
  840. $form['tripal_analysis_unigene_setting'] = array(
  841. '#type' => 'checkboxes',
  842. '#options' => $allowedoptions,
  843. '#default_value'=>variable_get('tripal_analysis_unigene_setting',array()),
  844. );
  845. $settings->form = $form;
  846. $settings->title = "Tripal Unigene";
  847. return $settings;
  848. }