tripal_analysis_unigene.module 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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_insert($node){
  225. // insert the analysis
  226. chado_analysis_insert($node);
  227. // add the unigene name as a property of the anslysis
  228. tripal_analysis_insert_property($node->analysis->analysis_id,'analysis_type','tripal_analysis_unigene');
  229. tripal_analysis_insert_property($node->analysis->analysis_id,'analysis_unigene_name',$node->unigene_name);
  230. tripal_analysis_insert_property($node->analysis->analysis_id,'analysis_unigene_num_contigs',$node->num_contigs);
  231. tripal_analysis_insert_property($node->analysis->analysis_id,'analysis_unigene_num_reads',$node->num_reads);
  232. tripal_analysis_insert_property($node->analysis->analysis_id,'analysis_unigene_avg_length',$node->avg_length);
  233. tripal_analysis_insert_property($node->analysis->analysis_id,'analysis_unigene_num_clusters',$node->num_clusters);
  234. tripal_analysis_insert_property($node->analysis->analysis_id,'analysis_unigene_num_singlets',$node->num_clusters);
  235. }
  236. /**
  237. *
  238. */
  239. function chado_analysis_unigene_update($node){
  240. chado_analysis_update($node);
  241. tripal_analysis_update_property($node->analysis_id,'analysis_unigene_name',$node->unigene_name,1);
  242. tripal_analysis_update_property($node->analysis_id,'analysis_unigene_num_contigs',$node->num_contigs,1);
  243. tripal_analysis_update_property($node->analysis_id,'analysis_unigene_num_reads',$node->num_reads,1);
  244. tripal_analysis_update_property($node->analysis_id,'analysis_unigene_avg_length',$node->avg_length,1);
  245. tripal_analysis_update_property($node->analysis_id,'analysis_unigene_num_clusters',$node->num_clusters,1);
  246. tripal_analysis_update_property($node->analysis_id,'analysis_unigene_num_singlets',$node->num_singlets,1);
  247. }
  248. /**
  249. *
  250. */
  251. function chado_analysis_unigene_delete($node){
  252. chado_analysis_delete($node);
  253. }
  254. /**
  255. *
  256. */
  257. function chado_analysis_unigene_view ($node, $teaser = FALSE, $page = FALSE) {
  258. // use drupal's default node view:
  259. $node = node_prepare($node, $teaser);
  260. return $node;
  261. }
  262. /**
  263. *
  264. */
  265. function chado_analysis_unigene_load($node){
  266. // load the default set of analysis fields
  267. $additions = chado_analysis_load($node);
  268. // create some variables for easier lookup
  269. $analysis = $additions->analysis;
  270. $analysis_id = $analysis->analysis_id;
  271. // add in the properties
  272. $unigene_name = tripal_analysis_get_property($analysis_id,'analysis_unigene_name');
  273. $num_contigs = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_contigs');
  274. $num_reads = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_reads');
  275. $avg_length = tripal_analysis_get_property($analysis_id,'analysis_unigene_avg_length');
  276. $num_clusters = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_clusters');
  277. $num_singlets = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_singlets');
  278. $analysis->tripal_analysis_unigene->unigene_name = $unigene_name->value;
  279. $analysis->tripal_analysis_unigene->num_contigs = $num_contigs->value;
  280. $analysis->tripal_analysis_unigene->num_reads = $num_reads->value;
  281. $analysis->tripal_analysis_unigene->avg_length = $avg_length->value;
  282. $analysis->tripal_analysis_unigene->num_clusters = $num_clusters->value;
  283. $analysis->tripal_analysis_unigene->num_singlets = $num_singlets->value;
  284. // add in organism information using the materialized view
  285. $sql = "SELECT * FROM {organism_unigene_mview} OUM ".
  286. " INNER JOIN {organism} O on OUM.organism_id = O.organism_id ".
  287. "WHERE OUM.analysis_id = %d ".
  288. "ORDER BY O.genus, O.species";
  289. $previous_db = tripal_db_set_active('chado'); // use chado database
  290. $organisms = db_query($sql,$analysis_id);
  291. tripal_db_set_active($previous_db); // now use drupal database
  292. while($organism = db_fetch_object($organisms)){
  293. $sql = "SELECT nid FROM {chado_organism} WHERE organism_id = %d";
  294. $c_org = db_fetch_object(db_query($sql,$organism->organism_id));
  295. $organism->nid = $c_org->nid;
  296. $analysis->tripal_analysis_unigene->organisms[] = $organism;
  297. }
  298. return $additions;
  299. }
  300. /**
  301. *
  302. *
  303. * @ingroup tripal_analysis_unigene
  304. */
  305. function tripal_analysis_unigene_block($op = 'list', $delta = 0, $edit=array()){
  306. switch($op) {
  307. case 'list':
  308. $blocks['base_ugene']['info'] = t('Analysis: Unigene Details');
  309. $blocks['base_ugene']['cache'] = BLOCK_NO_CACHE;
  310. $blocks['feature_ugene']['info'] = t('Tripal Feature Unigene');
  311. $blocks['feature_ugene']['cache'] = BLOCK_NO_CACHE;
  312. $blocks['org_ugene']['info'] = t('Tripal Organism Unigene');
  313. $blocks['org_ugene']['cache'] = BLOCK_NO_CACHE;
  314. return $blocks;
  315. case 'view':
  316. if(user_access('access chado_analysis_unigene content') and arg(0) == 'node' and is_numeric(arg(1))) {
  317. $nid = arg(1);
  318. $node = node_load($nid);
  319. $block = array();
  320. switch($delta){
  321. case 'base_ugene':
  322. $block['subject'] = t('Unigene Details');
  323. $block['content'] = theme('tripal_analysis_unigene_base',$node);
  324. break;
  325. case 'feature_ugene':
  326. $block['subject'] = t('Unigene');
  327. $block['content'] = theme('tripal_feature_unigenes',$node);
  328. break;
  329. case 'org_ugene':
  330. $block['subject'] = t('Unigene');
  331. $block['content'] = theme('tripal_organism_unigenes',$node);
  332. break;
  333. default :
  334. }
  335. return $block;
  336. }
  337. }
  338. }
  339. /*******************************************************************************
  340. * tripal_analysis_unigene_nodeapi()
  341. * HOOK: Implementation of hook_nodeapi()
  342. * Display unigene results for allowed node types
  343. */
  344. function tripal_analysis_unigene_nodeapi(&$node, $op, $teaser, $page) {
  345. switch ($op) {
  346. case 'view':
  347. // Find out which node types for showing the unigene
  348. $types_to_show = variable_get('tripal_analysis_unigene_setting',
  349. array('chado_feature','chado_organism'));
  350. // Abort if this node is not one of the types we should show.
  351. if (in_array($node->type, $types_to_show, TRUE)) {
  352. // Add unigene to the content item if it's not a teaser
  353. if ($teaser) {
  354. return '';
  355. }
  356. // add the alignment to the feature search indexing
  357. if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
  358. $node->content['tripal_analysis_unigene_index_version'] = array(
  359. '#value' => theme('tripal_analysis_unigene_search_index',$node),
  360. '#weight' => 4,
  361. );
  362. } else if ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
  363. $node->content['tripal_analysis_unigene_index_version'] = array(
  364. '#value' => theme('tripal_analysis_unigene_search_result',$node),
  365. '#weight' => 4,
  366. );
  367. } else {
  368. if(strcmp($node->type,'chado_organism')==0){
  369. $node->content['tripal_organism_unigenes'] = array(
  370. '#value' => theme('tripal_organism_unigenes', $node),
  371. '#weight' => 4
  372. );
  373. }
  374. if(strcmp($node->type,'chado_feature')==0){
  375. $node->content['tripal_feature_unigenes'] = array(
  376. '#value' => theme('tripal_feature_unigenes', $node),
  377. '#weight' => 4
  378. );
  379. }
  380. }
  381. }
  382. break;
  383. }
  384. }
  385. /************************************************************************
  386. * We need to let drupal know about our theme functions and their arguments.
  387. * We create theme functions to allow users of the module to customize the
  388. * look and feel of the output generated in this module
  389. */
  390. function tripal_analysis_unigene_theme () {
  391. return array(
  392. 'tripal_analysis_unigene_search_index' => array (
  393. 'arguments' => array('node'),
  394. ),
  395. 'tripal_analysis_unigene_search_result' => array (
  396. 'arguments' => array('node'),
  397. ),
  398. 'tripal_organism_unigenes' => array (
  399. 'arguments' => array('node'=> null),
  400. 'template' => 'tripal_organism_unigenes',
  401. ),
  402. 'tripal_feature_unigenes' => array (
  403. 'arguments' => array('node'=> null),
  404. 'template' => 'tripal_feature_unigenes',
  405. ),
  406. 'tripal_analysis_unigene_base' => array (
  407. 'arguments' => array('node'=> null),
  408. 'template' => 'tripal_analysis_unigene_base',
  409. ),
  410. );
  411. }
  412. /*******************************************************************************
  413. *
  414. */
  415. function tripal_analysis_unigene_preprocess_tripal_organism_unigenes(&$variables){
  416. $node = $variables['node'];
  417. $organism = $node->organism;
  418. $unigenes = tripal_analysis_unigene_load_organism_unigenes($organism);
  419. $node->organism->tripal_analysis_unigene->unigenes = $unigenes;
  420. }
  421. /*******************************************************************************
  422. *
  423. */
  424. function tripal_analysis_unigene_preprocess_tripal_feature_unigenes(&$variables){
  425. $node = $variables['node'];
  426. $feature = $node->feature;
  427. $unigenes = tripal_analysis_unigene_load_feature_unigenes($feature);
  428. $node->feature->tripal_analysis_unigene->unigenes = $unigenes;
  429. }
  430. /************************************************************************
  431. * This function is an extension of the chado_feature_view by providing
  432. * the markup for the feature object THAT WILL BE INDEXED.
  433. */
  434. function theme_tripal_analysis_unigene_search_index ($node) {
  435. $feature = $node->feature;
  436. $obj_feature = tripal_analysis_unigene_get_alignments($feature);
  437. $alignments = $obj_feature->alignments;
  438. $content = "<strong>";
  439. if(count($alignments) > 0){
  440. // iterate through each alignment
  441. foreach ($alignments as $result){
  442. // EST alignments in chado use an EST_match type to map ESTs to
  443. // contigs and a rank to indicate the major srcfeature.
  444. // We don't want to show EST_matches on the alignment view
  445. // since that doesn't make much sense to the end user. If this
  446. // is an EST_match and the feature is an EST then we want to show
  447. // the contig in the alignments. The contig name is part of the
  448. // uniquename in the EST_match
  449. if($node->feature->cvname == 'EST' && $result->cvname == 'EST_match'){
  450. $sql = "SELECT srcfeature_id ".
  451. "FROM {featureloc} ".
  452. "WHERE feature_id = $result->feature_id ".
  453. "ORDER BY rank";
  454. $previous_db = tripal_db_set_active ('chado');
  455. $contig_fid = db_result(db_query($sql));
  456. $sql = "SELECT name FROM {feature} WHERE feature_id = $contig_fid";
  457. $contig_name = db_result(db_query($sql));
  458. tripal_db_set_active($previous_db);
  459. $sql = "SELECT nid ".
  460. "FROM {chado_feature} ".
  461. "WHERE feature_id = $contig_fid";
  462. $contig_nid = db_result(db_query($sql));
  463. // Check if the EST exists as a drupal node. If yes, add a link to
  464. // it. If no, just show the name
  465. if ($contig_nid != 0) {
  466. $sql = "SELECT nid FROM {node} WHERE nid=$contig_nid";
  467. $node_exists = db_result(db_query($sql));
  468. }
  469. $content .= "$contig_name " ;
  470. }
  471. elseif($node->feature->cvname == 'contig' && $result->cvname == 'EST_match'){
  472. $sql = "SELECT vid ".
  473. "FROM {node} ".
  474. "WHERE title = '$result->feature_name'".
  475. "ORDER BY vid DESC";
  476. // since the feature name is also the node title we can look it up
  477. $est_node = db_fetch_object(db_query($sql));
  478. $content .= "$result->feature_name ";
  479. }
  480. else {
  481. $content .= "$result->cvname $result->feature_name ";
  482. }
  483. }
  484. }
  485. $content .= "</strong>";
  486. return $content;
  487. }
  488. /************************************************************************
  489. * This function is an extension of the chado_feature_view by providing
  490. * the markup for the feature object to show on a search result page.
  491. */
  492. function theme_tripal_analysis_unigene_search_result ($node) {
  493. $feature = $node->feature;
  494. $obj_feature = tripal_analysis_unigene_get_alignments($feature);
  495. $alignments = $obj_feature->alignments;
  496. $content = "<strong>";
  497. if(count($alignments) > 0){
  498. // iterate through each alignment
  499. foreach ($alignments as $result){
  500. // EST alignments in chado use an EST_match type to map ESTs to
  501. // contigs and a rank to indicate the major srcfeature.
  502. // We don't want to show EST_matches on the alignment view
  503. // since that doesn't make much sense to the end user. If this
  504. // is an EST_match and the feature is an EST then we want to show
  505. // the contig in the alignments. The contig name is part of the
  506. // uniquename in the EST_match
  507. if($node->feature->cvname == 'EST' && $result->cvname == 'EST_match'){
  508. $sql = "SELECT srcfeature_id ".
  509. "FROM {featureloc} ".
  510. "WHERE feature_id = $result->feature_id ".
  511. "ORDER BY rank";
  512. $previous_db = tripal_db_set_active ('chado');
  513. $contig_fid = db_result(db_query($sql));
  514. $sql = "SELECT name FROM {feature} WHERE feature_id = $contig_fid";
  515. $contig_name = db_result(db_query($sql));
  516. tripal_db_set_active($previous_db);
  517. $sql = "SELECT nid ".
  518. "FROM {chado_feature} ".
  519. "WHERE feature_id = $contig_fid";
  520. $contig_nid = db_result(db_query($sql));
  521. // Check if the EST exists as a drupal node. If yes, add a link to
  522. // it. If no, just show the name
  523. if ($contig_nid != 0) {
  524. $sql = "SELECT nid FROM {node} WHERE nid=$contig_nid";
  525. $node_exists = db_result(db_query($sql));
  526. }
  527. $content .= "Alignment to contig $contig_name. " ;
  528. }
  529. elseif($node->feature->cvname == 'contig' && $result->cvname == 'EST_match'){
  530. $sql = "SELECT vid ".
  531. "FROM {node} ".
  532. "WHERE title = '$result->feature_name'".
  533. "ORDER BY vid DESC";
  534. // since the feature name is also the node title we can look it up
  535. $est_node = db_fetch_object(db_query($sql));
  536. $content .= "Aligned EST: $result->feature_name ";
  537. }
  538. else {
  539. $content .= "Aligned $result->cvname: $result->feature_name ";
  540. }
  541. }
  542. }
  543. $content .= "</strong>";
  544. return $content;
  545. }
  546. /*******************************************************************************
  547. * tripal_analysis_unigene_results ()
  548. * Prepare unigene result for the feature shown on the page
  549. */
  550. function theme_tripal_analysis_unigene_feature_alignments($node) {
  551. $feature = $node->feature;
  552. $obj_feature = tripal_analysis_unigene_get_alignments($feature);
  553. $alignments = $obj_feature->alignments;
  554. if(count($alignments) > 0){
  555. $content = "<div id=\"tripal_analysis_unigene_box\" class=\"tripal_unigene-info-box\">";
  556. // we're showing contig alignments in GBrowse so create a link here for
  557. // that if this feature is a contig
  558. if($node->feature->cvname == 'contig'){
  559. $content .= "<div class=\"tripal_expandableBox\">".
  560. " <h3>ESTs in this contig</h3>".
  561. "</div>";
  562. $content .= "<div class=\"tripal_expandableBoxContent\">";
  563. } else {
  564. $content .= "<div class=\"tripal_expandableBox\">".
  565. " <h3>Alignments</h3>".
  566. "</div>";
  567. $content .= "<div class=\"tripal_expandableBoxContent\">";
  568. }
  569. $content .= "".
  570. "<table class=\"tripal_table_horz\">".
  571. " <tr>".
  572. " <th>Type</th>".
  573. " <th>Feature</th>".
  574. " <th align=\"right\">Position</th>".
  575. " </tr>";
  576. // iterate through each alignment
  577. foreach ($alignments as $result){
  578. // EST alignments in chado use an EST_match type to map ESTs to
  579. // contigs and a rank to indicate the major srcfeature.
  580. // We don't want to show EST_matches on the alignment view
  581. // since that doesn't make much sense to the end user. If this
  582. // is an EST_match and the feature is an EST then we want to show
  583. // the contig in the alignments. The contig name is part of the
  584. // uniquename in the EST_match
  585. if($node->feature->cvname == 'EST' && $result->cvname == 'EST_match'){
  586. $sql = "SELECT srcfeature_id ".
  587. "FROM {featureloc} ".
  588. "WHERE feature_id = $result->feature_id ".
  589. "ORDER BY rank";
  590. $previous_db = tripal_db_set_active ('chado');
  591. $contig_fid = db_result(db_query($sql));
  592. $sql = "SELECT name FROM {feature} WHERE feature_id = $contig_fid";
  593. $contig_name = db_result(db_query($sql));
  594. tripal_db_set_active($previous_db);
  595. $sql = "SELECT nid ".
  596. "FROM {chado_feature} ".
  597. "WHERE feature_id = $contig_fid";
  598. $contig_nid = db_result(db_query($sql));
  599. // Check if the EST exists as a drupal node. If yes, add a link to
  600. // it. If no, just show the name
  601. if ($contig_nid != 0) {
  602. $sql = "SELECT nid FROM {node} WHERE nid=$contig_nid";
  603. $node_exists = db_result(db_query($sql));
  604. }
  605. $content .=
  606. "<tr>".
  607. " <td>Contig</td>".
  608. " <td>";
  609. if ($node_exists != 0) {
  610. $content .= "<a href=\"".url("node/$contig_nid")."\">".
  611. "$contig_name</a>";
  612. } else {
  613. $content .= $contig_name;
  614. }
  615. $content .=
  616. " </td>".
  617. " <td align=\"right\">".
  618. number_format($result->fmin).
  619. "-".
  620. number_format($result->fmax).
  621. " </td>".
  622. "</tr>";
  623. }
  624. elseif($node->feature->cvname == 'contig' &&
  625. $result->cvname == 'EST_match'){
  626. $sql = "SELECT vid ".
  627. "FROM {node} ".
  628. "WHERE title = '$result->feature_name'".
  629. "ORDER BY vid DESC";
  630. // since the feature name is also the node title we can look it up
  631. $est_node = db_fetch_object(db_query($sql));
  632. $content .=
  633. "<tr>".
  634. " <td>EST</td>".
  635. " <td>";
  636. //Check if the EST exists as a drupal node before adding a hyperlink
  637. if ($est_node->vid != 0) {
  638. $content .=
  639. "<a href=\"".url("node/$est_node->vid")."\">".
  640. $result->feature_name.
  641. "</a>";
  642. } else {
  643. $content .= $result->feature_name;
  644. }
  645. $content .=
  646. " </td>".
  647. " <td align=\"right\">".
  648. number_format($result->fmin).
  649. "-".
  650. number_format($result->fmax).
  651. " </td>".
  652. "</tr>";
  653. }
  654. else {
  655. $content .= "".
  656. "<tr>".
  657. " <td>$result->cvname</td>".
  658. " <td>$result->feature_name</td>".
  659. " <td align=\"right\">$result->fmin</td>".
  660. " <td align=\"right\">$result->fmax</td>".
  661. " <td align=\"right\">$result->strand</td>".
  662. "</tr>";
  663. }
  664. }
  665. $content .= "</table>";
  666. /* if this is a contig then get the alignment
  667. if($node->feature->cvname == 'contig'){
  668. // get the directory prefix
  669. $prefix = preg_replace("/^(\d*)\d{3}$/","$1",$node->feature_id);
  670. if(!$prefix){
  671. $prefix = '0';
  672. }
  673. $data_url = variable_get('chado_feature_data_url','sites/default/files/data');
  674. $fh = fopen("$data_url/misc/$prefix/$node->feature->feature_id/alignment.txt", 'r');
  675. if($fh){
  676. $content .= "<b>Alignment:</b><div class=\"tripal_feature_assembly_alignment\"><pre>";
  677. while(!feof($fh)){
  678. $content .= fgets($fh);
  679. }
  680. $content .="</pre></div>";
  681. }
  682. fclose($fh);
  683. }
  684. */
  685. $content .= "</div></div>";
  686. }
  687. return $content;
  688. }
  689. /*******************************************************************************
  690. *
  691. */
  692. function tripal_analysis_unigene_get_alignments($map) {
  693. // get the alignments for this feature
  694. $sql = "SELECT F.name as feature_name, FL.fmin, FL.fmax, FL.strand, ".
  695. " FL.phase, CVT.name as cvname, F.feature_id, F.uniquename, ".
  696. " FL.featureloc_id ".
  697. "FROM {featureloc} FL ".
  698. " INNER JOIN Feature F ON F.feature_id = FL.feature_id ".
  699. " INNER JOIN Cvterm CVT ON CVT.cvterm_id = F.type_id ".
  700. "WHERE srcfeature_id = %d AND ".
  701. " NOT(CVT.name = 'match' or CVT.name = 'match_part') ".
  702. "ORDER BY FL.fmin, FL.fmax";
  703. $previous_db = tripal_db_set_active('chado');
  704. $results = db_query($sql,$map->feature_id);
  705. $alignments = array();
  706. $i=0;
  707. while($subfeature = db_fetch_object($results)){
  708. $alignments[$i++] = $subfeature;
  709. }
  710. $additions->alignments = $alignments;
  711. /* get the GO Terms
  712. $sql = "SELECT DISTINCT * FROM {go_results_mview} ".
  713. "WHERE feature_id = %d";
  714. $results = db_query($sql,$map->feature_id);
  715. $go_terms = array();
  716. $i=0;
  717. while($term = db_fetch_object($results)){
  718. $go_terms[$i++] = $term;
  719. }
  720. $additions->go_terms = $go_terms;
  721. // get the feature properties
  722. $sql = "SELECT FP.value,FP.rank,CVT.name,CVT.definition ".
  723. "FROM {featureprop} FP".
  724. " INNER JOIN Cvterm CVT ".
  725. " ON FP.type_id = CVT.cvterm_id ".
  726. "WHERE feature_id = %d";
  727. $results = db_query($sql,$map->feature_id);
  728. $properties = array();
  729. $i=0;
  730. while($property = db_fetch_object($results)){
  731. $properties[$i++] = $property;
  732. }
  733. $additions->properties = $properties;
  734. */
  735. tripal_db_set_active($previous_db);
  736. return $additions;
  737. }
  738. /************************************************************************
  739. *
  740. */
  741. function tripal_analysis_unigene_load_organism_unigenes($organism){
  742. // get information about this assemblies and add it to the items in this node
  743. $sql = "SELECT * FROM {organism_unigene_mview} OUM ".
  744. " INNER JOIN {analysis} A ON A.analysis_id = OUM.analysis_id ".
  745. "WHERE OUM.organism_id = %d ".
  746. "ORDER BY A.timeexecuted DESC";
  747. $previous_db = tripal_db_set_active('chado'); // use chado database
  748. $results = db_query($sql,$organism->organism_id);
  749. tripal_db_set_active($previous_db); // now use drupal database
  750. $unigenes = array();
  751. $i=0;
  752. $sql = "SELECT nid FROM {chado_analysis} WHERE analysis_id = %d";
  753. while($unigene = db_fetch_object($results)){
  754. $analysis_id = $unigene->analysis_id;
  755. $c_node = db_fetch_object(db_query($sql,$analysis_id));
  756. if($c_node){
  757. $unigene->nid = $c_node->nid;
  758. }
  759. // add in the properties
  760. $unigene_name = tripal_analysis_get_property($analysis_id,'analysis_unigene_name');
  761. $num_contigs = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_contigs');
  762. $num_reads = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_reads');
  763. $num_clusters = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_clusters');
  764. $num_singlets = tripal_analysis_get_property($analysis_id,'analysis_unigene_num_singlets');
  765. $unigene->unigene_name = $unigene_name->value;
  766. $unigene->num_reads = $num_reads->value;
  767. $unigene->num_clusters = $num_clusters->value;
  768. $unigene->num_contigs = $num_contigs->value;
  769. $unigene->num_singlets = $num_singlets->value;
  770. $unigenes[$i++] = $unigene;
  771. }
  772. return $unigenes;
  773. }
  774. /************************************************************************
  775. *
  776. */
  777. function tripal_analysis_unigene_load_feature_unigenes($feature){
  778. // first get all the unigene analyses for this organism
  779. $sql = "SELECT * FROM {organism_unigene_mview} OUM ".
  780. " INNER JOIN {analysis} A ON A.analysis_id = OUM.analysis_id ".
  781. "WHERE OUM.organism_id = %d ".
  782. "ORDER BY A.timeexecuted DESC";
  783. $previous_db = tripal_db_set_active('chado'); // use chado database
  784. $results = db_query($sql,$feature->organism_id->organism_id);
  785. tripal_db_set_active($previous_db); // now use drupal database
  786. // iterate through the unigenes and find those that use this feature
  787. $unigenes = array();
  788. $i=0;
  789. $sql = "SELECT nid FROM {chado_analysis} WHERE analysis_id = %d";
  790. while($unigene = db_fetch_object($results)){
  791. $analysis_id = $unigene->analysis_id;
  792. // check if this feature is present in the unigene
  793. $values = array(
  794. 'feature_id' => $feature->feature_id,
  795. 'analysis_id' => $analysis_id,
  796. );
  797. $hasFeature = tripal_core_chado_select('analysisfeature',array('*'),$values);
  798. // if the feature is present then get information about it
  799. if(sizeof($hasFeature) > 0){
  800. // see if there is a drupal node for this unigene
  801. $c_node = db_fetch_object(db_query($sql,$analysis_id));
  802. if($c_node){
  803. $unigene->nid = $c_node->nid;
  804. }
  805. // add in the properties
  806. $unigene_name = tripal_analysis_get_property($analysis_id,'analysis_unigene_name');
  807. $singlet = tripal_core_get_property('analysisfeature',$analysis_id,'singlet','tripal');
  808. $unigene->unigene_name = $unigene_name->value;
  809. $unigene->singlet = $num_singlets->value;
  810. $unigenes[$i++] = $unigene;
  811. }
  812. }
  813. return $unigenes;
  814. }
  815. /*******************************************************************************
  816. * Tripal Unigene administrative setting form. This function is called by
  817. * tripal_analysis module which asks for an admin form to show on the page
  818. */
  819. function tripal_analysis_unigene_get_settings() {
  820. // Get an array of node types with internal names as keys
  821. $options = node_get_types('names');
  822. // Add 'chado_feature' to allowed content types for showing unigene results
  823. $allowedoptions ['chado_feature'] = "Show 'ESTs in this contig' on feature pages";
  824. $allowedoptions ['chado_organism'] = "Show assemblies on organism pages";
  825. $form['description'] = array(
  826. '#type' => 'item',
  827. '#value' => t("This option allows user to display the unigene assembly ".
  828. "information. For contigs, this would include an alignment and for ".
  829. "organisms this would be a list of assemblies. Check the box to ".
  830. "enable the display of unigene information. Uncheck to disable."),
  831. '#weight' => 0,
  832. );
  833. $form['tripal_analysis_unigene_setting'] = array(
  834. '#type' => 'checkboxes',
  835. '#options' => $allowedoptions,
  836. '#default_value'=>variable_get('tripal_analysis_unigene_setting',array()),
  837. );
  838. $settings->form = $form;
  839. $settings->title = "Tripal Unigene";
  840. return $settings;
  841. }