tripal_feature.module 76 KB

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