tripal_feature.module 40 KB

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