tripal_feature.module 73 KB

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