tripal_analysis_unigene.module 30 KB

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