tripal_analysis_unigene.module 35 KB

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