tripal_feature.module 88 KB

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