tripal_feature.module 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. * @defgroup tripal_feature Feature
  8. * @{
  9. * Provides functions for managing chado features including creating details pages for each feature
  10. * @}
  11. * @ingroup tripal_modules
  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. 'manage chado_feature aggregator',
  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' => 'tripal_feature_module_description_page',
  146. 'access arguments' => array('administer site configuration'),
  147. 'type' => MENU_NORMAL_ITEM,
  148. );
  149. $items['admin/tripal/tripal_feature/configuration'] = array(
  150. 'title' => 'Feature Configuration',
  151. 'description' => 'Settings for Chado Features',
  152. 'page callback' => 'drupal_get_form',
  153. 'page arguments' => array('tripal_feature_admin'),
  154. 'access arguments' => array('administer site configuration'),
  155. 'type' => MENU_NORMAL_ITEM,
  156. );
  157. $items['admin/tripal/tripal_feature/fasta_loader'] = array(
  158. 'title' => 'Import a multi-FASTA file',
  159. 'description' => 'Load sequences from a multi-FASTA file into Chado',
  160. 'page callback' => 'drupal_get_form',
  161. 'page arguments' => array('tripal_feature_fasta_load_form'),
  162. 'access arguments' => array('administer site configuration'),
  163. 'type' => MENU_NORMAL_ITEM,
  164. );
  165. $items['admin/tripal/tripal_feature/gff3_load'] = array(
  166. 'title' => 'Import a GFF3 file',
  167. 'description' => 'Import a GFF3 file into Chado',
  168. 'page callback' => 'drupal_get_form',
  169. 'page arguments' => array('tripal_feature_gff3_load_form'),
  170. 'access arguments' => array('access administration pages'),
  171. 'type' => MENU_NORMAL_ITEM,
  172. );
  173. $items['admin/tripal/tripal_feature/delete'] = array(
  174. 'title' => ' Delete Features',
  175. 'description' => 'Delete multiple features from Chado',
  176. 'page callback' => 'drupal_get_form',
  177. 'page arguments' => array('tripal_feature_delete_form'),
  178. 'access arguments' => array('access administration pages'),
  179. 'type' => MENU_NORMAL_ITEM,
  180. );
  181. $items['admin/tripal/tripal_feature/sync'] = array(
  182. 'title' => ' Sync Features',
  183. 'description' => 'Sync features from Chado with Drupal',
  184. 'page callback' => 'drupal_get_form',
  185. 'page arguments' => array('tripal_feature_sync_form'),
  186. 'access arguments' => array('access administration pages'),
  187. 'type' => MENU_NORMAL_ITEM,
  188. );
  189. // Adding Secondary Properties
  190. /**
  191. $items['node/%tf_node/tf_properties'] = array(
  192. 'title' => t('Add Properties & Synonyms'),
  193. 'description' => t('Settings for Features'),
  194. 'page callback' => 'tripal_feature_add_ALL_property_page',
  195. 'page arguments' => array(1),
  196. 'access arguments' => array('create chado_feature content'),
  197. 'type' => MENU_CALLBACK
  198. );
  199. $items['node/%tf_node/tf_db_references'] = array(
  200. 'title' => t('Add Database References'),
  201. 'description' => t('Settings for Features'),
  202. 'page callback' => 'tripal_feature_add_ALL_dbreferences_page',
  203. 'page arguments' => array(1),
  204. 'access arguments' => array('create chado_feature content'),
  205. 'type' => MENU_CALLBACK
  206. );
  207. $items['node/%tf_node/tf_relationships'] = array(
  208. 'title' => t('Add Relationships'),
  209. 'description' => t('Settings for Features'),
  210. 'page callback' => 'tripal_feature_add_ALL_relationships_page',
  211. 'page arguments' => array(1),
  212. 'access arguments' => array('create chado_feature content'),
  213. 'type' => MENU_CALLBACK
  214. );
  215. */
  216. //Edit/Deleting Secondary Properties-------------
  217. $items['node/%tf_node/edit_feature_properties'] = array(
  218. 'title' => 'Edit Properties',
  219. 'description' => 'Settings for Features',
  220. 'page callback' => 'tripal_feature_edit_ALL_properties_page',
  221. 'page arguments' => array(1),
  222. 'access arguments' => array('edit chado_feature content'),
  223. 'type' => MENU_LOCAL_TASK,
  224. 'weight' => 8,
  225. );
  226. /**
  227. $items['node/%tf_node/tf_edit_relationships'] = array(
  228. 'title' => t('Edit Relationships'),
  229. 'description' => t('Settings for Feature'),
  230. 'page callback' => 'tripal_feature_edit_ALL_relationships_page',
  231. 'page arguments' => array(1),
  232. 'access arguments' => array('edit chado_feature content'),
  233. 'type' => MENU_LOCAL_TASK,
  234. 'weight' => 9,
  235. );
  236. */
  237. $items['node/%tf_node/tf_edit_db_references'] = array(
  238. 'title' => 'Edit References',
  239. 'description' => 'Settings for Feature',
  240. 'page callback' => 'tripal_feature_edit_ALL_dbreferences_page',
  241. 'page arguments' => array(1),
  242. 'access arguments' => array('edit chado_feature content'),
  243. 'type' => MENU_LOCAL_TASK,
  244. 'weight' => 10,
  245. );
  246. // managing relationship aggregates
  247. $items['admin/tripal/tripal_feature/aggregate'] = array(
  248. 'title' => 'Feature Relationship Aggegators',
  249. 'description' => 'Features have relationships with other features and it may be desirable to aggregate the content from one ore more child or parent feature.',
  250. 'page callback' => 'tripal_feature_aggregator_page',
  251. 'access arguments' => array('manage chado_feature aggregator'),
  252. 'type' => MENU_NORMAL_ITEM,
  253. );
  254. $items['admin/tripal/tripal_feature/aggregate/new'] = array(
  255. 'title' => 'Add an Aggregator',
  256. 'page callback' => 'drupal_get_form',
  257. 'page arguments' => array('tripal_feature_aggregator_form'),
  258. 'access arguments' => array('manage chado_feature aggregator'),
  259. 'type' => MENU_NORMAL_ITEM,
  260. );
  261. $items['admin/tripal/tripal_feature/aggregate/edit/js'] = array(
  262. 'title' => 'Edit an Aggegator',
  263. 'page callback' => 'tripal_feature_aggregator_ajax_edit',
  264. 'access arguments' => array('manage chado_feature aggregator'),
  265. 'type' => MENU_CALLBACK,
  266. );
  267. // the menu link for addressing any feature (by name, uniquename, synonym)
  268. $items['feature/%'] = array(
  269. 'title' => 'Matched Features',
  270. '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.',
  271. 'page callback' => 'tripal_feature_match_features_page',
  272. 'page arguments' => array(1),
  273. 'access arguments' => array('access chado_feature content'),
  274. 'type' => MENU_NORMAL_ITEM,
  275. );
  276. return $items;
  277. }
  278. /**
  279. * Implements Menu wildcard_load hook
  280. * Purpose: Allows the node ID of a chado feature to be dynamically
  281. * pulled from the path. The node is loaded from this node ID
  282. * and supplied to the page as an arguement
  283. *
  284. * @ingroup tripal_feature
  285. */
  286. function tf_node_load($nid) {
  287. if (is_numeric($nid)) {
  288. $node = node_load($nid);
  289. if ($node->type == 'chado_feature') {
  290. return $node;
  291. }
  292. }
  293. return FALSE;
  294. }
  295. /**
  296. *
  297. *
  298. * @ingroup tripal_feature
  299. */
  300. function tripal_feature_block($op = 'list', $delta = 0, $edit=array()) {
  301. switch ($op) {
  302. case 'list':
  303. $blocks['references']['info'] = t('Tripal Feature References');
  304. $blocks['references']['cache'] = BLOCK_NO_CACHE;
  305. $blocks['base']['info'] = t('Tripal Feature Details');
  306. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  307. $blocks['sequence']['info'] = t('Tripal Feature Sequence');
  308. $blocks['sequence']['cache'] = BLOCK_NO_CACHE;
  309. $blocks['featureloc_sequences']['info'] = t('Tripal Feature Annotated Sequence');
  310. $blocks['featureloc_sequences']['cache'] = BLOCK_NO_CACHE;
  311. $blocks['synonyms']['info'] = t('Tripal Feature Synonyms');
  312. $blocks['synonyms']['cache'] = BLOCK_NO_CACHE;
  313. $blocks['properties']['info'] = t('Tripal Feature Properties');
  314. $blocks['properties']['cache'] = BLOCK_NO_CACHE;;
  315. $blocks['alignments']['info'] = t('Tripal Feature Alignments');
  316. $blocks['alignments']['cache'] = BLOCK_NO_CACHE;
  317. $blocks['relationships']['info'] = t('Tripal Feature Relationships');
  318. $blocks['relationships']['cache'] = BLOCK_NO_CACHE;
  319. $blocks['org_feature_counts']['info'] = t('Tripal Organism Feature Counts');
  320. $blocks['org_feature_counts']['cache'] = BLOCK_NO_CACHE;
  321. $blocks['org_feature_browser']['info'] = t('Tripal Organism Feature Browser');
  322. $blocks['org_feature_browser']['cache'] = BLOCK_NO_CACHE;
  323. return $blocks;
  324. case 'view':
  325. if (user_access('access chado_feature content') and arg(0) == 'node' and is_numeric(arg(1))) {
  326. $nid = arg(1);
  327. $node = node_load($nid);
  328. $block = array();
  329. switch ($delta) {
  330. case 'references':
  331. $block['subject'] = t('References');
  332. $block['content'] = theme('tripal_feature_references', $node);
  333. break;
  334. case 'base':
  335. $block['subject'] = t('Feature Details');
  336. $block['content'] = theme('tripal_feature_base', $node);
  337. break;
  338. case 'synonyms':
  339. $block['subject'] = t('Synonyms');
  340. $block['content'] = theme('tripal_feature_synonyms', $node);
  341. break;
  342. case 'properties':
  343. $block['subject'] = t('Properties');
  344. $block['content'] = theme('tripal_feature_properties', $node);
  345. break;;
  346. case 'sequence':
  347. $block['subject'] = t('Sequence');
  348. $block['content'] = theme('tripal_feature_sequence', $node);
  349. break;
  350. case 'featureloc_sequences':
  351. $block['subject'] = t('Formatted Sequences');
  352. $block['content'] = theme('tripal_feature_featureloc_sequences', $node);
  353. break;
  354. case 'alignments':
  355. $block['subject'] = t('Alignments');
  356. $block['content'] = theme('tripal_feature_alignments', $node);
  357. break;
  358. case 'relationships':
  359. $block['subject'] = t('Relationships');
  360. $block['content'] = theme('tripal_feature_relationships', $node);
  361. break;
  362. case 'org_feature_counts':
  363. $block['subject'] = t('Feature Type Summary');
  364. $block['content'] = theme('tripal_organism_feature_counts', $node);
  365. break;
  366. case 'org_feature_browser':
  367. $block['subject'] = t('Feature Browser');
  368. $block['content'] = theme('tripal_organism_feature_browser', $node);
  369. break;
  370. case 'library_feature_browser':
  371. $block['subject'] = t('Library Feature Browser');
  372. $block['content'] = theme('tripal_library_feature_browser', $node);
  373. break;
  374. case 'analysis_feature_browser':
  375. $block['subject'] = t('Analysis Feature Browser');
  376. $block['content'] = theme('tripal_analysis_feature_browser', $node);
  377. break;
  378. default :
  379. }
  380. return $block;
  381. }
  382. }
  383. }
  384. /**
  385. * When a new chado_feature node is created we also need to add information
  386. * to our chado_feature table. This function is called on insert of a new node
  387. * of type 'chado_feature' and inserts the necessary information.
  388. *
  389. * @ingroup tripal_feature
  390. */
  391. function chado_feature_insert($node) {
  392. // remove spaces, newlines from residues
  393. $residues = preg_replace("/[\n\r\s]/", "", $node->residues);
  394. $obsolete = 'FALSE';
  395. if ($node->is_obsolete) {
  396. $obsolete = 'TRUE';
  397. }
  398. $values = array(
  399. 'cv_id' => array(
  400. 'name' => 'sequence'
  401. ),
  402. 'name' => $node->feature_type
  403. );
  404. $type = tripal_core_chado_select('cvterm', array('cvterm_id'), $values);
  405. $values = array(
  406. 'organism_id' => $node->organism_id,
  407. 'name' => $node->fname,
  408. 'uniquename' => $node->uniquename,
  409. 'residues' => $residues,
  410. 'seqlen' => drupal_strlen($residues),
  411. 'is_obsolete' => $obsolete,
  412. 'type_id' => $type[0]->cvterm_id,
  413. 'md5checksum' => md5($residues)
  414. );
  415. // check to see if we are inserting a duplicate record.
  416. $options = array('is_duplicate' => TRUE, 'has_record' => TRUE);
  417. $exists = tripal_core_chado_select('feature', array('*'), $values, $options);
  418. // if the record is not a duplicate then add it
  419. if (!$exists) {
  420. $istatus = tripal_core_chado_insert('feature', $values);
  421. if (!$istatus) {
  422. drupal_set_message(t('Unable to add feature.'), 'warning');
  423. watchdog('tripal_feature', 'Insert feature: Unable to create feature where values: %values',
  424. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  425. }
  426. }
  427. // now get the newly added record
  428. $values = array(
  429. 'organism_id' => $node->organism_id,
  430. 'uniquename' => $node->uniquename,
  431. 'type_id' => $type[0]->cvterm_id,
  432. );
  433. $feature = tripal_core_chado_select('feature', array('feature_id'), $values);
  434. // add the genbank accession and synonyms
  435. chado_feature_add_synonyms($node->synonyms, $feature[0]->feature_id);
  436. // make sure the entry for this feature doesn't already exist in the chado_feature table
  437. // if it doesn't exist then we want to add it.
  438. $node_check_sql = "SELECT * FROM {chado_feature} " .
  439. "WHERE feature_id = '%s'";
  440. $node_check = db_fetch_object(db_query($node_check_sql, $feature[0]->feature_id));
  441. if (!$node_check) {
  442. // next add the item to the drupal table
  443. $sql = "INSERT INTO {chado_feature} (nid, vid, feature_id, sync_date) ".
  444. "VALUES (%d, %d, %d, " . time() . ")";
  445. db_query($sql, $node->nid, $node->vid, $feature[0]->feature_id);
  446. }
  447. }
  448. /**
  449. *
  450. *
  451. * @ingroup tripal_feature
  452. */
  453. function chado_feature_update($node) {
  454. if ($node->revision) {
  455. // TODO -- decide what to do about revisions
  456. }
  457. else {
  458. $residues = preg_replace("/[\n\r\s]/", "", $node->residues);
  459. $obsolete = 'FALSE';
  460. if ($node->is_obsolete) {
  461. $obsolete = 'TRUE';
  462. }
  463. // get the feature type id
  464. $values = array(
  465. 'cv_id' => array(
  466. 'name' => 'sequence'
  467. ),
  468. 'name' => $node->feature_type
  469. );
  470. $type = tripal_core_chado_select('cvterm', array('cvterm_id'), $values);
  471. $feature_id = chado_get_id_for_node('feature', $node) ;
  472. if (sizeof($type) > 0) {
  473. $match = array(
  474. 'feature_id' => $feature_id,
  475. );
  476. $values = array(
  477. 'organism_id' => $node->organism_id,
  478. 'name' => $node->fname,
  479. 'uniquename' => $node->uniquename,
  480. 'residues' => $residues,
  481. 'seqlen' => drupal_strlen($residues),
  482. 'is_obsolete' => $obsolete,
  483. 'type_id' => $type[0]->cvterm_id,
  484. 'md5checksum' => md5($residues)
  485. );
  486. $status = tripal_core_chado_update('feature', $match, $values);
  487. // add the genbank synonyms
  488. chado_feature_add_synonyms($node->synonyms, $feature_id);
  489. }
  490. else {
  491. drupal_set_message(t('Unable to update feature.'), 'warning');
  492. watchdog('tripal_feature',
  493. 'Update feature: Unable to update feature where values: %values',
  494. array('%values' => print_r($values, TRUE)),
  495. WATCHDOG_WARNING
  496. );
  497. }
  498. }
  499. }
  500. /**
  501. *
  502. *
  503. * @ingroup tripal_feature
  504. */
  505. function chado_feature_delete($node) {
  506. $feature_id = chado_get_id_for_node('feature', $node);
  507. // if we don't have a library id for this node then this isn't a node of
  508. // type chado_library or the entry in the chado_library table was lost.
  509. if (!$feature_id) {
  510. return;
  511. }
  512. // remove the drupal content
  513. $sql_del = "DELETE FROM {chado_feature} ".
  514. "WHERE nid = %d ".
  515. "AND vid = %d";
  516. db_query($sql_del, $node->nid, $node->vid);
  517. $sql_del = "DELETE FROM {node} ".
  518. "WHERE nid = %d ".
  519. "AND vid = %d";
  520. db_query($sql_del, $node->nid, $node->vid);
  521. $sql_del = "DELETE FROM {node_revisions} ".
  522. "WHERE nid = %d ".
  523. "AND vid = %d";
  524. db_query($sql_del, $node->nid, $node->vid);
  525. // Remove data from feature tables of chado database. This will
  526. // cause a cascade delete and remove all data in referencing tables
  527. // for this feature
  528. chado_query("DELETE FROM {feature} WHERE feature_id = %d", $feature_id);
  529. drupal_set_message(t("The feature and all associated data were removed from") .
  530. "chado");
  531. }
  532. /**
  533. *
  534. *
  535. * @ingroup tripal_feature
  536. */
  537. function chado_feature_add_synonyms($synonyms, $feature_id) {
  538. // make sure we only have a single space between each synonym
  539. $synonyms = preg_replace("/[\s\n\r]+/", " ", $synonyms);
  540. // split the synonyms into an array based on a space as the delimieter
  541. $syn_array = array();
  542. $syn_array = explode(" ", $synonyms);
  543. // remove any old synonyms
  544. $feature_syn_dsql = "DELETE FROM {feature_synonym} WHERE feature_id = %d";
  545. if (!chado_query($feature_syn_dsql, $feature_id)) {
  546. $error .= "Could not remove synonyms from feature. ";
  547. }
  548. // return if we don't have any synonmys to add
  549. if (!$synonyms) {
  550. return;
  551. }
  552. // iterate through each synonym and add it to the database
  553. foreach ($syn_array as $syn) {
  554. // skip this item if it's empty
  555. if (!$syn) {
  556. break; }
  557. // check to see if we have this accession number already in the database
  558. // if so then don't add it again. it messes up drupal if the insert fails.
  559. // It is possible for the accession number to be present and not the feature
  560. $synonym_sql = "SELECT synonym_id FROM {synonym} ".
  561. "WHERE name = '%s'";
  562. $synonym = db_fetch_object(chado_query($synonym_sql, $syn));
  563. if (!$synonym) {
  564. $synonym_isql = "INSERT INTO {synonym} (name,synonym_sgml,type_id) ".
  565. "VALUES ('%s','%s', ".
  566. " (SELECT cvterm_id ".
  567. " FROM {CVTerm} CVT ".
  568. " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
  569. " WHERE CV.name = 'feature_property' and CVT.name = 'synonym'))";
  570. if (!chado_query($synonym_isql, $syn, $syn)) {
  571. $error .= "Could not add synonym. ";
  572. }
  573. // now get the synonym we just added
  574. $synonym_sql = "SELECT synonym_id FROM {synonym} ".
  575. "WHERE name = '%s'";
  576. $synonym = db_fetch_object(chado_query($synonym_sql, $syn));
  577. }
  578. // now add in our new sysnonym
  579. $feature_syn_isql = "INSERT INTO {feature_synonym} (synonym_id,feature_id,pub_id) ".
  580. "VALUES (%d,%d,1)";
  581. if (!chado_query($feature_syn_isql, $synonym->synonym_id, $feature_id)) {
  582. $error .= "Could not add synonyms to feature. ";
  583. }
  584. }
  585. // return to the drupal database
  586. return $error;
  587. }
  588. /**
  589. *
  590. *
  591. * @ingroup tripal_feature
  592. */
  593. function chado_feature_add_gbaccession($accession, $feature_id) {
  594. // use chado database
  595. // remove any old accession from genbank dbEST
  596. $fdbxref_dsql = "DELETE FROM {feature_dbxref} ".
  597. "WHERE feature_id = %d and dbxref_id IN ".
  598. " (SELECT DBX.dbxref_id FROM {dbxref} DBX ".
  599. " INNER JOIN DB ON DB.db_id = DBX.db_id ".
  600. " INNER JOIN feature_dbxref FDBX ON DBX.dbxref_id = FDBX.dbxref_id ".
  601. " WHERE DB.name = 'DB:Genbank' and FDBX.feature_id = %d)";
  602. if (!chado_query($fdbxref_dsql, $feature_id, $feature_id)) {
  603. $error .= "Could not remove accession from feature. ";
  604. }
  605. // if we don't have an accession number to add then just return
  606. if (!$accession) {
  607. return;
  608. }
  609. // get the db_id
  610. $db_sql = "SELECT db_id FROM {DB} ".
  611. "WHERE name = 'DB:Genbank_est'";
  612. $db = db_fetch_object(chado_query($db_sql));
  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. $dbxref_sql = "SELECT dbxref_id FROM {dbxref} ".
  617. "WHERE db_id = %d and accession = '%s'";
  618. $dbxref = db_fetch_object(chado_query($dbxref_sql, $db->db_id, $accession));
  619. if (!$dbxref) {
  620. // add the accession number
  621. $dbxref_isql = "INSERT INTO {dbxref} (db_id,accession) ".
  622. " VALUES (%d, '%s') ";
  623. if (!chado_query($dbxref_isql, $db->db_id, $accession)) {
  624. $error .= 'Could not add accession as a database reference ';
  625. }
  626. // get the dbxref_id for the just added accession number
  627. $dbxref_sql = "SELECT dbxref_id FROM {dbxref} ".
  628. "WHERE db_id = %d and accession = '%s'";
  629. $dbxref = db_fetch_object(chado_query($dbxref_sql, $db->db_id, $accession));
  630. }
  631. // associate the accession number with the feature
  632. $feature_dbxref_isql = "INSERT INTO {feature_dbxref} (feature_id,dbxref_id) ".
  633. " VALUES (%d, %d) ";
  634. if (!chado_query($feature_dbxref_isql, $feature_id, $dbxref->dbxref_id)) {
  635. $error .= 'Could not add feature database reference. ';
  636. }
  637. return $error;
  638. }
  639. /**
  640. *
  641. *
  642. * @ingroup tripal_feature
  643. */
  644. function chado_feature_form($node, $param) {
  645. $type = node_get_types('type', $node);
  646. $form = array();
  647. $feature = $node->feature;
  648. // add the residues to the feature object
  649. $feature = tripal_core_expand_chado_vars($feature, 'field', 'feature.residues');
  650. // if the node has synonyms then use that as the form may be returning
  651. // from an error. Otherwise try to find synonyms from the database
  652. $synonyms = $node->synonyms;
  653. $feature = tripal_core_expand_chado_vars($feature, 'table', 'feature_synonym');
  654. $feature_synonyms = $feature->feature_synonym;
  655. if (!$synonyms) {
  656. if (!is_array($feature_synonyms)) {
  657. $synonyms = $feature_synonyms->synonym_id->name;
  658. }
  659. elseif (is_array($feature_synonyms)) {
  660. foreach ($feature_synonyms as $index => $synonym) {
  661. $synonyms .= $synonym->synonym_id->name . "\n";
  662. }
  663. }
  664. }
  665. $analyses = $node->analyses;
  666. $references = $node->references;
  667. // We need to pass above variables for preview to show
  668. $form['feature'] = array(
  669. '#type' => 'value',
  670. '#value' => $feature
  671. );
  672. // This field is read when previewing a node
  673. $form['synonyms'] = array(
  674. '#type' => 'value',
  675. '#value' => $synonyms
  676. );
  677. // This field is read when previewing a node
  678. $form['analyses'] = array(
  679. '#type' => 'value',
  680. '#value' => $analyses
  681. );
  682. // This field is read when previewing a node
  683. $form['references'] = array(
  684. '#type' => 'value',
  685. '#value' => $references
  686. );
  687. // keep track of the feature id if we have one. If we do have one then
  688. // this would indicate an update as opposed to an insert.
  689. $form['feature_id'] = array(
  690. '#type' => 'value',
  691. '#value' => $feature->feature_id,
  692. );
  693. $form['title']= array(
  694. '#type' => 'textfield',
  695. '#title' => t('Title'),
  696. '#required' => TRUE,
  697. '#default_value' => $node->title,
  698. '#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.'),
  699. '#weight' => 1,
  700. '#maxlength' => 255
  701. );
  702. $form['uniquename']= array(
  703. '#type' => 'textfield',
  704. '#title' => t('Unique Feature Name'),
  705. '#required' => TRUE,
  706. '#default_value' => $feature->uniquename,
  707. '#description' => t('Enter a unique name for this feature. This name must be unique for the organism and feature type.'),
  708. '#weight' => 1,
  709. '#maxlength' => 255
  710. );
  711. $form['fname']= array(
  712. '#type' => 'textfield',
  713. '#title' => t('Feature Name'),
  714. '#required' => TRUE,
  715. '#default_value' => $feature->name,
  716. '#description' => t('Enter the name used by humans to refer to this feature.'),
  717. '#weight' => 1,
  718. '#maxlength' => 255
  719. );
  720. // get the list of supported feature types
  721. $ftypes = array();
  722. $ftypes[''] = '';
  723. $supported_ftypes = split("[ \n]", variable_get('tripal_feature_type_setting', 'gene mRNA EST contig'));
  724. foreach ($supported_ftypes as $ftype) {
  725. $ftypes["$ftype"] = $ftype;
  726. }
  727. $form['feature_type'] = array(
  728. '#title' => t('Feature Type'),
  729. '#type' => t('select'),
  730. '#description' => t("Choose the feature type."),
  731. '#required' => TRUE,
  732. '#default_value' => $feature->type_id->name,
  733. '#options' => $ftypes,
  734. '#weight' => 2
  735. );
  736. // get the list of organisms
  737. $sql = "SELECT * FROM {Organism} ORDER BY genus, species";
  738. $org_rset = chado_query($sql);
  739. //
  740. $organisms = array();
  741. $organisms[''] = '';
  742. while ($organism = db_fetch_object($org_rset)) {
  743. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  744. }
  745. $form['organism_id'] = array(
  746. '#title' => t('Organism'),
  747. '#type' => t('select'),
  748. '#description' => t("Choose the organism with which this feature is associated"),
  749. '#required' => TRUE,
  750. '#default_value' => $feature->organism_id->organism_id,
  751. '#options' => $organisms,
  752. '#weight' => 3,
  753. );
  754. // Get synonyms
  755. if ($synonyms) {
  756. if (is_array($synonyms)) {
  757. foreach ($synonyms as $synonym) {
  758. $syn_text .= "$synonym->name\n";
  759. }
  760. }
  761. else {
  762. $syn_text = $synonyms;
  763. }
  764. }
  765. $form['synonyms']= array(
  766. '#type' => 'textarea',
  767. '#title' => t('Synonyms'),
  768. '#required' => FALSE,
  769. '#default_value' => $syn_text,
  770. '#description' => t('Enter alternate names (synonmys) for this feature to help in searching and identification. You may enter as many alternate names as needed separated by spaces or on different lines.'),
  771. '#weight' => 5,
  772. );
  773. $form['residues']= array(
  774. '#type' => 'textarea',
  775. '#title' => t('Residues'),
  776. '#required' => FALSE,
  777. '#default_value' => $feature->residues,
  778. '#description' => t('Enter the nucelotide sequences for this feature'),
  779. '#weight' => 6
  780. );
  781. $checked = '';
  782. if ($feature->is_obsolete == 't') {
  783. $checked = '1';
  784. }
  785. $form['is_obsolete']= array(
  786. '#type' => 'checkbox',
  787. '#title' => t('Is Obsolete'),
  788. '#required' => FALSE,
  789. '#default_value' => $checked,
  790. '#description' => t('Check this box if this sequence should be retired and no longer included in further analysis.'),
  791. '#weight' => 8
  792. );
  793. return $form;
  794. }
  795. /**
  796. *
  797. *
  798. * @ingroup tripal_feature
  799. */
  800. function chado_feature_validate($node) {
  801. $result = 0;
  802. // if this is an update, we want to make sure that a different feature for
  803. // the organism doesn't already have this uniquename. We don't want to give
  804. // two sequences the same uniquename
  805. if ($node->feature_id) {
  806. $sql = "SELECT *
  807. FROM {Feature} F
  808. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  809. WHERE uniquename = '%s'
  810. AND organism_id = %d AND CVT.name = '%s' AND NOT feature_id = %d";
  811. $result = db_fetch_object(chado_query($sql, $node->uniquename, $node->organism_id, $node->feature_type, $node->feature_id));
  812. if ($result) {
  813. 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."));
  814. }
  815. }
  816. // if this is an insert then we just need to make sure this name doesn't
  817. // already exist for this organism if it does then we need to throw an error
  818. else {
  819. $sql = "SELECT *
  820. FROM {Feature} F
  821. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  822. WHERE uniquename = '%s'
  823. AND organism_id = %d AND CVT.name = '%s'";
  824. $result = db_fetch_object(chado_query($sql, $node->uniquename, $node->organism_id, $node->feature_type));
  825. if ($result) {
  826. 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."));
  827. }
  828. }
  829. // we want to remove all characters except IUPAC nucleotide characters from the
  830. // the residues. however, residues are not required so if blank then we'll skip
  831. // this step
  832. if ($node->residues) {
  833. $residues = preg_replace("/[^\w]/", '', $node->residues);
  834. if (!preg_match("/^[ACTGURYMKSWBDHVN]+$/i", $residues)) {
  835. form_set_error('residues', t("The residues in feature $node->name contains more than the nucleotide IUPAC characters. Only the following characters are allowed: A,C,T,G,U,R,Y,M,K,S,W,B,D,H,V,N: '" . $residues . "'"));
  836. }
  837. }
  838. // we don't allow a genbank accession number for a contig
  839. if ($node->feature_type == 'contig' and $node->gbaccession) {
  840. form_set_error('gbaccession', t("Contigs cannot have a genbank accession number. Please change the feature type or remove the accession number"));
  841. }
  842. }
  843. /**
  844. * When a node is requested by the user this function is called to allow us
  845. * to add auxiliary data to the node object.
  846. *
  847. * @ingroup tripal_feature
  848. */
  849. function chado_feature_load($node) {
  850. // get the feature details from chado
  851. $feature_id = chado_get_id_for_node('feature', $node);
  852. $values = array('feature_id' => $feature_id);
  853. $feature = tripal_core_generate_chado_var('feature', $values);
  854. if (strcmp($feature->name, $feature->uniquename)==0) {
  855. $node->title = $feature->name . " (" . $feature->type_id->name . ") " . $feature->organism_id->genus . " " . $feature->organism_id->species ;
  856. }
  857. $additions = new stdClass();
  858. $additions->feature = $feature;
  859. return $additions;
  860. }
  861. /**
  862. *
  863. *
  864. * @ingroup tripal_feature
  865. */
  866. function tripal_feature_load_organism($organism_id) {
  867. // add organism details
  868. $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
  869. $organism = db_fetch_object(chado_query($sql, $organism_id));
  870. return $organism;
  871. }
  872. /**
  873. *
  874. *
  875. * @ingroup tripal_feature
  876. */
  877. function tripal_feature_load_synonyms($feature_id) {
  878. $sql = "SELECT S.name ".
  879. "FROM {Feature_Synonym} FS ".
  880. " INNER JOIN {Synonym} S ".
  881. " ON FS.synonym_id = S.Synonym_id ".
  882. "WHERE FS.feature_id = %d ".
  883. "ORDER BY S.name ";
  884. $results = chado_query($sql, $feature_id);
  885. $synonyms = array();
  886. $i=0;
  887. while ($synonym = db_fetch_object($results)) {
  888. $synonyms[$i++] = $synonym;
  889. }
  890. return $synonyms;
  891. }
  892. /**
  893. *
  894. *
  895. * @ingroup tripal_feature
  896. */
  897. function tripal_feature_load_properties($feature_id) {
  898. $sql = "SELECT CVT.name as cvname, FS.type_id, FS.value, FS.rank,
  899. CVT.definition, CVT.is_obsolete,
  900. DBX.dbxref_id,DBX.accession,DB.name as dbname,
  901. DB.urlprefix, DB.description as db_description, DB.url
  902. FROM {featureprop} FS
  903. INNER JOIN {cvterm} CVT ON FS.type_id = CVT.cvterm_id
  904. INNER JOIN {dbxref} DBX ON CVT.dbxref_id = DBX.dbxref_id
  905. INNER JOIN {db} DB ON DB.db_id = DBX.db_id
  906. WHERE FS.feature_id = %d
  907. ORDER BY FS.rank ASC";
  908. $results = chado_query($sql, $feature_id);
  909. $i=0;
  910. $properties = array();
  911. while ($property = db_fetch_object($results)) {
  912. $properties[$i++] = $property;
  913. }
  914. return $properties;
  915. }
  916. /**
  917. *
  918. *
  919. * @ingroup tripal_feature
  920. */
  921. function tripal_feature_load_references($feature_id) {
  922. $sql = "SELECT F.uniquename,F.Feature_id,DBX.accession,DB.description as dbdesc, ".
  923. " DB.db_id, DB.name as db_name, DB.urlprefix,DBX.dbxref_id ".
  924. "FROM {feature} F ".
  925. " INNER JOIN {feature_dbxref} FDBX on F.feature_id = FDBX.feature_id ".
  926. " INNER JOIN {dbxref} DBX on DBX.dbxref_id = FDBX.dbxref_id ".
  927. " INNER JOIN {db} on DB.db_id = DBX.db_id ".
  928. "WHERE F.feature_id = %d ".
  929. "ORDER BY DB.name ";
  930. $results = chado_query($sql, $feature_id);
  931. $references = array();
  932. $i=0;
  933. while ($accession = db_fetch_object($results)) {
  934. $references[$i++] = $accession;
  935. }
  936. return $references;
  937. }
  938. /**
  939. *
  940. *
  941. * @ingroup tripal_feature
  942. */
  943. function tripal_feature_load_featurelocs($feature_id, $side = 'as_parent', $aggregate = 1) {
  944. $sql = "SELECT
  945. F.name, F.feature_id, F.uniquename,
  946. FS.name as src_name,
  947. FS.feature_id as src_feature_id,
  948. FS.uniquename as src_uniquename,
  949. CVT.name as cvname, CVT.cvterm_id,
  950. CVTS.name as src_cvname, CVTS.cvterm_id as src_cvterm_id,
  951. FL.fmin, FL.fmax, FL.is_fmin_partial, FL.is_fmax_partial,FL.strand,
  952. FL.phase
  953. FROM {featureloc} FL
  954. INNER JOIN {feature} F on FL.feature_id = F.feature_id
  955. INNER JOIN {feature} FS on FS.feature_id = FL.srcfeature_id
  956. INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id
  957. INNER JOIN {cvterm} CVTS on FS.type_id = CVTS.cvterm_id
  958. ";
  959. if (strcmp($side, 'as_parent')==0) {
  960. $sql .= "WHERE FL.srcfeature_id = %d ";
  961. }
  962. if (strcmp($side, 'as_child')==0) {
  963. $sql .= "WHERE FL.feature_id = %d ";
  964. }
  965. $flresults = chado_query($sql, $feature_id);
  966. // copy the results into an array
  967. $i=0;
  968. $featurelocs = array();
  969. while ($loc = db_fetch_object($flresults)) {
  970. // if a drupal node exists for this feature then add the nid to the
  971. // results object
  972. $sql = 'SELECT nid FROM {chado_feature} WHERE feature_id = %d';
  973. $ffeature = db_fetch_object(db_query($sql, $loc->feature_id));
  974. $sfeature = db_fetch_object(db_query($sql, $loc->src_feature_id));
  975. $loc->fnid = $ffeature->nid;
  976. $loc->snid = $sfeature->nid;
  977. // add the result to the array
  978. $featurelocs[$i++] = $loc;
  979. }
  980. // Add the relationship feature locs if aggregate is turned on
  981. if ($aggregate and strcmp($side, 'as_parent')==0) {
  982. // get the relationships for this feature without substituting any children
  983. // for the parent. We want all relationships
  984. $relationships = tripal_feature_get_aggregate_relationships($feature_id, 0);
  985. foreach ($relationships as $rindex => $rel) {
  986. // get the featurelocs for each of the relationship features
  987. $rel_featurelocs = tripal_feature_load_featurelocs($rel->subject_id, 'as_child', 0);
  988. foreach ($rel_featurelocs as $findex => $rfloc) {
  989. $featurelocs[$i++] = $rfloc;
  990. }
  991. }
  992. }
  993. usort($featurelocs, 'tripal_feature_sort_locations');
  994. return $featurelocs;
  995. }
  996. /**
  997. * used to sort the feature locs by start position
  998. *
  999. * @ingroup tripal_feature
  1000. */
  1001. function tripal_feature_sort_locations($a, $b) {
  1002. return strnatcmp($a->fmin, $b->fmin);
  1003. }
  1004. /**
  1005. *
  1006. *
  1007. * @ingroup tripal_feature
  1008. */
  1009. function tripal_feature_load_relationships($feature_id, $side = 'as_subject') {
  1010. // get the relationships for this feature. The query below is used for both
  1011. // querying the object and subject relationships
  1012. $sql = "SELECT
  1013. FS.name as subject_name,
  1014. FS.uniquename as subject_uniquename,
  1015. CVTS.name as subject_type,
  1016. CVTS.cvterm_id as subject_type_id,
  1017. FR.subject_id,
  1018. FR.type_id as relationship_type_id,
  1019. CVT.name as rel_type,
  1020. FO.name as object_name,
  1021. FO.uniquename as object_uniquename,
  1022. CVTO.name as object_type,
  1023. CVTO.cvterm_id as object_type_id,
  1024. FR.object_id,
  1025. FR.rank
  1026. FROM {feature_relationship} FR
  1027. INNER JOIN {cvterm} CVT ON FR.type_id = CVT.cvterm_id
  1028. INNER JOIN {feature} FS ON FS.feature_id = FR.subject_id
  1029. INNER JOIN {feature} FO ON FO.feature_id = FR.object_id
  1030. INNER JOIN {cvterm} CVTO ON FO.type_id = CVTO.cvterm_id
  1031. INNER JOIN {cvterm} CVTS ON FS.type_id = CVTS.cvterm_id
  1032. ";
  1033. if (strcmp($side, 'as_object')==0) {
  1034. $sql .= " WHERE FR.object_id = %d";
  1035. }
  1036. if (strcmp($side, 'as_subject')==0) {
  1037. $sql .= " WHERE FR.subject_id = %d";
  1038. }
  1039. $sql .= " ORDER BY FR.rank";
  1040. // get the relationships
  1041. $results = chado_query($sql, $feature_id);
  1042. // iterate through the relationships, put these in an array and add
  1043. // in the Drupal node id if one exists
  1044. $i=0;
  1045. $nodesql = "SELECT nid FROM {chado_feature} WHERE feature_id = %d";
  1046. $relationships = array();
  1047. while ($rel = db_fetch_object($results)) {
  1048. $node = db_fetch_object(db_query($nodesql, $rel->subject_id));
  1049. if ($node) {
  1050. $rel->subject_nid = $node->nid;
  1051. }
  1052. $node = db_fetch_object(db_query($nodesql, $rel->object_id));
  1053. if ($node) {
  1054. $rel->object_nid = $node->nid;
  1055. }
  1056. $relationships[$i++] = $rel;
  1057. }
  1058. return $relationships;
  1059. }
  1060. /**
  1061. *
  1062. *
  1063. * @ingroup tripal_feature
  1064. */
  1065. function tripal_feature_get_aggregate_types($feature_id) {
  1066. // get the feature details
  1067. $sql = 'SELECT type_id FROM {feature} WHERE feature_id = %d';
  1068. $feature = db_fetch_object(chado_query($sql, $feature_id));
  1069. // check to see if this feature is of a type with an aggregate
  1070. $sql = "SELECT * FROM {tripal_feature_relagg} WHERE type_id = %d";
  1071. $types = array();
  1072. $results = db_query($sql, $feature->type_id);
  1073. while ($agg = db_fetch_object($results)) {
  1074. $types[] = $agg->rel_type_id;
  1075. }
  1076. return $types;
  1077. }
  1078. /**
  1079. *
  1080. *
  1081. * @ingroup tripal_feature
  1082. */
  1083. function tripal_feature_get_aggregate_relationships($feature_id, $substitute=1,
  1084. $levels=0, $base_type_id=NULL, $depth=0) {
  1085. // we only want to recurse to as many levels deep as indicated by the
  1086. // $levels variable, but only if this variable is > 0. If 0 then we
  1087. // recurse until we reach the end of the relationships tree.
  1088. if ($levels > 0 and $levels == $depth) {
  1089. return NULL;
  1090. }
  1091. // first get the relationships for this feature
  1092. $relationships = tripal_feature_load_relationships($feature_id, 'as_object');
  1093. // next, iterate through these relationships and descend, adding in those
  1094. // that are specified by the aggregator.
  1095. $i=0;
  1096. $new_relationships = array();
  1097. foreach ($relationships as $rindex => $rel) {
  1098. // set the base type id
  1099. if (!$base_type_id) {
  1100. $base_type_id = $rel->object_type_id;
  1101. }
  1102. // check to see if we have an aggregator for this base type
  1103. $sql = "SELECT * FROM {tripal_feature_relagg} WHERE type_id = %d and rel_type_id = %d";
  1104. $agg = db_fetch_object(db_query($sql, $base_type_id, $rel->subject_type_id));
  1105. if ($agg) {
  1106. // if we're not going to substitute the resulting relationships for the
  1107. // parent then we need to add the parent to our list
  1108. if (!$substitute) {
  1109. $new_relationships[$i++] = $rel;
  1110. }
  1111. // recurse all relationships
  1112. $agg_relationships = tripal_feature_get_aggregate_relationships(
  1113. $rel->subject_id, $levels, $base_type_id, $depth++);
  1114. // if we have an aggregate defined but we have no relationships beyond
  1115. // this point then there's nothing we can substitute
  1116. if (!$agg_relationships and $substitute) {
  1117. $new_relationships[$i++] = $rel;
  1118. }
  1119. // merge all relationships into one array
  1120. foreach ($agg_relationships as $aindex => $arel) {
  1121. $new_relationships[$i++] = $arel;
  1122. }
  1123. }
  1124. else {
  1125. // if we don't have an aggregate then keep the current relationship
  1126. $new_relationships[$i++] = $rel;
  1127. }
  1128. }
  1129. return $new_relationships;
  1130. }
  1131. /**
  1132. *
  1133. *
  1134. * @ingroup tripal_feature
  1135. */
  1136. function tripal_feature_load_featureloc_sequences($feature_id, $featurelocs) {
  1137. // if we don't have any featurelocs then no point in continuing
  1138. if (!$featurelocs) {
  1139. return array();
  1140. }
  1141. // get the list of relationships (including any aggregators) and iterate
  1142. // through each one to find information needed to color-code the reference sequence
  1143. $relationships = tripal_feature_get_aggregate_relationships($feature_id);
  1144. if (!$relationships) {
  1145. return array();
  1146. }
  1147. // iterate through each of the realtionships features and get their
  1148. // locations
  1149. foreach ($relationships as $rindex => $rel) {
  1150. // get the featurelocs for each of the relationship features
  1151. $rel_featurelocs = tripal_feature_load_featurelocs($rel->subject_id, 'as_child', 0);
  1152. foreach ($rel_featurelocs as $rfindex => $rel_featureloc) {
  1153. // keep track of this unique source feature
  1154. $src = $rel_featureloc->src_feature_id ."-". $rel_featureloc->src_cvterm_id;
  1155. // copy over the results to the relationship object. Since there can
  1156. // be more than one feature location for each relationship feature we
  1157. // use the '$src' variable to keep track of these.
  1158. $rel->featurelocs->$src->src_uniquename = $rel_featureloc->src_uniquename;
  1159. $rel->featurelocs->$src->src_cvterm_id = $rel_featureloc->src_cvterm_id;
  1160. $rel->featurelocs->$src->src_cvname = $rel_featureloc->src_cvname;
  1161. $rel->featurelocs->$src->fmin = $rel_featureloc->fmin;
  1162. $rel->featurelocs->$src->fmax = $rel_featureloc->fmax;
  1163. $rel->featurelocs->$src->src_name = $rel_featureloc->src_name;
  1164. // keep track of the individual parts for each relationship
  1165. $start = $rel->featurelocs->$src->fmin;
  1166. $end = $rel->featurelocs->$src->fmax;
  1167. $type = $rel->subject_type;
  1168. $rel_locs[$src]['parts'][$start][$type]['start'] = $start;
  1169. $rel_locs[$src]['parts'][$start][$type]['end'] = $end;
  1170. $rel_locs[$src]['parts'][$start][$type]['type'] = $type;
  1171. }
  1172. }
  1173. // the featurelocs array provided to the function contains the locations
  1174. // where this feature is found. We want to get the sequence for each
  1175. // location and then annotate it with the parts found from the relationships
  1176. // locations determiend above.
  1177. $sql = "SELECT substring(residues from %d for %d) as residues ".
  1178. "FROM {feature} ".
  1179. "WHERE feature_id = %d";
  1180. $floc_sequences = array();
  1181. foreach ($featurelocs as $featureloc) {
  1182. // build the src name so we can keep track of the different parts for each feature
  1183. $src = $featureloc->srcfeature_id->feature_id ."-". $featureloc->srcfeature_id->type_id->cvterm_id;
  1184. // orient the parts to the beginning of the feature sequence
  1185. if (!empty($rel_locs[$src]['parts'])) {
  1186. $parts = $rel_locs[$src]['parts'];
  1187. $rparts = array(); // we will fill this up if we're on the reverse strand
  1188. foreach ($parts as $start => $types) {
  1189. foreach ($types as $type_name => $type) {
  1190. if ($featureloc->strand >= 0) {
  1191. // this is on the forward strand. We need to convert the start on the src feature to the
  1192. // start on this feature's sequence
  1193. $parts[$start][$type_name]['start'] = $parts[$start][$type_name]['start'] - $featureloc->fmin;
  1194. $parts[$start][$type_name]['end'] = $parts[$start][$type_name]['end'] - $featureloc->fmin;
  1195. $parts[$start][$type_name]['type'] = $type_name;
  1196. }
  1197. else {
  1198. // this is on the reverse strand. We need to swap the start and stop and calculate from the
  1199. // begining of the reverse sequence
  1200. $size = ($featureloc->fmax - $featureloc->fmin);
  1201. $start_orig = $parts[$start][$type_name]['start'];
  1202. $end_orig = $parts[$start][$type_name]['end'];
  1203. $new_start = $size - ($end_orig - $featureloc->fmin);
  1204. $new_end = $size - ($start_orig - $featureloc->fmin);
  1205. $rparts[$new_start][$type_name]['start'] = $new_start;
  1206. $rparts[$new_start][$type_name]['end'] = $new_end;
  1207. $rparts[$new_start][$type_name]['type'] = $type_name;
  1208. }
  1209. }
  1210. }
  1211. // now sort the parts
  1212. // if we're on the reverse strand we need to resort
  1213. if ($featureloc->strand >= 0) {
  1214. usort($parts, 'tripal_feature_sort_rel_parts_by_start');
  1215. }
  1216. else {
  1217. usort($rparts, 'tripal_feature_sort_rel_parts_by_start');
  1218. $parts = $rparts;
  1219. }
  1220. $floc_sequences[$src]['src'] = $src;
  1221. $floc_sequences[$src]['type'] = $featureloc->feature_id->type_id->name;
  1222. $sequence = db_fetch_object(chado_query($sql, $featureloc->fmin + 1, ($featureloc->fmax - $featureloc->fmin), $featureloc->srcfeature_id->feature_id));
  1223. $residues = $sequence->residues;
  1224. if ($featureloc->strand < 0) {
  1225. $residues = tripal_feature_reverse_complement($residues);
  1226. }
  1227. $strand = '.';
  1228. if ($featureloc->strand == 1) {
  1229. $strand = '+';
  1230. }
  1231. elseif ($featureloc->strand == -1) {
  1232. $strand = '-';
  1233. }
  1234. $defline = $featureloc->feature_id->name . " " . $featureloc->srcfeature_id->name . ":" . ($featureloc->fmin + 1) . ".." . $featureloc->fmax . " " . $strand;
  1235. $floc_sequences[$src]['formatted_seq'] = tripal_feature_color_sequence($residues, $parts, $defline);
  1236. }
  1237. }
  1238. return $floc_sequences;
  1239. }
  1240. /**
  1241. *
  1242. *
  1243. * @ingroup tripal_feature
  1244. */
  1245. function tripal_feature_get_matched_alignments($feature) {
  1246. // This function is for features that align through an intermediate such
  1247. // as 'EST_match' or 'match'. This occurs in the case where two sequences
  1248. // align but where one does not align perfectly. Some ESTs may be in a contig
  1249. // but not all of the EST. Portions may overhang and not be included in the
  1250. // consensus if quality is bad.
  1251. // For example:
  1252. //
  1253. // Feature 1: Contig --------------------
  1254. // Feature 2: EST_match -------
  1255. // Feature 3: EST ---------
  1256. //
  1257. // The feature provided to the function will always be the feature 1. The
  1258. // featureloc columns prefixed with 'right' (e.g. right_fmin) belong to the
  1259. // alignment of feature 3 with feature 2
  1260. //
  1261. // Features may align to more than one feature and are not matches. We do
  1262. // not want to include these, so we have to filter on the SO terms:
  1263. // match, or %_match
  1264. //
  1265. $sql = "SELECT ".
  1266. " FL1.featureloc_id as left_featureloc_id, ".
  1267. " FL1.srcfeature_id as left_srcfeature_id, ".
  1268. " FL1.feature_id as left_feature_id, ".
  1269. " FL1.fmin as left_fmin, ".
  1270. " FL1.is_fmin_partial as left_is_fmin_partial, ".
  1271. " FL1.fmax as left_fmax, ".
  1272. " FL1.is_fmax_partial as left_is_fmax_partial, ".
  1273. " FL1.strand as left_strand, ".
  1274. " FL1.phase as left_phase, ".
  1275. " FL1.locgroup as left_locgroup, ".
  1276. " FL1.rank as left_rank, ".
  1277. " FL2.featureloc_id as right_featureloc_id, ".
  1278. " FL2.srcfeature_id as right_srcfeature_id, ".
  1279. " FL2.feature_id as right_feature_id, ".
  1280. " FL2.fmin as right_fmin, ".
  1281. " FL2.is_fmin_partial as right_is_fmin_partial, ".
  1282. " FL2.fmax as right_fmax, ".
  1283. " FL2.is_fmax_partial as right_is_fmax_partial, ".
  1284. " FL2.strand as right_strand, ".
  1285. " FL2.phase as right_phase, ".
  1286. " FL2.locgroup as right_locgroup, ".
  1287. " FL2.rank as right_rank ".
  1288. "FROM feature F1 ".
  1289. " INNER JOIN featureloc FL1 on FL1.srcfeature_id = F1.feature_id ".
  1290. " INNER JOIN feature F2 on FL1.feature_id = F2.feature_id ".
  1291. " INNER JOIN featureloc FL2 on FL2.feature_id = F2.feature_id ".
  1292. " INNER JOIN feature F3 on FL2.srcfeature_id = F3.feature_id ".
  1293. " INNER JOIN cvterm CVT2 on F2.type_id = CVT2.cvterm_id ".
  1294. "WHERE F1.feature_id = %d and NOT F3.feature_id = %d ".
  1295. " AND (CVT2.name = 'match' or CVT2.name like '%_match') ".
  1296. "ORDER BY FL1.fmin";
  1297. $results = chado_query($sql, $feature->feature_id, $feature->feature_id);
  1298. // iterate through the results and add them to our featurelocs array
  1299. $featurelocs = array();
  1300. while ($fl = db_fetch_object($results)) {
  1301. $featurelocs[] = $fl ;
  1302. }
  1303. return $featurelocs;
  1304. }
  1305. /**
  1306. *
  1307. *
  1308. * @ingroup tripal_feature
  1309. */
  1310. function tripal_feature_load_organism_feature_counts($organism) {
  1311. // don't show the browser if the settings in the admin page is turned off
  1312. // instead return the array indicating the status of the browser
  1313. $show_counts = variable_get('tripal_feature_summary_setting', 'show_feature_summary');
  1314. if (strcmp($show_counts, 'show_feature_summary')!=0) {
  1315. return array('enabled' => FALSE );
  1316. }
  1317. $args = array();
  1318. $names = array();
  1319. $order = array();
  1320. // build the where clause for the SQL statement if we have a custom term list
  1321. // we'll also keep track of the names the admin provided (if any) and the
  1322. // order that the terms should appear.
  1323. $is_custom = 0;
  1324. $temp = rtrim(variable_get('tripal_feature_summary_report_mapping', ''));
  1325. $where = '';
  1326. if ($temp) {
  1327. $is_custom = 1;
  1328. $temp = explode("\n", $temp);
  1329. foreach ($temp as $key => $value) {
  1330. // separate the key value pairs
  1331. $temp2 = explode("=", $value);
  1332. $feature_type = rtrim($temp2[0]);
  1333. $args[] = $feature_type;
  1334. $order[] = $feature_type;
  1335. // if a new name is provided then use that otherwise just
  1336. // use the feature type
  1337. if (count($temp2) == 2) {
  1338. $names[] = rtrim($temp2[1]);
  1339. }
  1340. else {
  1341. $names[] = $feature_type;
  1342. }
  1343. $where .= "OFC.feature_type = '%s' OR \n";
  1344. }
  1345. if ($where) {
  1346. $where = drupal_substr($where, 0, -5); # remove OR from the end
  1347. $where = "($where) AND";
  1348. }
  1349. }
  1350. // get the feature counts. This is dependent on a materialized view
  1351. // installed with the organism module
  1352. $sql = "
  1353. SELECT OFC.num_features,OFC.feature_type,CVT.definition
  1354. FROM {organism_feature_count} OFC
  1355. INNER JOIN {cvterm} CVT on OFC.cvterm_id = CVT.cvterm_id
  1356. WHERE $where organism_id = %d
  1357. ORDER BY num_features desc
  1358. ";
  1359. $args[] = $organism->organism_id;
  1360. $org_features = chado_query($sql, $args);
  1361. // iterate through the types
  1362. $types = array();
  1363. while ($type = db_fetch_object($org_features)) {
  1364. $types[$type->feature_type] = $type;
  1365. // if we don't have an order this means we didn't go through the loop
  1366. // above to set the names, so do that now
  1367. if (!$is_custom) {
  1368. $names[] = $type->feature_type;
  1369. $order[] = $type->feature_type;
  1370. }
  1371. }
  1372. # now reorder the types
  1373. $ordered_types = array();
  1374. foreach ($order as $type) {
  1375. $ordered_types[] = $types[$type];
  1376. }
  1377. return array( 'types' => $ordered_types, 'names' => $names, 'enabled' => TRUE );
  1378. }
  1379. /**
  1380. *
  1381. *
  1382. * @ingroup tripal_feature
  1383. */
  1384. function tripal_feature_load_organism_feature_browser($organism) {
  1385. if (!$organism) {
  1386. return array();
  1387. }
  1388. // don't show the browser if the settings in the admin page is turned off
  1389. // instead return the array indicating the status of the browser
  1390. $show_browser = variable_get('tripal_feature_browse_setting', 'show_feature_browser');
  1391. if (strcmp($show_browser, 'show_feature_browser')!=0) {
  1392. return array('enabled' => FALSE);
  1393. }
  1394. // get the list of available sequence ontology terms for which
  1395. // we will build drupal pages from features in chado. If a feature
  1396. // is not one of the specified typse we won't build a node for it.
  1397. $allowed_types = variable_get('chado_browser_feature_types', 'EST contig');
  1398. $allowed_types = preg_replace("/[\s\n\r]+/", " ", $allowed_types);
  1399. $so_terms = split(' ', $allowed_types);
  1400. $where_cvt = "";
  1401. foreach ($so_terms as $term) {
  1402. $where_cvt .= "CVT.name = '$term' OR ";
  1403. }
  1404. $where_cvt = drupal_substr($where_cvt, 0, drupal_strlen($where_cvt)-3); # strip trailing 'OR'
  1405. // get the features for this organism
  1406. $sql = "SELECT F.name,F.feature_id,F.uniquename,CVT.name as cvname ".
  1407. "FROM {feature} F ".
  1408. " INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id ".
  1409. "WHERE organism_id = %s and ($where_cvt) ".
  1410. "ORDER BY feature_id ASC";
  1411. // the counting SQL
  1412. $csql = "SELECT count(*) ".
  1413. "FROM {feature} F".
  1414. " INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id ".
  1415. "WHERE organism_id = %s and ($where_cvt) ".
  1416. "GROUP BY organism_id ";
  1417. $previous_db = tripal_db_set_active('chado'); // use chado database
  1418. $org_features = pager_query($sql, 10, 0, $csql, $organism->organism_id);
  1419. tripal_db_set_active($previous_db); // now use drupal database
  1420. $pager = theme('pager');
  1421. // prepare the query that will lookup node ids
  1422. $sql = "SELECT nid FROM {chado_feature} ".
  1423. "WHERE feature_id = %d";
  1424. $i=0;
  1425. $features = array();
  1426. while ($feature = db_fetch_object($org_features)) {
  1427. $node = db_fetch_object(db_query($sql, $feature->feature_id));
  1428. $feature->nid = $node->nid;
  1429. $features[$i++] = $feature;
  1430. }
  1431. return array( 'features' => $features, 'pager' => $pager, 'enabled' => TRUE );
  1432. }
  1433. /**
  1434. * This generates the Feature Browser which can optionally be included on library pages
  1435. * and shows all features belonging to the given library. This Browse can be shown/hidden
  1436. * on the Feature Configuration page.
  1437. *
  1438. * @ingroup tripal_feature
  1439. */
  1440. function tripal_feature_load_library_feature_browser($library) {
  1441. // don't show the browser if the settings in the admin page is turned off
  1442. // instead return the array indicating the status of the browser
  1443. $show_browser = variable_get('tripal_library_feature_browse_setting', 'show_feature_browser');
  1444. if (strcmp($show_browser, 'show_feature_browser')!=0) {
  1445. return array('enabled' => FALSE);
  1446. }
  1447. // get a list of feature types to include in the browser
  1448. $allowed_types = variable_get('chado_browser_feature_types', 'EST contig');
  1449. $allowed_types = preg_replace("/[\s\n\r]+/", " ", $allowed_types);
  1450. $so_terms = split(' ', $allowed_types);
  1451. $where_cvt = "";
  1452. foreach ($so_terms as $term) {
  1453. $where_cvt .= "CVT.name = '$term' OR ";
  1454. }
  1455. $where_cvt = drupal_substr($where_cvt, 0, drupal_strlen($where_cvt)-3); # strip trailing 'OR'
  1456. // get the features for this library
  1457. $sql = "SELECT F.name,F.feature_id,F.uniquename,CVT.name as cvname ".
  1458. "FROM {feature} F ".
  1459. " INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id ".
  1460. " INNER JOIN {library_feature} LF on F.feature_id = LF.feature_id ".
  1461. " INNER JOIN {library} L on LF.library_id = L.library_id ".
  1462. "WHERE LF.library_id = %d and ($where_cvt) ".
  1463. "ORDER BY feature_id ASC";
  1464. // the counting SQL
  1465. $csql = "SELECT count(*) ".
  1466. "FROM {feature} F".
  1467. " INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id ".
  1468. " INNER JOIN {library_feature} LF on F.feature_id = LF.feature_id ".
  1469. " INNER JOIN {library} L on LF.library_id = L.library_id ".
  1470. "WHERE LF.library_id = %d and ($where_cvt) ".
  1471. "GROUP BY L.library_id ";
  1472. $previous_db = tripal_db_set_active('chado'); // use chado database
  1473. $org_features = pager_query($sql, 10, 0, $csql, $library->library_id);
  1474. tripal_db_set_active($previous_db); // now use drupal database
  1475. $pager = theme('pager');
  1476. // prepare the query that will lookup node ids
  1477. $sql = "SELECT nid FROM {chado_feature} ".
  1478. "WHERE feature_id = %d";
  1479. $i=0;
  1480. $features = array();
  1481. while ($feature = db_fetch_object($org_features)) {
  1482. $node = db_fetch_object(db_query($sql, $feature->feature_id));
  1483. $feature->nid = $node->nid;
  1484. $features[$i++] = $feature;
  1485. }
  1486. return array( 'features' => $features, 'pager' => $pager, 'enabled' => TRUE );
  1487. }
  1488. /**
  1489. * This generates the Feature Browse which can optionally be included on library pages
  1490. * and shows all features belonging to the given library. This Browse can be shown/hidden
  1491. * on the Feature Configuration page.
  1492. *
  1493. * @ingroup tripal_feature
  1494. */
  1495. function tripal_feature_load_analysis_feature_browser($analysis) {
  1496. // don't show the browser if the settings in the admin page is turned off
  1497. // instead return the array indicating the status of the browser
  1498. $show_browser = variable_get('tripal_analysis_feature_browse_setting', 'show_feature_browser');
  1499. if (strcmp($show_browser, 'show_feature_browser')!=0) {
  1500. return array('enabled' => FALSE);
  1501. }
  1502. // get a list of feature types to include in the browser
  1503. $allowed_types = variable_get('chado_browser_feature_types', 'EST contig');
  1504. $allowed_types = preg_replace("/[\s\n\r]+/", " ", $allowed_types);
  1505. $so_terms = split(' ', $allowed_types);
  1506. $where_cvt = "";
  1507. foreach ($so_terms as $term) {
  1508. $where_cvt .= "CVT.name = '$term' OR ";
  1509. }
  1510. $where_cvt = drupal_substr($where_cvt, 0, drupal_strlen($where_cvt)-3); # strip trailing 'OR'
  1511. // get the features for this library
  1512. $sql = "SELECT F.name,F.feature_id,F.uniquename,CVT.name as cvname ".
  1513. "FROM {feature} F ".
  1514. " INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id ".
  1515. " INNER JOIN {analysisfeature} AF on F.feature_id = AF.feature_id ".
  1516. " INNER JOIN {analysis} A on AF.analysis_id = A.analysis_id ".
  1517. "WHERE A.analysis_id = %d and ($where_cvt) ".
  1518. "ORDER BY feature_id ASC";
  1519. // the counting SQL
  1520. $csql = "SELECT count(*) ".
  1521. "FROM {feature} F".
  1522. " INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id ".
  1523. " INNER JOIN {analysisfeature} AF on F.feature_id = AF.feature_id ".
  1524. " INNER JOIN {analysis} A on AF.analysis_id = A.analysis_id ".
  1525. "WHERE A.analysis_id = %d and ($where_cvt) ".
  1526. "GROUP BY A.analysis_id ";
  1527. $previous_db = tripal_db_set_active('chado'); // use chado database
  1528. $org_features = pager_query($sql, 10, 0, $csql, $analysis->analysis_id);
  1529. tripal_db_set_active($previous_db); // now use drupal database
  1530. $pager = theme('pager');
  1531. // prepare the query that will lookup node ids
  1532. $sql = "SELECT nid FROM {chado_feature} ".
  1533. "WHERE feature_id = %d";
  1534. $i=0;
  1535. $features = array();
  1536. while ($feature = db_fetch_object($org_features)) {
  1537. $node = db_fetch_object(db_query($sql, $feature->feature_id));
  1538. $feature->nid = $node->nid;
  1539. $features[$i++] = $feature;
  1540. }
  1541. return array( 'features' => $features, 'pager' => $pager, 'enabled' => TRUE );
  1542. }
  1543. /**
  1544. * used to sort the list of relationship objects by start position
  1545. *
  1546. * @ingroup tripal_feature
  1547. */
  1548. function tripal_feature_sort_rel_objects($a, $b) {
  1549. return strnatcmp($a->fmin, $b->fmin);
  1550. }
  1551. /**
  1552. * used to sort the list of relationship parts by start position
  1553. *
  1554. * @ingroup tripal_feature
  1555. */
  1556. function tripal_feature_sort_rel_parts_by_start($a, $b) {
  1557. foreach ($a as $type_name => $details) {
  1558. $astart = $a[$type_name]['start'];
  1559. break;
  1560. }
  1561. foreach ($b as $type_name => $details) {
  1562. $bstart = $b[$type_name]['start'];
  1563. break;
  1564. }
  1565. return strnatcmp($astart, $bstart);
  1566. }
  1567. /**
  1568. * used to sort the list of relationship parts by start position
  1569. *
  1570. * @ingroup tripal_feature
  1571. */
  1572. function tripal_feature_sort_rel_parts_by_end($a, $b) {
  1573. $val = strnatcmp($b['end'], $a['end']);
  1574. if ($val == 0) {
  1575. return strcmp($a['type'], $b['type']);
  1576. }
  1577. return $val;
  1578. }
  1579. /**
  1580. *
  1581. *
  1582. * @ingroup tripal_feature
  1583. */
  1584. function tripal_feature_color_sequence($sequence, $parts, $defline) {
  1585. $types = array();
  1586. // first get the list of types so we can create a color legend
  1587. foreach ($parts as $index => $t) {
  1588. foreach ($t as $type_name => $details) {
  1589. $types[$type_name] = 1;
  1590. }
  1591. }
  1592. $newseq .= "<div id=\"tripal_feature-featureloc_sequence-legend\">Legend: ";
  1593. foreach ($types as $type_name => $present) {
  1594. $newseq .= "<span id=\"tripal_feature-legend-$type_name\" class=\"tripal_feature-legend-item tripal_feature-featureloc_sequence-$type_name\" script=\"\">$type_name</span>";
  1595. }
  1596. $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.";
  1597. // set the background color of the rows based on the type
  1598. $pos = 0;
  1599. $newseq .= "<pre id=\"tripal_feature-featureloc_sequence\">";
  1600. $newseq .= ">$defline\n";
  1601. // iterate through the parts. They should be in order.
  1602. $ends = array();
  1603. foreach ($parts as $index => $types) {
  1604. // get the start for this part. All types in this part start at the
  1605. // same position so we only need the first record
  1606. foreach ($types as $type => $child) {
  1607. $start = $child['start'];
  1608. break;
  1609. }
  1610. // add in the sequence up to the start of this part
  1611. for ($i = $pos; $i < $start; $i++) {
  1612. $newseq .= $sequence{$pos};
  1613. $seqcount++;
  1614. if ($seqcount % 50 == 0) {
  1615. $newseq .= "\n";
  1616. }
  1617. if (array_key_exists($pos, $ends)) {
  1618. foreach ($ends[$pos] as $end) {
  1619. $newseq .= "</span>";
  1620. }
  1621. }
  1622. $pos++;
  1623. }
  1624. // we want to sort the parts by their end. We want the span tag to
  1625. // to be added in the order the parts end.
  1626. usort($types, 'tripal_feature_sort_rel_parts_by_end');
  1627. // now add the child span for all types that start at this position
  1628. foreach ($types as $type) {
  1629. $class = "tripal_feature-featureloc_sequence-" . $type['type'];
  1630. $newseq .= "<span class=\"$class\">";
  1631. // add the end position
  1632. $end = $type['end'];
  1633. $ends[$end][] = $end;
  1634. }
  1635. }
  1636. // add in rest of the sequence
  1637. for ($i = $pos; $i <= strlen($sequence); $i++) {
  1638. $newseq .= $sequence{$pos};
  1639. $seqcount++;
  1640. if ($seqcount % 50 == 0) {
  1641. $newseq .= "\n";
  1642. }
  1643. if (array_key_exists($pos, $ends)) {
  1644. foreach ($ends[$pos] as $end) {
  1645. $newseq .= "</span>";
  1646. }
  1647. }
  1648. $pos++;
  1649. }
  1650. $newseq .= "</pre>";
  1651. return $newseq;
  1652. }
  1653. /**
  1654. * This function customizes the view of the chado_feature node. It allows
  1655. * us to generate the markup.
  1656. *
  1657. * @ingroup tripal_feature
  1658. */
  1659. function chado_feature_view($node, $teaser = FALSE, $page = FALSE) {
  1660. if (!$teaser) {
  1661. // use drupal's default node view:
  1662. $node = node_prepare($node, $teaser);
  1663. // if we're building the node for searching then
  1664. // we want to handle this within the module and
  1665. // not allow theme customization. We don't want to
  1666. // index all items (such as DNA sequence).
  1667. if ($node->build_mode == NODE_BUILD_SEARCH_INDEX) {
  1668. $node->content['index_version'] = array(
  1669. '#value' => theme('tripal_feature_search_index', $node),
  1670. '#weight' => 1,
  1671. );
  1672. }
  1673. elseif ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
  1674. $node->content['index_version'] = array(
  1675. '#value' => theme('tripal_feature_search_results', $node),
  1676. '#weight' => 1,
  1677. );
  1678. }
  1679. else {
  1680. // do nothing here, let the theme derived template handle display
  1681. }
  1682. }
  1683. return $node;
  1684. }
  1685. /**
  1686. * Display feature information for associated organisms. This function also
  1687. * provides contents for indexing
  1688. *
  1689. * @ingroup tripal_feature
  1690. */
  1691. function tripal_feature_nodeapi(&$node, $op, $teaser, $page) {
  1692. switch ($op) {
  1693. // Note that this function only adds feature view to an organism node.
  1694. // The view of a feature node is controled by the theme *.tpl file
  1695. case 'view':
  1696. switch ($node->type) {
  1697. case 'chado_organism':
  1698. // Show feature browser
  1699. $types_to_show = array('chado_organism', 'chado_library');
  1700. if (in_array($node->type, $types_to_show, TRUE)) {
  1701. $node->content['tripal_organism_feature_counts'] = array(
  1702. '#value' => theme('tripal_organism_feature_counts', $node),
  1703. );
  1704. $node->content['tripal_organism_feature_browser'] = array(
  1705. '#value' => theme('tripal_organism_feature_browser', $node),
  1706. );
  1707. }
  1708. break;
  1709. }
  1710. break;
  1711. }
  1712. }
  1713. /**
  1714. * We need to let drupal know about our theme functions and their arguments.
  1715. * We create theme functions to allow users of the module to customize the
  1716. * look and feel of the output generated in this module
  1717. *
  1718. * @ingroup tripal_feature
  1719. */
  1720. function tripal_feature_theme() {
  1721. return array(
  1722. 'tripal_feature_search_index' => array(
  1723. 'arguments' => array('node'),
  1724. ),
  1725. 'tripal_feature_search_results' => array(
  1726. 'arguments' => array('node'),
  1727. ),
  1728. 'tripal_organism_feature_browser' => array(
  1729. 'arguments' => array('node' => NULL),
  1730. 'template' => 'tripal_organism_feature_browser',
  1731. ),
  1732. 'tripal_organism_feature_counts' => array(
  1733. 'arguments' => array('node' => NULL),
  1734. 'template' => 'tripal_organism_feature_counts',
  1735. ),
  1736. 'tripal_feature_base' => array(
  1737. 'arguments' => array('node' => NULL),
  1738. 'template' => 'tripal_feature_base',
  1739. ),
  1740. 'tripal_feature_sequence' => array(
  1741. 'arguments' => array('node' => NULL),
  1742. 'template' => 'tripal_feature_sequence',
  1743. ),
  1744. 'tripal_feature_synonyms' => array(
  1745. 'arguments' => array('node' => NULL),
  1746. 'template' => 'tripal_feature_synonyms',
  1747. ),
  1748. 'tripal_feature_featureloc_sequences' => array(
  1749. 'arguments' => array('node' => NULL),
  1750. 'template' => 'tripal_feature_featureloc_sequences',
  1751. ),
  1752. 'tripal_feature_references' => array(
  1753. 'arguments' => array('node' => NULL),
  1754. 'template' => 'tripal_feature_references',
  1755. ),
  1756. 'tripal_feature_properties' => array(
  1757. 'arguments' => array('node' => NULL),
  1758. 'template' => 'tripal_feature_properties',
  1759. ),
  1760. 'tripal_feature_alignments' => array(
  1761. 'arguments' => array('node' => NULL),
  1762. 'template' => 'tripal_feature_alignments',
  1763. ),
  1764. 'tripal_feature_relationships' => array(
  1765. 'arguments' => array('node' => NULL),
  1766. 'template' => 'tripal_feature_relationships',
  1767. ),
  1768. 'tripal_feature_edit_ALL_properties_form' => array(
  1769. 'arguments' => array('form' => NULL),
  1770. 'function' => 'theme_tripal_feature_edit_ALL_properties_form',
  1771. ),
  1772. );
  1773. }
  1774. /**
  1775. *
  1776. *
  1777. * @ingroup tripal_feature
  1778. */
  1779. function tripal_feature_preprocess_tripal_feature_relationships(&$variables) {
  1780. // we want to provide a new variable that contains the matched features.
  1781. $feature = $variables['node']->feature;
  1782. // expand the feature object to include the feature relationships.
  1783. $options = array(
  1784. 'return_array' => 1,
  1785. 'order_by' => array('rank' => 'ASC'),
  1786. );
  1787. $feature = tripal_core_expand_chado_vars($feature, 'table',
  1788. 'feature_relationship', $options);
  1789. // get the subject relationships
  1790. $srelationships = $feature->feature_relationship->subject_id;
  1791. $orelationships = $feature->feature_relationship->object_id;
  1792. // get alignment as child. The $feature->featureloc element
  1793. // is already populated from the alignment preprocess function
  1794. $feature = tripal_core_expand_chado_vars($feature, 'table', 'featureloc');
  1795. $cfeaturelocs = $feature->featureloc->feature_id;
  1796. if (!$cfeaturelocs) {
  1797. $cfeaturelocs = array();
  1798. }
  1799. elseif (!is_array($cfeaturelocs)) {
  1800. $cfeaturelocs = array($cfeaturelocs);
  1801. }
  1802. // prepare the SQL statement to get the featureloc for the
  1803. // feature in the relationships.
  1804. $connection = tripal_db_persistent_chado();
  1805. $psql = "
  1806. PREPARE sel_featureloc_preprocess_relationships (int, int) AS
  1807. SELECT
  1808. FL.featureloc_id, F.name as srcfeature_name, FL.srcfeature_id,
  1809. FL.feature_id, FL.fmin, FL.fmax, FL.strand, FL.phase
  1810. FROM featureloc FL
  1811. INNER JOIN feature F ON F.feature_id = FL.srcfeature_id
  1812. WHERE FL.feature_id = $1 and FL.srcfeature_id = $2
  1813. ";
  1814. tripal_core_chado_prepare('sel_featureloc_preprocess_relationships', $psql, array('int', 'int'));
  1815. // combine both object and subject relationshisp into a single array
  1816. $relationships = array();
  1817. $relationships['object'] = array();
  1818. $relationships['subject'] = array();
  1819. // iterate through the object relationships
  1820. if ($orelationships) {
  1821. foreach ($orelationships as $relationship) {
  1822. $rel = new stdClass();
  1823. // get locations where the child feature and this feature overlap with the
  1824. // same landmark feature.
  1825. $rel->child_featurelocs = array();
  1826. foreach ($cfeaturelocs as $featureloc) {
  1827. $res = chado_query("EXECUTE sel_featureloc_preprocess_relationships (%d, %d)",
  1828. $relationship->subject_id->feature_id,
  1829. $featureloc->srcfeature_id->feature_id);
  1830. while ($loc = db_fetch_object($res)) {
  1831. // add in the node id of the src feature if it exists and save this location
  1832. $loc->nid = $featureloc->srcfeature_id->nid;
  1833. $rel->child_featurelocs[] = $loc;
  1834. }
  1835. }
  1836. $rel->record = $relationship;
  1837. // get the relationship and child types
  1838. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  1839. $child_type = $relationship->subject_id->type_id->name;
  1840. // get the node id of the subject
  1841. $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = %d";
  1842. $n = db_fetch_object(db_query($sql, $relationship->subject_id->feature_id));
  1843. if ($n) {
  1844. $rel->record->nid = $n->nid;
  1845. }
  1846. if (!array_key_exists($rel_type, $relationships['object'])) {
  1847. $relationships['object'][$rel_type] = array();
  1848. }
  1849. if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
  1850. $relationships['object'][$rel_type][$child_type] = array();
  1851. }
  1852. $relationships['object'][$rel_type][$child_type][] = $rel;
  1853. }
  1854. }
  1855. // now add in the subject relationships
  1856. if ($srelationships) {
  1857. foreach ($srelationships as $relationship) {
  1858. $rel = new stdClass();
  1859. // get locations where this feature overlaps with the parent
  1860. $rel->parent_featurelocs = array();
  1861. foreach ($cfeaturelocs as $featureloc) {
  1862. $res = chado_query("EXECUTE sel_featureloc_preprocess_relationships (%d, %d)",
  1863. $relationship->object_id->feature_id,
  1864. $featureloc->srcfeature_id->feature_id);
  1865. while ($loc = db_fetch_object($res)) {
  1866. // add in the node id of the src feature if it exists and save this location
  1867. $loc->nid = $featureloc->srcfeature_id->nid;
  1868. $rel->parent_featurelocs[] = $loc;
  1869. }
  1870. }
  1871. $rel->record = $relationship;
  1872. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  1873. $parent_type = $relationship->object_id->type_id->name;
  1874. // get the node id of the subject
  1875. $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = %d";
  1876. $n = db_fetch_object(db_query($sql, $relationship->object_id->feature_id));
  1877. if ($n) {
  1878. $rel->record->nid = $n->nid;
  1879. }
  1880. if (!array_key_exists($rel_type, $relationships['subject'])) {
  1881. $relationships['subject'][$rel_type] = array();
  1882. }
  1883. if (!array_key_exists($child_type, $relationships['subject'][$rel_type])) {
  1884. $relationships['subject'][$rel_type][$parent_type] = array();
  1885. }
  1886. $relationships['subject'][$rel_type][$parent_type][] = $rel;
  1887. }
  1888. }
  1889. $feature->all_relationships = $relationships;
  1890. }
  1891. /**
  1892. *
  1893. *
  1894. * @ingroup tripal_feature
  1895. */
  1896. function tripal_feature_preprocess_tripal_feature_alignments(&$variables) {
  1897. // we want to provide a new variable that contains the matched features.
  1898. $feature = $variables['node']->feature;
  1899. $feature = tripal_core_expand_chado_vars($feature, 'table', 'featureloc');
  1900. // get alignments as child
  1901. $cfeaturelocs = $feature->featureloc->feature_id;
  1902. if (!$cfeaturelocs) {
  1903. $cfeaturelocs = array();
  1904. }
  1905. elseif (!is_array($cfeaturelocs)) {
  1906. $cfeaturelocs = array($cfeaturelocs);
  1907. }
  1908. // get alignment as parent
  1909. $pfeaturelocs = $feature->featureloc->srcfeature_id;
  1910. if (!$pfeaturelocs) {
  1911. $pfeaturelocs = array();
  1912. }
  1913. elseif (!is_array($pfeaturelocs)) {
  1914. $pfeaturelocs = array($pfeaturelocs);
  1915. }
  1916. // get matched alignments (those with an itermediate 'match' or 'EST_match', etc
  1917. $mfeaturelocs = tripal_feature_get_matched_alignments($feature);
  1918. $feature->matched_featurelocs = tripal_feature_get_matched_alignments($feature);
  1919. // combine all three alignments into a single array for printing together in
  1920. // a single list
  1921. $alignments = array();
  1922. foreach ($pfeaturelocs as $featureloc) {
  1923. // if type is a 'match' then ignore it. We will handle those below
  1924. if (preg_match('/(^match$|^.*?_match)$/', $featureloc->feature_id->type_id->name)) {
  1925. continue;
  1926. }
  1927. $alignment = new stdClass();
  1928. $alignment->record = $featureloc;
  1929. $alignment->name = $featureloc->feature_id->name;
  1930. $alignment->nid = $featureloc->feature_id->nid;
  1931. $alignment->type = $featureloc->feature_id->type_id->name;
  1932. $alignment->fmin = $featureloc->fmin;
  1933. $alignment->fmax = $featureloc->fmax;
  1934. $alignment->phase = $featureloc->phase;
  1935. $alignment->strand = $featureloc->strand;
  1936. $alignments[] = $alignment;
  1937. }
  1938. foreach ($cfeaturelocs as $featureloc) {
  1939. // if type is a 'match' then ignore it. We will handle those below
  1940. if (preg_match('/(^match$|^.*?_match)$/', $featureloc->feature_id->type_id->name)) {
  1941. continue;
  1942. }
  1943. $alignment = new stdClass();
  1944. $alignment->record = $featureloc;
  1945. $alignment->name = $featureloc->srcfeature_id->name;
  1946. $alignment->nid = $featureloc->srcfeature_id->nid;
  1947. $alignment->type = $featureloc->srcfeature_id->type_id->name;
  1948. $alignment->fmin = $featureloc->fmin;
  1949. $alignment->is_fmin_partial = $featureloc->is_fmin_partial;
  1950. $alignment->fmax = $featureloc->fmax;
  1951. $alignment->is_fmax_partial = $featureloc->is_fmax_partial;
  1952. $alignment->phase = $featureloc->phase;
  1953. $alignment->strand = $featureloc->strand;
  1954. $alignments[] = $alignment;
  1955. }
  1956. // in matching features, the left feature is always the feature
  1957. // provided to this function.
  1958. foreach ($mfeaturelocs as $featureloc) {
  1959. // get more information about the right feature
  1960. $select = array('feature_id' => $featureloc->right_srcfeature_id);
  1961. $rfeature = tripal_core_generate_chado_var('feature', $select);
  1962. // now add to the list
  1963. $alignment = new stdClass();
  1964. $alignment->record = $featureloc;
  1965. $alignment->right_feature = $rfeature;
  1966. $alignment->name = $rfeature->name;
  1967. $alignment->nid = $rfeature->nid;
  1968. $alignment->type = $rfeature->type_id->name;
  1969. $alignment->fmin = $featureloc->left_fmin;
  1970. $alignment->is_fmin_partial = $featureloc->left_is_fmin_partial;
  1971. $alignment->fmax = $featureloc->left_fmax;
  1972. $alignment->is_fmax_partial = $featureloc->left_is_fmax_partial;
  1973. $alignment->phase = $featureloc->left_phase;
  1974. $alignment->strand = $featureloc->left_strand;
  1975. $alignment->right_fmin = $featureloc->right_fmin;
  1976. $alignment->right_is_fmin_partial = $featureloc->right_is_fmin_partial;
  1977. $alignment->right_fmax = $featureloc->right_fmax;
  1978. $alignment->right_is_fmax_partial = $featureloc->right_is_fmax_partial;
  1979. $alignment->right_phase = $featureloc->right_phase;
  1980. $alignment->right_strand = $featureloc->right_strand;
  1981. $alignments[] = $alignment;
  1982. }
  1983. $feature->all_featurelocs = $alignments;
  1984. }
  1985. /**
  1986. *
  1987. *
  1988. * @ingroup tripal_feature
  1989. */
  1990. function tripal_feature_preprocess_tripal_organism_feature_counts(&$variables) {
  1991. $organism = $variables['node']->organism;
  1992. $organism->feature_counts = tripal_feature_load_organism_feature_counts($organism);
  1993. }
  1994. /**
  1995. *
  1996. *
  1997. * @ingroup tripal_feature
  1998. */
  1999. function tripal_feature_preprocess_tripal_organism_feature_browser(&$variables) {
  2000. $organism = $variables['node']->organism;
  2001. $organism->feature_browser = tripal_feature_load_organism_feature_browser($organism);
  2002. }
  2003. /**
  2004. * Preprocessor function for the Library Feature Browser
  2005. *
  2006. * @ingroup tripal_feature
  2007. */
  2008. function tripal_feature_preprocess_tripal_library_feature_browser(&$variables) {
  2009. $library = $variables['node']->library;
  2010. $library->feature_browser = tripal_feature_load_library_feature_browser($library);
  2011. }
  2012. /**
  2013. * Preprocessor function for the Analysis Feature Browser
  2014. *
  2015. * @ingroup tripal_feature
  2016. */
  2017. function tripal_feature_preprocess_tripal_analysis_feature_browser(&$variables) {
  2018. $analysis = $variables['node']->analysis;
  2019. $analysis->feature_browser = tripal_feature_load_analysis_feature_browser($analysis);
  2020. }
  2021. /**
  2022. *
  2023. *
  2024. * @ingroup tripal_feature
  2025. */
  2026. function tripal_feature_cv_chart($chart_id) {
  2027. // we only want the chart to show feature types setup by the admin
  2028. $temp = rtrim(variable_get('tripal_feature_summary_report_mapping', ''));
  2029. $where = '';
  2030. if ($temp) {
  2031. $temp = explode("\n", $temp);
  2032. foreach ($temp as $key => $value) {
  2033. $temp2 = explode("=", $value);
  2034. $feature_type = rtrim($temp2[0]);
  2035. $where .= "CNT.feature_type = '$feature_type' OR \n";
  2036. }
  2037. if ($where) {
  2038. $where = drupal_substr($where, 0, -5); # remove OR from the end
  2039. $where = "($where) AND";
  2040. }
  2041. }
  2042. $organism_id = preg_replace("/^tripal_feature_cv_chart_(\d+)$/", "$1", $chart_id);
  2043. // The CV module will create the JSON array necessary for buillding a
  2044. // pie chart using jgChart and Google Charts. We have to pass to it
  2045. // a table that contains count information, tell it which column
  2046. // contains the cvterm_id and provide a filter for getting the
  2047. // results we want from the table.
  2048. $options = array(
  2049. count_mview => 'organism_feature_count',
  2050. cvterm_id_column => 'cvterm_id',
  2051. count_column => 'num_features',
  2052. size => '550x200',
  2053. filter => "$where CNT.organism_id = $organism_id",
  2054. );
  2055. return $options;
  2056. }
  2057. /**
  2058. *
  2059. *
  2060. * @ingroup tripal_feature
  2061. */
  2062. function tripal_feature_cv_tree($tree_id) {
  2063. // The CV module will create the JSON array necessary for buillding a
  2064. // pie chart using jgChart and Google Charts. We have to pass to it
  2065. // a table that contains count information, tell it which column
  2066. // contains the cvterm_id and provide a filter for getting the
  2067. // results we want from the table.
  2068. $organism_id = preg_replace("/^tripal_feature_cv_tree_(\d+)$/", "$1", $tree_id);
  2069. $options = array(
  2070. cv_id => tripal_cv_get_cv_id('sequence'),
  2071. count_mview => 'organism_feature_count',
  2072. cvterm_id_column => 'cvterm_id',
  2073. count_column => 'num_features',
  2074. filter => "CNT.organism_id = $organism_id",
  2075. label => 'Features',
  2076. );
  2077. return $options;
  2078. }
  2079. /**
  2080. * This function is an extension of the chado_feature_view by providing
  2081. * the markup for the feature object THAT WILL BE INDEXED.
  2082. *
  2083. * @ingroup tripal_feature
  2084. */
  2085. function theme_tripal_feature_search_index($node) {
  2086. $feature = $node->feature;
  2087. $content = '';
  2088. // get the accession prefix
  2089. $aprefix = variable_get('chado_feature_accession_prefix', 'ID');
  2090. $content .= "<h1>$feature->uniquename</h1>. ";
  2091. $content .= "<strong>$aprefix$feature->feature_id.</strong> ";
  2092. $content .= "$feature->cvname ";
  2093. $content .= "$feature->common_name ";
  2094. // add the synonyms of this feature to the text for searching
  2095. $synonyms = $node->synonyms;
  2096. if (count($synonyms) > 0) {
  2097. foreach ($synonyms as $result) {
  2098. $content .= "$result->name ";
  2099. }
  2100. }
  2101. return $content;
  2102. }
  2103. /**
  2104. * This function is an extension of the chado_feature_view by providing
  2105. * the markup for the feature object THAT WILL BE INDEXED.
  2106. *
  2107. * @ingroup tripal_feature
  2108. */
  2109. function theme_tripal_feature_search_results($node) {
  2110. $feature = $node->feature;
  2111. $content = '';
  2112. // get the accession prefix
  2113. $aprefix = variable_get('chado_feature_accession_prefix', 'ID');
  2114. $content .= "Feature Name: <h1>$feature->uniquename</h1>. ";
  2115. $content .= "<strong>Accession: $aprefix$feature->feature_id.</strong>";
  2116. $content .= "Type: $feature->cvname. ";
  2117. $content .= "Organism: $feature->common_name. ";
  2118. // add the synonyms of this feature to the text for searching
  2119. $synonyms = $node->synonyms;
  2120. if (count($synonyms) > 0) {
  2121. $content .= "Synonyms: ";
  2122. foreach ($synonyms as $result) {
  2123. $content .= "$result->name, ";
  2124. }
  2125. }
  2126. return $content;
  2127. }
  2128. /**
  2129. *
  2130. *
  2131. * @ingroup tripal_feature
  2132. */
  2133. function tripal_feature_set_vocabulary() {
  2134. //include the file containing the required functions for adding taxonomy vocabs
  2135. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  2136. // get the vocabularies so that we make sure we don't recreate
  2137. // the vocabs that already exist
  2138. $vocabularies = taxonomy_get_vocabularies();
  2139. $ft_vid = NULL;
  2140. $op_vid = NULL;
  2141. $lb_vid = NULL;
  2142. $an_vid = NULL;
  2143. // These taxonomic terms are hard coded because we
  2144. // konw we have these relationships in the chado tables
  2145. // through foreign key relationships. The tripal
  2146. // modules that correspond to these chado "modules" don't
  2147. // need to be installed for the taxonomy to work.
  2148. foreach ($vocabularies as $vocab) {
  2149. if ($vocab->name == 'Feature Type') {
  2150. $ft_vid = $vocab->vid;
  2151. }
  2152. if ($vocab->name == 'Organism') {
  2153. $op_vid = $vocab->vid;
  2154. }
  2155. if ($vocab->name == 'Library') {
  2156. $lb_vid = $vocab->vid;
  2157. }
  2158. if ($vocab->name == 'Analysis') {
  2159. $an_vid = $vocab->vid;
  2160. }
  2161. }
  2162. if (!$ft_vid) {
  2163. $form_state = array();
  2164. $values = array(
  2165. 'name' => t('Feature Type'),
  2166. 'nodes' => array('chado_feature' => 'chado_feature'),
  2167. 'description' => t('The feature type (or SO cvterm for this feature).'),
  2168. 'help' => t('Select the term that matches the feature'),
  2169. 'tags' => 0,
  2170. 'hierarchy' => 1,
  2171. 'relations' => 1,
  2172. 'multiple' => 0,
  2173. 'required' => 0,
  2174. 'weight' => 1,
  2175. );
  2176. drupal_execute('taxonomy_form_vocabulary', $form_state, $values);
  2177. drupal_execute('taxonomy_form_vocabulary', $form_state);
  2178. }
  2179. if (!$op_vid) {
  2180. $form_state = array();
  2181. $values = array(
  2182. 'name' => t('Organism'),
  2183. 'nodes' => array('chado_feature' => 'chado_feature'),
  2184. 'description' => t('The organism to which this feature belongs.'),
  2185. 'help' => t('Select the term that matches the feature'),
  2186. 'tags' => 0,
  2187. 'hierarchy' => 1,
  2188. 'relations' => 1,
  2189. 'multiple' => 0,
  2190. 'required' => 0,
  2191. 'weight' => 2,
  2192. );
  2193. drupal_execute('taxonomy_form_vocabulary', $form_state, $values);
  2194. drupal_execute('taxonomy_form_vocabulary', $form_state);
  2195. }
  2196. if (!$lb_vid) {
  2197. $form_state = array();
  2198. $values = array(
  2199. 'name' => t('Library'),
  2200. 'nodes' => array('chado_feature' => 'chado_feature'),
  2201. 'description' => t('Chado features associated with a library are assigned the term associated with the library'),
  2202. 'help' => t('Select the term that matches the feature'),
  2203. 'tags' => 0,
  2204. 'hierarchy' => 1,
  2205. 'relations' => 1,
  2206. 'multiple' => 0,
  2207. 'required' => 0,
  2208. 'weight' => 3,
  2209. );
  2210. drupal_execute('taxonomy_form_vocabulary', $form_state, $values);
  2211. drupal_execute('taxonomy_form_vocabulary', $form_state);
  2212. }
  2213. if (!$an_vid) {
  2214. $form_state = array();
  2215. $values = array(
  2216. 'name' => t('Analysis'),
  2217. 'nodes' => array('chado_feature' => 'chado_feature'),
  2218. 'description' => t('Any analysis to which this feature belongs.'),
  2219. 'help' => t('Select the term that matches the feature'),
  2220. 'tags' => 0,
  2221. 'hierarchy' => 1,
  2222. 'relations' => 1,
  2223. 'multiple' => 1,
  2224. 'required' => 0,
  2225. 'weight' => 4,
  2226. );
  2227. drupal_execute('taxonomy_form_vocabulary', $form_state, $values);
  2228. drupal_execute('taxonomy_form_vocabulary', $form_state);
  2229. }
  2230. }
  2231. /**
  2232. *
  2233. *
  2234. * @ingroup tripal_feature
  2235. */
  2236. function tripal_feature_del_vocabulary() {
  2237. //include the file containing the required functions for adding taxonomy vocabs
  2238. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  2239. // get the vocabularies
  2240. $vocabularies = taxonomy_get_vocabularies();
  2241. // These taxonomic terms are hard coded because we
  2242. // know we have these relationships in the chado tables
  2243. // through foreign key relationships. The tripal
  2244. // modules that correspond to these chado "modules" don't
  2245. // need to be installed for the taxonomy to work.
  2246. foreach ($vocabularies as $vocab) {
  2247. if ($vocab->name == 'Feature Type') {
  2248. taxonomy_del_vocabulary($vocab->vid);
  2249. }
  2250. if ($vocab->name == 'Organism') {
  2251. taxonomy_del_vocabulary($vocab->vid);
  2252. }
  2253. if ($vocab->name == 'Library') {
  2254. taxonomy_del_vocabulary($vocab->vid);
  2255. }
  2256. if ($vocab->name == 'Analysis') {
  2257. taxonomy_del_vocabulary($vocab->vid);
  2258. }
  2259. }
  2260. }
  2261. /**
  2262. *
  2263. *
  2264. * @ingroup tripal_feature
  2265. */
  2266. function tripal_features_set_taxonomy($max_sync = 0, $job_id = NULL) {
  2267. // make sure our vocabularies are cleaned and reset before proceeding
  2268. tripal_feature_del_vocabulary();
  2269. tripal_feature_set_vocabulary();
  2270. // iterate through all drupal feature nodes and set the taxonomy
  2271. $results = db_query("SELECT * FROM {chado_feature}");
  2272. $nsql = "SELECT * FROM {node} ".
  2273. "WHERE nid = %d";
  2274. $i = 0;
  2275. // load into ids array
  2276. $count = 0;
  2277. $chado_features = array();
  2278. while ($chado_feature = db_fetch_object($results)) {
  2279. $chado_features[$count] = $chado_feature;
  2280. $count++;
  2281. }
  2282. // Iterate through features that need to be synced
  2283. $interval = intval($count * 0.01);
  2284. foreach ($chado_features as $chado_feature) {
  2285. // update the job status every 1% features
  2286. if ($job_id and $i % $interval == 0) {
  2287. tripal_job_set_progress($job_id, intval(($i/$count)*100));
  2288. }
  2289. print "$i of $count: ";
  2290. $node = db_fetch_object(db_query($nsql, $chado_feature->nid));
  2291. tripal_feature_set_taxonomy($node, $chado_feature->feature_id);
  2292. $i++;
  2293. }
  2294. }
  2295. /**
  2296. *
  2297. *
  2298. * @ingroup tripal_feature
  2299. */
  2300. function tripal_feature_set_taxonomy($node, $feature_id) {
  2301. // iterate through the taxonomy classes that have been
  2302. // selected by the admin user and make sure we only set those
  2303. $tax_classes = variable_get('tax_classes', '');
  2304. $do_ft = 0;
  2305. $do_op = 0;
  2306. $do_lb = 0;
  2307. $do_an = 0;
  2308. foreach ($tax_classes as $class) {
  2309. if (strcmp($class , 'organism')==0) {
  2310. $do_op = 1;
  2311. }
  2312. if (strcmp($class, 'feature_type')==0) {
  2313. $do_ft = 1;
  2314. }
  2315. if (strcmp($class, 'library')==0) {
  2316. $do_lb = 1;
  2317. }
  2318. if (strcmp($class, 'analysis')==0) {
  2319. $do_an = 1;
  2320. }
  2321. }
  2322. // get the list of vocabularies and find our two vocabularies of interest
  2323. $vocabularies = taxonomy_get_vocabularies();
  2324. $ft_vid = NULL;
  2325. $op_vid = NULL;
  2326. $lb_vid = NULL;
  2327. $an_vid = NULL;
  2328. foreach ($vocabularies as $vocab) {
  2329. if ($vocab->name == 'Feature Type') {
  2330. $ft_vid = $vocab->vid;
  2331. }
  2332. if ($vocab->name == 'Organism') {
  2333. $op_vid = $vocab->vid;
  2334. }
  2335. if ($vocab->name == 'Library') {
  2336. $lb_vid = $vocab->vid;
  2337. }
  2338. if ($vocab->name == 'Analysis') {
  2339. $an_vid = $vocab->vid;
  2340. }
  2341. }
  2342. // get the cvterm and the organism for this feature
  2343. $sql = "SELECT CVT.name AS cvname, O.genus, O.species ".
  2344. "FROM {CVTerm} CVT ".
  2345. " INNER JOIN Feature F on F.type_id = CVT.cvterm_id ".
  2346. " INNER JOIN Organism O ON F.organism_id = O.organism_id ".
  2347. "WHERE F.feature_id = $feature_id";
  2348. $feature = db_fetch_object(chado_query($sql));
  2349. // Set the feature type for this feature
  2350. if ($do_ft && $ft_vid) {
  2351. $tags["$ft_vid"] = "$feature->cvname";
  2352. }
  2353. // Set the organism for this feature type
  2354. if ($do_op && $op_vid) {
  2355. $tags["$op_vid"] = "$feature->genus $feature->species";
  2356. }
  2357. // get the library that this feature may belong to and add it as taxonomy
  2358. if ($do_lb && $lb_vid) {
  2359. $sql = "SELECT L.name ".
  2360. "FROM {Library} L ".
  2361. " INNER JOIN Library_feature LF ON LF.library_id = L.library_id ".
  2362. "WHERE LF.feature_id = %d ";
  2363. $library = db_fetch_object(chado_query($sql, $feature_id));
  2364. $tags["$lb_vid"] = "$library->name";
  2365. }
  2366. // now add the taxonomy to the node
  2367. $terms['tags'] = $tags;
  2368. taxonomy_node_save($node, $terms);
  2369. // print "Setting $node->name: " . implode(", ",$tags) . "\n";
  2370. // get the analysis that this feature may belong to and add it as taxonomy
  2371. // We'll add each one individually since there may be more than one analysis
  2372. if ($do_an && $an_vid) {
  2373. $sql = "SELECT A.name ".
  2374. "FROM {Analysis} A ".
  2375. " INNER JOIN Analysisfeature AF ON AF.analysis_id = A.analysis_id ".
  2376. "WHERE AF.feature_id = $feature_id ";
  2377. $results = chado_query($sql);
  2378. $analysis_terms = array();
  2379. while ($analysis=db_fetch_object($results)) {
  2380. $tags2["$an_vid"] = "$analysis->name";
  2381. $terms['tags'] = $tags2;
  2382. taxonomy_node_save($node, $terms);
  2383. }
  2384. }
  2385. }
  2386. /**
  2387. *
  2388. * Remove orphaned drupal nodes
  2389. *
  2390. * @param $dummy
  2391. * Not Used -kept for backwards compatibility
  2392. * @param $job_id
  2393. * The id of the tripal job executing this function
  2394. *
  2395. * @ingroup tripal_feature
  2396. */
  2397. function tripal_features_cleanup($dummy = NULL, $job_id = NULL) {
  2398. return tripal_core_clean_orphaned_nodes('feature', $job_id);
  2399. }
  2400. /**
  2401. *
  2402. *
  2403. * @ingroup tripal_feature
  2404. */
  2405. function tripal_feature_return_fasta($feature, $desc) {
  2406. $fasta = ">" . variable_get('chado_feature_accession_prefix', 'ID') . "$feature->feature_id|$feature->name";
  2407. $fasta .= " $desc\n";
  2408. $fasta .= wordwrap($feature->residues, 50, "\n", TRUE);
  2409. $fasta .= "\n\n";
  2410. return $fasta;
  2411. }
  2412. /**
  2413. *
  2414. *
  2415. * @ingroup tripal_feature
  2416. */
  2417. function tripal_feature_job_describe_args($callback, $args) {
  2418. $new_args = array();
  2419. if ($callback == 'tripal_feature_load_fasta') {
  2420. $new_args['FASTA file'] = $args[0];
  2421. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[1]));
  2422. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  2423. $new_args['Sequence Type'] = $args[2];
  2424. $new_args['Name Match Type'] = $args[14];
  2425. $new_args['Name RE'] = $args[4];
  2426. $new_args['Unique Name RE'] = $args[5];
  2427. // add in the relationship arguments
  2428. $new_args['Relationship Type'] = $args[8];
  2429. $new_args['Relationship Parent RE'] = $args[9];
  2430. $new_args['Relationship Parent Type'] = $args[10];
  2431. // add in the database reference arguments
  2432. if ($args[7]) {
  2433. $db = tripal_core_chado_select('db', array('name'), array('db_id' => $args[7]));
  2434. }
  2435. $new_args['Database Reference'] = $db[0]->name;
  2436. $new_args['Accession RE'] = $args[6];
  2437. $new_args['Method'] = $args[11];
  2438. // add in the analysis
  2439. if ($args[13]) {
  2440. $analysis = tripal_core_chado_select('analysis', array('name'), array('analysis_id' => $args[13]));
  2441. }
  2442. $new_args['Analysis'] = $analysis[0]->name;
  2443. }
  2444. if ($callback == 'tripal_feature_delete_features') {
  2445. if ($args[0]) {
  2446. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[0]));
  2447. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  2448. }
  2449. else {
  2450. $new_args['Organism'] = '';
  2451. }
  2452. if ($args[1]) {
  2453. $analysis = tripal_core_chado_select('analysis', array('name'), array('analysis_id' => $args[1]));
  2454. $new_args['Analysis'] = $analysis[0]->name;
  2455. }
  2456. else {
  2457. $new_args['Analysis'] = '';
  2458. }
  2459. $new_args['Sequence Type'] = $args[2];
  2460. $new_args['Is Unique Name'] = $args[3];
  2461. $new_args['Features Names'] = $args[4];
  2462. }
  2463. elseif ($callback == 'tripal_feature_load_gff3') {
  2464. $new_args['GFF File'] = $args[0];
  2465. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[1]));
  2466. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  2467. $analysis = tripal_core_chado_select('analysis', array('name'), array('analysis_id' => $args[2]));
  2468. $new_args['Analysis'] = $analysis[0]->name;
  2469. $new_args['Use a Transaction'] = ($args[7] == 1) ? "Yes" : "No";
  2470. $new_args['Import only new features'] = ($args[3] == 1) ? "Yes" : "No";
  2471. $new_args['Import all and update'] = ($args[4] == 1) ? "Yes" : "No";
  2472. $new_args['Import all and replace'] = ($args[5] == 1) ? "Yes" : "No";
  2473. $new_args['Delete features'] = ($args[6] == 1) ? "Yes" : "No";
  2474. }
  2475. if ($callback == 'tripal_feature_sync_features') {
  2476. if ($args[0]) {
  2477. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[0]));
  2478. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  2479. }
  2480. else {
  2481. $new_args['Organism'] = '';
  2482. }
  2483. $new_args['Feature Types'] = $args[1];
  2484. }
  2485. return $new_args;
  2486. }
  2487. /**
  2488. * Implements hook_coder_ignore().
  2489. * Defines the path to the file (tripal_core.coder_ignores.txt) where ignore rules for coder are stored
  2490. */
  2491. function tripal_feature_coder_ignore() {
  2492. return array(
  2493. 'path' => drupal_get_path('module', 'tripal_feature'),
  2494. 'line prefix' => drupal_get_path('module', 'tripal_feature'),
  2495. );
  2496. }
  2497. /*
  2498. * Uses the value provided in the $id argument to find all features that match
  2499. * that ID by name, featurename or synonym. If it matches uniquenly to a single
  2500. * feature it will redirect to that feature page, otherwise, a list of matching
  2501. * features is shown.
  2502. */
  2503. function tripal_feature_match_features_page($id) {
  2504. $sql = "
  2505. SELECT
  2506. F.name, F.uniquename, F.feature_id,
  2507. O.genus, O.species, O.organism_id,
  2508. CVT.cvterm_id, CVT.name as type_name,
  2509. CF.nid,
  2510. array_agg(S.name) as synonyms
  2511. FROM feature F
  2512. INNER JOIN organism O on F.organism_id = O.organism_id
  2513. INNER JOIN cvterm CVT on CVT.cvterm_id = F.type_id
  2514. LEFT JOIN feature_synonym FS on FS.feature_id = F.feature_id
  2515. LEFT JOIN synonym S on S.synonym_id = FS.synonym_id
  2516. INNER JOIN chado_feature CF on CF.feature_id = F.feature_id
  2517. WHERE
  2518. F.uniquename = '%s' or
  2519. F.name = '%s' or
  2520. S.name = '%s'
  2521. GROUP BY F.name, F.uniquename, F.feature_id, O.genus, O.species,
  2522. O.organism_id, CVT.cvterm_id, CVT.name, CF.nid
  2523. ";
  2524. $results = chado_query($sql, $id, $id, $id);
  2525. $num_matches = 0;
  2526. // iterate through the matches and build the table for showing matches
  2527. $header = array('Uniquename', 'Name', 'Type', 'Species', 'Synonyms');
  2528. $rows = array();
  2529. $curr_match;
  2530. while ($match = db_fetch_object($results)) {
  2531. $curr_match = $match;
  2532. $synonyms = $match->synonyms;
  2533. $synonyms = preg_replace('/[\"\{\}]/', '', $synonyms);
  2534. $rows[] = array(
  2535. $match->uniquename,
  2536. "<a href=\"" . url("node/". $match->nid) ."\">" . $match->name . "</a>",
  2537. $match->type_name,
  2538. '<i>' . $match->genus . ' ' . $match->species . '</i>',
  2539. $synonyms,
  2540. );
  2541. $num_matches++;
  2542. }
  2543. // if we have more than one match then generate the table, otherwise, redirect
  2544. // to the matched feature
  2545. if ($num_matches == 1) {
  2546. drupal_goto(url("node/". $curr_match->nid));
  2547. }
  2548. if ($num_matches == 0) {
  2549. return "<p>No features matched the given name '$id'</p>";
  2550. }
  2551. $table_attrs = array(
  2552. 'class' => 'tripal-table tripal-table-horz'
  2553. );
  2554. $output = "<p>The following features match the name '$id'.</p>";
  2555. $output .= theme_table($header, $rows, $table_attrs, $caption);
  2556. return $output;
  2557. }