tripal_feature.module 81 KB

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