tripal_feature.module 77 KB

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