tripal_analysis_unigene.module 29 KB

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