tripal_analysis_unigene.module 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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. 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. * We need to let drupal know about our theme functions and their arguments.
  396. * We create theme functions to allow users of the module to customize the
  397. * look and feel of the output generated in this module
  398. */
  399. function tripal_analysis_unigene_theme () {
  400. return array(
  401. 'tripal_analysis_unigene_search_index' => array (
  402. 'arguments' => array('node'),
  403. ),
  404. 'tripal_analysis_unigene_search_result' => array (
  405. 'arguments' => array('node'),
  406. ),
  407. 'tripal_organism_unigenes' => array (
  408. 'arguments' => array('node'=> null),
  409. 'template' => 'tripal_organism_unigenes',
  410. ),
  411. 'tripal_feature_unigenes' => array (
  412. 'arguments' => array('node'=> null),
  413. 'template' => 'tripal_feature_unigenes',
  414. ),
  415. 'tripal_analysis_unigene_base' => array (
  416. 'arguments' => array('node'=> null),
  417. 'template' => 'tripal_analysis_unigene_base',
  418. ),
  419. );
  420. }
  421. /*******************************************************************************
  422. *
  423. */
  424. function tripal_analysis_unigene_preprocess_tripal_organism_unigenes(&$variables){
  425. $node = $variables['node'];
  426. $organism = $node->organism;
  427. $unigenes = tripal_analysis_unigene_load_organism_unigenes($organism);
  428. $node->organism->tripal_analysis_unigene->unigenes = $unigenes;
  429. }
  430. /*******************************************************************************
  431. *
  432. */
  433. function tripal_analysis_unigene_preprocess_tripal_feature_unigenes(&$variables){
  434. $node = $variables['node'];
  435. $feature = $node->feature;
  436. $unigenes = tripal_analysis_unigene_load_feature_unigenes($feature);
  437. $node->feature->tripal_analysis_unigene->unigenes = $unigenes;
  438. }
  439. /************************************************************************
  440. * This function is an extension of the chado_feature_view by providing
  441. * the markup for the feature object THAT WILL BE INDEXED.
  442. */
  443. function theme_tripal_analysis_unigene_search_index ($node) {
  444. $feature = $node->feature;
  445. $obj_feature = tripal_analysis_unigene_get_alignments($feature);
  446. $alignments = $obj_feature->alignments;
  447. $content = "<strong>";
  448. if(count($alignments) > 0){
  449. // iterate through each alignment
  450. foreach ($alignments as $result){
  451. // EST alignments in chado use an EST_match type to map ESTs to
  452. // contigs and a rank to indicate the major srcfeature.
  453. // We don't want to show EST_matches on the alignment view
  454. // since that doesn't make much sense to the end user. If this
  455. // is an EST_match and the feature is an EST then we want to show
  456. // the contig in the alignments. The contig name is part of the
  457. // uniquename in the EST_match
  458. if($node->feature->cvname == 'EST' && $result->cvname == 'EST_match'){
  459. $sql = "SELECT srcfeature_id ".
  460. "FROM {featureloc} ".
  461. "WHERE feature_id = $result->feature_id ".
  462. "ORDER BY rank";
  463. $previous_db = tripal_db_set_active ('chado');
  464. $contig_fid = db_result(db_query($sql));
  465. $sql = "SELECT name FROM {feature} WHERE feature_id = $contig_fid";
  466. $contig_name = db_result(db_query($sql));
  467. tripal_db_set_active($previous_db);
  468. $sql = "SELECT nid ".
  469. "FROM {chado_feature} ".
  470. "WHERE feature_id = $contig_fid";
  471. $contig_nid = db_result(db_query($sql));
  472. // Check if the EST exists as a drupal node. If yes, add a link to
  473. // it. If no, just show the name
  474. if ($contig_nid != 0) {
  475. $sql = "SELECT nid FROM {node} WHERE nid=$contig_nid";
  476. $node_exists = db_result(db_query($sql));
  477. }
  478. $content .= "$contig_name " ;
  479. }
  480. elseif($node->feature->cvname == 'contig' && $result->cvname == 'EST_match'){
  481. $sql = "SELECT vid ".
  482. "FROM {node} ".
  483. "WHERE title = '$result->feature_name'".
  484. "ORDER BY vid DESC";
  485. // since the feature name is also the node title we can look it up
  486. $est_node = db_fetch_object(db_query($sql));
  487. $content .= "$result->feature_name ";
  488. }
  489. else {
  490. $content .= "$result->cvname $result->feature_name ";
  491. }
  492. }
  493. }
  494. $content .= "</strong>";
  495. return $content;
  496. }
  497. /************************************************************************
  498. * This function is an extension of the chado_feature_view by providing
  499. * the markup for the feature object to show on a search result page.
  500. */
  501. function theme_tripal_analysis_unigene_search_result ($node) {
  502. $feature = $node->feature;
  503. $obj_feature = tripal_analysis_unigene_get_alignments($feature);
  504. $alignments = $obj_feature->alignments;
  505. $content = "<strong>";
  506. if(count($alignments) > 0){
  507. // iterate through each alignment
  508. foreach ($alignments as $result){
  509. // EST alignments in chado use an EST_match type to map ESTs to
  510. // contigs and a rank to indicate the major srcfeature.
  511. // We don't want to show EST_matches on the alignment view
  512. // since that doesn't make much sense to the end user. If this
  513. // is an EST_match and the feature is an EST then we want to show
  514. // the contig in the alignments. The contig name is part of the
  515. // uniquename in the EST_match
  516. if($node->feature->cvname == 'EST' && $result->cvname == 'EST_match'){
  517. $sql = "SELECT srcfeature_id ".
  518. "FROM {featureloc} ".
  519. "WHERE feature_id = $result->feature_id ".
  520. "ORDER BY rank";
  521. $previous_db = tripal_db_set_active ('chado');
  522. $contig_fid = db_result(db_query($sql));
  523. $sql = "SELECT name FROM {feature} WHERE feature_id = $contig_fid";
  524. $contig_name = db_result(db_query($sql));
  525. tripal_db_set_active($previous_db);
  526. $sql = "SELECT nid ".
  527. "FROM {chado_feature} ".
  528. "WHERE feature_id = $contig_fid";
  529. $contig_nid = db_result(db_query($sql));
  530. // Check if the EST exists as a drupal node. If yes, add a link to
  531. // it. If no, just show the name
  532. if ($contig_nid != 0) {
  533. $sql = "SELECT nid FROM {node} WHERE nid=$contig_nid";
  534. $node_exists = db_result(db_query($sql));
  535. }
  536. $content .= "Alignment to contig $contig_name. " ;
  537. }
  538. elseif($node->feature->cvname == 'contig' && $result->cvname == 'EST_match'){
  539. $sql = "SELECT vid ".
  540. "FROM {node} ".
  541. "WHERE title = '$result->feature_name'".
  542. "ORDER BY vid DESC";
  543. // since the feature name is also the node title we can look it up
  544. $est_node = db_fetch_object(db_query($sql));
  545. $content .= "Aligned EST: $result->feature_name ";
  546. }
  547. else {
  548. $content .= "Aligned $result->cvname: $result->feature_name ";
  549. }
  550. }
  551. }
  552. $content .= "</strong>";
  553. return $content;
  554. }
  555. /*******************************************************************************
  556. * tripal_analysis_unigene_results ()
  557. * Prepare unigene result for the feature shown on the page
  558. */
  559. function theme_tripal_analysis_unigene_feature_alignments($node) {
  560. $feature = $node->feature;
  561. $obj_feature = tripal_analysis_unigene_get_alignments($feature);
  562. $alignments = $obj_feature->alignments;
  563. if(count($alignments) > 0){
  564. $content = "<div id=\"tripal_analysis_unigene_box\" class=\"tripal_unigene-info-box\">";
  565. // we're showing contig alignments in GBrowse so create a link here for
  566. // that if this feature is a contig
  567. if($node->feature->cvname == 'contig'){
  568. $content .= "<div class=\"tripal_expandableBox\">".
  569. " <h3>ESTs in this contig</h3>".
  570. "</div>";
  571. $content .= "<div class=\"tripal_expandableBoxContent\">";
  572. } else {
  573. $content .= "<div class=\"tripal_expandableBox\">".
  574. " <h3>Alignments</h3>".
  575. "</div>";
  576. $content .= "<div class=\"tripal_expandableBoxContent\">";
  577. }
  578. $content .= "".
  579. "<table class=\"tripal_table_horz\">".
  580. " <tr>".
  581. " <th>Type</th>".
  582. " <th>Feature</th>".
  583. " <th align=\"right\">Position</th>".
  584. " </tr>";
  585. // iterate through each alignment
  586. foreach ($alignments as $result){
  587. // EST alignments in chado use an EST_match type to map ESTs to
  588. // contigs and a rank to indicate the major srcfeature.
  589. // We don't want to show EST_matches on the alignment view
  590. // since that doesn't make much sense to the end user. If this
  591. // is an EST_match and the feature is an EST then we want to show
  592. // the contig in the alignments. The contig name is part of the
  593. // uniquename in the EST_match
  594. if($node->feature->cvname == 'EST' && $result->cvname == 'EST_match'){
  595. $sql = "SELECT srcfeature_id ".
  596. "FROM {featureloc} ".
  597. "WHERE feature_id = $result->feature_id ".
  598. "ORDER BY rank";
  599. $previous_db = tripal_db_set_active ('chado');
  600. $contig_fid = db_result(db_query($sql));
  601. $sql = "SELECT name FROM {feature} WHERE feature_id = $contig_fid";
  602. $contig_name = db_result(db_query($sql));
  603. tripal_db_set_active($previous_db);
  604. $sql = "SELECT nid ".
  605. "FROM {chado_feature} ".
  606. "WHERE feature_id = $contig_fid";
  607. $contig_nid = db_result(db_query($sql));
  608. // Check if the EST exists as a drupal node. If yes, add a link to
  609. // it. If no, just show the name
  610. if ($contig_nid != 0) {
  611. $sql = "SELECT nid FROM {node} WHERE nid=$contig_nid";
  612. $node_exists = db_result(db_query($sql));
  613. }
  614. $content .=
  615. "<tr>".
  616. " <td>Contig</td>".
  617. " <td>";
  618. if ($node_exists != 0) {
  619. $content .= "<a href=\"".url("node/$contig_nid")."\">".
  620. "$contig_name</a>";
  621. } else {
  622. $content .= $contig_name;
  623. }
  624. $content .=
  625. " </td>".
  626. " <td align=\"right\">".
  627. number_format($result->fmin).
  628. "-".
  629. number_format($result->fmax).
  630. " </td>".
  631. "</tr>";
  632. }
  633. elseif($node->feature->cvname == 'contig' &&
  634. $result->cvname == 'EST_match'){
  635. $sql = "SELECT vid ".
  636. "FROM {node} ".
  637. "WHERE title = '$result->feature_name'".
  638. "ORDER BY vid DESC";
  639. // since the feature name is also the node title we can look it up
  640. $est_node = db_fetch_object(db_query($sql));
  641. $content .=
  642. "<tr>".
  643. " <td>EST</td>".
  644. " <td>";
  645. //Check if the EST exists as a drupal node before adding a hyperlink
  646. if ($est_node->vid != 0) {
  647. $content .=
  648. "<a href=\"".url("node/$est_node->vid")."\">".
  649. $result->feature_name.
  650. "</a>";
  651. } else {
  652. $content .= $result->feature_name;
  653. }
  654. $content .=
  655. " </td>".
  656. " <td align=\"right\">".
  657. number_format($result->fmin).
  658. "-".
  659. number_format($result->fmax).
  660. " </td>".
  661. "</tr>";
  662. }
  663. else {
  664. $content .= "".
  665. "<tr>".
  666. " <td>$result->cvname</td>".
  667. " <td>$result->feature_name</td>".
  668. " <td align=\"right\">$result->fmin</td>".
  669. " <td align=\"right\">$result->fmax</td>".
  670. " <td align=\"right\">$result->strand</td>".
  671. "</tr>";
  672. }
  673. }
  674. $content .= "</table>";
  675. /* if this is a contig then get the alignment
  676. if($node->feature->cvname == 'contig'){
  677. // get the directory prefix
  678. $prefix = preg_replace("/^(\d*)\d{3}$/","$1",$node->feature_id);
  679. if(!$prefix){
  680. $prefix = '0';
  681. }
  682. $data_url = variable_get('chado_feature_data_url','sites/default/files/data');
  683. $fh = fopen("$data_url/misc/$prefix/$node->feature->feature_id/alignment.txt", 'r');
  684. if($fh){
  685. $content .= "<b>Alignment:</b><div class=\"tripal_feature_assembly_alignment\"><pre>";
  686. while(!feof($fh)){
  687. $content .= fgets($fh);
  688. }
  689. $content .="</pre></div>";
  690. }
  691. fclose($fh);
  692. }
  693. */
  694. $content .= "</div></div>";
  695. }
  696. return $content;
  697. }
  698. /*******************************************************************************
  699. *
  700. */
  701. function tripal_analysis_unigene_get_alignments($map) {
  702. // get the alignments for this feature
  703. $sql = "SELECT F.name as feature_name, FL.fmin, FL.fmax, FL.strand, ".
  704. " FL.phase, CVT.name as cvname, F.feature_id, F.uniquename, ".
  705. " FL.featureloc_id ".
  706. "FROM {featureloc} FL ".
  707. " INNER JOIN Feature F ON F.feature_id = FL.feature_id ".
  708. " INNER JOIN Cvterm CVT ON CVT.cvterm_id = F.type_id ".
  709. "WHERE srcfeature_id = %d AND ".
  710. " NOT(CVT.name = 'match' or CVT.name = 'match_part') ".
  711. "ORDER BY FL.fmin, FL.fmax";
  712. $previous_db = tripal_db_set_active('chado');
  713. $results = db_query($sql,$map->feature_id);
  714. $alignments = array();
  715. $i=0;
  716. while($subfeature = db_fetch_object($results)){
  717. $alignments[$i++] = $subfeature;
  718. }
  719. $additions->alignments = $alignments;
  720. /* get the GO Terms
  721. $sql = "SELECT DISTINCT * FROM {go_results_mview} ".
  722. "WHERE feature_id = %d";
  723. $results = db_query($sql,$map->feature_id);
  724. $go_terms = array();
  725. $i=0;
  726. while($term = db_fetch_object($results)){
  727. $go_terms[$i++] = $term;
  728. }
  729. $additions->go_terms = $go_terms;
  730. // get the feature properties
  731. $sql = "SELECT FP.value,FP.rank,CVT.name,CVT.definition ".
  732. "FROM {featureprop} FP".
  733. " INNER JOIN Cvterm CVT ".
  734. " ON FP.type_id = CVT.cvterm_id ".
  735. "WHERE feature_id = %d";
  736. $results = db_query($sql,$map->feature_id);
  737. $properties = array();
  738. $i=0;
  739. while($property = db_fetch_object($results)){
  740. $properties[$i++] = $property;
  741. }
  742. $additions->properties = $properties;
  743. */
  744. tripal_db_set_active($previous_db);
  745. return $additions;
  746. }
  747. /************************************************************************
  748. *
  749. */
  750. function tripal_analysis_unigene_load_organism_unigenes($organism){
  751. // get information about this assemblies and add it to the items in this node
  752. $sql = "SELECT * FROM {organism_unigene_mview} OUM ".
  753. " INNER JOIN {analysis} A ON A.analysis_id = OUM.analysis_id ".
  754. "WHERE OUM.organism_id = %d ".
  755. "ORDER BY A.timeexecuted DESC";
  756. $previous_db = tripal_db_set_active('chado'); // use chado database
  757. $results = db_query($sql,$organism->organism_id);
  758. tripal_db_set_active($previous_db); // now use drupal database
  759. $unigenes = array();
  760. $i=0;
  761. $sql = "SELECT nid FROM {chado_analysis} WHERE analysis_id = %d";
  762. while($unigene = db_fetch_object($results)){
  763. $analysis_id = $unigene->analysis_id;
  764. $c_node = db_fetch_object(db_query($sql,$analysis_id));
  765. if($c_node){
  766. $unigene->nid = $c_node->nid;
  767. }
  768. // add in the properties
  769. $unigene_name = tripal_analysis_get_property($analysis_id,'analysis_unigene_name');
  770. $num_contigs = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_contigs');
  771. $num_reads = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_reads');
  772. $num_clusters = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_clusters');
  773. $num_singlets = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_singlets');
  774. $unigene->unigene_name = $unigene_name->value;
  775. $unigene->num_reads = $num_reads->value;
  776. $unigene->num_clusters = $num_clusters->value;
  777. $unigene->num_contigs = $num_contigs->value;
  778. $unigene->num_singlets = $num_singlets->value;
  779. $unigenes[$i++] = $unigene;
  780. }
  781. return $unigenes;
  782. }
  783. /************************************************************************
  784. *
  785. */
  786. function tripal_analysis_unigene_load_feature_unigenes($feature){
  787. // first get all the unigene analyses for this organism
  788. $sql = "SELECT * FROM {organism_unigene_mview} OUM ".
  789. " INNER JOIN {analysis} A ON A.analysis_id = OUM.analysis_id ".
  790. "WHERE OUM.organism_id = %d ".
  791. "ORDER BY A.timeexecuted DESC";
  792. $previous_db = tripal_db_set_active('chado'); // use chado database
  793. $results = db_query($sql,$feature->organism_id->organism_id);
  794. tripal_db_set_active($previous_db); // now use drupal database
  795. // iterate through the unigenes and find those that use this feature
  796. $unigenes = array();
  797. $i=0;
  798. $sql = "SELECT nid FROM {chado_analysis} WHERE analysis_id = %d";
  799. while($unigene = db_fetch_object($results)){
  800. $analysis_id = $unigene->analysis_id;
  801. // check if this feature is present in the unigene
  802. $values = array(
  803. 'feature_id' => $feature->feature_id,
  804. 'analysis_id' => $analysis_id,
  805. );
  806. $hasFeature = tripal_core_chado_select('analysisfeature',array('*'),$values);
  807. // if the feature is present then get information about it
  808. if(sizeof($hasFeature) > 0){
  809. // see if there is a drupal node for this unigene
  810. $c_node = db_fetch_object(db_query($sql,$analysis_id));
  811. if($c_node){
  812. $unigene->nid = $c_node->nid;
  813. }
  814. // add in the properties
  815. $unigene_name = tripal_analysis_get_property($analysis_id,'analysis_unigene_name');
  816. $singlet = tripal_core_get_property('analysisfeature',$analysis_id,'singlet','tripal');
  817. $unigene->unigene_name = $unigene_name->value;
  818. $unigene->singlet = $num_singlets->value;
  819. $unigenes[$i++] = $unigene;
  820. }
  821. }
  822. return $unigenes;
  823. }
  824. /*******************************************************************************
  825. * Tripal Unigene administrative setting form. This function is called by
  826. * tripal_analysis module which asks for an admin form to show on the page
  827. */
  828. function tripal_analysis_unigene_get_settings() {
  829. // Get an array of node types with internal names as keys
  830. $options = node_get_types('names');
  831. // Add 'chado_feature' to allowed content types for showing unigene results
  832. $allowedoptions ['chado_feature'] = "Show 'ESTs in this contig' on feature pages";
  833. $allowedoptions ['chado_organism'] = "Show assemblies on organism pages";
  834. $form['description'] = array(
  835. '#type' => 'item',
  836. '#value' => t("This option allows user to display the unigene assembly ".
  837. "information. For contigs, this would include an alignment and for ".
  838. "organisms this would be a list of assemblies. Check the box to ".
  839. "enable the display of unigene information. Uncheck to disable."),
  840. '#weight' => 0,
  841. );
  842. $form['tripal_analysis_unigene_setting'] = array(
  843. '#type' => 'checkboxes',
  844. '#options' => $allowedoptions,
  845. '#default_value'=>variable_get('tripal_analysis_unigene_setting',array()),
  846. );
  847. $settings->form = $form;
  848. $settings->title = "Tripal Unigene";
  849. return $settings;
  850. }