tripal_analysis_unigene.module 33 KB

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