tripal_feature.module 78 KB

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