tripal_feature.module 81 KB

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