tripal_feature.module 93 KB

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