tripal_feature.module 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. <?php
  2. /**
  3. * @defgroup tripal_feature Feature Module
  4. * @ingroup tripal_modules
  5. * @{
  6. * Integrates the Chado Sequence module with Drupal Nodes & Views
  7. * @}
  8. */
  9. require_once "api/tripal_feature.api.inc";
  10. require_once "api/tripal_feature.schema.api.inc";
  11. require_once "theme/tripal_feature.theme.inc";
  12. require_once "includes/tripal_feature.admin.inc";
  13. require_once "includes/tripal_feature.fasta_loader.inc";
  14. require_once "includes/tripal_feature.gff_loader.inc";
  15. require_once "includes/tripal_feature.seq_extract.inc";
  16. require_once "includes/tripal_feature.delete.inc";
  17. require_once "includes/tripal_feature.chado_node.inc";
  18. require_once "includes/tripal_feature.blocks.inc";
  19. /**
  20. *
  21. * @ingroup tripal_feature
  22. */
  23. function tripal_feature_init() {
  24. }
  25. /**
  26. * Implements hook_views_api()
  27. *
  28. * Purpose: Essentially this hook tells drupal that there is views support for
  29. * for this module which then includes tripal_db.views.inc where all the
  30. * views integration code is
  31. *
  32. * @ingroup tripal_feature
  33. */
  34. function tripal_feature_views_api() {
  35. return array(
  36. 'api' => 2.0,
  37. );
  38. }
  39. /**
  40. * Display help and module information
  41. *
  42. * @param
  43. * path which path of the site we're displaying help
  44. * @param
  45. * arg array that holds the current path as would be returned from arg() function
  46. *
  47. * @return
  48. * help text for the path
  49. *
  50. * @ingroup tripal_feature
  51. */
  52. function tripal_feature_help($path, $arg) {
  53. $output = '';
  54. switch ($path) {
  55. case "admin/help#tripal_feature":
  56. $output='<p>' . t("Displays links to nodes created on this date") . '</p>';
  57. break;
  58. }
  59. return $output;
  60. }
  61. /**
  62. * Set the permission types that the chado module uses. Essentially we
  63. * want permissionis that protect creation, editing and deleting of chado
  64. * data objects
  65. *
  66. * @ingroup tripal_feature
  67. */
  68. function tripal_feature_permissions() {
  69. return array(
  70. 'access chado_feature content' => array(
  71. 'title' => t('View Features'),
  72. 'description' => t('Allow users to view feature pages.'),
  73. ),
  74. 'create chado_feature content' => array(
  75. 'title' => t('Create Features'),
  76. 'description' => t('Allow users to create new feature pages.'),
  77. ),
  78. 'delete chado_feature content' => array(
  79. 'title' => t('Delete Features'),
  80. 'description' => t('Allow users to delete feature pages.'),
  81. ),
  82. 'edit chado_feature content' => array(
  83. 'title' => t('Edit Features'),
  84. 'description' => t('Allow users to edit feature pages.'),
  85. ),
  86. 'adminster tripal feature' => array(
  87. 'title' => t('Administer Features'),
  88. 'description' => t('Allow users to administer all features.'),
  89. ),
  90. );
  91. }
  92. /**
  93. * Menu items are automatically added for the new node types created
  94. * by this module to the 'Create Content' Navigation menu item. This function
  95. * adds more menu items needed for this module.
  96. *
  97. * @ingroup tripal_feature
  98. */
  99. function tripal_feature_menu() {
  100. $items = array();
  101. // the administative settings menu
  102. $items['find/sequences'] = array(
  103. 'title' => 'Sequence Retrieval',
  104. 'description' => 'Download a file of sequences',
  105. 'page callback' => 'tripal_feature_seq_extract_page',
  106. 'access arguments' => array('access chado_feature content'),
  107. 'type' => MENU_CALLBACK,
  108. );
  109. $items['find/sequences/ajax'] = array(
  110. 'title' => 'Sequence Retrieval',
  111. 'page callback' => 'tripal_feature_seq_extract_form_ahah_update',
  112. 'access arguments' => array('access chado_feature content'),
  113. 'type' => MENU_CALLBACK,
  114. );
  115. // the menu link for addressing any feature (by name, uniquename, synonym)
  116. $items['feature/%'] = array(
  117. 'page callback' => 'tripal_feature_match_features_page',
  118. 'page arguments' => array(1),
  119. 'access arguments' => array('access chado_feature content'),
  120. 'type' => MENU_LOCAL_TASK,
  121. );
  122. // the administative settings menu
  123. $items['admin/tripal/chado/tripal_feature'] = array(
  124. 'title' => 'Features',
  125. 'description' => 'A biological sequence or a section of a biological sequence, or a collection of such sections.',
  126. 'page callback' => 'tripal_feature_admin_feature_view',
  127. 'access arguments' => array('administer tripal feature'),
  128. 'type' => MENU_NORMAL_ITEM,
  129. );
  130. $items['admin/tripal/chado/tripal_feature/delete'] = array(
  131. 'title' => ' Delete',
  132. 'description' => 'Delete multiple features from Chado',
  133. 'page callback' => 'drupal_get_form',
  134. 'page arguments' => array('tripal_feature_delete_form'),
  135. 'access arguments' => array('administer tripal feature'),
  136. 'type' => MENU_LOCAL_TASK,
  137. 'weight' => 2
  138. );
  139. $items['admin/tripal/chado/tripal_feature/sync'] = array(
  140. 'title' => ' Sync',
  141. 'description' => 'Create pages on this site for features stored in Chado',
  142. 'page callback' => 'drupal_get_form',
  143. 'page arguments' => array('tripal_core_chado_node_sync_form', 'tripal_feature', 'chado_feature'),
  144. 'access arguments' => array('administer tripal feature'),
  145. 'type' => MENU_LOCAL_TASK,
  146. 'weight' => 1
  147. );
  148. $items['admin/tripal/chado/tripal_feature/configuration'] = array(
  149. 'title' => 'Settings',
  150. 'description' => 'Configure the Tripal Feature module.',
  151. 'page callback' => 'drupal_get_form',
  152. 'page arguments' => array('tripal_feature_admin'),
  153. 'access arguments' => array('administer tripal feature'),
  154. 'type' => MENU_LOCAL_TASK,
  155. 'weight' => 5
  156. );
  157. $items['admin/tripal/chado/tripal_feature/help'] = array(
  158. 'title' => 'Help',
  159. 'description' => 'Help with the Tripal Feature module.',
  160. 'page callback' => 'theme',
  161. 'page arguments' => array('tripal_feature_help'),
  162. 'access arguments' => array('administer tripal feature'),
  163. 'type' => MENU_LOCAL_TASK,
  164. 'weight' => 10
  165. );
  166. /** Loaders */
  167. $items['admin/tripal/loaders/fasta_loader'] = array(
  168. 'title' => 'Multi-FASTA file Loader',
  169. 'description' => 'Load sequences from a multi-FASTA file into Chado',
  170. 'page callback' => 'drupal_get_form',
  171. 'page arguments' => array('tripal_feature_fasta_load_form'),
  172. 'access arguments' => array('administer tripal feature'),
  173. 'type' => MENU_NORMAL_ITEM,
  174. );
  175. $items['admin/tripal/loaders/gff3_load'] = array(
  176. 'title' => 'GFF3 file Loader',
  177. 'description' => 'Import a GFF3 file into Chado',
  178. 'page callback' => 'drupal_get_form',
  179. 'page arguments' => array('tripal_feature_gff3_load_form'),
  180. 'access arguments' => array('administer tripal feature'),
  181. 'type' => MENU_NORMAL_ITEM,
  182. );
  183. // Enable admin view
  184. $items['admin/tripal/chado/tripal_feature/views/features/enable'] = array(
  185. 'title' => 'Enable feature Administrative View',
  186. 'page callback' => 'tripal_views_admin_enable_view',
  187. 'page arguments' => array('tripal_feature_admin_features', 'admin/tripal/chado/tripal_feature'),
  188. 'access arguments' => array('administer tripal feature'),
  189. 'type' => MENU_CALLBACK,
  190. );
  191. return $items;
  192. }
  193. /**
  194. * We need to let drupal know about our theme functions and their arguments.
  195. * We create theme functions to allow users of the module to customize the
  196. * look and feel of the output generated in this module
  197. *
  198. * @ingroup tripal_feature
  199. */
  200. function tripal_feature_theme($existing, $type, $theme, $path) {
  201. $core_path = drupal_get_path('module', 'tripal_core');
  202. $items = array(
  203. 'node__chado_feature' => array(
  204. 'template' => 'node--chado-generic',
  205. 'render element' => 'node',
  206. 'base hook' => 'node',
  207. 'path' => "$core_path/theme",
  208. ),
  209. 'tripal_feature_alignments' => array(
  210. 'variables' => array('node' => NULL),
  211. 'template' => 'tripal_feature_alignments',
  212. 'path' => "$path/theme/tripal_feature",
  213. ),
  214. 'tripal_feature_analyses' => array(
  215. 'variables' => array('node' => NULL),
  216. 'template' => 'tripal_feature_analyses',
  217. 'path' => "$path/theme/tripal_feature",
  218. ),
  219. 'tripal_feature_base' => array(
  220. 'variables' => array('node' => NULL),
  221. 'template' => 'tripal_feature_base',
  222. 'path' => "$path/theme/tripal_feature",
  223. ),
  224. 'tripal_feature_featurepos' => array(
  225. 'arguments' => array('node' => NULL),
  226. 'template' => 'tripal_feature_featurepos',
  227. 'path' => "$path/theme/tripal_feature",
  228. ),
  229. 'tripal_feature_sequence' => array(
  230. 'variables' => array('node' => NULL),
  231. 'template' => 'tripal_feature_sequence',
  232. 'path' => "$path/theme/tripal_feature",
  233. ),
  234. 'tripal_feature_proteins' => array(
  235. 'variables' => array('node' => NULL),
  236. 'template' => 'tripal_feature_proteins',
  237. 'path' => "$path/theme/tripal_feature",
  238. ),
  239. 'tripal_feature_publications' => array(
  240. 'variables' => array('node' => NULL),
  241. 'template' => 'tripal_feature_publications',
  242. 'path' => "$path/theme/tripal_feature",
  243. ),
  244. 'tripal_feature_synonyms' => array(
  245. 'variables' => array('node' => NULL),
  246. 'template' => 'tripal_feature_synonyms',
  247. 'path' => "$path/theme/tripal_feature",
  248. ),
  249. 'tripal_feature_references' => array(
  250. 'variables' => array('node' => NULL),
  251. 'template' => 'tripal_feature_references',
  252. 'path' => "$path/theme/tripal_feature",
  253. ),
  254. 'tripal_feature_properties' => array(
  255. 'variables' => array('node' => NULL),
  256. 'template' => 'tripal_feature_properties',
  257. 'path' => "$path/theme/tripal_feature",
  258. ),
  259. 'tripal_feature_terms' => array(
  260. 'variables' => array('node' => NULL),
  261. 'template' => 'tripal_feature_terms',
  262. 'path' => "$path/theme/tripal_feature",
  263. ),
  264. 'tripal_feature_relationships' => array(
  265. 'variables' => array('node' => NULL),
  266. 'template' => 'tripal_feature_relationships',
  267. 'path' => "$path/theme/tripal_feature",
  268. ),
  269. 'tripal_feature_help' => array(
  270. 'template' => 'tripal_feature_help',
  271. 'variables' => array(NULL),
  272. 'path' => "$path/theme/"
  273. ),
  274. // template for the organism page
  275. 'tripal_organism_feature_browser' => array(
  276. 'variables' => array('node' => NULL),
  277. 'template' => 'tripal_organism_feature_browser',
  278. 'path' => "$path/theme/tripal_organism",
  279. ),
  280. 'tripal_organism_feature_counts' => array(
  281. 'variables' => array('node' => NULL),
  282. 'template' => 'tripal_organism_feature_counts',
  283. 'path' => "$path/theme/tripal_organism",
  284. ),
  285. // themed forms
  286. 'tripal_feature_seq_extract_form' => array(
  287. 'arguments' => array('form'),
  288. ),
  289. // themed teaser
  290. 'tripal_feature_teaser' => array(
  291. 'variables' => array('node' => NULL),
  292. 'template' => 'tripal_feature_teaser',
  293. 'path' => "$path/theme/tripal_feature",
  294. ),
  295. );
  296. return $items;
  297. }
  298. /**
  299. *
  300. *
  301. * @ingroup tripal_feature
  302. */
  303. function tripal_feature_load_featurelocs($feature_id, $side = 'as_parent', $aggregate = 1) {
  304. $sql = "
  305. SELECT
  306. F.name, F.feature_id, F.uniquename,
  307. FS.name as src_name, FS.feature_id as src_feature_id, FS.uniquename as src_uniquename,
  308. CVT.name as cvname, CVT.cvterm_id,
  309. CVTS.name as src_cvname, CVTS.cvterm_id as src_cvterm_id,
  310. FL.fmin, FL.fmax, FL.is_fmin_partial, FL.is_fmax_partial,FL.strand, FL.phase
  311. FROM {featureloc} FL
  312. INNER JOIN {feature} F ON FL.feature_id = F.feature_id
  313. INNER JOIN {feature} FS ON FS.feature_id = FL.srcfeature_id
  314. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  315. INNER JOIN {cvterm} CVTS ON FS.type_id = CVTS.cvterm_id
  316. ";
  317. if (strcmp($side, 'as_parent')==0) {
  318. $sql .= "WHERE FL.srcfeature_id = :feature_id ";
  319. }
  320. if (strcmp($side, 'as_child')==0) {
  321. $sql .= "WHERE FL.feature_id = :feature_id ";
  322. }
  323. $flresults = chado_query($sql, array(':feature_id' => $feature_id));
  324. // copy the results into an array
  325. $i=0;
  326. $featurelocs = array();
  327. while ($loc = $flresults->fetchObject()) {
  328. // if a drupal node exists for this feature then add the nid to the
  329. // results object
  330. $loc->fnid = chado_get_nid_from_id('feature', $loc->feature_id);
  331. $loc->snid = chado_get_nid_from_id('feature', $loc->src_feature_id);
  332. // add the result to the array
  333. $featurelocs[$i++] = $loc;
  334. }
  335. // Add the relationship feature locs if aggregate is turned on
  336. if ($aggregate and strcmp($side, 'as_parent')==0) {
  337. // get the relationships for this feature without substituting any children
  338. // for the parent. We want all relationships
  339. $relationships = tripal_feature_get_aggregate_relationships($feature_id, 0);
  340. foreach ($relationships as $rindex => $rel) {
  341. // get the featurelocs for each of the relationship features
  342. $rel_featurelocs = tripal_feature_load_featurelocs($rel->subject_id, 'as_child', 0);
  343. foreach ($rel_featurelocs as $findex => $rfloc) {
  344. $featurelocs[$i++] = $rfloc;
  345. }
  346. }
  347. }
  348. usort($featurelocs, 'tripal_feature_sort_locations');
  349. return $featurelocs;
  350. }
  351. /**
  352. * used to sort the feature locs by start position
  353. *
  354. * @ingroup tripal_feature
  355. */
  356. function tripal_feature_sort_locations($a, $b) {
  357. return strnatcmp($a->fmin, $b->fmin);
  358. }
  359. /**
  360. *
  361. *
  362. * @ingroup tripal_feature
  363. */
  364. function tripal_feature_load_relationships($feature_id, $side = 'as_subject') {
  365. // get the relationships for this feature. The query below is used for both
  366. // querying the object and subject relationships
  367. $sql = "
  368. SELECT
  369. FS.name as subject_name, FS.uniquename as subject_uniquename,
  370. CVTS.name as subject_type, CVTS.cvterm_id as subject_type_id,
  371. FR.subject_id, FR.type_id as relationship_type_id, FR.object_id, FR.rank,
  372. CVT.name as rel_type,
  373. FO.name as object_name, FO.uniquename as object_uniquename,
  374. CVTO.name as object_type, CVTO.cvterm_id as object_type_id
  375. FROM {feature_relationship} FR
  376. INNER JOIN {cvterm} CVT ON FR.type_id = CVT.cvterm_id
  377. INNER JOIN {feature} FS ON FS.feature_id = FR.subject_id
  378. INNER JOIN {feature} FO ON FO.feature_id = FR.object_id
  379. INNER JOIN {cvterm} CVTO ON FO.type_id = CVTO.cvterm_id
  380. INNER JOIN {cvterm} CVTS ON FS.type_id = CVTS.cvterm_id
  381. ";
  382. if (strcmp($side, 'as_object')==0) {
  383. $sql .= " WHERE FR.object_id = :feature_id";
  384. }
  385. if (strcmp($side, 'as_subject')==0) {
  386. $sql .= " WHERE FR.subject_id = :feature_id";
  387. }
  388. $sql .= " ORDER BY FR.rank";
  389. // get the relationships
  390. $results = chado_query($sql, array(':feature_id' => $feature_id));
  391. // iterate through the relationships, put these in an array and add
  392. // in the Drupal node id if one exists
  393. $i=0;
  394. $nodesql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
  395. $relationships = array();
  396. while ($rel = $results->fetchObject()) {
  397. $node = db_query($nodesql, array(':feature_id' => $rel->subject_id))->fetchObject();
  398. if ($node) {
  399. $rel->subject_nid = $node->nid;
  400. }
  401. $node = db_query($nodesql, array(':feature_id' => $rel->object_id))->fetchObject();
  402. if ($node) {
  403. $rel->object_nid = $node->nid;
  404. }
  405. $relationships[$i++] = $rel;
  406. }
  407. return $relationships;
  408. }
  409. /**
  410. *
  411. *
  412. * @ingroup tripal_feature
  413. */
  414. function tripal_feature_get_aggregate_relationships($feature_id, $substitute=1,
  415. $levels=0, $base_type_id=NULL, $depth=0) {
  416. // we only want to recurse to as many levels deep as indicated by the
  417. // $levels variable, but only if this variable is > 0. If 0 then we
  418. // recurse until we reach the end of the relationships tree.
  419. if ($levels > 0 and $levels == $depth) {
  420. return NULL;
  421. }
  422. // first get the relationships for this feature
  423. return tripal_feature_load_relationships($feature_id, 'as_object');
  424. }
  425. /**
  426. *
  427. *
  428. * @ingroup tripal_feature
  429. */
  430. function tripal_feature_load_featureloc_sequences($feature_id, $featurelocs) {
  431. // if we don't have any featurelocs then no point in continuing
  432. if (!$featurelocs) {
  433. return array();
  434. }
  435. // get the list of relationships (including any aggregators) and iterate
  436. // through each one to find information needed to color-code the reference sequence
  437. $relationships = tripal_feature_get_aggregate_relationships($feature_id);
  438. if (!$relationships) {
  439. return array();
  440. }
  441. // iterate through each of the realtionships features and get their
  442. // locations
  443. foreach ($relationships as $rindex => $rel) {
  444. // get the featurelocs for each of the relationship features
  445. $rel_featurelocs = tripal_feature_load_featurelocs($rel->subject_id, 'as_child', 0);
  446. foreach ($rel_featurelocs as $rfindex => $rel_featureloc) {
  447. // keep track of this unique source feature
  448. $src = $rel_featureloc->src_feature_id . "-" . $rel_featureloc->src_cvterm_id;
  449. // copy over the results to the relationship object. Since there can
  450. // be more than one feature location for each relationship feature we
  451. // use the '$src' variable to keep track of these.
  452. $rel->featurelocs = new stdClass();
  453. $rel->featurelocs->$src = new stdClass();
  454. $rel->featurelocs->$src->src_uniquename = $rel_featureloc->src_uniquename;
  455. $rel->featurelocs->$src->src_cvterm_id = $rel_featureloc->src_cvterm_id;
  456. $rel->featurelocs->$src->src_cvname = $rel_featureloc->src_cvname;
  457. $rel->featurelocs->$src->fmin = $rel_featureloc->fmin;
  458. $rel->featurelocs->$src->fmax = $rel_featureloc->fmax;
  459. $rel->featurelocs->$src->src_name = $rel_featureloc->src_name;
  460. // keep track of the individual parts for each relationship
  461. $start = $rel->featurelocs->$src->fmin;
  462. $end = $rel->featurelocs->$src->fmax;
  463. $type = $rel->subject_type;
  464. $rel_locs[$src]['parts'][$start][$type]['start'] = $start;
  465. $rel_locs[$src]['parts'][$start][$type]['end'] = $end;
  466. $rel_locs[$src]['parts'][$start][$type]['type'] = $type;
  467. }
  468. }
  469. // the featurelocs array provided to the function contains the locations
  470. // where this feature is found. We want to get the sequence for each
  471. // location and then annotate it with the parts found from the relationships
  472. // locations determiend above.
  473. $floc_sequences = array();
  474. foreach ($featurelocs as $featureloc) {
  475. // build the src name so we can keep track of the different parts for each feature
  476. $src = $featureloc->srcfeature_id->feature_id . "-" . $featureloc->srcfeature_id->type_id->cvterm_id;
  477. // orient the parts to the beginning of the feature sequence
  478. if (!empty($rel_locs[$src]['parts'])) {
  479. $parts = $rel_locs[$src]['parts'];
  480. $rparts = array(); // we will fill this up if we're on the reverse strand
  481. foreach ($parts as $start => $types) {
  482. foreach ($types as $type_name => $type) {
  483. if ($featureloc->strand >= 0) {
  484. // this is on the forward strand. We need to convert the start on the src feature to the
  485. // start on this feature's sequence
  486. $parts[$start][$type_name]['start'] = $parts[$start][$type_name]['start'] - $featureloc->fmin;
  487. $parts[$start][$type_name]['end'] = $parts[$start][$type_name]['end'] - $featureloc->fmin;
  488. $parts[$start][$type_name]['type'] = $type_name;
  489. }
  490. else {
  491. // this is on the reverse strand. We need to swap the start and stop and calculate from the
  492. // begining of the reverse sequence
  493. $size = ($featureloc->fmax - $featureloc->fmin);
  494. $start_orig = $parts[$start][$type_name]['start'];
  495. $end_orig = $parts[$start][$type_name]['end'];
  496. $new_start = $size - ($end_orig - $featureloc->fmin);
  497. $new_end = $size - ($start_orig - $featureloc->fmin);
  498. $rparts[$new_start][$type_name]['start'] = $new_start;
  499. $rparts[$new_start][$type_name]['end'] = $new_end;
  500. $rparts[$new_start][$type_name]['type'] = $type_name;
  501. }
  502. }
  503. }
  504. // now sort the parts
  505. // if we're on the reverse strand we need to resort
  506. if ($featureloc->strand >= 0) {
  507. usort($parts, 'tripal_feature_sort_rel_parts_by_start');
  508. }
  509. else {
  510. usort($rparts, 'tripal_feature_sort_rel_parts_by_start');
  511. $parts = $rparts;
  512. }
  513. $floc_sequences[$src]['src'] = $src;
  514. $floc_sequences[$src]['type'] = $featureloc->feature_id->type_id->name;
  515. $args = array(':feature_id' => $featureloc->srcfeature_id->feature_id);
  516. $start = $featureloc->fmin + 1;
  517. $size = $featureloc->fmax - $featureloc->fmin;
  518. // TODO: fix the hard coded $start and $size
  519. // the $start and $size variables are hard-coded in the SQL statement
  520. // because the db_query function places quotes around all placeholders
  521. // (e.g. :start & :size) and screws up the substring function
  522. $sql = "
  523. SELECT substring(residues from $start for $size) as residues
  524. FROM {feature}
  525. WHERE feature_id = :feature_id
  526. ";
  527. $sequence = chado_query($sql, $args)->fetchObject();
  528. $residues = $sequence->residues;
  529. if ($featureloc->strand < 0) {
  530. $residues = tripal_feature_reverse_complement($residues);
  531. }
  532. $strand = '.';
  533. if ($featureloc->strand == 1) {
  534. $strand = '+';
  535. }
  536. elseif ($featureloc->strand == -1) {
  537. $strand = '-';
  538. }
  539. $defline = $featureloc->feature_id->name . " " . $featureloc->srcfeature_id->name . ":" . ($featureloc->fmin + 1) . ".." . $featureloc->fmax . " " . $strand;
  540. $floc_sequences[$src]['formatted_seq'] = tripal_feature_color_sequence($residues, $parts, $defline);
  541. }
  542. }
  543. return $floc_sequences;
  544. }
  545. /**
  546. *
  547. *
  548. * @ingroup tripal_feature
  549. */
  550. function tripal_feature_get_matched_alignments($feature) {
  551. // This function is for features that align through an intermediate such
  552. // as 'EST_match' or 'match'. This occurs in the case where two sequences
  553. // align but where one does not align perfectly. Some ESTs may be in a contig
  554. // but not all of the EST. Portions may overhang and not be included in the
  555. // consensus if quality is bad.
  556. // For example:
  557. //
  558. // Feature 1: Contig --------------------
  559. // Feature 2: EST_match -------
  560. // Feature 3: EST ---------
  561. //
  562. // The feature provided to the function will always be the feature 1. The
  563. // featureloc columns prefixed with 'right' (e.g. right_fmin) belong to the
  564. // alignment of feature 3 with feature 2
  565. //
  566. // Features may align to more than one feature and are not matches. We do
  567. // not want to include these, so we have to filter on the SO terms:
  568. // match, or %_match
  569. //
  570. $sql = "
  571. SELECT
  572. FL1.featureloc_id as left_featureloc_id,
  573. FL1.srcfeature_id as left_srcfeature_id,
  574. FL1.feature_id as left_feature_id,
  575. FL1.fmin as left_fmin,
  576. FL1.is_fmin_partial as left_is_fmin_partial,
  577. FL1.fmax as left_fmax,
  578. FL1.is_fmax_partial as left_is_fmax_partial,
  579. FL1.strand as left_strand,
  580. FL1.phase as left_phase,
  581. FL1.locgroup as left_locgroup,
  582. FL1.rank as left_rank,
  583. FL2.featureloc_id as right_featureloc_id,
  584. FL2.srcfeature_id as right_srcfeature_id,
  585. FL2.feature_id as right_feature_id,
  586. FL2.fmin as right_fmin,
  587. FL2.is_fmin_partial as right_is_fmin_partial,
  588. FL2.fmax as right_fmax,
  589. FL2.is_fmax_partial as right_is_fmax_partial,
  590. FL2.strand as right_strand,
  591. FL2.phase as right_phase,
  592. FL2.locgroup as right_locgroup,
  593. FL2.rank as right_rank
  594. FROM {feature} F1
  595. INNER JOIN {featureloc} FL1 on FL1.srcfeature_id = F1.feature_id
  596. INNER JOIN {feature} F2 on FL1.feature_id = F2.feature_id
  597. INNER JOIN {featureloc} FL2 on FL2.feature_id = F2.feature_id
  598. INNER JOIN {cvterm} CVT2 on F2.type_id = CVT2.cvterm_id
  599. WHERE
  600. F1.feature_id = :feature_id AND
  601. (CVT2.name = 'match' or CVT2.name like '%_match')
  602. ORDER BY FL1.fmin
  603. ";
  604. $results = chado_query($sql, array(':feature_id' => $feature->feature_id));
  605. // iterate through the results and add them to our featurelocs array
  606. $featurelocs = array();
  607. while ($fl = $results->fetchObject()) {
  608. // ignore featurelocs where the left and right srcfeature is the same
  609. if (strcmp($fl->left_srcfeature_id, $fl->right_srcfeature_id) == 0) {
  610. continue;
  611. }
  612. $featurelocs[] = $fl ;
  613. }
  614. return $featurelocs;
  615. }
  616. /**
  617. *
  618. *
  619. * @ingroup tripal_feature
  620. */
  621. function tripal_feature_load_organism_feature_counts($organism) {
  622. // don't show the browser if the settings in the admin page is turned off
  623. // instead return the array indicating the status of the browser
  624. $show_counts = variable_get('tripal_feature_summary_setting', 'show_feature_summary');
  625. if (strcmp($show_counts, 'show_feature_summary')!=0) {
  626. return array('enabled' => FALSE );
  627. }
  628. $args = array();
  629. $names = array();
  630. $order = array();
  631. // build the where clause for the SQL statement if we have a custom term list
  632. // we'll also keep track of the names the admin provided (if any) and the
  633. // order that the terms should appear.
  634. $is_custom = 0;
  635. $temp = rtrim(variable_get('tripal_feature_summary_report_mapping', ''));
  636. $where = '';
  637. if ($temp) {
  638. $is_custom = 1;
  639. $temp = explode("\n", $temp);
  640. $i = 0;
  641. foreach ($temp as $key => $value) {
  642. // separate the key value pairs
  643. $temp2 = explode("=", $value);
  644. $feature_type = rtrim($temp2[0]);
  645. $args[] = $feature_type;
  646. $order[] = $feature_type;
  647. // if a new name is provided then use that otherwise just
  648. // use the feature type
  649. if (count($temp2) == 2) {
  650. $names[":name$i"] = rtrim($temp2[1]);
  651. }
  652. else {
  653. $names[":name$i"] = $feature_type;
  654. }
  655. $where .= " OFC.feature_type = :name$i OR ";
  656. $i++;
  657. }
  658. if ($where) {
  659. $where = drupal_substr($where, 0, -5); # remove OR from the end
  660. $where = "($where) AND";
  661. }
  662. }
  663. // get the feature counts. This is dependent on a materialized view
  664. // installed with the organism module
  665. $sql = "
  666. SELECT OFC.num_features,OFC.feature_type,CVT.definition
  667. FROM {organism_feature_count} OFC
  668. INNER JOIN {cvterm} CVT on OFC.cvterm_id = CVT.cvterm_id
  669. WHERE $where organism_id = :organism_id
  670. ORDER BY num_features desc
  671. ";
  672. $args[':organism_id'] = $organism->organism_id;
  673. $org_features = chado_query($sql, $args);
  674. // iterate through the types
  675. $types = array();
  676. while ($type = $org_features->fetchObject()) {
  677. $types[$type->feature_type] = $type;
  678. // if we don't have an order this means we didn't go through the loop
  679. // above to set the names, so do that now
  680. if (!$is_custom) {
  681. $names[] = $type->feature_type;
  682. $order[] = $type->feature_type;
  683. }
  684. }
  685. # now reorder the types
  686. $ordered_types = array();
  687. foreach ($order as $type) {
  688. $ordered_types[] = $types[$type];
  689. }
  690. return array( 'types' => $ordered_types, 'names' => $names, 'enabled' => TRUE );
  691. }
  692. /**
  693. * used to sort the list of relationship parts by start position
  694. *
  695. * @ingroup tripal_feature
  696. */
  697. function tripal_feature_sort_rel_parts_by_start($a, $b) {
  698. foreach ($a as $type_name => $details) {
  699. $astart = $a[$type_name]['start'];
  700. break;
  701. }
  702. foreach ($b as $type_name => $details) {
  703. $bstart = $b[$type_name]['start'];
  704. break;
  705. }
  706. return strnatcmp($astart, $bstart);
  707. }
  708. /**
  709. * used to sort the list of relationship parts by start position
  710. *
  711. * @ingroup tripal_feature
  712. */
  713. function tripal_feature_sort_rel_parts_by_end($a, $b) {
  714. $val = strnatcmp($b['end'], $a['end']);
  715. if ($val == 0) {
  716. return strcmp($a['type'], $b['type']);
  717. }
  718. return $val;
  719. }
  720. /**
  721. *
  722. *
  723. * @ingroup tripal_feature
  724. */
  725. function tripal_feature_color_sequence($sequence, $parts, $defline) {
  726. $types = array();
  727. // first get the list of types so we can create a color legend
  728. foreach ($parts as $index => $t) {
  729. foreach ($t as $type_name => $details) {
  730. $types[$type_name] = 1;
  731. }
  732. }
  733. $newseq = "<div id=\"tripal_feature-featureloc_sequence-legend\">Legend: ";
  734. foreach ($types as $type_name => $present) {
  735. $newseq .= "<span id=\"tripal_feature-legend-$type_name\" class=\"tripal_feature-legend-item tripal_feature-featureloc_sequence-$type_name\" script=\"\">$type_name</span>";
  736. }
  737. $newseq .= "</div>Hold the cursor over a type above to highlight its positions in the sequence below. The colors in the sequence below merge when types overlap.";
  738. // set the background color of the rows based on the type
  739. $pos = 0;
  740. $newseq .= "<pre id=\"tripal_feature-featureloc_sequence\">";
  741. $newseq .= ">$defline\n";
  742. // iterate through the parts. They should be in order.
  743. $ends = array();
  744. $seqcount = 0;
  745. foreach ($parts as $index => $types) {
  746. // get the start for this part. All types in this part start at the
  747. // same position so we only need the first record
  748. foreach ($types as $type => $child) {
  749. $start = $child['start'];
  750. break;
  751. }
  752. // add in the sequence up to the start of this part
  753. for ($i = $pos; $i < $start; $i++) {
  754. $newseq .= $sequence{$pos};
  755. $seqcount++;
  756. if ($seqcount % 50 == 0) {
  757. $newseq .= "\n";
  758. }
  759. if (array_key_exists($pos, $ends)) {
  760. foreach ($ends[$pos] as $end) {
  761. $newseq .= "</span>";
  762. }
  763. }
  764. $pos++;
  765. }
  766. // we want to sort the parts by their end. We want the span tag to
  767. // to be added in the order the parts end.
  768. usort($types, 'tripal_feature_sort_rel_parts_by_end');
  769. // now add the child span for all types that start at this position
  770. foreach ($types as $type) {
  771. $class = "tripal_feature-featureloc_sequence-" . $type['type'];
  772. $newseq .= "<span class=\"$class\">";
  773. // add the end position
  774. $end = $type['end'];
  775. $ends[$end][] = $end;
  776. }
  777. }
  778. // add in rest of the sequence
  779. for ($i = $pos; $i < strlen($sequence); $i++) {
  780. $newseq .= $sequence{$pos};
  781. $seqcount++;
  782. if ($seqcount % 50 == 0) {
  783. $newseq .= "\n";
  784. }
  785. if (array_key_exists($pos, $ends)) {
  786. foreach ($ends[$pos] as $end) {
  787. $newseq .= "</span>";
  788. }
  789. }
  790. $pos++;
  791. }
  792. $newseq .= "</pre>";
  793. return $newseq;
  794. }
  795. /**
  796. *
  797. *
  798. * @ingroup tripal_feature
  799. */
  800. function tripal_feature_cv_chart($chart_id) {
  801. // we only want the chart to show feature types setup by the admin
  802. $temp = rtrim(variable_get('tripal_feature_summary_report_mapping', ''));
  803. $where = '';
  804. if ($temp) {
  805. $temp = explode("\n", $temp);
  806. foreach ($temp as $key => $value) {
  807. $temp2 = explode("=", $value);
  808. $feature_type = rtrim($temp2[0]);
  809. $where .= "CNT.feature_type = '$feature_type' OR \n";
  810. }
  811. if ($where) {
  812. $where = drupal_substr($where, 0, -5); # remove OR from the end
  813. $where = "($where) AND";
  814. }
  815. }
  816. $organism_id = preg_replace("/^tripal_feature_cv_chart_(\d+)$/", "$1", $chart_id);
  817. // The CV module will create the JSON array necessary for buillding a
  818. // pie chart using jgChart and Google Charts. We have to pass to it
  819. // a table that contains count information, tell it which column
  820. // contains the cvterm_id and provide a filter for getting the
  821. // results we want from the table.
  822. $options = array(
  823. count_mview => 'organism_feature_count',
  824. cvterm_id_column => 'cvterm_id',
  825. count_column => 'num_features',
  826. size => '550x200',
  827. filter => "$where CNT.organism_id = $organism_id",
  828. );
  829. return $options;
  830. }
  831. /**
  832. *
  833. *
  834. * @ingroup tripal_feature
  835. */
  836. function tripal_feature_cv_tree($tree_id) {
  837. // The CV module will create the JSON array necessary for buillding a
  838. // pie chart using jgChart and Google Charts. We have to pass to it
  839. // a table that contains count information, tell it which column
  840. // contains the cvterm_id and provide a filter for getting the
  841. // results we want from the table.
  842. $organism_id = preg_replace("/^tripal_feature_cv_tree_(\d+)$/", "$1", $tree_id);
  843. $options = array(
  844. cv_id => tripal_cv_get_cv_id('sequence'),
  845. count_mview => 'organism_feature_count',
  846. cvterm_id_column => 'cvterm_id',
  847. count_column => 'num_features',
  848. filter => "CNT.organism_id = $organism_id",
  849. label => 'Features',
  850. );
  851. return $options;
  852. }
  853. /**
  854. *
  855. *
  856. * @ingroup tripal_feature
  857. */
  858. function tripal_feature_del_vocabulary() {
  859. //include the file containing the required functions for adding taxonomy vocabs
  860. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  861. // get the vocabularies
  862. $vocabularies = taxonomy_get_vocabularies();
  863. // These taxonomic terms are hard coded because we
  864. // know we have these relationships in the chado tables
  865. // through foreign key relationships. The tripal
  866. // modules that correspond to these chado "modules" don't
  867. // need to be installed for the taxonomy to work.
  868. foreach ($vocabularies as $vocab) {
  869. if ($vocab->name == 'Feature Type') {
  870. taxonomy_vocabulary_delete($vocab->vid);
  871. }
  872. if ($vocab->name == 'Organism') {
  873. taxonomy_vocabulary_delete($vocab->vid);
  874. }
  875. if ($vocab->name == 'Library') {
  876. taxonomy_vocabulary_delete($vocab->vid);
  877. }
  878. if ($vocab->name == 'Analysis') {
  879. taxonomy_vocabulary_delete($vocab->vid);
  880. }
  881. }
  882. }
  883. /**
  884. *
  885. *
  886. * @ingroup tripal_feature
  887. */
  888. function tripal_feature_job_describe_args($callback, $args) {
  889. $new_args = array();
  890. if ($callback == 'tripal_feature_load_fasta') {
  891. $new_args['FASTA file'] = $args[0];
  892. $organism = chado_select_record('organism', array('genus', 'species'), array('organism_id' => $args[1]));
  893. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  894. $new_args['Sequence Type'] = $args[2];
  895. $new_args['Name Match Type'] = $args[14];
  896. $new_args['Name RE'] = $args[4];
  897. $new_args['Unique Name RE'] = $args[5];
  898. // add in the relationship arguments
  899. $new_args['Relationship Type'] = $args[8];
  900. $new_args['Relationship Parent RE'] = $args[9];
  901. $new_args['Relationship Parent Type'] = $args[10];
  902. // add in the database reference arguments
  903. if ($args[7]) {
  904. $db = chado_select_record('db', array('name'), array('db_id' => $args[7]));
  905. }
  906. $new_args['Database Reference'] = $db[0]->name;
  907. $new_args['Accession RE'] = $args[6];
  908. $new_args['Method'] = $args[11];
  909. // add in the analysis
  910. if ($args[13]) {
  911. $analysis = chado_select_record('analysis', array('name'), array('analysis_id' => $args[13]));
  912. }
  913. $new_args['Analysis'] = $analysis[0]->name;
  914. }
  915. if ($callback == 'tripal_feature_delete_features') {
  916. if ($args[0]) {
  917. $organism = chado_select_record('organism', array('genus', 'species'), array('organism_id' => $args[0]));
  918. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  919. }
  920. else {
  921. $new_args['Organism'] = '';
  922. }
  923. if ($args[1]) {
  924. $analysis = chado_select_record('analysis', array('name'), array('analysis_id' => $args[1]));
  925. $new_args['Analysis'] = $analysis[0]->name;
  926. }
  927. else {
  928. $new_args['Analysis'] = '';
  929. }
  930. $new_args['Sequence Type'] = $args[2];
  931. $new_args['Is Unique Name'] = $args[3];
  932. $new_args['Features Names'] = $args[4];
  933. }
  934. elseif ($callback == 'tripal_feature_load_gff3') {
  935. $new_args['GFF File'] = $args[0];
  936. $organism = chado_select_record('organism', array('genus', 'species'), array('organism_id' => $args[1]));
  937. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  938. $analysis = chado_select_record('analysis', array('name'), array('analysis_id' => $args[2]));
  939. $new_args['Analysis'] = $analysis[0]->name;
  940. $new_args['Use a Transaction'] = ($args[7] == 1) ? "Yes" : "No";
  941. $new_args['Import only new features'] = ($args[3] == 1) ? "Yes" : "No";
  942. $new_args['Import all and update'] = ($args[4] == 1) ? "Yes" : "No";
  943. $new_args['Import all and replace'] = ($args[5] == 1) ? "Yes" : "No";
  944. $new_args['Delete features'] = ($args[6] == 1) ? "Yes" : "No";
  945. if ($args[8]) {
  946. $target_organism = chado_select_record('organism', array('genus', 'species'), array('organism_id' => $args[8]));
  947. $new_args['Target organism'] = $target_organism[0]->genus . " " . $target_organism[0]->species;
  948. }
  949. else {
  950. $new_args['Target organism'] = '';
  951. }
  952. $new_args['Target type'] = $args[9];
  953. $new_args['Create target'] = ($args[10] == 1) ? "Yes" : "No";
  954. $new_args['Starting line'] = $args[11];
  955. $new_args['Landmark Type'] = $args[12];
  956. $new_args['Alternate ID attribute'] = $args[13];
  957. $new_args['Create Organism'] = ($args[14] == 1) ? "Yes" : "No";
  958. }
  959. return $new_args;
  960. }
  961. /**
  962. * Implements hook_coder_ignore().
  963. * Defines the path to the file (tripal_core.coder_ignores.txt) where ignore rules for coder are stored
  964. */
  965. function tripal_feature_coder_ignore() {
  966. return array(
  967. 'path' => drupal_get_path('module', 'tripal_feature'),
  968. 'line prefix' => drupal_get_path('module', 'tripal_feature'),
  969. );
  970. }
  971. /*
  972. * Uses the value provided in the $id argument to find all features that match
  973. * that ID by name, featurename or synonym. If it matches uniquenly to a single
  974. * feature it will redirect to that feature page, otherwise, a list of matching
  975. * features is shown.
  976. */
  977. function tripal_feature_match_features_page($id) {
  978. // if the URL alias configuration is set such that the URL
  979. // always begins with 'feature' then we want to use the ID as it is and
  980. // forward it on. Otherwise, try to find the matching feature.
  981. $url_alias = variable_get('chado_feature_url_string', '/feature/[genus]/[species]/[type]/[uniquename]');
  982. if (!$url_alias) {
  983. $url_alias = '/feature/[genus]/[species]/[type]/[uniquename]';
  984. }
  985. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  986. if (preg_match('/^feature\//', $url_alias)) {
  987. drupal_goto($id);
  988. }
  989. $sql = "
  990. SELECT
  991. F.name, F.uniquename, F.feature_id,
  992. O.genus, O.species, O.organism_id,
  993. CVT.cvterm_id, CVT.name as type_name,
  994. CF.nid,
  995. array_agg(S.name) as synonyms
  996. FROM {feature} F
  997. INNER JOIN {organism} O on F.organism_id = O.organism_id
  998. INNER JOIN {cvterm} CVT on CVT.cvterm_id = F.type_id
  999. LEFT JOIN {feature_synonym} FS on FS.feature_id = F.feature_id
  1000. LEFT JOIN {synonym} S on S.synonym_id = FS.synonym_id
  1001. INNER JOIN public.chado_feature CF on CF.feature_id = F.feature_id
  1002. WHERE
  1003. F.uniquename = :uname or
  1004. F.name = :fname' or
  1005. S.name = :sname
  1006. GROUP BY F.name, F.uniquename, F.feature_id, O.genus, O.species,
  1007. O.organism_id, CVT.cvterm_id, CVT.name, CF.nid
  1008. ";
  1009. $results = chado_query($sql, array(':uname' => $id, ':fname' => $id, ':sname' => $id));
  1010. $num_matches = 0;
  1011. // iterate through the matches and build the table for showing matches
  1012. $header = array('Uniquename', 'Name', 'Type', 'Species', 'Synonyms');
  1013. $rows = array();
  1014. $curr_match;
  1015. while ($match = $results->fetchObject()) {
  1016. $curr_match = $match;
  1017. $synonyms = $match->synonyms;
  1018. $synonyms = preg_replace('/[\"\{\}]/', '', $synonyms);
  1019. $rows[] = array(
  1020. $match->uniquename,
  1021. "<a href=\"" . url("node/" . $match->nid) . "\">" . $match->name . "</a>",
  1022. $match->type_name,
  1023. '<i>' . $match->genus . ' ' . $match->species . '</i>',
  1024. $synonyms,
  1025. );
  1026. $num_matches++;
  1027. }
  1028. // if we have more than one match then generate the table, otherwise, redirect
  1029. // to the matched feature
  1030. if ($num_matches == 1) {
  1031. drupal_goto("node/" . $curr_match->nid);
  1032. }
  1033. if ($num_matches == 0) {
  1034. return "<p>No features matched the given name '$id'</p>";
  1035. }
  1036. $table_attrs = array(
  1037. 'class' => 'tripal-table tripal-table-horz'
  1038. );
  1039. $output = "<p>The following features match the name '$id'.</p>";
  1040. $output .= theme_table($header, $rows, $table_attrs, $caption);
  1041. return $output;
  1042. }
  1043. /**
  1044. * Implementation of hook_form_alter()
  1045. *
  1046. * @param $form
  1047. * @param $form_state
  1048. * @param $form_id
  1049. */
  1050. function tripal_feature_form_alter(&$form, &$form_state, $form_id) {
  1051. if ($form_id == "tripal_feature_seq_extract_form") {
  1052. // updating the form through the ahah callback sets the action of
  1053. // the form to the ahah callback URL. We need to set it back
  1054. // to the normal form URL
  1055. $form['#action'] = url("find/sequences");
  1056. }
  1057. // turn off preview button for insert/updates
  1058. if ($form_id == "chado_feature_node_form") {
  1059. $form['actions']['preview']['#access'] = FALSE;
  1060. }
  1061. }