tripal_feature.module 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. * @defgroup tripal_feature Feature Module
  8. * @ingroup tripal_modules
  9. * @{
  10. * Provides functions for managing chado features including creating details pages for each feature
  11. * @}
  12. */
  13. require_once "api/tripal_feature.api.inc";
  14. require_once "api/tripal_feature.schema.api.inc";
  15. require_once "includes/tripal_feature.admin.inc";
  16. require_once "includes/tripal_feature.sync_features.inc";
  17. require_once "includes/tripal_feature.fasta_loader.inc";
  18. require_once "includes/tripal_feature.gff_loader.inc";
  19. require_once "includes/tripal_feature.seq_extract.inc";
  20. require_once "includes/tripal_feature.delete.inc";
  21. require_once "includes/tripal_feature.form.inc";
  22. /**
  23. *
  24. * @ingroup tripal_feature
  25. */
  26. function tripal_feature_init() {
  27. drupal_add_css(drupal_get_path('module', 'tripal_feature') . '/theme/css/tripal_feature.css');
  28. drupal_add_js(drupal_get_path('module', 'tripal_feature') . '/theme/js/tripal_feature.js');
  29. }
  30. /**
  31. * Implements hook_views_api()
  32. *
  33. * Purpose: Essentially this hook tells drupal that there is views support for
  34. * for this module which then includes tripal_db.views.inc where all the
  35. * views integration code is
  36. *
  37. * @ingroup tripal_feature
  38. */
  39. function tripal_feature_views_api() {
  40. return array(
  41. 'api' => 2.0,
  42. );
  43. }
  44. /**
  45. * Display help and module information
  46. *
  47. * @param
  48. * path which path of the site we're displaying help
  49. * @param
  50. * arg array that holds the current path as would be returned from arg() function
  51. *
  52. * @return
  53. * help text for the path
  54. *
  55. * @ingroup tripal_feature
  56. */
  57. function tripal_feature_help($path, $arg) {
  58. $output = '';
  59. switch ($path) {
  60. case "admin/help#tripal_feature":
  61. $output='<p>' . t("Displays links to nodes created on this date") . '</p>';
  62. break;
  63. }
  64. return $output;
  65. }
  66. /**
  67. * Provide information to drupal about the node types that we're creating
  68. * in this module
  69. *
  70. * @ingroup tripal_feature
  71. */
  72. function tripal_feature_node_info() {
  73. $nodes = array();
  74. $nodes['chado_feature'] = array(
  75. 'name' => t('Feature'),
  76. 'base' => 'chado_feature',
  77. 'description' => t('A feature from the chado database'),
  78. 'has_title' => FALSE,
  79. 'title_label' => t('Feature'),
  80. 'has_body' => FALSE,
  81. 'body_label' => t('Feature Description'),
  82. 'locked' => TRUE
  83. );
  84. return $nodes;
  85. }
  86. /**
  87. * Set the permission types that the chado module uses. Essentially we
  88. * want permissionis that protect creation, editing and deleting of chado
  89. * data objects
  90. *
  91. * @ingroup tripal_feature
  92. */
  93. function tripal_feature_permissions() {
  94. return array(
  95. 'access chado_feature content' => array(
  96. 'title' => t('View Features'),
  97. 'description' => t('Allow users to view feature pages.'),
  98. ),
  99. 'create chado_feature content' => array(
  100. 'title' => t('Create Features'),
  101. 'description' => t('Allow users to create new feature pages.'),
  102. ),
  103. 'delete chado_feature content' => array(
  104. 'title' => t('Delete Features'),
  105. 'description' => t('Allow users to delete feature pages.'),
  106. ),
  107. 'edit chado_feature content' => array(
  108. 'title' => t('Edit Features'),
  109. 'description' => t('Allow users to edit feature pages.'),
  110. ),
  111. 'adminster tripal feature' => array(
  112. 'title' => t('Administer Features'),
  113. 'description' => t('Allow users to administer all features.'),
  114. ),
  115. );
  116. }
  117. /**
  118. * Implement hook_access().
  119. *
  120. * This hook allows node modules to limit access to the node types they define.
  121. *
  122. * @param $node
  123. * The node on which the operation is to be performed, or, if it does not yet exist, the
  124. * type of node to be created
  125. *
  126. * @param $op
  127. * The operation to be performed
  128. *
  129. * @param $account
  130. * A user object representing the user for whom the operation is to be performed
  131. *
  132. * @return
  133. * If the permission for the specified operation is not set then return FALSE. If the
  134. * permission is set then return NULL as this allows other modules to disable
  135. * access. The only exception is when the $op == 'create'. We will always
  136. * return TRUE if the permission is set.
  137. *
  138. * @ingroup tripal_feature
  139. */
  140. function chado_feature_node_access($node, $op, $account) {
  141. if ($op == 'create') {
  142. if (!user_access('create chado_feature content', $account)) {
  143. return FALSE;
  144. }
  145. return TRUE;
  146. }
  147. if ($op == 'update') {
  148. if (!user_access('edit chado_feature content', $account)) {
  149. return FALSE;
  150. }
  151. }
  152. if ($op == 'delete') {
  153. if (!user_access('delete chado_feature content', $account)) {
  154. return FALSE;
  155. }
  156. }
  157. if ($op == 'view') {
  158. if (!user_access('access chado_feature content', $account)) {
  159. return FALSE;
  160. }
  161. }
  162. return NULL;
  163. }
  164. /**
  165. * Menu items are automatically added for the new node types created
  166. * by this module to the 'Create Content' Navigation menu item. This function
  167. * adds more menu items needed for this module.
  168. *
  169. * @ingroup tripal_feature
  170. */
  171. function tripal_feature_menu() {
  172. $items = array();
  173. // the administative settings menu
  174. $items['find/sequences'] = array(
  175. 'title' => 'Sequence Retrieval',
  176. 'description' => 'Download a file of sequences',
  177. 'page callback' => 'tripal_feature_seq_extract_page',
  178. 'access arguments' => array('access chado_feature content'),
  179. 'type' => MENU_CALLBACK,
  180. );
  181. $items['find/sequences/ajax'] = array(
  182. 'title' => 'Sequence Retrieval',
  183. 'page callback' => 'tripal_feature_seq_extract_form_ahah_update',
  184. 'access arguments' => array('access chado_feature content'),
  185. 'type' => MENU_CALLBACK,
  186. );
  187. // the administative settings menu
  188. $items['admin/tripal/chado/tripal_feature'] = array(
  189. 'title' => 'Features',
  190. 'description' => 'A biological sequence or a section of a biological sequence, or a collection of such sections.',
  191. 'page callback' => 'tripal_feature_admin_feature_view',
  192. 'access arguments' => array('administer tripal features'),
  193. 'type' => MENU_NORMAL_ITEM,
  194. );
  195. $items['admin/tripal/chado/tripal_feature/sync'] = array(
  196. 'title' => 'Sync',
  197. 'description' => 'Sync features from Chado with Drupal',
  198. 'page callback' => 'drupal_get_form',
  199. 'page arguments' => array('tripal_feature_sync_form'),
  200. 'access arguments' => array('administer tripal features'),
  201. 'type' => MENU_LOCAL_TASK,
  202. 'weight' => 0
  203. );
  204. $items['admin/tripal/chado/tripal_feature/delete'] = array(
  205. 'title' => ' Delete',
  206. 'description' => 'Delete multiple features from Chado',
  207. 'page callback' => 'drupal_get_form',
  208. 'page arguments' => array('tripal_feature_delete_form'),
  209. 'access arguments' => array('administer tripal features'),
  210. 'type' => MENU_LOCAL_TASK,
  211. 'weight' => 2
  212. );
  213. $items['admin/tripal/chado/tripal_feature/configuration'] = array(
  214. 'title' => 'Settings',
  215. 'description' => 'Configure the Tripal Feature module.',
  216. 'page callback' => 'drupal_get_form',
  217. 'page arguments' => array('tripal_feature_admin'),
  218. 'access arguments' => array('administer tripal features'),
  219. 'type' => MENU_LOCAL_TASK,
  220. 'weight' => 5
  221. );
  222. $items['admin/tripal/chado/tripal_feature/help'] = array(
  223. 'title' => 'Help',
  224. 'description' => 'Help with the Tripal Feature module.',
  225. 'page callback' => 'theme',
  226. 'page arguments' => array('tripal_feature_help'),
  227. 'access arguments' => array('administer tripal features'),
  228. 'type' => MENU_LOCAL_TASK,
  229. 'weight' => 10
  230. );
  231. /** Loaders */
  232. $items['admin/tripal/loaders/fasta_loader'] = array(
  233. 'title' => 'Multi-FASTA file Loader',
  234. 'description' => 'Load sequences from a multi-FASTA file into Chado',
  235. 'page callback' => 'drupal_get_form',
  236. 'page arguments' => array('tripal_feature_fasta_load_form'),
  237. 'access arguments' => array('administer tripal features'),
  238. 'type' => MENU_NORMAL_ITEM,
  239. );
  240. $items['admin/tripal/loaders/gff3_load'] = array(
  241. 'title' => 'GFF3 file Loader',
  242. 'description' => 'Import a GFF3 file into Chado',
  243. 'page callback' => 'drupal_get_form',
  244. 'page arguments' => array('tripal_feature_gff3_load_form'),
  245. 'access arguments' => array('administer tripal features'),
  246. 'type' => MENU_NORMAL_ITEM,
  247. );
  248. // the menu link for addressing any feature (by name, uniquename, synonym)
  249. $items['feature/%'] = array(
  250. 'page callback' => 'tripal_feature_match_features_page',
  251. 'page arguments' => array(1),
  252. 'access arguments' => array('access chado_feature content'),
  253. 'type' => MENU_LOCAL_TASK,
  254. );
  255. // Enable admin view
  256. $items['admin/tripal/chado/tripal_feature/views/features/enable'] = array(
  257. 'title' => 'Enable feature Administrative View',
  258. 'page callback' => 'tripal_views_admin_enable_view',
  259. 'page arguments' => array('tripal_feature_admin_features', 'admin/tripal/chado/tripal_feature'),
  260. 'access arguments' => array('administer tripal_bulk_loader'),
  261. 'type' => MENU_CALLBACK,
  262. );
  263. return $items;
  264. }
  265. /**
  266. * We need to let drupal know about our theme functions and their arguments.
  267. * We create theme functions to allow users of the module to customize the
  268. * look and feel of the output generated in this module
  269. *
  270. * @ingroup tripal_feature
  271. */
  272. function tripal_feature_theme($existing, $type, $theme, $path) {
  273. $core_path = drupal_get_path('module', 'tripal_core');
  274. $items = array(
  275. 'node__chado_feature' => array(
  276. 'template' => 'node--chado-generic',
  277. 'render element' => 'node',
  278. 'base hook' => 'node',
  279. 'path' => "$core_path/theme",
  280. ),
  281. 'tripal_feature_alignments' => array(
  282. 'variables' => array('node' => NULL),
  283. 'template' => 'tripal_feature_alignments',
  284. 'path' => "$path/theme/tripal_feature",
  285. ),
  286. 'tripal_feature_analyses' => array(
  287. 'variables' => array('node' => NULL),
  288. 'template' => 'tripal_feature_analyses',
  289. 'path' => "$path/theme/tripal_feature",
  290. ),
  291. 'tripal_feature_base' => array(
  292. 'variables' => array('node' => NULL),
  293. 'template' => 'tripal_feature_base',
  294. 'path' => "$path/theme/tripal_feature",
  295. ),
  296. 'tripal_feature_featurepos' => array(
  297. 'arguments' => array('node' => NULL),
  298. 'template' => 'tripal_feature_featurepos',
  299. 'path' => "$path/theme/tripal_feature",
  300. ),
  301. 'tripal_feature_sequence' => array(
  302. 'variables' => array('node' => NULL),
  303. 'template' => 'tripal_feature_sequence',
  304. 'path' => "$path/theme/tripal_feature",
  305. ),
  306. 'tripal_feature_proteins' => array(
  307. 'variables' => array('node' => NULL),
  308. 'template' => 'tripal_feature_proteins',
  309. 'path' => "$path/theme/tripal_feature",
  310. ),
  311. 'tripal_feature_synonyms' => array(
  312. 'variables' => array('node' => NULL),
  313. 'template' => 'tripal_feature_synonyms',
  314. 'path' => "$path/theme/tripal_feature",
  315. ),
  316. 'tripal_feature_phenotypes' => array(
  317. 'variables' => array('node' => NULL),
  318. 'template' => 'tripal_feature_phenotypes',
  319. 'path' => "$path/theme/tripal_feature",
  320. ),
  321. 'tripal_feature_references' => array(
  322. 'variables' => array('node' => NULL),
  323. 'template' => 'tripal_feature_references',
  324. 'path' => "$path/theme/tripal_feature",
  325. ),
  326. 'tripal_feature_properties' => array(
  327. 'variables' => array('node' => NULL),
  328. 'template' => 'tripal_feature_properties',
  329. 'path' => "$path/theme/tripal_feature",
  330. ),
  331. 'tripal_feature_terms' => array(
  332. 'variables' => array('node' => NULL),
  333. 'template' => 'tripal_feature_terms',
  334. 'path' => "$path/theme/tripal_feature",
  335. ),
  336. 'tripal_feature_relationships' => array(
  337. 'variables' => array('node' => NULL),
  338. 'template' => 'tripal_feature_relationships',
  339. 'path' => "$path/theme/tripal_feature",
  340. ),
  341. 'tripal_feature_help' => array(
  342. 'template' => 'tripal_feature_help',
  343. 'variables' => array(NULL),
  344. 'path' => drupal_get_path('module', 'tripal_feature') . '/theme'
  345. ),
  346. // template for the organism page
  347. 'tripal_organism_feature_browser' => array(
  348. 'variables' => array('node' => NULL),
  349. 'template' => 'tripal_organism_feature_browser',
  350. 'path' => "$path/theme/tripal_organism",
  351. ),
  352. 'tripal_organism_feature_counts' => array(
  353. 'variables' => array('node' => NULL),
  354. 'template' => 'tripal_organism_feature_counts',
  355. 'path' => "$path/theme/tripal_organism",
  356. ),
  357. // themed forms
  358. 'tripal_feature_seq_extract_form' => array(
  359. 'arguments' => array('form'),
  360. ),
  361. // themed teaser
  362. 'tripal_feature_teaser' => array(
  363. 'variables' => array('node' => NULL),
  364. 'template' => 'tripal_feature_teaser',
  365. 'path' => "$path/theme/tripal_feature",
  366. ),
  367. );
  368. return $items;
  369. }
  370. /**
  371. *
  372. *
  373. * @ingroup tripal_feature
  374. */
  375. function tripal_feature_block_info() {
  376. $blocks['references']['info'] = t('Tripal Feature References');
  377. $blocks['references']['cache'] = 'BLOCK_NO_CACHE';
  378. $blocks['base']['info'] = t('Tripal Feature Details');
  379. $blocks['base']['cache'] = 'BLOCK_NO_CACHE';
  380. $blocks['sequence']['info'] = t('Tripal Feature Sequence');
  381. $blocks['sequence']['cache'] = 'BLOCK_NO_CACHE';
  382. $blocks['featureloc_sequences']['info'] = t('Tripal Feature Annotated Sequence');
  383. $blocks['featureloc_sequences']['cache'] = 'BLOCK_NO_CACHE';
  384. $blocks['synonyms']['info'] = t('Tripal Feature Synonyms');
  385. $blocks['synonyms']['cache'] = 'BLOCK_NO_CACHE';
  386. $blocks['properties']['info'] = t('Tripal Feature Properties');
  387. $blocks['properties']['cache'] = 'BLOCK_NO_CACHE';;
  388. $blocks['terms']['info'] = t('Tripal Annotated Terms');
  389. $blocks['terms']['cache'] = 'BLOCK_NO_CACHE';;
  390. $blocks['alignments']['info'] = t('Tripal Feature Alignments');
  391. $blocks['alignments']['cache'] = 'BLOCK_NO_CACHE';
  392. $blocks['relationships']['info'] = t('Tripal Feature Relationships');
  393. $blocks['relationships']['cache'] = 'BLOCK_NO_CACHE';
  394. $blocks['org_feature_counts']['info'] = t('Tripal Organism Feature Counts');
  395. $blocks['org_feature_counts']['cache'] = 'BLOCK_NO_CACHE';
  396. $blocks['org_feature_browser']['info'] = t('Tripal Organism Feature Browser');
  397. $blocks['org_feature_browser']['cache'] = 'BLOCK_NO_CACHE';
  398. return $blocks;
  399. }
  400. /**
  401. *
  402. *
  403. * @ingroup tripal_feature
  404. */
  405. function tripal_feature_block_view($delta = '') {
  406. if (user_access('access chado_feature content') and arg(0) == 'node' and is_numeric(arg(1))) {
  407. $nid = arg(1);
  408. $node = node_load($nid);
  409. $block = array();
  410. switch ($delta) {
  411. case 'references':
  412. $block['subject'] = t('References');
  413. $block['content'] = theme('tripal_feature_references', $node);
  414. break;
  415. case 'base':
  416. $block['subject'] = t('Feature Details');
  417. $block['content'] = theme('tripal_feature_base', $node);
  418. break;
  419. case 'synonyms':
  420. $block['subject'] = t('Synonyms');
  421. $block['content'] = theme('tripal_feature_synonyms', $node);
  422. break;
  423. case 'properties':
  424. $block['subject'] = t('Properties');
  425. $block['content'] = theme('tripal_feature_properties', $node);
  426. break;
  427. case 'terms':
  428. $block['subject'] = t('Annotated Terms');
  429. $block['content'] = theme('tripal_feature_terms', $node);
  430. break;
  431. case 'sequence':
  432. $block['subject'] = t('Sequence');
  433. $block['content'] = theme('tripal_feature_sequence', $node);
  434. break;
  435. case 'featureloc_sequences':
  436. $block['subject'] = t('Formatted Sequences');
  437. $block['content'] = theme('tripal_feature_featureloc_sequences', $node);
  438. break;
  439. case 'alignments':
  440. $block['subject'] = t('Alignments');
  441. $block['content'] = theme('tripal_feature_alignments', $node);
  442. break;
  443. case 'relationships':
  444. $block['subject'] = t('Relationships');
  445. $block['content'] = theme('tripal_feature_relationships', $node);
  446. break;
  447. case 'org_feature_counts':
  448. $block['subject'] = t('Feature Type Summary');
  449. $block['content'] = theme('tripal_organism_feature_counts', array('node' => $node));
  450. break;
  451. case 'org_feature_browser':
  452. $block['subject'] = t('Feature Browser');
  453. $block['content'] = theme('tripal_organism_feature_browser', array('node' => $node));
  454. break;
  455. case 'library_feature_browser':
  456. $block['subject'] = t('Library Feature Browser');
  457. $block['content'] = theme('tripal_library_feature_browser', $node);
  458. break;
  459. case 'analysis_feature_browser':
  460. $block['subject'] = t('Analysis Feature Browser');
  461. $block['content'] = theme('tripal_analysis_feature_browser', $node);
  462. break;
  463. default :
  464. }
  465. return $block;
  466. }
  467. }
  468. /**
  469. * When a new chado_feature node is created we also need to add information
  470. * to our chado_feature table. This function is called on insert of a new node
  471. * of type 'chado_feature' and inserts the necessary information.
  472. *
  473. * @ingroup tripal_feature
  474. */
  475. function chado_feature_insert($node) {
  476. $node->uniquename = trim($node->uniquename);
  477. $node->fname = trim($node->fname);
  478. $node->feature_type = trim($node->feature_type);
  479. $node->residues = trim($node->residues);
  480. // remove spaces, newlines from residues
  481. $residues = preg_replace("/[\n\r\s]/", "", $node->residues);
  482. $obsolete = 'FALSE';
  483. if ($node->is_obsolete) {
  484. $obsolete = 'TRUE';
  485. }
  486. $feature_id = '';
  487. // if there is an feature_id in the $node object then this must be a sync so
  488. // we can skip adding the feature as it is already there, although
  489. // we do need to proceed with the rest of the insert
  490. if (!property_exists($node, 'feature_id')) {
  491. $values = array(
  492. 'organism_id' => $node->organism_id,
  493. 'name' => $node->fname,
  494. 'uniquename' => $node->uniquename,
  495. 'residues' => $residues,
  496. 'seqlen' => drupal_strlen($residues),
  497. 'is_obsolete' => $obsolete,
  498. 'type_id' => $type[0]->cvterm_id,
  499. 'md5checksum' => md5($residues)
  500. );
  501. $feature = tripal_core_chado_select('feature', array('*'), $values);
  502. if (!$feature) {
  503. drupal_set_message(t('Unable to add feature.'), 'warning');
  504. watchdog('tripal_feature', 'Insert feature: Unable to create feature where values: %values',
  505. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  506. return;
  507. }
  508. $feature_id = $feature->feature_id;
  509. // add the genbank accession and synonyms
  510. chado_feature_add_synonyms($node->synonyms, $node->feature_id);
  511. }
  512. else {
  513. $feature_id = $node->feature_id;
  514. }
  515. // Make sure the entry for this feature doesn't already exist in the
  516. // chado_feature table if it doesn't exist then we want to add it.
  517. $check_org_id = chado_get_id_for_node('feature', $node->nid);
  518. if (!$check_org_id) {
  519. $record = new stdClass();
  520. $record->nid = $node->nid;
  521. $record->vid = $node->vid;
  522. $record->feature_id = $feature_id;
  523. drupal_write_record('chado_feature', $record);
  524. }
  525. }
  526. /**
  527. *
  528. *
  529. * @ingroup tripal_feature
  530. */
  531. function chado_feature_update($node) {
  532. $node->uniquename = trim($node->uniquename);
  533. $node->fname = trim($node->fname);
  534. $node->feature_type = trim($node->feature_type);
  535. $node->residues = trim($node->residues);
  536. if ($node->revision) {
  537. // there is no way to handle revisions in Chado but leave
  538. // this here just to make not we've addressed it.
  539. }
  540. $residues = preg_replace("/[\n\r\s]/", "", $node->residues);
  541. $obsolete = 'FALSE';
  542. if ($node->is_obsolete) {
  543. $obsolete = 'TRUE';
  544. }
  545. // get the feature type id
  546. $values = array(
  547. 'cv_id' => array(
  548. 'name' => 'sequence'
  549. ),
  550. 'name' => $node->feature_type
  551. );
  552. $type = tripal_core_chado_select('cvterm', array('cvterm_id'), $values);
  553. $feature_id = chado_get_id_for_node('feature', $node->nid) ;
  554. if (sizeof($type) > 0) {
  555. $match = array(
  556. 'feature_id' => $feature_id,
  557. );
  558. $values = array(
  559. 'organism_id' => $node->organism_id,
  560. 'name' => $node->fname,
  561. 'uniquename' => $node->uniquename,
  562. 'residues' => $residues,
  563. 'seqlen' => drupal_strlen($residues),
  564. 'is_obsolete' => $obsolete,
  565. 'type_id' => $type[0]->cvterm_id,
  566. 'md5checksum' => md5($residues)
  567. );
  568. $options = array('return_record' => TRUE);
  569. $status = tripal_core_chado_update('feature', $match, $values, $options);
  570. // add the genbank synonyms
  571. chado_feature_add_synonyms($node->synonyms, $feature_id);
  572. }
  573. else {
  574. drupal_set_message(t('Unable to update feature.'), 'warning');
  575. watchdog('tripal_feature',
  576. 'Update feature: Unable to update feature where values: %values',
  577. array('%values' => print_r($values, TRUE)),
  578. WATCHDOG_WARNING
  579. );
  580. }
  581. }
  582. /**
  583. *
  584. *
  585. * @ingroup tripal_feature
  586. */
  587. function chado_feature_delete($node) {
  588. $feature_id = chado_get_id_for_node('feature', $node->nid);
  589. // if we don't have a library id for this node then this isn't a node of
  590. // type chado_library or the entry in the chado_library table was lost.
  591. if (!$feature_id) {
  592. return;
  593. }
  594. // remove the drupal content
  595. $sql_del = "DELETE FROM {chado_feature} WHERE nid = :nid AND vid = :vid";
  596. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  597. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  598. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  599. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  600. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  601. // Remove data from feature tables of chado database. This will
  602. // cause a cascade delete and remove all data in referencing tables
  603. // for this feature
  604. chado_query("DELETE FROM {feature} WHERE feature_id = :feature_id", array(':feature_id' => $feature_id));
  605. drupal_set_message(t("The feature and all associated data were removed from") .
  606. "chado");
  607. }
  608. /**
  609. *
  610. *
  611. * @ingroup tripal_feature
  612. */
  613. function chado_feature_add_synonyms($synonyms, $feature_id) {
  614. // separate synomys by carriage returns
  615. $synonyms = preg_replace("/[\n\r]+/", " ", $synonyms);
  616. // split the synonyms into an array based on a space as the delimieter
  617. $syn_array = array();
  618. $syn_array = explode(" ", $synonyms);
  619. // remove any old synonyms
  620. $feature_syn_dsql = "DELETE FROM {feature_synonym} WHERE feature_id = :feature_id";
  621. if (!chado_query($feature_syn_dsql, array(':feature_id' => $feature_id))) {
  622. watchdog('tripal_feature', "Could not remove synonyms from feature. ", array(), WATCHDOG_ERROR);
  623. return;
  624. }
  625. // return if we don't have any synonmys to add
  626. if (!$synonyms) {
  627. return;
  628. }
  629. // iterate through each synonym and add it to the database
  630. foreach ($syn_array as $syn) {
  631. // skip this item if it's empty
  632. if (!$syn) {
  633. break;
  634. }
  635. // check to see if we have this accession number already in the database
  636. // if so then don't add it again. it messes up drupal if the insert fails.
  637. // It is possible for the accession number to be present and not the feature
  638. $synonym_sql = "SELECT synonym_id FROM {synonym} WHERE name = :name";
  639. $synonym = chado_query($synonym_sql, array(':name' => $syn))->fetchObject();
  640. if (!$synonym) {
  641. $synonym_isql = "
  642. INSERT INTO {synonym} (name, synonym_sgml, type_id)
  643. VALUES (:name, :synonym_sgml,
  644. (SELECT cvterm_id
  645. FROM {cvterm} CVT
  646. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  647. WHERE CV.name = 'feature_property' and CVT.name = 'synonym')
  648. )
  649. ";
  650. if (!chado_query($synonym_isql, array(':name' => $syn, ':synonym_sgml' => $syn))) {
  651. watchdog('tripal_feature', "Could not add synonym. ", array(), WATCHDOG_WARNING);
  652. return;
  653. }
  654. // now get the synonym we just added
  655. $synonym_sql = "SELECT synonym_id FROM {synonym} WHERE name = :name";
  656. $synonym = chado_query($synonym_sql, array(':name' => $syn))->fetchObject();
  657. }
  658. // now add in our new sysnonym
  659. $feature_syn_isql = "
  660. INSERT INTO {feature_synonym} (synonym_id,feature_id,pub_id)
  661. VALUES (:synonym_id, :feature_id, :pub_id)";
  662. $args = array(':synonym_id' => $synonym->synonym_id, ':feature_id' => $feature_id, ':pub_id'=> 1);
  663. if (!chado_query($feature_syn_isql, $args)) {
  664. watchdog('tripal_feature', "Could not associate synonym with feature. ", array(), WATCHDOG_WARNING);
  665. return;
  666. }
  667. }
  668. }
  669. /**
  670. * When a node is requested by the user this function is called to allow us
  671. * to add auxiliary data to the node object.
  672. *
  673. * @ingroup tripal_feature
  674. */
  675. function chado_feature_load($nodes) {
  676. foreach ($nodes as $nid => $node) {
  677. // find the feature and add in the details
  678. $feature_id = chado_get_id_for_node('feature', $nid);
  679. // build the feature variable
  680. $values = array('feature_id' => $feature_id);
  681. $feature = tripal_core_generate_chado_var('feature', $values);
  682. $nodes[$nid]->feature = $feature;
  683. // by default, the titles are saved using the unique constraint. We will
  684. // keep it the same, but remove the duplicate name if the unique name and name
  685. // are identical. This doesn't change the title saved in the database, just what is shown
  686. // to the user on the page
  687. $title_type = variable_get('chado_feature_title', 'unique_constraint');
  688. if ($title_type == 'unique_constraint') {
  689. if (strcmp($feature->name, $feature->uniquename)==0) {
  690. $node->title = $feature->name . " (" . $feature->type_id->name . ") " . $feature->organism_id->genus . " " . $feature->organism_id->species ;
  691. }
  692. // in previous version of Tripal, the feature title was simply the unique name.
  693. // so, we recreate the title just to be sure all of our feature pages are consistent
  694. else {
  695. $node->title = $feature->name . ", " . $feature->uniquename . " (" . $feature->type_id->name . ") " . $feature->organism_id->genus . " " . $feature->organism_id->species ;
  696. }
  697. }
  698. // set the title to be the feature name or uniquename as configured
  699. if ($title_type == 'feature_name') {
  700. $node->title = $feature->name;
  701. }
  702. if ($title_type == 'feature_unique_name') {
  703. $node->title = $feature->uniquename;
  704. }
  705. }
  706. }
  707. /**
  708. *
  709. *
  710. * @ingroup tripal_feature
  711. */
  712. function tripal_feature_load_featurelocs($feature_id, $side = 'as_parent', $aggregate = 1) {
  713. $sql = "
  714. SELECT
  715. F.name, F.feature_id, F.uniquename,
  716. FS.name as src_name, FS.feature_id as src_feature_id, FS.uniquename as src_uniquename,
  717. CVT.name as cvname, CVT.cvterm_id,
  718. CVTS.name as src_cvname, CVTS.cvterm_id as src_cvterm_id,
  719. FL.fmin, FL.fmax, FL.is_fmin_partial, FL.is_fmax_partial,FL.strand, FL.phase
  720. FROM {featureloc} FL
  721. INNER JOIN {feature} F ON FL.feature_id = F.feature_id
  722. INNER JOIN {feature} FS ON FS.feature_id = FL.srcfeature_id
  723. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  724. INNER JOIN {cvterm} CVTS ON FS.type_id = CVTS.cvterm_id
  725. ";
  726. if (strcmp($side, 'as_parent')==0) {
  727. $sql .= "WHERE FL.srcfeature_id = :feature_id ";
  728. }
  729. if (strcmp($side, 'as_child')==0) {
  730. $sql .= "WHERE FL.feature_id = :feature_id ";
  731. }
  732. $flresults = chado_query($sql, array(':feature_id' => $feature_id));
  733. // copy the results into an array
  734. $i=0;
  735. $featurelocs = array();
  736. while ($loc = $flresults->fetchObject()) {
  737. // if a drupal node exists for this feature then add the nid to the
  738. // results object
  739. $loc->fnid = chado_get_node_id('feature', $loc->feature_id);
  740. $loc->snid = chado_get_node_id('feature', $loc->src_feature_id);
  741. // add the result to the array
  742. $featurelocs[$i++] = $loc;
  743. }
  744. // Add the relationship feature locs if aggregate is turned on
  745. if ($aggregate and strcmp($side, 'as_parent')==0) {
  746. // get the relationships for this feature without substituting any children
  747. // for the parent. We want all relationships
  748. $relationships = tripal_feature_get_aggregate_relationships($feature_id, 0);
  749. foreach ($relationships as $rindex => $rel) {
  750. // get the featurelocs for each of the relationship features
  751. $rel_featurelocs = tripal_feature_load_featurelocs($rel->subject_id, 'as_child', 0);
  752. foreach ($rel_featurelocs as $findex => $rfloc) {
  753. $featurelocs[$i++] = $rfloc;
  754. }
  755. }
  756. }
  757. usort($featurelocs, 'tripal_feature_sort_locations');
  758. return $featurelocs;
  759. }
  760. /**
  761. * used to sort the feature locs by start position
  762. *
  763. * @ingroup tripal_feature
  764. */
  765. function tripal_feature_sort_locations($a, $b) {
  766. return strnatcmp($a->fmin, $b->fmin);
  767. }
  768. /**
  769. *
  770. *
  771. * @ingroup tripal_feature
  772. */
  773. function tripal_feature_load_relationships($feature_id, $side = 'as_subject') {
  774. // get the relationships for this feature. The query below is used for both
  775. // querying the object and subject relationships
  776. $sql = "
  777. SELECT
  778. FS.name as subject_name, FS.uniquename as subject_uniquename,
  779. CVTS.name as subject_type, CVTS.cvterm_id as subject_type_id,
  780. FR.subject_id, FR.type_id as relationship_type_id, FR.object_id, FR.rank,
  781. CVT.name as rel_type,
  782. FO.name as object_name, FO.uniquename as object_uniquename,
  783. CVTO.name as object_type, CVTO.cvterm_id as object_type_id
  784. FROM {feature_relationship} FR
  785. INNER JOIN {cvterm} CVT ON FR.type_id = CVT.cvterm_id
  786. INNER JOIN {feature} FS ON FS.feature_id = FR.subject_id
  787. INNER JOIN {feature} FO ON FO.feature_id = FR.object_id
  788. INNER JOIN {cvterm} CVTO ON FO.type_id = CVTO.cvterm_id
  789. INNER JOIN {cvterm} CVTS ON FS.type_id = CVTS.cvterm_id
  790. ";
  791. if (strcmp($side, 'as_object')==0) {
  792. $sql .= " WHERE FR.object_id = :feature_id";
  793. }
  794. if (strcmp($side, 'as_subject')==0) {
  795. $sql .= " WHERE FR.subject_id = :feature_id";
  796. }
  797. $sql .= " ORDER BY FR.rank";
  798. // get the relationships
  799. $results = chado_query($sql, array(':feature_id' => $feature_id));
  800. // iterate through the relationships, put these in an array and add
  801. // in the Drupal node id if one exists
  802. $i=0;
  803. $nodesql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
  804. $relationships = array();
  805. while ($rel = $results->fetchObject()) {
  806. $node = db_query($nodesql, array(':feature_id' => $rel->subject_id))->fetchObject();
  807. if ($node) {
  808. $rel->subject_nid = $node->nid;
  809. }
  810. $node = db_query($nodesql, array(':feature_id' => $rel->object_id))->fetchObject();
  811. if ($node) {
  812. $rel->object_nid = $node->nid;
  813. }
  814. $relationships[$i++] = $rel;
  815. }
  816. return $relationships;
  817. }
  818. /**
  819. *
  820. *
  821. * @ingroup tripal_feature
  822. */
  823. function tripal_feature_get_aggregate_relationships($feature_id, $substitute=1,
  824. $levels=0, $base_type_id=NULL, $depth=0) {
  825. // we only want to recurse to as many levels deep as indicated by the
  826. // $levels variable, but only if this variable is > 0. If 0 then we
  827. // recurse until we reach the end of the relationships tree.
  828. if ($levels > 0 and $levels == $depth) {
  829. return NULL;
  830. }
  831. // first get the relationships for this feature
  832. return tripal_feature_load_relationships($feature_id, 'as_object');
  833. }
  834. /**
  835. *
  836. *
  837. * @ingroup tripal_feature
  838. */
  839. function tripal_feature_load_featureloc_sequences($feature_id, $featurelocs) {
  840. // if we don't have any featurelocs then no point in continuing
  841. if (!$featurelocs) {
  842. return array();
  843. }
  844. // get the list of relationships (including any aggregators) and iterate
  845. // through each one to find information needed to color-code the reference sequence
  846. $relationships = tripal_feature_get_aggregate_relationships($feature_id);
  847. if (!$relationships) {
  848. return array();
  849. }
  850. // iterate through each of the realtionships features and get their
  851. // locations
  852. foreach ($relationships as $rindex => $rel) {
  853. // get the featurelocs for each of the relationship features
  854. $rel_featurelocs = tripal_feature_load_featurelocs($rel->subject_id, 'as_child', 0);
  855. foreach ($rel_featurelocs as $rfindex => $rel_featureloc) {
  856. // keep track of this unique source feature
  857. $src = $rel_featureloc->src_feature_id . "-" . $rel_featureloc->src_cvterm_id;
  858. // copy over the results to the relationship object. Since there can
  859. // be more than one feature location for each relationship feature we
  860. // use the '$src' variable to keep track of these.
  861. $rel->featurelocs = new stdClass();
  862. $rel->featurelocs->$src = new stdClass();
  863. $rel->featurelocs->$src->src_uniquename = $rel_featureloc->src_uniquename;
  864. $rel->featurelocs->$src->src_cvterm_id = $rel_featureloc->src_cvterm_id;
  865. $rel->featurelocs->$src->src_cvname = $rel_featureloc->src_cvname;
  866. $rel->featurelocs->$src->fmin = $rel_featureloc->fmin;
  867. $rel->featurelocs->$src->fmax = $rel_featureloc->fmax;
  868. $rel->featurelocs->$src->src_name = $rel_featureloc->src_name;
  869. // keep track of the individual parts for each relationship
  870. $start = $rel->featurelocs->$src->fmin;
  871. $end = $rel->featurelocs->$src->fmax;
  872. $type = $rel->subject_type;
  873. $rel_locs[$src]['parts'][$start][$type]['start'] = $start;
  874. $rel_locs[$src]['parts'][$start][$type]['end'] = $end;
  875. $rel_locs[$src]['parts'][$start][$type]['type'] = $type;
  876. }
  877. }
  878. // the featurelocs array provided to the function contains the locations
  879. // where this feature is found. We want to get the sequence for each
  880. // location and then annotate it with the parts found from the relationships
  881. // locations determiend above.
  882. $floc_sequences = array();
  883. foreach ($featurelocs as $featureloc) {
  884. // build the src name so we can keep track of the different parts for each feature
  885. $src = $featureloc->srcfeature_id->feature_id . "-" . $featureloc->srcfeature_id->type_id->cvterm_id;
  886. // orient the parts to the beginning of the feature sequence
  887. if (!empty($rel_locs[$src]['parts'])) {
  888. $parts = $rel_locs[$src]['parts'];
  889. $rparts = array(); // we will fill this up if we're on the reverse strand
  890. foreach ($parts as $start => $types) {
  891. foreach ($types as $type_name => $type) {
  892. if ($featureloc->strand >= 0) {
  893. // this is on the forward strand. We need to convert the start on the src feature to the
  894. // start on this feature's sequence
  895. $parts[$start][$type_name]['start'] = $parts[$start][$type_name]['start'] - $featureloc->fmin;
  896. $parts[$start][$type_name]['end'] = $parts[$start][$type_name]['end'] - $featureloc->fmin;
  897. $parts[$start][$type_name]['type'] = $type_name;
  898. }
  899. else {
  900. // this is on the reverse strand. We need to swap the start and stop and calculate from the
  901. // begining of the reverse sequence
  902. $size = ($featureloc->fmax - $featureloc->fmin);
  903. $start_orig = $parts[$start][$type_name]['start'];
  904. $end_orig = $parts[$start][$type_name]['end'];
  905. $new_start = $size - ($end_orig - $featureloc->fmin);
  906. $new_end = $size - ($start_orig - $featureloc->fmin);
  907. $rparts[$new_start][$type_name]['start'] = $new_start;
  908. $rparts[$new_start][$type_name]['end'] = $new_end;
  909. $rparts[$new_start][$type_name]['type'] = $type_name;
  910. }
  911. }
  912. }
  913. // now sort the parts
  914. // if we're on the reverse strand we need to resort
  915. if ($featureloc->strand >= 0) {
  916. usort($parts, 'tripal_feature_sort_rel_parts_by_start');
  917. }
  918. else {
  919. usort($rparts, 'tripal_feature_sort_rel_parts_by_start');
  920. $parts = $rparts;
  921. }
  922. $floc_sequences[$src]['src'] = $src;
  923. $floc_sequences[$src]['type'] = $featureloc->feature_id->type_id->name;
  924. $args = array(':feature_id' => $featureloc->srcfeature_id->feature_id);
  925. $start = $featureloc->fmin + 1;
  926. $size = $featureloc->fmax - $featureloc->fmin;
  927. // TODO: fix the hard coded $start and $size
  928. // the $start and $size variables are hard-coded in the SQL statement
  929. // because the db_query function places quotes around all placeholders
  930. // (e.g. :start & :size) and screws up the substring function
  931. $sql = "
  932. SELECT substring(residues from $start for $size) as residues
  933. FROM {feature}
  934. WHERE feature_id = :feature_id
  935. ";
  936. $sequence = chado_query($sql, $args)->fetchObject();
  937. $residues = $sequence->residues;
  938. if ($featureloc->strand < 0) {
  939. $residues = tripal_feature_reverse_complement($residues);
  940. }
  941. $strand = '.';
  942. if ($featureloc->strand == 1) {
  943. $strand = '+';
  944. }
  945. elseif ($featureloc->strand == -1) {
  946. $strand = '-';
  947. }
  948. $defline = $featureloc->feature_id->name . " " . $featureloc->srcfeature_id->name . ":" . ($featureloc->fmin + 1) . ".." . $featureloc->fmax . " " . $strand;
  949. $floc_sequences[$src]['formatted_seq'] = tripal_feature_color_sequence($residues, $parts, $defline);
  950. }
  951. }
  952. return $floc_sequences;
  953. }
  954. /**
  955. *
  956. *
  957. * @ingroup tripal_feature
  958. */
  959. function tripal_feature_get_matched_alignments($feature) {
  960. // This function is for features that align through an intermediate such
  961. // as 'EST_match' or 'match'. This occurs in the case where two sequences
  962. // align but where one does not align perfectly. Some ESTs may be in a contig
  963. // but not all of the EST. Portions may overhang and not be included in the
  964. // consensus if quality is bad.
  965. // For example:
  966. //
  967. // Feature 1: Contig --------------------
  968. // Feature 2: EST_match -------
  969. // Feature 3: EST ---------
  970. //
  971. // The feature provided to the function will always be the feature 1. The
  972. // featureloc columns prefixed with 'right' (e.g. right_fmin) belong to the
  973. // alignment of feature 3 with feature 2
  974. //
  975. // Features may align to more than one feature and are not matches. We do
  976. // not want to include these, so we have to filter on the SO terms:
  977. // match, or %_match
  978. //
  979. $sql = "
  980. SELECT
  981. FL1.featureloc_id as left_featureloc_id,
  982. FL1.srcfeature_id as left_srcfeature_id,
  983. FL1.feature_id as left_feature_id,
  984. FL1.fmin as left_fmin,
  985. FL1.is_fmin_partial as left_is_fmin_partial,
  986. FL1.fmax as left_fmax,
  987. FL1.is_fmax_partial as left_is_fmax_partial,
  988. FL1.strand as left_strand,
  989. FL1.phase as left_phase,
  990. FL1.locgroup as left_locgroup,
  991. FL1.rank as left_rank,
  992. FL2.featureloc_id as right_featureloc_id,
  993. FL2.srcfeature_id as right_srcfeature_id,
  994. FL2.feature_id as right_feature_id,
  995. FL2.fmin as right_fmin,
  996. FL2.is_fmin_partial as right_is_fmin_partial,
  997. FL2.fmax as right_fmax,
  998. FL2.is_fmax_partial as right_is_fmax_partial,
  999. FL2.strand as right_strand,
  1000. FL2.phase as right_phase,
  1001. FL2.locgroup as right_locgroup,
  1002. FL2.rank as right_rank
  1003. FROM {feature} F1
  1004. INNER JOIN {featureloc} FL1 on FL1.srcfeature_id = F1.feature_id
  1005. INNER JOIN {feature} F2 on FL1.feature_id = F2.feature_id
  1006. INNER JOIN {featureloc} FL2 on FL2.feature_id = F2.feature_id
  1007. INNER JOIN {cvterm} CVT2 on F2.type_id = CVT2.cvterm_id
  1008. WHERE
  1009. F1.feature_id = :feature_id AND
  1010. (CVT2.name = 'match' or CVT2.name like '%_match')
  1011. ORDER BY FL1.fmin
  1012. ";
  1013. $results = chado_query($sql, array(':feature_id' => $feature->feature_id));
  1014. // iterate through the results and add them to our featurelocs array
  1015. $featurelocs = array();
  1016. while ($fl = $results->fetchObject()) {
  1017. // ignore featurelocs where the left and right srcfeature is the same
  1018. if (strcmp($fl->left_srcfeature_id, $fl->right_srcfeature_id) == 0) {
  1019. continue;
  1020. }
  1021. $featurelocs[] = $fl ;
  1022. }
  1023. return $featurelocs;
  1024. }
  1025. /**
  1026. *
  1027. *
  1028. * @ingroup tripal_feature
  1029. */
  1030. function tripal_feature_load_organism_feature_counts($organism) {
  1031. // don't show the browser if the settings in the admin page is turned off
  1032. // instead return the array indicating the status of the browser
  1033. $show_counts = variable_get('tripal_feature_summary_setting', 'show_feature_summary');
  1034. if (strcmp($show_counts, 'show_feature_summary')!=0) {
  1035. return array('enabled' => FALSE );
  1036. }
  1037. $args = array();
  1038. $names = array();
  1039. $order = array();
  1040. // build the where clause for the SQL statement if we have a custom term list
  1041. // we'll also keep track of the names the admin provided (if any) and the
  1042. // order that the terms should appear.
  1043. $is_custom = 0;
  1044. $temp = rtrim(variable_get('tripal_feature_summary_report_mapping', ''));
  1045. $where = '';
  1046. if ($temp) {
  1047. $is_custom = 1;
  1048. $temp = explode("\n", $temp);
  1049. $i = 0;
  1050. foreach ($temp as $key => $value) {
  1051. // separate the key value pairs
  1052. $temp2 = explode("=", $value);
  1053. $feature_type = rtrim($temp2[0]);
  1054. $args[] = $feature_type;
  1055. $order[] = $feature_type;
  1056. // if a new name is provided then use that otherwise just
  1057. // use the feature type
  1058. if (count($temp2) == 2) {
  1059. $names[":name$i"] = rtrim($temp2[1]);
  1060. }
  1061. else {
  1062. $names[":name$i"] = $feature_type;
  1063. }
  1064. $where .= " OFC.feature_type = :name$i OR ";
  1065. $i++;
  1066. }
  1067. if ($where) {
  1068. $where = drupal_substr($where, 0, -5); # remove OR from the end
  1069. $where = "($where) AND";
  1070. }
  1071. }
  1072. // get the feature counts. This is dependent on a materialized view
  1073. // installed with the organism module
  1074. $sql = "
  1075. SELECT OFC.num_features,OFC.feature_type,CVT.definition
  1076. FROM {organism_feature_count} OFC
  1077. INNER JOIN {cvterm} CVT on OFC.cvterm_id = CVT.cvterm_id
  1078. WHERE $where organism_id = :organism_id
  1079. ORDER BY num_features desc
  1080. ";
  1081. $args[':organism_id'] = $organism->organism_id;
  1082. $org_features = chado_query($sql, $args);
  1083. // iterate through the types
  1084. $types = array();
  1085. while ($type = $org_features->fetchObject()) {
  1086. $types[$type->feature_type] = $type;
  1087. // if we don't have an order this means we didn't go through the loop
  1088. // above to set the names, so do that now
  1089. if (!$is_custom) {
  1090. $names[] = $type->feature_type;
  1091. $order[] = $type->feature_type;
  1092. }
  1093. }
  1094. # now reorder the types
  1095. $ordered_types = array();
  1096. foreach ($order as $type) {
  1097. $ordered_types[] = $types[$type];
  1098. }
  1099. return array( 'types' => $ordered_types, 'names' => $names, 'enabled' => TRUE );
  1100. }
  1101. /**
  1102. * used to sort the list of relationship parts by start position
  1103. *
  1104. * @ingroup tripal_feature
  1105. */
  1106. function tripal_feature_sort_rel_parts_by_start($a, $b) {
  1107. foreach ($a as $type_name => $details) {
  1108. $astart = $a[$type_name]['start'];
  1109. break;
  1110. }
  1111. foreach ($b as $type_name => $details) {
  1112. $bstart = $b[$type_name]['start'];
  1113. break;
  1114. }
  1115. return strnatcmp($astart, $bstart);
  1116. }
  1117. /**
  1118. * used to sort the list of relationship parts by start position
  1119. *
  1120. * @ingroup tripal_feature
  1121. */
  1122. function tripal_feature_sort_rel_parts_by_end($a, $b) {
  1123. $val = strnatcmp($b['end'], $a['end']);
  1124. if ($val == 0) {
  1125. return strcmp($a['type'], $b['type']);
  1126. }
  1127. return $val;
  1128. }
  1129. /**
  1130. *
  1131. *
  1132. * @ingroup tripal_feature
  1133. */
  1134. function tripal_feature_color_sequence($sequence, $parts, $defline) {
  1135. $types = array();
  1136. // first get the list of types so we can create a color legend
  1137. foreach ($parts as $index => $t) {
  1138. foreach ($t as $type_name => $details) {
  1139. $types[$type_name] = 1;
  1140. }
  1141. }
  1142. $newseq = "<div id=\"tripal_feature-featureloc_sequence-legend\">Legend: ";
  1143. foreach ($types as $type_name => $present) {
  1144. $newseq .= "<span id=\"tripal_feature-legend-$type_name\" class=\"tripal_feature-legend-item tripal_feature-featureloc_sequence-$type_name\" script=\"\">$type_name</span>";
  1145. }
  1146. $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.";
  1147. // set the background color of the rows based on the type
  1148. $pos = 0;
  1149. $newseq .= "<pre id=\"tripal_feature-featureloc_sequence\">";
  1150. $newseq .= ">$defline\n";
  1151. // iterate through the parts. They should be in order.
  1152. $ends = array();
  1153. $seqcount = 0;
  1154. foreach ($parts as $index => $types) {
  1155. // get the start for this part. All types in this part start at the
  1156. // same position so we only need the first record
  1157. foreach ($types as $type => $child) {
  1158. $start = $child['start'];
  1159. break;
  1160. }
  1161. // add in the sequence up to the start of this part
  1162. for ($i = $pos; $i < $start; $i++) {
  1163. $newseq .= $sequence{$pos};
  1164. $seqcount++;
  1165. if ($seqcount % 50 == 0) {
  1166. $newseq .= "\n";
  1167. }
  1168. if (array_key_exists($pos, $ends)) {
  1169. foreach ($ends[$pos] as $end) {
  1170. $newseq .= "</span>";
  1171. }
  1172. }
  1173. $pos++;
  1174. }
  1175. // we want to sort the parts by their end. We want the span tag to
  1176. // to be added in the order the parts end.
  1177. usort($types, 'tripal_feature_sort_rel_parts_by_end');
  1178. // now add the child span for all types that start at this position
  1179. foreach ($types as $type) {
  1180. $class = "tripal_feature-featureloc_sequence-" . $type['type'];
  1181. $newseq .= "<span class=\"$class\">";
  1182. // add the end position
  1183. $end = $type['end'];
  1184. $ends[$end][] = $end;
  1185. }
  1186. }
  1187. // add in rest of the sequence
  1188. for ($i = $pos; $i < strlen($sequence); $i++) {
  1189. $newseq .= $sequence{$pos};
  1190. $seqcount++;
  1191. if ($seqcount % 50 == 0) {
  1192. $newseq .= "\n";
  1193. }
  1194. if (array_key_exists($pos, $ends)) {
  1195. foreach ($ends[$pos] as $end) {
  1196. $newseq .= "</span>";
  1197. }
  1198. }
  1199. $pos++;
  1200. }
  1201. $newseq .= "</pre>";
  1202. return $newseq;
  1203. }
  1204. /**
  1205. *
  1206. * @ingroup tripal_feature
  1207. */
  1208. function tripal_feature_node_presave($node) {
  1209. // set the title to ensure it is always unique
  1210. switch ($node->type) {
  1211. case 'chado_feature':
  1212. $values = array('organism_id' => $node->organism_id);
  1213. $organism = tripal_core_chado_select('organism', array('genus', 'species'), $values);
  1214. $node->title = $node->fname . ', ' . $node->uniquename . ' (' . $node->feature_type . ') ' . $organism[0]->genus . ' ' . $organism[0]->species;
  1215. break;
  1216. }
  1217. }
  1218. /**
  1219. *
  1220. * @ingroup tripal_feature
  1221. */
  1222. function tripal_feature_node_insert($node) {
  1223. // set the URL path after inserting. We do it here because we do not
  1224. // know the feature_id in the presave
  1225. switch ($node->type) {
  1226. case 'chado_feature':
  1227. if (!$node->feature_id) {
  1228. $sql = "SELECT * FROM {chado_feature} WHERE nid = :nid";
  1229. $chado_feature = db_query($sql, array(':nid' => $node->nid))->fetchObject();
  1230. $node->feature_id = $chado_feature->feature_id;
  1231. }
  1232. // remove any previous alias
  1233. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  1234. // set the URL for this feature page
  1235. $url_alias = tripal_feature_get_feature_url($node);
  1236. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  1237. path_save($path_alias);
  1238. break;
  1239. }
  1240. }
  1241. /**
  1242. *
  1243. * @ingroup tripal_feature
  1244. */
  1245. function tripal_feature_node_view($node, $view_mode, $langcode) {
  1246. switch ($node->type) {
  1247. case 'chado_feature':
  1248. // Show feature browser and counts
  1249. if ($view_mode == 'full') {
  1250. $node->content['tripal_feature_alignments'] = array(
  1251. '#value' => theme('tripal_feature_alignments', array('node' => $node)),
  1252. );
  1253. $node->content['tripal_feature_analyses'] = array(
  1254. '#value' => theme('tripal_feature_analyses', array('node' => $node)),
  1255. );
  1256. $node->content['tripal_feature_base'] = array(
  1257. '#value' => theme('tripal_feature_base', array('node' => $node)),
  1258. );
  1259. $node->content['tripal_feature_phenotypes'] = array(
  1260. '#value' => theme('tripal_feature_phenotypes', array('node' => $node)),
  1261. );
  1262. $node->content['tripal_feature_properties'] = array(
  1263. '#value' => theme('tripal_feature_properties', array('node' => $node)),
  1264. );
  1265. $node->content['tripal_feature_references'] = array(
  1266. '#value' => theme('tripal_feature_references', array('node' => $node)),
  1267. );
  1268. $node->content['tripal_feature_relationships'] = array(
  1269. '#value' => theme('tripal_feature_relationships', array('node' => $node)),
  1270. );
  1271. $node->content['tripal_feature_seqence'] = array(
  1272. '#value' => theme('tripal_feature_sequence', array('node' => $node)),
  1273. );
  1274. $node->content['tripal_feature_synonyms'] = array(
  1275. '#value' => theme('tripal_feature_synonyms', array('node' => $node)),
  1276. );
  1277. $node->content['tripal_feature_terms'] = array(
  1278. '#value' => theme('tripal_feature_terms', array('node' => $node)),
  1279. );
  1280. }
  1281. if ($view_mode == 'teaser') {
  1282. $node->content['tripal_feature_teaser'] = array(
  1283. '#value' => theme('tripal_feature_teaser', array('node' => $node)),
  1284. );
  1285. }
  1286. break;
  1287. case 'chado_organism':
  1288. // Show feature browser and counts
  1289. if ($view_mode == 'full') {
  1290. $node->content['tripal_organism_feature_counts'] = array(
  1291. '#value' => theme('tripal_organism_feature_counts', array('node' => $node)),
  1292. );
  1293. $node->content['tripal_organism_feature_browser'] = array(
  1294. '#value' => theme('tripal_organism_feature_browser', array('node' => $node)),
  1295. );
  1296. }
  1297. break;
  1298. // TODO: handle these node types. Should we also have a feature browser?
  1299. case 'chado_library':
  1300. break;
  1301. case 'chado_stock':
  1302. break;
  1303. case 'chado_analysis':
  1304. break;
  1305. }
  1306. }
  1307. /**
  1308. *
  1309. * @ingroup tripal_feature
  1310. */
  1311. function tripal_feature_node_update($node) {
  1312. // add items to other nodes, build index and search results
  1313. switch ($node->type) {
  1314. case 'chado_feature':
  1315. // remove any previous alias
  1316. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  1317. // set the URL for this feature page
  1318. $url_alias = tripal_feature_get_feature_url($node);
  1319. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  1320. path_save($path_alias);
  1321. break;
  1322. }
  1323. }
  1324. /**
  1325. * @ingroup tripal_feature
  1326. */
  1327. function tripal_feature_preprocess_tripal_feature_sequence(&$variables) {
  1328. // we want to provide a new variable that contains the matched features.
  1329. $feature = $variables['node']->feature;
  1330. // get the featureloc src features
  1331. $options = array(
  1332. 'return_array' => 1,
  1333. 'include_fk' => array(
  1334. 'srcfeature_id' => array(
  1335. 'type_id' => 1
  1336. ),
  1337. ),
  1338. );
  1339. $feature = tripal_core_expand_chado_vars($feature, 'table', 'featureloc', $options);
  1340. // because there are two foriegn keys in the featureloc table with the feature table
  1341. // we have to access the records for each by specifying the field name after the table name:
  1342. $ffeaturelocs = $feature->featureloc->feature_id;
  1343. // now extract the sequences
  1344. $featureloc_sequences = tripal_feature_load_featureloc_sequences($feature->feature_id, $ffeaturelocs);
  1345. $feature->featureloc_sequences = $featureloc_sequences;
  1346. }
  1347. /**
  1348. *
  1349. *
  1350. * @ingroup tripal_feature
  1351. */
  1352. function tripal_feature_preprocess_tripal_feature_relationships(&$variables) {
  1353. // we want to provide a new variable that contains the matched features.
  1354. $feature = $variables['node']->feature;
  1355. if (!property_exists($feature, 'all_relationships')) {
  1356. $feature->all_relationships = tripal_feature_get_feature_relationships($feature);
  1357. }
  1358. }
  1359. /**
  1360. *
  1361. *
  1362. * @ingroup tripal_feature
  1363. */
  1364. function tripal_feature_preprocess_tripal_feature_proteins(&$variables) {
  1365. // we want to provide a new variable that contains the matched features.
  1366. $feature = $variables['node']->feature;
  1367. if (!property_exists($feature, 'all_relationships')) {
  1368. $feature->all_relationships = tripal_feature_get_feature_relationships($feature);
  1369. }
  1370. }
  1371. /**
  1372. *
  1373. *
  1374. * @ingroup tripal_feature
  1375. */
  1376. function tripal_feature_preprocess_tripal_feature_alignments(&$variables) {
  1377. // we want to provide a new variable that contains the matched features.
  1378. $feature = $variables['node']->feature;
  1379. $options = array(
  1380. 'return_array' => 1,
  1381. 'include_fk' => array(
  1382. 'srcfeature_id' => array(
  1383. 'type_id' => 1,
  1384. ),
  1385. 'feature_id' => array(
  1386. 'type_id' => 1
  1387. ),
  1388. )
  1389. );
  1390. $feature = tripal_core_expand_chado_vars($feature, 'table', 'featureloc', $options);
  1391. // get alignments as child
  1392. $cfeaturelocs = $feature->featureloc->feature_id;
  1393. if (!$cfeaturelocs) {
  1394. $cfeaturelocs = array();
  1395. }
  1396. // get alignment as parent
  1397. $pfeaturelocs = $feature->featureloc->srcfeature_id;
  1398. if (!$pfeaturelocs) {
  1399. $pfeaturelocs = array();
  1400. }
  1401. // get matched alignments (those with an itermediate 'match' or 'EST_match', etc
  1402. $mfeaturelocs = tripal_feature_get_matched_alignments($feature);
  1403. $feature->matched_featurelocs = $mfeaturelocs;
  1404. // combine all three alignments into a single array for printing together in
  1405. // a single list
  1406. $alignments = array();
  1407. foreach ($pfeaturelocs as $featureloc) {
  1408. // if type is a 'match' then ignore it. We will handle those below
  1409. if (preg_match('/(^match$|^.*?_match|match_part)$/', $featureloc->feature_id->type_id->name)) {
  1410. continue;
  1411. }
  1412. $alignment = new stdClass();
  1413. $alignment->record = $featureloc;
  1414. $alignment->name = $featureloc->feature_id->name;
  1415. $alignment->type = $featureloc->feature_id->type_id->name;
  1416. $alignment->fmin = $featureloc->fmin;
  1417. $alignment->fmax = $featureloc->fmax;
  1418. $alignment->phase = $featureloc->phase;
  1419. $alignment->strand = $featureloc->strand;
  1420. $alignments[] = $alignment;
  1421. if (property_exists($featureloc->feature_id, 'nid')) {
  1422. $alignment->nid = $featureloc->feature_id->nid;
  1423. }
  1424. }
  1425. foreach ($cfeaturelocs as $featureloc) {
  1426. // if type is a 'match' then ignore it. We will handle those below
  1427. if (preg_match('/(^match$|^.*?_match|match_part)$/', $featureloc->feature_id->type_id->name)) {
  1428. continue;
  1429. }
  1430. $alignment = new stdClass();
  1431. $alignment->record = $featureloc;
  1432. $alignment->name = $featureloc->srcfeature_id->name;
  1433. $alignment->type = $featureloc->srcfeature_id->type_id->name;
  1434. $alignment->fmin = $featureloc->fmin;
  1435. $alignment->is_fmin_partial = $featureloc->is_fmin_partial;
  1436. $alignment->fmax = $featureloc->fmax;
  1437. $alignment->is_fmax_partial = $featureloc->is_fmax_partial;
  1438. $alignment->phase = $featureloc->phase;
  1439. $alignment->strand = $featureloc->strand;
  1440. $alignments[] = $alignment;
  1441. if (property_exists($featureloc->srcfeature_id, 'nid')) {
  1442. $alignment->nid = $featureloc->srcfeature_id->nid;
  1443. }
  1444. }
  1445. // in matching features, the left feature is always the feature
  1446. // provided to this function.
  1447. foreach ($mfeaturelocs as $featureloc) {
  1448. // get more information about the right feature
  1449. $select = array('feature_id' => $featureloc->right_srcfeature_id);
  1450. $rfeature = tripal_core_generate_chado_var('feature', $select);
  1451. // now add to the list
  1452. $alignment = new stdClass();
  1453. $alignment->record = $featureloc;
  1454. $alignment->right_feature = $rfeature;
  1455. $alignment->name = $rfeature->name;
  1456. $alignment->type = $rfeature->type_id->name;
  1457. $alignment->fmin = $featureloc->left_fmin;
  1458. $alignment->is_fmin_partial = $featureloc->left_is_fmin_partial;
  1459. $alignment->fmax = $featureloc->left_fmax;
  1460. $alignment->is_fmax_partial = $featureloc->left_is_fmax_partial;
  1461. $alignment->phase = $featureloc->left_phase;
  1462. $alignment->strand = $featureloc->left_strand;
  1463. $alignment->right_fmin = $featureloc->right_fmin;
  1464. $alignment->right_is_fmin_partial = $featureloc->right_is_fmin_partial;
  1465. $alignment->right_fmax = $featureloc->right_fmax;
  1466. $alignment->right_is_fmax_partial = $featureloc->right_is_fmax_partial;
  1467. $alignment->right_phase = $featureloc->right_phase;
  1468. $alignment->right_strand = $featureloc->right_strand;
  1469. $alignments[] = $alignment;
  1470. if (property_exists($rfeature, 'nid')) {
  1471. $alignment->nid = $rfeature->nid;
  1472. }
  1473. }
  1474. $feature->all_featurelocs = $alignments;
  1475. }
  1476. /**
  1477. *
  1478. *
  1479. * @ingroup tripal_feature
  1480. */
  1481. function tripal_feature_preprocess_tripal_organism_feature_counts(&$variables, $hook) {
  1482. $organism = $variables['node']->organism;
  1483. $organism->feature_counts = tripal_feature_load_organism_feature_counts($organism);
  1484. }
  1485. /**
  1486. *
  1487. *
  1488. * @ingroup tripal_feature
  1489. */
  1490. function tripal_feature_cv_chart($chart_id) {
  1491. // we only want the chart to show feature types setup by the admin
  1492. $temp = rtrim(variable_get('tripal_feature_summary_report_mapping', ''));
  1493. $where = '';
  1494. if ($temp) {
  1495. $temp = explode("\n", $temp);
  1496. foreach ($temp as $key => $value) {
  1497. $temp2 = explode("=", $value);
  1498. $feature_type = rtrim($temp2[0]);
  1499. $where .= "CNT.feature_type = '$feature_type' OR \n";
  1500. }
  1501. if ($where) {
  1502. $where = drupal_substr($where, 0, -5); # remove OR from the end
  1503. $where = "($where) AND";
  1504. }
  1505. }
  1506. $organism_id = preg_replace("/^tripal_feature_cv_chart_(\d+)$/", "$1", $chart_id);
  1507. // The CV module will create the JSON array necessary for buillding a
  1508. // pie chart using jgChart and Google Charts. We have to pass to it
  1509. // a table that contains count information, tell it which column
  1510. // contains the cvterm_id and provide a filter for getting the
  1511. // results we want from the table.
  1512. $options = array(
  1513. count_mview => 'organism_feature_count',
  1514. cvterm_id_column => 'cvterm_id',
  1515. count_column => 'num_features',
  1516. size => '550x200',
  1517. filter => "$where CNT.organism_id = $organism_id",
  1518. );
  1519. return $options;
  1520. }
  1521. /**
  1522. *
  1523. *
  1524. * @ingroup tripal_feature
  1525. */
  1526. function tripal_feature_cv_tree($tree_id) {
  1527. // The CV module will create the JSON array necessary for buillding a
  1528. // pie chart using jgChart and Google Charts. We have to pass to it
  1529. // a table that contains count information, tell it which column
  1530. // contains the cvterm_id and provide a filter for getting the
  1531. // results we want from the table.
  1532. $organism_id = preg_replace("/^tripal_feature_cv_tree_(\d+)$/", "$1", $tree_id);
  1533. $options = array(
  1534. cv_id => tripal_cv_get_cv_id('sequence'),
  1535. count_mview => 'organism_feature_count',
  1536. cvterm_id_column => 'cvterm_id',
  1537. count_column => 'num_features',
  1538. filter => "CNT.organism_id = $organism_id",
  1539. label => 'Features',
  1540. );
  1541. return $options;
  1542. }
  1543. /**
  1544. *
  1545. *
  1546. * @ingroup tripal_feature
  1547. */
  1548. function tripal_feature_del_vocabulary() {
  1549. //include the file containing the required functions for adding taxonomy vocabs
  1550. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  1551. // get the vocabularies
  1552. $vocabularies = taxonomy_get_vocabularies();
  1553. // These taxonomic terms are hard coded because we
  1554. // know we have these relationships in the chado tables
  1555. // through foreign key relationships. The tripal
  1556. // modules that correspond to these chado "modules" don't
  1557. // need to be installed for the taxonomy to work.
  1558. foreach ($vocabularies as $vocab) {
  1559. if ($vocab->name == 'Feature Type') {
  1560. taxonomy_vocabulary_delete($vocab->vid);
  1561. }
  1562. if ($vocab->name == 'Organism') {
  1563. taxonomy_vocabulary_delete($vocab->vid);
  1564. }
  1565. if ($vocab->name == 'Library') {
  1566. taxonomy_vocabulary_delete($vocab->vid);
  1567. }
  1568. if ($vocab->name == 'Analysis') {
  1569. taxonomy_vocabulary_delete($vocab->vid);
  1570. }
  1571. }
  1572. }
  1573. /**
  1574. *
  1575. *
  1576. * @ingroup tripal_feature
  1577. */
  1578. function tripal_feature_job_describe_args($callback, $args) {
  1579. $new_args = array();
  1580. if ($callback == 'tripal_feature_load_fasta') {
  1581. $new_args['FASTA file'] = $args[0];
  1582. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[1]));
  1583. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  1584. $new_args['Sequence Type'] = $args[2];
  1585. $new_args['Name Match Type'] = $args[14];
  1586. $new_args['Name RE'] = $args[4];
  1587. $new_args['Unique Name RE'] = $args[5];
  1588. // add in the relationship arguments
  1589. $new_args['Relationship Type'] = $args[8];
  1590. $new_args['Relationship Parent RE'] = $args[9];
  1591. $new_args['Relationship Parent Type'] = $args[10];
  1592. // add in the database reference arguments
  1593. if ($args[7]) {
  1594. $db = tripal_core_chado_select('db', array('name'), array('db_id' => $args[7]));
  1595. }
  1596. $new_args['Database Reference'] = $db[0]->name;
  1597. $new_args['Accession RE'] = $args[6];
  1598. $new_args['Method'] = $args[11];
  1599. // add in the analysis
  1600. if ($args[13]) {
  1601. $analysis = tripal_core_chado_select('analysis', array('name'), array('analysis_id' => $args[13]));
  1602. }
  1603. $new_args['Analysis'] = $analysis[0]->name;
  1604. }
  1605. if ($callback == 'tripal_feature_delete_features') {
  1606. if ($args[0]) {
  1607. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[0]));
  1608. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  1609. }
  1610. else {
  1611. $new_args['Organism'] = '';
  1612. }
  1613. if ($args[1]) {
  1614. $analysis = tripal_core_chado_select('analysis', array('name'), array('analysis_id' => $args[1]));
  1615. $new_args['Analysis'] = $analysis[0]->name;
  1616. }
  1617. else {
  1618. $new_args['Analysis'] = '';
  1619. }
  1620. $new_args['Sequence Type'] = $args[2];
  1621. $new_args['Is Unique Name'] = $args[3];
  1622. $new_args['Features Names'] = $args[4];
  1623. }
  1624. elseif ($callback == 'tripal_feature_load_gff3') {
  1625. $new_args['GFF File'] = $args[0];
  1626. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[1]));
  1627. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  1628. $analysis = tripal_core_chado_select('analysis', array('name'), array('analysis_id' => $args[2]));
  1629. $new_args['Analysis'] = $analysis[0]->name;
  1630. $new_args['Use a Transaction'] = ($args[7] == 1) ? "Yes" : "No";
  1631. $new_args['Import only new features'] = ($args[3] == 1) ? "Yes" : "No";
  1632. $new_args['Import all and update'] = ($args[4] == 1) ? "Yes" : "No";
  1633. $new_args['Import all and replace'] = ($args[5] == 1) ? "Yes" : "No";
  1634. $new_args['Delete features'] = ($args[6] == 1) ? "Yes" : "No";
  1635. if ($args[8]) {
  1636. $target_organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[8]));
  1637. $new_args['Target organism'] = $target_organism[0]->genus . " " . $target_organism[0]->species;
  1638. }
  1639. else {
  1640. $new_args['Target organism'] = '';
  1641. }
  1642. $new_args['Target type'] = $args[9];
  1643. $new_args['Create target'] = ($args[10] == 1) ? "Yes" : "No";
  1644. $new_args['Starting line'] = $args[11];
  1645. $new_args['Landmark Type'] = $args[12];
  1646. $new_args['Alternate ID attribute'] = $args[13];
  1647. $new_args['Create Organism'] = ($args[14] == 1) ? "Yes" : "No";
  1648. }
  1649. if ($callback == 'tripal_feature_sync_features') {
  1650. if ($args[0]) {
  1651. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[0]));
  1652. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  1653. }
  1654. else {
  1655. $new_args['Organism'] = '';
  1656. }
  1657. $new_args['Feature Types'] = $args[1];
  1658. }
  1659. return $new_args;
  1660. }
  1661. /**
  1662. * Implements hook_coder_ignore().
  1663. * Defines the path to the file (tripal_core.coder_ignores.txt) where ignore rules for coder are stored
  1664. */
  1665. function tripal_feature_coder_ignore() {
  1666. return array(
  1667. 'path' => drupal_get_path('module', 'tripal_feature'),
  1668. 'line prefix' => drupal_get_path('module', 'tripal_feature'),
  1669. );
  1670. }
  1671. /*
  1672. * Uses the value provided in the $id argument to find all features that match
  1673. * that ID by name, featurename or synonym. If it matches uniquenly to a single
  1674. * feature it will redirect to that feature page, otherwise, a list of matching
  1675. * features is shown.
  1676. */
  1677. function tripal_feature_match_features_page($id) {
  1678. // if the URL alias configuration is set such that the URL
  1679. // always begins with 'feature' then we want to use the ID as it is and
  1680. // forward it on. Otherwise, try to find the matching feature.
  1681. $url_alias = variable_get('chado_feature_url_string', '/feature/[genus]/[species]/[type]/[uniquename]');
  1682. if (!$url_alias) {
  1683. $url_alias = '/feature/[genus]/[species]/[type]/[uniquename]';
  1684. }
  1685. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  1686. if (preg_match('/^feature\//', $url_alias)) {
  1687. drupal_goto($id);
  1688. }
  1689. $sql = "
  1690. SELECT
  1691. F.name, F.uniquename, F.feature_id,
  1692. O.genus, O.species, O.organism_id,
  1693. CVT.cvterm_id, CVT.name as type_name,
  1694. CF.nid,
  1695. array_agg(S.name) as synonyms
  1696. FROM {feature} F
  1697. INNER JOIN {organism} O on F.organism_id = O.organism_id
  1698. INNER JOIN {cvterm} CVT on CVT.cvterm_id = F.type_id
  1699. LEFT JOIN {feature_synonym} FS on FS.feature_id = F.feature_id
  1700. LEFT JOIN {synonym} S on S.synonym_id = FS.synonym_id
  1701. INNER JOIN public.chado_feature CF on CF.feature_id = F.feature_id
  1702. WHERE
  1703. F.uniquename = :uname or
  1704. F.name = :fname' or
  1705. S.name = :sname
  1706. GROUP BY F.name, F.uniquename, F.feature_id, O.genus, O.species,
  1707. O.organism_id, CVT.cvterm_id, CVT.name, CF.nid
  1708. ";
  1709. $results = chado_query($sql, array(':uname' => $id, ':fname' => $id, ':sname' => $id));
  1710. $num_matches = 0;
  1711. // iterate through the matches and build the table for showing matches
  1712. $header = array('Uniquename', 'Name', 'Type', 'Species', 'Synonyms');
  1713. $rows = array();
  1714. $curr_match;
  1715. while ($match = $results->fetchObject()) {
  1716. $curr_match = $match;
  1717. $synonyms = $match->synonyms;
  1718. $synonyms = preg_replace('/[\"\{\}]/', '', $synonyms);
  1719. $rows[] = array(
  1720. $match->uniquename,
  1721. "<a href=\"" . url("node/" . $match->nid) . "\">" . $match->name . "</a>",
  1722. $match->type_name,
  1723. '<i>' . $match->genus . ' ' . $match->species . '</i>',
  1724. $synonyms,
  1725. );
  1726. $num_matches++;
  1727. }
  1728. // if we have more than one match then generate the table, otherwise, redirect
  1729. // to the matched feature
  1730. if ($num_matches == 1) {
  1731. drupal_goto("node/" . $curr_match->nid);
  1732. }
  1733. if ($num_matches == 0) {
  1734. return "<p>No features matched the given name '$id'</p>";
  1735. }
  1736. $table_attrs = array(
  1737. 'class' => 'tripal-table tripal-table-horz'
  1738. );
  1739. $output = "<p>The following features match the name '$id'.</p>";
  1740. $output .= theme_table($header, $rows, $table_attrs, $caption);
  1741. return $output;
  1742. }
  1743. /**
  1744. * Implementation of hook_form_alter()
  1745. *
  1746. * @param $form
  1747. * @param $form_state
  1748. * @param $form_id
  1749. */
  1750. function tripal_feature_form_alter(&$form, &$form_state, $form_id) {
  1751. if ($form_id == "tripal_feature_seq_extract_form") {
  1752. // updating the form through the ahah callback sets the action of
  1753. // the form to the ahah callback URL. We need to set it back
  1754. // to the normal form URL
  1755. $form['#action'] = url("find/sequences");
  1756. }
  1757. // turn off preview button for insert/updates
  1758. if ($form_id == "chado_feature_node_form") {
  1759. $form['actions']['preview']['#access'] = FALSE;
  1760. }
  1761. }