tripal_feature.module 77 KB

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