tripal_analysis_unigene.module 30 KB

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