tripal_feature.module 76 KB

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