tripal_feature.module 79 KB

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