tripal_analysis_unigene.module 35 KB

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