tripal_feature.module 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975
  1. <?php
  2. //
  3. // Copyright 2009 Clemson University
  4. //
  5. /*************************************************************************
  6. *
  7. */
  8. function tripal_feature_init(){
  9. // add the jGCharts JS and CSS
  10. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/tripal_feature.js');
  11. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jgcharts/jgcharts.js');
  12. }
  13. /************************************************************************
  14. *
  15. */
  16. function tripal_feature_admin () {
  17. // before proceeding check to see if we have any
  18. // currently processing jobs. If so, we don't want
  19. // to give the opportunity to sync libraries
  20. $active_jobs = FALSE;
  21. if(tripal_get_module_active_jobs('tripal_feature')){
  22. $active_jobs = TRUE;
  23. }
  24. if(!$active_jobs){
  25. $form['chado_feature_accession_prefix'] = array (
  26. '#title' => t('Accession Prefix'),
  27. '#type' => t('textfield'),
  28. '#description' => t("Accession numbers for features consist of the ".
  29. "chado feature_id and a site specific prefix. Set the prefix that ".
  30. "will be incorporated in front of each feature_id to form a unique ".
  31. "accession number for this site."),
  32. '#required' => TRUE,
  33. '#default_value' => variable_get('chado_feature_accession_prefix','ID'),
  34. );
  35. $form['chado_feature_types'] = array(
  36. '#title' => t('Feature Types'),
  37. '#type' => 'textarea',
  38. '#description' => t('Enter the names of the sequence types that the ".
  39. "site will support with independent pages. Pages for these data ".
  40. "types will be built automatically for features that exist in the ".
  41. "chado database. The names listed here should be spearated by ".
  42. "spaces or entered separately on new lines. The names must match ".
  43. "exactly (spelling and case) with terms in the sequence ontology'),
  44. '#required' => TRUE,
  45. '#default_value' => variable_get('chado_feature_types','EST contig'),
  46. );
  47. $form['browser'] = array(
  48. '#type' => 'fieldset',
  49. '#title' => t('Feature Browser')
  50. );
  51. $allowedoptions ['show_feature_browser'] = "Show the feature browser on the organism page. The Browser loads when page loads. This may be slow for large sites.";
  52. $allowedoptions ['hide_feature_browser'] = "Hide the feature browser on the organism page. Disables the feature browser completely.";
  53. // $allowedoptions ['allow_feature_browser'] = "Allow loading of the feature browsing through AJAX. For large sites the initial page load will be quick with the feature browser loading afterwards.";
  54. $form['browser']['browse_features'] = array(
  55. '#title' => 'Feature Browser on Organism Page',
  56. '#description' => 'A feature browser can be added to an organism page to allow users to quickly '.
  57. 'access a feature. This will most likely not be the ideal mechanism for accessing feature '.
  58. 'information, especially for large sites, but it will alow users exploring the site (such '.
  59. 'as students) to better understand the data types available on the site.',
  60. '#type' => 'radios',
  61. '#options' => $allowedoptions,
  62. '#default_value'=>variable_get('tripal_feature_browse_setting',
  63. array('show_feature_browser')),
  64. );
  65. $form['browser']['set_browse_button'] = array(
  66. '#type' => 'submit',
  67. '#value' => t('Set Browser'),
  68. '#weight' => 2,
  69. );
  70. get_tripal_feature_admin_form_sync_set($form);
  71. get_tripal_feature_admin_form_taxonomy_set($form);
  72. get_tripal_feature_admin_form_reindex_set($form);
  73. get_tripal_feature_admin_form_cleanup_set($form);
  74. } else {
  75. $form['notice'] = array(
  76. '#type' => 'fieldset',
  77. '#title' => t('Feature Management Temporarily Unavailable')
  78. );
  79. $form['notice']['message'] = array(
  80. '#value' => t('Currently, feature management jobs are waiting or ".
  81. "are running. Managemment features have been hidden until these ".
  82. "jobs complete. Please check back later once these jobs have ".
  83. "finished. You can view the status of pending jobs in the Tripal ".
  84. "jobs page.'),
  85. );
  86. }
  87. return system_settings_form($form);
  88. }
  89. /************************************************************************
  90. *
  91. */
  92. function tripal_feature_admin_validate($form, &$form_state) {
  93. global $user; // we need access to the user info
  94. $job_args = array();
  95. // if the user wants to sync up the chado features then
  96. // add the job to the management queue
  97. if ($form_state['values']['op'] == t('Sync all Features')) {
  98. tripal_add_job('Sync all features','tripal_feature',
  99. 'tripal_feature_sync_features',$job_args,$user->uid);
  100. }
  101. if ($form_state['values']['op'] == t('Set/Reset Taxonomy for all feature nodes')) {
  102. tripal_add_job('Set all feature taxonomy','tripal_feature',
  103. 'tripal_features_set_taxonomy',$job_args,$user->uid);
  104. }
  105. if ($form_state['values']['op'] == t('Reindex all feature nodes')) {
  106. tripal_add_job('Reindex all features','tripal_feature',
  107. 'tripal_features_reindex',$job_args,$user->uid);
  108. }
  109. if ($form_state['values']['op'] == t('Clean up orphaned features')) {
  110. tripal_add_job('Cleanup orphaned features','tripal_feature',
  111. 'tripal_features_cleanup',$job_args,$user->uid);
  112. }
  113. if ($form_state['values']['op'] == t('Set Browser')) {
  114. variable_set('tripal_feature_browse_setting',$form_state['values']['browse_features']);
  115. }
  116. }
  117. /************************************************************************
  118. *
  119. */
  120. function get_tripal_feature_admin_form_cleanup_set(&$form) {
  121. $form['cleanup'] = array(
  122. '#type' => 'fieldset',
  123. '#title' => t('Clean Up')
  124. );
  125. $form['cleanup']['description'] = array(
  126. '#type' => 'item',
  127. '#value' => t("With Drupal and chado residing in different databases ".
  128. "it is possible that nodes in Drupal and features in Chado become ".
  129. "\"orphaned\". This can occur if a feature node in Drupal is ".
  130. "deleted but the corresponding chado feature is not and/or vice ".
  131. "versa. The Cleanup function will also remove nodes for features ".
  132. "that are not in the list of allowed feature types as specified ".
  133. "above. This is helpful when a feature type needs to be ".
  134. "removed but was previously present as Drupal nodes. ".
  135. "Click the button below to resolve these discrepancies."),
  136. '#weight' => 1,
  137. );
  138. $form['cleanup']['button'] = array(
  139. '#type' => 'submit',
  140. '#value' => t('Clean up orphaned features'),
  141. '#weight' => 2,
  142. );
  143. }
  144. /************************************************************************
  145. *
  146. */
  147. function get_tripal_feature_admin_form_reindex_set(&$form) {
  148. $form['reindex'] = array(
  149. '#type' => 'fieldset',
  150. '#title' => t('Reindex')
  151. );
  152. $form['reindex']['description'] = array(
  153. '#type' => 'item',
  154. '#value' => t("Reindexing of nodes is important when content for nodes ".
  155. "is updated external to drupal, such as external uploads to chado. ".
  156. "Features need to be reindexed to ensure that updates to features ".
  157. "are searchable. Depending on the number of features this may take ".
  158. "quite a while. Click the button below to begin reindexing of ".
  159. "features."),
  160. '#weight' => 1,
  161. );
  162. $form['reindex']['button'] = array(
  163. '#type' => 'submit',
  164. '#value' => t('Reindex all feature nodes'),
  165. '#weight' => 2,
  166. );
  167. }
  168. /************************************************************************
  169. *
  170. */
  171. function get_tripal_feature_admin_form_taxonomy_set (&$form) {
  172. $form['taxonomy'] = array(
  173. '#type' => 'fieldset',
  174. '#title' => t('Set Taxonomy')
  175. );
  176. $form['taxonomy']['description'] = array(
  177. '#type' => 'item',
  178. '#value' => t("Drupal allows for assignment of \"taxonomy\" or ".
  179. "catagorical terms to nodes. These terms allow for advanced ".
  180. "filtering during searching."),
  181. '#weight' => 1,
  182. );
  183. $tax_options = array (
  184. 'organism' => t('Organism name'),
  185. 'feature_type' => t('Feature Type (e.g. EST, mRNA, etc.)'),
  186. 'analysis' => t('Analysis Name'),
  187. 'library' => t('Library Name'),
  188. );
  189. $form['taxonomy']['tax_classes'] = array (
  190. '#title' => t('Available Taxonomic Classes'),
  191. '#type' => t('checkboxes'),
  192. '#description' => t("Please select the class of terms to assign to ".
  193. "chado features"),
  194. '#required' => FALSE,
  195. '#prefix' => '<div id="taxclass_boxes">',
  196. '#suffix' => '</div>',
  197. '#options' => $tax_options,
  198. '#weight' => 2,
  199. '#default_value' => variable_get('tax_classes',''),
  200. );
  201. $form['taxonomy']['button'] = array(
  202. '#type' => 'submit',
  203. '#value' => t('Set/Reset Taxonomy for all feature nodes'),
  204. '#weight' => 3,
  205. );
  206. }
  207. /************************************************************************
  208. *
  209. */
  210. function get_tripal_feature_admin_form_sync_set (&$form) {
  211. // get the list of organisms which will be synced.
  212. $feature_sql = "SELECT * FROM {Feature} WHERE uniquename = '%s' and organism_id = %d";
  213. $previous_db = db_set_active('chado');
  214. $feature = db_fetch_object(db_query($feature_sql,$node->title,$node->organism_id));
  215. db_set_active($previous_db);
  216. // define the fieldsets
  217. $form['sync'] = array(
  218. '#type' => 'fieldset',
  219. '#title' => t('Sync Features')
  220. );
  221. $form['sync']['description'] = array(
  222. '#type' => 'item',
  223. '#value' => t("Click the 'Sync all Features' button to create Drupal ".
  224. "content for features in chado. Only features of the types listed ".
  225. "above in the Feature Types box will be synced. Depending on the ".
  226. "number of features in the chado database this may take a long ".
  227. "time to complete. "),
  228. '#weight' => 1,
  229. );
  230. $orgs = tripal_organism_get_synced();
  231. $org_list = '';
  232. foreach($orgs as $org){
  233. $org_list .= "$org->genus $org->species, ";
  234. }
  235. $form['sync']['description2'] = array(
  236. '#type' => 'item',
  237. '#value' => "Only features for the following organisms will be synced: ".
  238. " $org_list",
  239. '#weight' => 1,
  240. );
  241. $form['sync']['button'] = array(
  242. '#type' => 'submit',
  243. '#value' => t('Sync all Features'),
  244. '#weight' => 3,
  245. );
  246. }
  247. /************************************************************************
  248. * Display help and module information
  249. * @param path which path of the site we're displaying help
  250. * @param arg array that holds the current path as would be returned from arg() function
  251. * @return help text for the path
  252. */
  253. function tripal_feature_help($path, $arg) {
  254. $output = '';
  255. switch ($path) {
  256. case "admin/help#tripal_feature":
  257. $output='<p>'.t("Displays links to nodes created on this date").'</p>';
  258. break;
  259. }
  260. return $output;
  261. }
  262. /************************************************************************
  263. * Provide information to drupal about the node types that we're creating
  264. * in this module
  265. */
  266. function tripal_feature_node_info() {
  267. $nodes = array();
  268. $nodes['chado_feature'] = array(
  269. 'name' => t('Feature'),
  270. 'module' => 'chado_feature',
  271. 'description' => t('A feature from the chado database'),
  272. 'has_title' => FALSE,
  273. 'title_label' => t('Feature'),
  274. 'has_body' => FALSE,
  275. 'body_label' => t('Feature Description'),
  276. 'locked' => TRUE
  277. );
  278. return $nodes;
  279. }
  280. /************************************************************************
  281. * Set the permission types that the chado module uses. Essentially we
  282. * want permissionis that protect creation, editing and deleting of chado
  283. * data objects
  284. */
  285. function tripal_feature_perm(){
  286. return array(
  287. 'access chado_feature content',
  288. 'create chado_feature content',
  289. 'delete chado_feature content',
  290. 'edit chado_feature content',
  291. );
  292. }
  293. /************************************************************************
  294. * Set the permission types that the module uses.
  295. */
  296. function chado_feature_access($op, $node, $account) {
  297. if ($op == 'create') {
  298. return user_access('create chado_feature content', $account);
  299. }
  300. if ($op == 'update') {
  301. if (user_access('edit chado_feature content', $account)) {
  302. return TRUE;
  303. }
  304. }
  305. if ($op == 'delete') {
  306. if (user_access('delete chado_feature content', $account)) {
  307. return TRUE;
  308. }
  309. }
  310. if ($op == 'view') {
  311. if (user_access('access chado_feature content', $account)) {
  312. return TRUE;
  313. }
  314. }
  315. return FALSE;
  316. }
  317. /************************************************************************
  318. * Menu items are automatically added for the new node types created
  319. * by this module to the 'Create Content' Navigation menu item. This function
  320. * adds more menu items needed for this module.
  321. */
  322. function tripal_feature_menu() {
  323. $items = array();
  324. // the administative settings menu
  325. $items['admin/tripal/tripal_feature'] = array(
  326. 'title' => 'Features',
  327. 'description' => 'Settings for Chado Features',
  328. 'page callback' => 'drupal_get_form',
  329. 'page arguments' => array('tripal_feature_admin'),
  330. 'access arguments' => array('administer site configuration'),
  331. 'type' => MENU_NORMAL_ITEM,
  332. );
  333. $items['admin/settings/tripal/tripal_feature/load'] = array(
  334. 'title' => 'Bulk Load',
  335. 'description' => 'Upload Data into Chado & Drupal',
  336. 'page callback' => 'tripal_feature_bulkload',
  337. 'access arguments' => array('administer site configuration'),
  338. 'type' => MENU_NORMAL_ITEM,
  339. );
  340. return $items;
  341. }
  342. /************************************************************************
  343. * When a new chado_feature node is created we also need to add information
  344. * to our chado_feature table. This function is called on insert of a new node
  345. * of type 'chado_feature' and inserts the necessary information.
  346. */
  347. function chado_feature_insert($node){
  348. // remove spaces, newlines from residues
  349. $residues = preg_replace("/[\n\r\s]/","",$node->residues);
  350. // If this feature already exists then don't recreate it in chado
  351. // TODO: the unique index in chado for this also includes the type_id. If the site
  352. // ever needs to have the same feature name for different types then this will break.
  353. $feature_sql = "SELECT * FROM {Feature} WHERE uniquename = '%s' and organism_id = %d";
  354. $previous_db = db_set_active('chado');
  355. $feature = db_fetch_object(db_query($feature_sql,$node->title,$node->organism_id));
  356. db_set_active($previous_db);
  357. // if the feature doesn't exist then let's create it in chado.
  358. if(!$feature){
  359. $sql = "INSERT INTO {feature} (organism_id, name, uniquename, residues, seqlen,".
  360. " is_obsolete, type_id)".
  361. " VALUES(%d,'%s','%s','%s',%d, %s, ".
  362. " (SELECT cvterm_id ".
  363. " FROM {CVTerm} CVT ".
  364. " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
  365. " WHERE CV.name = 'sequence' and CVT.name = '%s'))";
  366. $obsolete = 'FALSE';
  367. if($node->is_obsolete){
  368. $obsolete = 'TRUE';
  369. }
  370. // use chado database
  371. $previous_db = db_set_active('chado');
  372. db_query($sql,$node->organism_id,$node->title,$node->title,
  373. $residues,strlen($residues),$obsolete,$node->feature_type);
  374. // now that we've added the feature, get the feature id for this feature
  375. $feature = db_fetch_object(db_query($feature_sql,$node->title,$node->organism_id));
  376. // now use drupal database
  377. db_set_active($previous_db);
  378. }
  379. // add the genbank accession and synonyms
  380. chado_feature_add_synonyms($node->synonyms,$feature->feature_id);
  381. // make sure the entry for this feature doesn't already exist in the chado_feature table
  382. // if it doesn't exist then we want to add it.
  383. $node_check_sql = "SELECT * FROM {chado_feature} ".
  384. "WHERE feature_id = '%s'";
  385. $node_check = db_fetch_object(db_query($node_check_sql,$feature->feature_id));
  386. if(!$node_check){
  387. // next add the item to the drupal table
  388. $sql = "INSERT INTO {chado_feature} (nid, vid, feature_id, sync_date) ".
  389. "VALUES (%d, %d, %d, " . time() . ")";
  390. db_query($sql,$node->nid,$node->vid,$feature->feature_id);
  391. }
  392. }
  393. /************************************************************************
  394. */
  395. function chado_feature_delete($node){
  396. // get feature_id so we can remove it from chado database
  397. $sql_drupal = "SELECT feature_id ".
  398. "FROM {chado_feature} ".
  399. "WHERE nid = %d AND vid = %d";
  400. $feature_id = db_result(db_query($sql_drupal, $node->nid, $node->vid));
  401. // remove the drupal content
  402. $sql_del = "DELETE FROM {chado_feature} ".
  403. "WHERE nid = %d ".
  404. "AND vid = %d";
  405. db_query($sql_del, $node->nid, $node->vid);
  406. $sql_del = "DELETE FROM {node} ".
  407. "WHERE nid = %d ".
  408. "AND vid = %d";
  409. db_query($sql_del, $node->nid, $node->vid);
  410. $sql_del = "DELETE FROM {node_revisions} ".
  411. "WHERE nid = %d ".
  412. "AND vid = %d";
  413. db_query($sql_del, $node->nid, $node->vid);
  414. // Remove data from feature tables of chado database. This will
  415. // cause a cascade delete and remove all data in referencing tables
  416. // for this feature
  417. $previous_db = db_set_active('chado');
  418. db_query("DELETE FROM {feature} WHERE feature_id = %d", $feature_id);
  419. db_set_active($previous_db);
  420. drupal_set_message("The feature and all associated data were removed from ".
  421. "chado");
  422. }
  423. /************************************************************************
  424. */
  425. function chado_feature_update($node){
  426. if($node->revision){
  427. // TODO -- decide what to do about revisions
  428. } else {
  429. // get the feature for this node:
  430. $sql = 'SELECT feature_id FROM {chado_feature} WHERE vid = %d';
  431. $feature = db_fetch_object(db_query($sql, $node->vid));
  432. // remove spaces, newlines from residues
  433. $residues = preg_replace("/[\n\r\s]/","",$node->residues);
  434. $sql = "UPDATE {feature} ".
  435. " SET residues = '%s', ".
  436. " name = '%s', ".
  437. " uniquename = '%s', ".
  438. " seqlen = %d, ".
  439. " organism_id = %d, ".
  440. " is_obsolete = %s, ".
  441. " type_id = (SELECT cvterm_id ".
  442. " FROM {CVTerm} CVT ".
  443. " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
  444. " WHERE CV.name = 'sequence' and CVT.name = '%s') ".
  445. "WHERE feature_id = %d ";
  446. $obsolete = 'FALSE';
  447. if($node->is_obsolete){
  448. $obsolete = 'TRUE';
  449. }
  450. $previous_db = db_set_active('chado'); // use chado database
  451. db_query($sql,$residues,$node->title,$node->title,
  452. strlen($residues),$node->organism_id,$obsolete,$node->feature_type,
  453. $feature->feature_id);
  454. db_set_active($previous_db); // now use drupal database
  455. // add the genbank accession & synonyms
  456. // chado_feature_add_gbaccession($node->gbaccession,$feature->feature_id);
  457. chado_feature_add_synonyms($node->synonyms,$feature->feature_id);
  458. }
  459. }
  460. /************************************************************************
  461. *
  462. */
  463. function chado_feature_add_synonyms($synonyms,$feature_id){
  464. // make sure we only have a single space between each synonym
  465. $synonyms = preg_replace("/[\s\n\r]+/"," ",$synonyms);
  466. // split the synonyms into an array based on a space as the delimieter
  467. $syn_array = array();
  468. $syn_array = explode(" ",$synonyms);
  469. // use the chado database
  470. $previous_db = db_set_active('chado');
  471. // remove any old synonyms
  472. $feature_syn_dsql = "DELETE FROM {feature_synonym} WHERE feature_id = %d";
  473. if(!db_query($feature_syn_dsql,$feature_id)){
  474. $error .= "Could not remove synonyms from feature. ";
  475. }
  476. // return if we don't have any synonmys to add
  477. if(!$synonyms){
  478. db_set_active($previous_db);
  479. return;
  480. }
  481. // iterate through each synonym and add it to the database
  482. foreach($syn_array as $syn){
  483. // skip this item if it's empty
  484. if(!$syn){ break; }
  485. // check to see if we have this accession number already in the database
  486. // if so then don't add it again. it messes up drupal if the insert fails.
  487. // It is possible for the accession number to be present and not the feature
  488. $synonym_sql = "SELECT synonym_id FROM {synonym} ".
  489. "WHERE name = '%s'";
  490. $synonym = db_fetch_object(db_query($synonym_sql,$syn));
  491. if(!$synonym){
  492. $synonym_isql = "INSERT INTO {synonym} (name,synonym_sgml,type_id) ".
  493. "VALUES ('%s','%s', ".
  494. " (SELECT cvterm_id ".
  495. " FROM {CVTerm} CVT ".
  496. " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
  497. " WHERE CV.name = 'feature_property' and CVT.name = 'synonym'))";
  498. if(!db_query($synonym_isql,$syn,$syn)){
  499. $error .= "Could not add synonym. ";
  500. }
  501. // now get the synonym we just added
  502. $synonym_sql = "SELECT synonym_id FROM {synonym} ".
  503. "WHERE name = '%s'";
  504. $synonym = db_fetch_object(db_query($synonym_sql,$syn));
  505. }
  506. // now add in our new sysnonym
  507. $feature_syn_isql = "INSERT INTO {feature_synonym} (synonym_id,feature_id,pub_id) ".
  508. "VALUES (%d,%d,1)";
  509. if(!db_query($feature_syn_isql,$synonym->synonym_id,$feature_id)){
  510. $error .= "Could not add synonyms to feature. ";
  511. }
  512. }
  513. // return to the drupal database
  514. db_set_active($previous_db);
  515. return $error;
  516. }
  517. /************************************************************************
  518. *
  519. */
  520. function chado_feature_add_gbaccession($accession,$feature_id){
  521. // use chado database
  522. $previous_db = db_set_active('chado');
  523. // remove any old accession from genbank dbEST
  524. $fdbxref_dsql = "DELETE FROM {feature_dbxref} ".
  525. "WHERE feature_id = %d and dbxref_id IN ".
  526. " (SELECT DBX.dbxref_id FROM {dbxref} DBX ".
  527. " INNER JOIN DB ON DB.db_id = DBX.db_id ".
  528. " INNER JOIN feature_dbxref FDBX ON DBX.dbxref_id = FDBX.dbxref_id ".
  529. " WHERE DB.name = 'DB:Genbank' and FDBX.feature_id = %d)";
  530. if(!db_query($fdbxref_dsql,$feature_id,$feature_id)){
  531. $error .= "Could not remove accession from feature. ";
  532. }
  533. // if we don't have an accession number to add then just return
  534. if(!$accession){
  535. db_set_active($previous_db);
  536. return;
  537. }
  538. // get the db_id
  539. $db_sql = "SELECT db_id FROM {DB} ".
  540. "WHERE name = 'DB:Genbank_est'";
  541. $db = db_fetch_object(db_query($db_sql));
  542. // check to see if we have this accession number already in the database
  543. // if so then don't add it again. it messes up drupal if the insert fails.
  544. // It is possible for the accession number to be present and not the feature
  545. $dbxref_sql = "SELECT dbxref_id FROM {dbxref} ".
  546. "WHERE db_id = %d and accession = '%s'";
  547. $dbxref = db_fetch_object(db_query($dbxref_sql,$db->db_id,$accession));
  548. if(!$dbxref){
  549. // add the accession number
  550. $dbxref_isql = "INSERT INTO {dbxref} (db_id,accession) ".
  551. " VALUES (%d, '%s') ";
  552. if(!db_query($dbxref_isql,$db->db_id,$accession)){
  553. $error .= 'Could not add accession as a database reference ';
  554. }
  555. // get the dbxref_id for the just added accession number
  556. $dbxref_sql = "SELECT dbxref_id FROM {dbxref} ".
  557. "WHERE db_id = %d and accession = '%s'";
  558. $dbxref = db_fetch_object(db_query($dbxref_sql,$db->db_id,$accession));
  559. }
  560. // associate the accession number with the feature
  561. $feature_dbxref_isql = "INSERT INTO {feature_dbxref} (feature_id,dbxref_id) ".
  562. " VALUES (%d, %d) ";
  563. if(!db_query($feature_dbxref_isql,$feature_id,$dbxref->dbxref_id)){
  564. $error .= 'Could not add feature database reference. ';
  565. }
  566. db_set_active($previous_db);
  567. return $error;
  568. }
  569. /************************************************************************
  570. *
  571. */
  572. function chado_feature_form ($node,$param){
  573. $type = node_get_types('type', $node);
  574. $form = array();
  575. $feature = $node->feature;
  576. $synonyms = $node->synonyms;
  577. $analyses = $node->analyses;
  578. $references = $node->references;
  579. // We need to pass above variables for preview to show
  580. $form['feature'] = array(
  581. '#type' => 'value',
  582. '#value' => $feature
  583. );
  584. // This field is read when previewing a node
  585. $form['synonyms'] = array(
  586. '#type' => 'value',
  587. '#value' => $synonyms
  588. );
  589. // This field is read when previewing a node
  590. $form['analyses'] = array(
  591. '#type' => 'value',
  592. '#value' => $analyses
  593. );
  594. // This field is read when previewing a node
  595. $form['references'] = array(
  596. '#type' => 'value',
  597. '#value' => $references
  598. );
  599. // keep track of the feature id if we have one. If we do have one then
  600. // this would indicate an update as opposed to an insert.
  601. $form['feature_id'] = array(
  602. '#type' => 'value',
  603. '#value' => $feature->feature_id,
  604. );
  605. $form['title']= array(
  606. '#type' => 'textfield',
  607. '#title' => t('Unique Feature Name'),
  608. '#required' => TRUE,
  609. '#default_value' => $feature->featurename,
  610. '#description' => t('Enter a unique name for this feature'),
  611. '#weight' => 1,
  612. '#maxlength' => 255
  613. );
  614. // get the list of supported feature types
  615. $ftypes = array();
  616. $ftypes[''] = '';
  617. $supported_ftypes = split("[ \n]",variable_get('chado_feature_types','EST contig'));
  618. foreach($supported_ftypes as $ftype){
  619. $ftypes["$ftype"] = $ftype;
  620. }
  621. $form['feature_type'] = array (
  622. '#title' => t('Feature Type'),
  623. '#type' => t('select'),
  624. '#description' => t("Choose the feature type."),
  625. '#required' => TRUE,
  626. '#default_value' => $feature->cvname,
  627. '#options' => $ftypes,
  628. '#weight' => 2
  629. );
  630. // get the list of organisms
  631. $sql = "SELECT * FROM {Organism} ORDER BY genus, species";
  632. $previous_db = db_set_active('chado'); // use chado database
  633. $org_rset = db_query($sql);
  634. db_set_active($previous_db); // now use drupal database
  635. //
  636. $organisms = array();
  637. $organisms[''] = '';
  638. while($organism = db_fetch_object($org_rset)){
  639. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  640. }
  641. $form['organism_id'] = array (
  642. '#title' => t('Organism'),
  643. '#type' => t('select'),
  644. '#description' => t("Choose the organism with which this feature is associated "),
  645. '#required' => TRUE,
  646. '#default_value' => $feature->organism_id,
  647. '#options' => $organisms,
  648. '#weight' => 3,
  649. );
  650. // Get synonyms
  651. if ($synonyms) {
  652. if (is_array($synonyms)) {
  653. foreach ($synonyms as $synonym){
  654. $syn_text .= "$synonym->name\n";
  655. }
  656. } else {
  657. $syn_text = $synonyms;
  658. }
  659. }
  660. $form['synonyms']= array(
  661. '#type' => 'textarea',
  662. '#title' => t('Synonyms'),
  663. '#required' => FALSE,
  664. '#default_value' => $syn_text,
  665. '#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.'),
  666. '#weight' => 5,
  667. );
  668. $form['residues']= array(
  669. '#type' => 'textarea',
  670. '#title' => t('Residues'),
  671. '#required' => FALSE,
  672. '#default_value' => $feature->residues,
  673. '#description' => t('Enter the nucelotide sequences for this feature'),
  674. '#weight' => 6
  675. );
  676. $checked = '';
  677. if($feature->is_obsolete == 't'){
  678. $checked = '1';
  679. }
  680. $form['is_obsolete']= array(
  681. '#type' => 'checkbox',
  682. '#title' => t('Is Obsolete'),
  683. '#required' => FALSE,
  684. '#default_value' => $checked,
  685. '#description' => t('Check this box if this sequence should be retired and no longer included in further analysis.'),
  686. '#weight' => 8
  687. );
  688. return $form;
  689. }
  690. /************************************************************************
  691. *
  692. */
  693. function chado_feature_validate($node){
  694. $result = 0;
  695. // if this is an update, we want to make sure that a different feature for
  696. // the organism doesn't already have this uniquename. We don't want to give
  697. // two sequences the same uniquename
  698. if($node->feature_id){
  699. $sql = "SELECT * FROM {Feature} WHERE uniquename = '%s' ".
  700. " AND organism_id = %d AND NOT feature_id = %d";
  701. $previous_db = db_set_active('chado');
  702. $result = db_fetch_object(db_query($sql, $node->title,$node->organism_id,$node->feature_id));
  703. db_set_active($previous_db);
  704. if($result){
  705. form_set_error('title',t("Feature update cannot proceed. The feature name '$node->title' is not unique for this organism. Please provide a unique name for this feature. "));
  706. }
  707. }
  708. // if this is an insert then we just need to make sure this name doesn't
  709. // already exist for this organism if it does then we need to throw an error
  710. else {
  711. $sql = "SELECT * FROM {Feature} WHERE uniquename = '%s' AND organism_id = %d";
  712. $previous_db = db_set_active('chado');
  713. $result = db_fetch_object(db_query($sql, $node->title,$node->organism_id));
  714. db_set_active($previous_db);
  715. if($result){
  716. form_set_error('title',t("Feature insert cannot proceed. The feature name '$node->title' already exists for this organism. Please provide a unique name for this feature. "));
  717. }
  718. }
  719. // we want to remove all characters except IUPAC nucleotide characters from the
  720. // the residues. however, residues are not required so if blank then we'll skip
  721. // this step
  722. if($node->residues){
  723. $residues = preg_replace("/[^\w]/",'',$node->residues);
  724. if(!preg_match("/^[ACTGURYMKSWBDHVN]+$/i",$residues)){
  725. form_set_error('residues',t("The residues in feature $node->title 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 ."'"));
  726. }
  727. }
  728. // we don't allow a genbank accession number for a contig
  729. if($node->feature_type == 'contig' and $node->gbaccession){
  730. form_set_error('gbaccession',t("Contigs cannot have a genbank accession number. Please change the feature type or remove the accession number"));
  731. }
  732. }
  733. /************************************************************************
  734. * When a node is requested by the user this function is called to allow us
  735. * to add auxiliary data to the node object.
  736. */
  737. function chado_feature_load($node){
  738. // get the feature_id for this node:
  739. $sql = 'SELECT feature_id FROM {chado_feature} WHERE vid = %d';
  740. $map = db_fetch_object(db_query($sql, $node->vid));
  741. $previous_db = db_set_active('chado'); // use chado database
  742. // get information about this organism and add it to the items in this node
  743. $sql = "SELECT F.feature_id, F.name as featurename, F.uniquename, ".
  744. "F.residues, F.seqlen, O.genus, O.species, O.common_name, ".
  745. " CVT.name as cvname, O.organism_id, F.type_id, F.is_obsolete ".
  746. "FROM {Feature} F ".
  747. " INNER JOIN Organism O ON F.organism_id = O.organism_id ".
  748. " INNER JOIN CVterm CVT ON F.type_id = CVT.cvterm_id ".
  749. "WHERE F.feature_id = %d";
  750. $feature = db_fetch_object(db_query($sql,$map->feature_id));
  751. $additions->feature = $feature;
  752. $additions->seqlen = $feature->seqlen;
  753. // get the feature synonyms
  754. $sql = "SELECT S.name ".
  755. "FROM {Feature_Synonym} FS ".
  756. " INNER JOIN Synonym S ".
  757. " ON FS.synonym_id = S.Synonym_id ".
  758. "WHERE FS.feature_id = %d";
  759. $results = db_query($sql,$map->feature_id);
  760. $synonyms = array();
  761. $i=0;
  762. while($synonym = db_fetch_object($results)){
  763. $synonyms[$i++] = $synonym;
  764. }
  765. $additions->synonyms = $synonyms;
  766. // get feature references in external databases
  767. $sql = "SELECT F.uniquename,F.Feature_id,DBX.accession,DB.description as dbdesc, ".
  768. " DB.db_id, DB.name as db_name, DB.urlprefix ".
  769. "FROM {Feature} F ".
  770. " INNER JOIN Feature_dbxref FDBX on F.feature_id = FDBX.feature_id ".
  771. " INNER JOIN Dbxref DBX on DBX.dbxref_id = FDBX.dbxref_id ".
  772. " INNER JOIN DB on DB.db_id = DBX.db_id ".
  773. "WHERE F.feature_id = %d";
  774. $results = db_query($sql,$map->feature_id);
  775. $references = array();
  776. $i=0;
  777. while($accession = db_fetch_object($results)){
  778. $references[$i++] = $accession;
  779. // we want to specifically pull out the genbank id
  780. if(preg_match("/Genbank_est/",$accession->db_name)){
  781. $additions->gbaccession = $accession;
  782. }
  783. }
  784. $additions->references = $references;
  785. db_set_active($previous_db); // now use drupal database
  786. // get organism node nid
  787. $sql = "SELECT nid FROM {chado_organism} WHERE organism_id = %d";
  788. $org_nid = db_result(db_query($sql, $additions->feature->organism_id));
  789. $additions->org_nid = $org_nid;
  790. return $additions;
  791. }
  792. /************************************************************************
  793. * This function customizes the view of the chado_feature node. It allows
  794. * us to generate the markup.
  795. */
  796. function chado_feature_view ($node, $teaser = FALSE, $page = FALSE) {
  797. if (!$teaser) {
  798. // use drupal's default node view:
  799. $node = node_prepare($node, $teaser);
  800. // if we're building the node for searching then
  801. // we want to handle this within the module and
  802. // not allow theme customization. We don't want to
  803. // index all items (such as DNA sequence).
  804. if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
  805. $node->content['index_version'] = array(
  806. '#value' => theme('tripal_feature_search_index',$node),
  807. '#weight' => 1,
  808. );
  809. }
  810. else if($node->build_mode == NODE_BUILD_SEARCH_RESULT){
  811. $node->content['index_version'] = array(
  812. '#value' => theme('tripal_feature_search_results',$node),
  813. '#weight' => 1,
  814. );
  815. }
  816. else {
  817. // do nothing here, let the theme derived template handle display
  818. }
  819. }
  820. return $node;
  821. }
  822. /*******************************************************************************
  823. * Display feature information for associated organisms. This function also
  824. * provides contents for indexing
  825. */
  826. function tripal_feature_nodeapi(&$node, $op, $teaser, $page) {
  827. switch ($op) {
  828. // Note that this function only adds feature view to an organism node.
  829. // The view of a feature node is controled by the theme *.tpl file
  830. case 'view':
  831. // Set the node types for showing feature information
  832. $types_to_show = array('chado_organism', 'chado_library');
  833. // Abort if this node is not one of the types we should show.
  834. if (!in_array($node->type, $types_to_show, TRUE)) {
  835. break;
  836. }
  837. // Add feature to the content item if it's not a teaser
  838. if (!$teaser) {
  839. // Show feature browser
  840. $node->content['tripal_feature_browser'] = array(
  841. '#value' => theme('tripal_feature_browser', $node),
  842. '#weight' => 5
  843. );
  844. $node->content['tripal_feature_org_counts'] = array(
  845. '#value' => theme('tripal_feature_counts', $node),
  846. '#weight' => 4
  847. );
  848. }
  849. }
  850. }
  851. /************************************************************************
  852. * We need to let drupal know about our theme functions and their arguments.
  853. * We create theme functions to allow users of the module to customize the
  854. * look and feel of the output generated in this module
  855. */
  856. function tripal_feature_theme () {
  857. return array(
  858. 'tripal_feature_search_index' => array (
  859. 'arguments' => array('node'),
  860. ),
  861. 'tripal_feature_search_results' => array (
  862. 'arguments' => array('node'),
  863. ),
  864. 'tripal_feature_browser' => array (
  865. 'arguments' => array('node'),
  866. ),
  867. 'tripal_feature_counts' => array (
  868. 'arguments' => array('node'),
  869. )
  870. );
  871. }
  872. /*******************************************************************************
  873. * create a list of features for the organism and pie chart
  874. */
  875. function theme_tripal_feature_counts($node){
  876. // get the feature counts. This is dependent on a materialized view
  877. // installed with the organism module
  878. $content = '';
  879. if ($node->organism_id && $node->type == 'chado_organism') {
  880. $sql = "SELECT * FROM {organism_feature_count} ".
  881. "WHERE organism_id = %d AND NOT feature_type = 'EST_match' ".
  882. "ORDER BY num_features desc";
  883. $features = array();
  884. $previous_db = db_set_active('chado'); // use chado database
  885. $results = db_query($sql,$node->organism_id);
  886. db_set_active($previous_db); // now use drupal database
  887. $feature = db_fetch_object($results); // retrieve the first result
  888. if ($feature) {
  889. $content .= "<div class=\"tripal_feature_summary-info-box\"><br>
  890. <div class=\"tripal_expandableBox\">".
  891. "<h3>Feature Summary</h3>".
  892. "</div>";
  893. $content .= "<div class=\"tripal_expandableBoxContent\">";
  894. $content .= "<table class=\"tripal_table_horz\">";
  895. $content .= " <tr>";
  896. $content .= " <th class=\"dbfieldname\">Type</th>";
  897. $content .= " <th class=\"dbfieldname\">Number</th>";
  898. $content .= " </tr>";
  899. do {
  900. $content .= "<tr>";
  901. $content .= " <td>$feature->feature_type</td>";
  902. $content .= " <td>". number_format($feature->num_features) . "</td>";
  903. $content .= "</tr>";
  904. } while($feature = db_fetch_object($results));
  905. $content .= "</table>";
  906. $content .= "
  907. <img class=\"tripal_cv_chart\" id=\"tripal_feature_cv_chart_$node->organism_id\" src=\"\" border=\"0\">
  908. ";
  909. $content .= "</div></div>";
  910. }
  911. }
  912. return $content;
  913. }
  914. /************************************************************************
  915. *
  916. */
  917. function tripal_feature_cv_chart($chart_id){
  918. // The CV module will create the JSON array necessary for buillding a
  919. // pie chart using jgChart and Google Charts. We have to pass to it
  920. // a table that contains count information, tell it which column
  921. // contains the cvterm_id and provide a filter for getting the
  922. // results we want from the table.
  923. $organism_id = preg_replace("/^tripal_feature_cv_chart_(\d+)$/","$1",$chart_id);
  924. $options = array(
  925. count_mview => 'organism_feature_count',
  926. cvterm_id_column => 'cvterm_id',
  927. count_column => 'num_features',
  928. filter => "CNT.organism_id = $organism_id AND NOT feature_type = 'EST_match' ",
  929. );
  930. return $options;
  931. }
  932. /************************************************************************
  933. *
  934. */
  935. function tripal_feature_cv_tree($tree_id){
  936. // The CV module will create the JSON array necessary for buillding a
  937. // pie chart using jgChart and Google Charts. We have to pass to it
  938. // a table that contains count information, tell it which column
  939. // contains the cvterm_id and provide a filter for getting the
  940. // results we want from the table.
  941. $organism_id = preg_replace("/^tripal_feature_cv_tree_(\d+)$/","$1",$tree_id);
  942. $options = array(
  943. cv_id => tripal_cv_get_cv_id('sequence'),
  944. count_mview => 'organism_feature_count',
  945. cvterm_id_column => 'cvterm_id',
  946. count_column => 'num_features',
  947. filter => "CNT.organism_id = $organism_id",
  948. label => 'Features',
  949. );
  950. return $options;
  951. }
  952. /*******************************************************************************
  953. * create a simple paged feature browser
  954. */
  955. function theme_tripal_feature_browser($node){
  956. // don't show the browser if the settings in the admin page is turned off
  957. $show_browser = variable_get('tripal_feature_browse_setting',array('show_feature_browser'));
  958. if(strcmp($show_browser,'show_feature_browser')!=0){
  959. return;
  960. }
  961. if ($node->organism_id && $node->type == 'chado_organism') {
  962. # get the list of available sequence ontology terms for which
  963. # we will build drupal pages from features in chado. If a feature
  964. # is not one of the specified typse we won't build a node for it.
  965. $allowed_types = variable_get('chado_feature_types','EST contig');
  966. $allowed_types = preg_replace("/[\s\n\r]+/"," ",$allowed_types);
  967. $so_terms = split(' ',$allowed_types);
  968. $where_cvt = "";
  969. foreach ($so_terms as $term){
  970. $where_cvt .= "CVT.name = '$term' OR ";
  971. }
  972. $where_cvt = substr($where_cvt,0,strlen($where_cvt)-3); # strip trailing 'OR'
  973. // get the features for this organism
  974. $sql = "SELECT F.name,F.feature_id,F.uniquename,CVT.name as cvname ".
  975. "FROM {feature} F ".
  976. " INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id ".
  977. "WHERE organism_id = $node->organism_id and ($where_cvt) ".
  978. "ORDER BY feature_id ASC";
  979. // the counting SQL
  980. $csql = "SELECT count(*) ".
  981. "FROM {feature} F".
  982. " INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id ".
  983. "WHERE organism_id = $node->organism_id and ($where_cvt) ".
  984. "GROUP BY organism_id ";
  985. $previous_db = db_set_active('chado'); // use chado database
  986. $features = pager_query($sql,10,0,$csql);
  987. db_set_active($previous_db); // now use drupal database
  988. $content = "<br><div id=\"tripal_feature_box\" class=\"feature-info-box\">";
  989. $content .= "<div class=\"tripal_expandableBox\">".
  990. "<h3>Browse Features</h3>".
  991. "</div>";
  992. $content .= "<div class=\"tripal_expandableBoxContent\">";
  993. $content .= "Below are the features associated with this organism.\n";
  994. $content .= "<table class=\"tripal_table_horz\">";
  995. $content .= " <tr>";
  996. $content .= " <th>Feature Name</th>";
  997. $content .= " <th>Type</th>";
  998. $content .= " </tr>";
  999. // prepare the query that will lookup node ids
  1000. $sql = "SELECT nid FROM {chado_feature} ".
  1001. "WHERE feature_id = %d";
  1002. while($feature = db_fetch_object($features)){
  1003. $node = db_fetch_object(db_query($sql,$feature->feature_id));
  1004. if($node){
  1005. $name= "<a href=\"" . url("node/$node->nid") . "\">$feature->name</a>";
  1006. } else {
  1007. $name= "$feature->name";
  1008. }
  1009. $content .= " <tr>";
  1010. $content .= " <td>$name</td>";
  1011. $content .= " <td>$feature->cvname</td>";
  1012. $content .= " </tr>";
  1013. }
  1014. $content .= "</table>";
  1015. $content .= theme('pager');
  1016. $content .= "</div></div>";
  1017. return $content;
  1018. }
  1019. }
  1020. /************************************************************************
  1021. * This function is an extension of the chado_feature_view by providing
  1022. * the markup for the feature object THAT WILL BE INDEXED.
  1023. */
  1024. function theme_tripal_feature_search_index ($node) {
  1025. $feature = $node->feature;
  1026. $content = '';
  1027. // get the accession prefix
  1028. $aprefix = variable_get('chado_feature_accession_prefix','ID');
  1029. $content .= "<h1>$feature->uniquename</h1>. ";
  1030. $content .= "<strong>$aprefix$feature->feature_id.</strong> ";
  1031. $content .= "$feature->cvname ";
  1032. $content .= "$feature->common_name ";
  1033. // add the synonyms of this feature to the text for searching
  1034. $synonyms = $node->synonyms;
  1035. if(count($synonyms) > 0){
  1036. foreach ($synonyms as $result){
  1037. $content .= "$result->name ";
  1038. }
  1039. }
  1040. return $content;
  1041. }
  1042. /************************************************************************
  1043. * This function is an extension of the chado_feature_view by providing
  1044. * the markup for the feature object THAT WILL BE INDEXED.
  1045. */
  1046. function theme_tripal_feature_search_results ($node) {
  1047. $feature = $node->feature;
  1048. $content = '';
  1049. // get the accession prefix
  1050. $aprefix = variable_get('chado_feature_accession_prefix','ID');
  1051. $content .= "Feature Name: <h1>$feature->uniquename</h1>. ";
  1052. $content .= "<strong>Accession: $aprefix$feature->feature_id.</strong>";
  1053. $content .= "Type: $feature->cvname. ";
  1054. $content .= "Organism: $feature->common_name. ";
  1055. // add the synonyms of this feature to the text for searching
  1056. $synonyms = $node->synonyms;
  1057. if(count($synonyms) > 0){
  1058. $content .= "Synonyms: ";
  1059. foreach ($synonyms as $result){
  1060. $content .= "$result->name, ";
  1061. }
  1062. }
  1063. return $content;
  1064. }
  1065. /************************************************************************
  1066. *
  1067. */
  1068. function tripal_feature_sync_features ($max_sync = 0, $job_id = NULL){
  1069. $i = 0;
  1070. // get the list of available sequence ontology terms for which
  1071. // we will build drupal pages from features in chado. If a feature
  1072. // is not one of the specified typse we won't build a node for it.
  1073. $allowed_types = variable_get('chado_feature_types','EST contig');
  1074. $allowed_types = preg_replace("/[\s\n\r]+/"," ",$allowed_types);
  1075. $so_terms = split(' ',$allowed_types);
  1076. $where_cvt = "";
  1077. foreach ($so_terms as $term){
  1078. $where_cvt .= "CVT.name = '$term' OR ";
  1079. }
  1080. $where_cvt = substr($where_cvt,0,strlen($where_cvt)-3); # strip trailing 'OR'
  1081. // get the list of organisms that are synced and only include features from
  1082. // those organisms
  1083. $orgs = tripal_organism_get_synced();
  1084. $where_org = "";
  1085. foreach($orgs as $org){
  1086. $where_org .= "F.organism_id = $org->organism_id OR ";
  1087. }
  1088. $where_org = substr($where_org,0,strlen($where_org)-3); # strip trailing 'OR'
  1089. // use this SQL statement to get the features that we're going to upload
  1090. $sql = "SELECT feature_id ".
  1091. "FROM {FEATURE} F ".
  1092. " INNER JOIN Cvterm CVT ON F.type_id = CVT.cvterm_id ".
  1093. "WHERE ($where_cvt) AND ($where_org) ".
  1094. "ORDER BY feature_id";
  1095. // get the list of features
  1096. $previous_db = db_set_active('chado'); // use chado database
  1097. $results = db_query($sql);
  1098. db_set_active($previous_db); // now use drupal database
  1099. // load into ids array
  1100. $count = 0;
  1101. $ids = array();
  1102. while($id = db_fetch_object($results)){
  1103. $ids[$count] = $id->feature_id;
  1104. $count++;
  1105. }
  1106. // make sure our vocabularies are set before proceeding
  1107. tripal_feature_set_vocabulary();
  1108. // pre-create the SQL statement that will be used to check
  1109. // if a feature has already been synced. We skip features
  1110. // that have been synced
  1111. $sql = "SELECT * FROM {chado_feature} WHERE feature_id = %d";
  1112. // Iterate through features that need to be synced
  1113. $interval = intval($count * 0.01);
  1114. foreach($ids as $feature_id){
  1115. // update the job status every 1% features
  1116. if($job_id and $i % $interval == 0){
  1117. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1118. }
  1119. // if we have a maximum number to sync then stop when we get there
  1120. // if not then just continue on
  1121. if($max_sync and $i == $max_sync){
  1122. return '';
  1123. }
  1124. if(!db_fetch_object(db_query($sql,$feature_id))){
  1125. tripal_feature_sync_feature ($feature_id);
  1126. }
  1127. $i++;
  1128. }
  1129. return '';
  1130. }
  1131. /************************************************************************
  1132. *
  1133. */
  1134. function tripal_feature_sync_feature ($feature_id){
  1135. global $user;
  1136. $create_node = 1; // set to 0 if the node exists and we just sync and not create
  1137. // get the accession prefix
  1138. $aprefix = variable_get('chado_feature_accession_prefix','ID');
  1139. // if we don't have a feature_id then return
  1140. if(!$feature_id){
  1141. drupal_set_message(t("Please provide a feature_id to sync"));
  1142. return '';
  1143. }
  1144. // get information about this feature
  1145. $fsql = "SELECT F.feature_id, F.name, F.uniquename,O.genus, ".
  1146. " O.species,CVT.name as cvname,F.residues,F.organism_id ".
  1147. "FROM {FEATURE} F ".
  1148. " INNER JOIN Cvterm CVT ON F.type_id = CVT.cvterm_id ".
  1149. " INNER JOIN Organism O ON F.organism_id = O.organism_ID ".
  1150. "WHERE F.feature_id = %d";
  1151. $previous_db = db_set_active('chado'); // use chado database
  1152. $feature = db_fetch_object(db_query($fsql,$feature_id));
  1153. db_set_active($previous_db); // now use drupal database
  1154. // check to make sure that we don't have any nodes with this feature name as a title
  1155. // but without a corresponding entry in the chado_feature table if so then we want to
  1156. // clean up that node. (If a node is found we don't know if it belongs to our feature or
  1157. // not since features can have the same name/title.)
  1158. $tsql = "SELECT * FROM {node} N ".
  1159. "WHERE title = '%s'";
  1160. $cnsql = "SELECT * FROM {chado_feature} ".
  1161. "WHERE nid = %d";
  1162. $nodes = db_query($tsql,$feature->name);
  1163. // cycle through all nodes that may have this title
  1164. while($node = db_fetch_object($nodes)){
  1165. $feature_nid = db_fetch_object(db_query($cnsql,$node->nid));
  1166. if(!$feature_nid){
  1167. drupal_set_message(t("$feature_id: A node is present but the chado_feature entry is missing... correcting"));
  1168. node_delete($node->nid);
  1169. }
  1170. }
  1171. // check if this feature already exists in the chado_feature table.
  1172. // if we have a chado feature, we want to check to see if we have a node
  1173. $cfsql = "SELECT * FROM {chado_feature} ".
  1174. "WHERE feature_id = %d";
  1175. $nsql = "SELECT * FROM {node} ".
  1176. "WHERE nid = %d";
  1177. $chado_feature = db_fetch_object(db_query($cfsql,$feature->feature_id));
  1178. if($chado_feature){
  1179. drupal_set_message(t("$feature_id: A chado_feature entry exists"));
  1180. $node = db_fetch_object(db_query($nsql,$chado_feature->nid));
  1181. if(!$node){
  1182. // if we have a chado_feature but not a node then we have a problem and
  1183. // need to cleanup
  1184. drupal_set_message(t("$feature_id: The node is missing, but has a chado_feature entry... correcting"));
  1185. $df_sql = "DELETE FROM {chado_feature} WHERE feature_id = %d";
  1186. db_query($df_sql,$feature_id);
  1187. } else {
  1188. drupal_set_message(t("$feature_id: A corresponding node exists"));
  1189. $create_node = 0;
  1190. }
  1191. }
  1192. // if we've encountered an error then just return.
  1193. if($error_msg = db_error()){
  1194. //print "$error_msg\n";
  1195. return '';
  1196. }
  1197. // if a drupal node does not exist for this feature then we want to
  1198. // create one. Note that the node_save call in this block
  1199. // will call the hook_submit function which
  1200. if($create_node){
  1201. drupal_set_message(t("$feature_id: Creating node $feature->name"));
  1202. $new_node = new stdClass();
  1203. $new_node->type = 'chado_feature';
  1204. $new_node->uid = $user->uid;
  1205. $new_node->title = "$feature->name";
  1206. $new_node->feature_id = $feature->feature_id;
  1207. $new_node->residues = $feature->residues;
  1208. $new_node->organism_id = $feature->organism_id;
  1209. $new_node->feature_type = $feature->cvname;
  1210. // validate the node and if okay then submit
  1211. node_validate($new_node);
  1212. if ($errors = form_get_errors()) {
  1213. foreach($errors as $key => $msg){
  1214. drupal_set_message($msg);
  1215. }
  1216. return $errors;
  1217. } else {
  1218. $node = node_submit($new_node);
  1219. node_save($node);
  1220. }
  1221. }
  1222. else {
  1223. $node = $chado_feature;
  1224. }
  1225. // set the taxonomy for this node
  1226. drupal_set_message(t("$feature_id ($node->nid): setting taxonomy"));
  1227. tripal_feature_set_taxonomy($node,$feature_id);
  1228. // reindex the node
  1229. drupal_set_message(t("$feature_id( $node->nid): indexing"));
  1230. tripal_feature_index_feature ($feature_id,$node->nid);
  1231. // remove any URL alias that may already exist and recreate
  1232. drupal_set_message(t("$feature_id ($node->nid): setting URL alias"));
  1233. db_query("DELETE FROM {url_alias} WHERE dst = '%s'", "$aprefix$feature_id");
  1234. path_set_alias("node/$node->nid","$aprefix$feature_id");
  1235. return '';
  1236. }
  1237. /************************************************************************
  1238. *
  1239. */
  1240. function tripal_feature_set_vocabulary (){
  1241. //include the file containing the required functions for adding taxonomy vocabs
  1242. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  1243. // get the vocabularies so that we make sure we don't recreate
  1244. // the vocabs that already exist
  1245. $vocabularies = taxonomy_get_vocabularies();
  1246. $ft_vid = NULL;
  1247. $op_vid = NULL;
  1248. $lb_vid = NULL;
  1249. $an_vid = NULL;
  1250. // These taxonomic terms are hard coded because we
  1251. // konw we have these relationships in the chado tables
  1252. // through foreign key relationships. The tripal
  1253. // modules that correspond to these chado "modules" don't
  1254. // need to be installed for the taxonomy to work.
  1255. foreach($vocabularies as $vocab){
  1256. if($vocab->name == 'Feature Type'){
  1257. $ft_vid = $vocab->vid;
  1258. }
  1259. if($vocab->name == 'Organism'){
  1260. $op_vid = $vocab->vid;
  1261. }
  1262. if($vocab->name == 'Library'){
  1263. $lb_vid = $vocab->vid;
  1264. }
  1265. if($vocab->name == 'Analysis'){
  1266. $an_vid = $vocab->vid;
  1267. }
  1268. }
  1269. if(!$ft_vid){
  1270. $form_state = array();
  1271. $values = array(
  1272. 'name' => t('Feature Type'),
  1273. 'nodes' => array('chado_feature' => 'chado_feature'),
  1274. 'description' => t('The feature type (or SO cvterm for this feature).'),
  1275. 'help' => t('Select the term that matches the feature '),
  1276. 'tags' => 0,
  1277. 'hierarchy' => 1,
  1278. 'relations' => 1,
  1279. 'multiple' => 0,
  1280. 'required' => 0,
  1281. 'weight' => 1,
  1282. );
  1283. drupal_execute('taxonomy_form_vocabulary', $form_state,$values);
  1284. drupal_execute('taxonomy_form_vocabulary', $form_state);
  1285. }
  1286. if(!$op_vid){
  1287. $form_state = array();
  1288. $values = array(
  1289. 'name' => t('Organism'),
  1290. 'nodes' => array('chado_feature' => 'chado_feature'),
  1291. 'description' => t('The organism to which this feature belongs.'),
  1292. 'help' => t('Select the term that matches the feature '),
  1293. 'tags' => 0,
  1294. 'hierarchy' => 1,
  1295. 'relations' => 1,
  1296. 'multiple' => 0,
  1297. 'required' => 0,
  1298. 'weight' => 2,
  1299. );
  1300. drupal_execute('taxonomy_form_vocabulary', $form_state,$values);
  1301. drupal_execute('taxonomy_form_vocabulary', $form_state);
  1302. }
  1303. if(!$lb_vid){
  1304. $form_state = array();
  1305. $values = array(
  1306. 'name' => t('Library'),
  1307. 'nodes' => array('chado_feature' => 'chado_feature'),
  1308. 'description' => t('Chado features associated with a library are assigned the term associated with the library'),
  1309. 'help' => t('Select the term that matches the feature '),
  1310. 'tags' => 0,
  1311. 'hierarchy' => 1,
  1312. 'relations' => 1,
  1313. 'multiple' => 0,
  1314. 'required' => 0,
  1315. 'weight' => 3,
  1316. );
  1317. drupal_execute('taxonomy_form_vocabulary', $form_state, $values);
  1318. drupal_execute('taxonomy_form_vocabulary', $form_state);
  1319. }
  1320. if(!$an_vid){
  1321. $form_state = array();
  1322. $values = array(
  1323. 'name' => t('Analysis'),
  1324. 'nodes' => array('chado_feature' => 'chado_feature'),
  1325. 'description' => t('Any analysis to which this feature belongs.'),
  1326. 'help' => t('Select the term that matches the feature '),
  1327. 'tags' => 0,
  1328. 'hierarchy' => 1,
  1329. 'relations' => 1,
  1330. 'multiple' => 1,
  1331. 'required' => 0,
  1332. 'weight' => 4,
  1333. );
  1334. drupal_execute('taxonomy_form_vocabulary', $form_state,$values);
  1335. drupal_execute('taxonomy_form_vocabulary', $form_state);
  1336. }
  1337. }
  1338. /************************************************************************
  1339. *
  1340. */
  1341. function tripal_feature_del_vocabulary(){
  1342. //include the file containing the required functions for adding taxonomy vocabs
  1343. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  1344. // get the vocabularies
  1345. $vocabularies = taxonomy_get_vocabularies();
  1346. // These taxonomic terms are hard coded because we
  1347. // know we have these relationships in the chado tables
  1348. // through foreign key relationships. The tripal
  1349. // modules that correspond to these chado "modules" don't
  1350. // need to be installed for the taxonomy to work.
  1351. foreach($vocabularies as $vocab){
  1352. if($vocab->name == 'Feature Type'){
  1353. taxonomy_del_vocabulary($vocab->vid);
  1354. }
  1355. if($vocab->name == 'Organism'){
  1356. taxonomy_del_vocabulary($vocab->vid);
  1357. }
  1358. if($vocab->name == 'Library'){
  1359. taxonomy_del_vocabulary($vocab->vid);
  1360. }
  1361. if($vocab->name == 'Analysis'){
  1362. taxonomy_del_vocabulary($vocab->vid);
  1363. }
  1364. }
  1365. }
  1366. /************************************************************************
  1367. *
  1368. */
  1369. function tripal_features_set_taxonomy($max_sync = 0,$job_id = NULL){
  1370. // make sure our vocabularies are cleaned and reset before proceeding
  1371. tripal_feature_del_vocabulary();
  1372. tripal_feature_set_vocabulary();
  1373. // iterate through all drupal feature nodes and set the taxonomy
  1374. $results = db_query("SELECT * FROM {chado_feature}");
  1375. $nsql = "SELECT * FROM {node} ".
  1376. "WHERE nid = %d";
  1377. $i = 0;
  1378. // load into ids array
  1379. $count = 0;
  1380. $chado_features = array();
  1381. while($chado_feature = db_fetch_object($results)){
  1382. $chado_features[$count] = $chado_feature;
  1383. $count++;
  1384. }
  1385. // Iterate through features that need to be synced
  1386. $interval = intval($count * 0.01);
  1387. foreach($chado_features as $chado_feature){
  1388. // update the job status every 1% features
  1389. if($job_id and $i % $interval == 0){
  1390. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1391. }
  1392. $node = db_fetch_object(db_query($nsql,$chado_feature->nid));
  1393. tripal_feature_set_taxonomy($node,$chado_feature->feature_id);
  1394. $i++;
  1395. }
  1396. }
  1397. /************************************************************************
  1398. *
  1399. */
  1400. function tripal_feature_set_taxonomy ($node,$feature_id){
  1401. // iterate through the taxonomy classes that have been
  1402. // selected by the admin user and make sure we only set those
  1403. $tax_classes = variable_get('tax_classes', '');
  1404. $do_ft = 0;
  1405. $do_op = 0;
  1406. $do_lb = 0;
  1407. $do_an = 0;
  1408. foreach($tax_classes as $class){
  1409. if(strcmp($class ,'organism')==0){
  1410. $do_op = 1;
  1411. }
  1412. if(strcmp($class,'feature_type')==0){
  1413. $do_ft = 1;
  1414. }
  1415. if(strcmp($class,'library')==0){
  1416. $do_lb = 1;
  1417. }
  1418. if(strcmp($class,'analysis')==0){
  1419. $do_an = 1;
  1420. }
  1421. }
  1422. // get the list of vocabularies and find our two vocabularies of interest
  1423. $vocabularies = taxonomy_get_vocabularies();
  1424. $ft_vid = NULL;
  1425. $op_vid = NULL;
  1426. $lb_vid = NULL;
  1427. $an_vid = NULL;
  1428. foreach($vocabularies as $vocab){
  1429. if($vocab->name == 'Feature Type'){
  1430. $ft_vid = $vocab->vid;
  1431. }
  1432. if($vocab->name == 'Organism'){
  1433. $op_vid = $vocab->vid;
  1434. }
  1435. if($vocab->name == 'Library'){
  1436. $lb_vid = $vocab->vid;
  1437. }
  1438. if($vocab->name == 'Analysis'){
  1439. $an_vid = $vocab->vid;
  1440. }
  1441. }
  1442. // get the cvterm and the organism for this feature
  1443. $sql = "SELECT CVT.name AS cvname, O.genus, O.species ".
  1444. "FROM {CVTerm} CVT ".
  1445. " INNER JOIN Feature F on F.type_id = CVT.cvterm_id ".
  1446. " INNER JOIN Organism O ON F.organism_id = O.organism_id ".
  1447. "WHERE F.feature_id = $feature_id";
  1448. $previous_db = db_set_active('chado'); // use chado database
  1449. $feature = db_fetch_object(db_query($sql));
  1450. db_set_active($previous_db); // now use drupal database
  1451. // Set the feature type for this feature
  1452. if($do_ft && $ft_vid){
  1453. $tags["$ft_vid"] = "$feature->cvname";
  1454. }
  1455. // Set the organism for this feature type
  1456. if($do_op && $op_vid){
  1457. $tags["$op_vid"] = "$feature->genus $feature->species";
  1458. }
  1459. // get the library that this feature may belong to and add it as taxonomy
  1460. if($do_lb && $lb_vid){
  1461. $sql = "SELECT L.name ".
  1462. "FROM {Library} L ".
  1463. " INNER JOIN Library_feature LF ON LF.library_id = L.library_id ".
  1464. "WHERE LF.feature_id = %d ";
  1465. $previous_db = db_set_active('chado'); // use chado database
  1466. $library = db_fetch_object(db_query($sql,$feature_id));
  1467. db_set_active($previous_db); // now use drupal database
  1468. $tags["$lb_vid"] = "$library->name";
  1469. }
  1470. // get the analysis that this feature may belong to and add it as taxonomy
  1471. if($do_an && $an_vid){
  1472. $sql = "SELECT A.name ".
  1473. "FROM {Analysis} A ".
  1474. " INNER JOIN Analysisfeature AF ON AF.analysis_id = A.analysis_id ".
  1475. "WHERE AF.feature_id = $feature_id ";
  1476. $results = db_query($sql);
  1477. $previous_db = db_set_active('chado'); // use chado database
  1478. $analysis_terms = array();
  1479. while($analysis=db_fetch_object($results)){
  1480. // TODO -- how to set more than one taxonmy term ????
  1481. $analysis_terms[] = "$analysis->name";
  1482. // $tags["$an_vid"] = "$analysis->name";
  1483. }
  1484. $tags["$an_vid"] = $analysis_terms;
  1485. db_set_active($previous_db); // now use drupal database
  1486. }
  1487. // now add the taxonomy to the node
  1488. $terms['tags'] = $tags;
  1489. taxonomy_node_save($node,$terms);
  1490. // print "Setting $node->title: " . implode(", ",$tags) . "\n";
  1491. }
  1492. /************************************************************************
  1493. *
  1494. */
  1495. function tripal_features_reindex ($max_sync,$job_id = NULL){
  1496. $i = 0;
  1497. // We register a shutdown function to ensure that the nodes
  1498. // that are indexed will have proper entries in the search_totals
  1499. // table. Without these entries, the searching doesn't work
  1500. // properly. This function may run for quite a while since
  1501. // it must calculate the sum of the scores of all entries in
  1502. // the search_index table. In the case of common words like
  1503. // 'contig', this will take quite a while
  1504. register_shutdown_function('search_update_totals');
  1505. // use this SQL statement to get the features that we're going to index. This
  1506. // SQL statement is derived from the hook_search function in the Drupal API.
  1507. // Essentially, this is the SQL statement that finds all nodes that need
  1508. // reindexing, but adjusted to include the chado_feature
  1509. $sql = "SELECT N.nid, N.title, CF.feature_id ".
  1510. "FROM {node} N ".
  1511. " INNER JOIN chado_feature CF ON CF.nid = N.nid ";
  1512. $results = db_query($sql);
  1513. // load into ids array
  1514. $count = 0;
  1515. $chado_features = array();
  1516. while($chado_feature = db_fetch_object($results)){
  1517. $chado_features[$count] = $chado_feature;
  1518. $count++;
  1519. }
  1520. // Iterate through features that need to be synced
  1521. $interval = intval($count * 0.01);
  1522. foreach($chado_features as $chado_feature){
  1523. // update the job status every 1% features
  1524. if($job_id and $i % $interval == 0){
  1525. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1526. }
  1527. // sync only the max requested
  1528. if($max_sync and $i == $max_sync){
  1529. return '';
  1530. }
  1531. tripal_feature_index_feature ($chado_feature->feature_id,$chado_feature->nid);
  1532. $i++;
  1533. }
  1534. return '';
  1535. }
  1536. /************************************************************************
  1537. *
  1538. */
  1539. function tripal_feature_index_feature ($feature_id,$nid){
  1540. // return if we haven't been provided with a feature_id
  1541. if(!$feature_id){
  1542. return 0;
  1543. }
  1544. // if we only have a feature_id then let's find a corresponding
  1545. // node. If we can't find a node then return.
  1546. if(!$nid){
  1547. $nsql = "SELECT N.nid,N.title FROM {chado_feature} CF ".
  1548. " INNER JOIN {node} N ON N.nid = CF.nid ".
  1549. "WHERE CF.feature_id = %d";
  1550. $node = db_fetch_object(db_query($nsql,$feature_id));
  1551. if(!$node){
  1552. return 0;
  1553. }
  1554. $node = node_load($node->nid);
  1555. } else {
  1556. $node = node_load($nid);
  1557. }
  1558. // node load the noad, the comments and the taxonomy and
  1559. // index
  1560. $node->build_mode = NODE_BUILD_SEARCH_INDEX;
  1561. $node = node_build_content($node, FALSE, FALSE);
  1562. $node->body = drupal_render($node->content);
  1563. node_invoke_nodeapi($node, 'view', FALSE, FALSE);
  1564. $node->body .= module_invoke('comment', 'nodeapi', $node, 'update index');
  1565. $node->body .= module_invoke('taxonomy','nodeapi', $node, 'update index');
  1566. // print "$node->title: $node->body\n";
  1567. search_index($node->nid,'node',$node->body);
  1568. return 1;
  1569. }
  1570. /************************************************************************
  1571. *
  1572. */
  1573. function tripal_features_cleanup($dummy = NULL, $job_id = NULL) {
  1574. // build the SQL statments needed to check if nodes point to valid features
  1575. $dsql = "SELECT * FROM {node} WHERE type = 'chado_feature' order by nid";
  1576. $nsql = "SELECT * FROM {node} WHERE nid = %d";
  1577. $csql = "SELECT * FROM {chado_feature} where nid = %d ";
  1578. $cfsql= "SELECT * FROM {chado_feature}";
  1579. $tsql = "SELECT * FROM {feature} F ".
  1580. " INNER JOIN CVTerm CVT ON F.type_id = CVT.cvterm_id ".
  1581. "WHERE feature_id = %d AND (";
  1582. $supported_ftypes = split("[ \n]",variable_get('chado_feature_types','EST contig'));
  1583. foreach($supported_ftypes as $ftype){
  1584. $tsql .= " CVT.name = '$ftype' OR ";
  1585. }
  1586. $tsql .= " 0=1) "; // add a 0=1 just as a filler so we don't have to remove a trailing 'OR'
  1587. // load into nodes array
  1588. $results = db_query($dsql);
  1589. $count = 0;
  1590. $nodes = array();
  1591. while($node = db_fetch_object($results)){
  1592. $nodes[$count] = $node;
  1593. $count++;
  1594. }
  1595. // load the chado_features into an array
  1596. $results = db_query($cfsql);
  1597. $cnodes = array();
  1598. while($node = db_fetch_object($results)){
  1599. $cnodes[$count] = $node;
  1600. $count++;
  1601. }
  1602. $interval = intval($count * 0.01);
  1603. // iterate through all of the chado_feature nodes and delete those that aren't valid
  1604. foreach($nodes as $nid){
  1605. // update the job status every 1% features
  1606. if($job_id and $i % $interval == 0){
  1607. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1608. }
  1609. // first check to see if the node has a corresponding entry
  1610. // in the chado_feature table. If not then delete the node.
  1611. $feature = db_fetch_object(db_query($csql,$nid->nid));
  1612. if(!$feature){
  1613. node_delete($nid->nid);
  1614. $message = "Missing in chado_feature table.... DELETING: $nid->nid\n";
  1615. watchdog('tripal_feature',$message,array(),WATCHDOG_WARNING);
  1616. continue;
  1617. }
  1618. // second check to see if the node is for a feature of an allowed type.
  1619. // if not, then delete the node. This check will also take care of the
  1620. // case when a node exists and an entry in the chado_feature table exists
  1621. // but no feature with a matching feature_id exists
  1622. $previous_db = db_set_active('chado'); // use chado database
  1623. $ftype = db_fetch_object(db_query($tsql,$feature->feature_id));
  1624. db_set_active($previous_db); // now use drupal database
  1625. if(!$ftype){
  1626. node_delete($nid->nid);
  1627. db_query("DELETE FROM {chado_feature} WHERE feature_id = $feature->feature_id");
  1628. $message = "Node of the wrong feature type.... DELETING: $nid->nid\n";
  1629. watchdog('tripal_feature',$message,array(),WATCHDOG_WARNING);
  1630. }
  1631. $i++;
  1632. }
  1633. // iterate through all of the chado_feature nodes and delete those that aren't valid
  1634. foreach($cnodes as $nid){
  1635. // update the job status every 1% features
  1636. if($job_id and $i % $interval == 0){
  1637. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1638. }
  1639. $node = db_fetch_object(db_query($nsql,$nid->nid));
  1640. if(!$node){
  1641. db_query("DELETE FROM {chado_feature} WHERE nid = $nid->nid");
  1642. $message = "chado_feature missing node.... DELETING: $nid->nid\n";
  1643. watchdog('tripal_feature',$message,array(),WATCHDOG_WARNING);
  1644. }
  1645. $i++;
  1646. }
  1647. return '';
  1648. }
  1649. /************************************************************************
  1650. *
  1651. */
  1652. function tripal_feature_bulkload(){
  1653. return drupal_get_form('tripal_feature_load_fasta_form');
  1654. }
  1655. /************************************************************************
  1656. *
  1657. */
  1658. function tripal_feature_load_fasta_form (&$form_state = NULL){
  1659. // get the list of organisms
  1660. $sql = "SELECT * FROM {Organism} ORDER BY genus,species";
  1661. $previous_db = db_set_active('chado'); // use chado database
  1662. $org_rset = db_query($sql);
  1663. db_set_active($previous_db); // now use drupal database
  1664. $organisms = array();
  1665. $organisms[''] = '';
  1666. while($organism = db_fetch_object($org_rset)){
  1667. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  1668. }
  1669. // get the list of supported feature types
  1670. $ftypes = array();
  1671. $ftypes[''] = '';
  1672. $supported_ftypes = split("[ \n]",variable_get('chado_feature_feature_types','EST contig'));
  1673. foreach($supported_ftypes as $ftype){
  1674. $ftypes["$ftype"] = $ftype;
  1675. }
  1676. // get the list of libraries
  1677. // TODO !!!! Use Ajax to filter this automatically based on the organism
  1678. // selected by the user. This will prevent mistakes from user input.
  1679. $sql = "SELECT * FROM {Library} L ".
  1680. " INNER JOIN Organism O ON L.organism_id = O.organism_id ".
  1681. "ORDER BY L.name";
  1682. $previous_db = db_set_active('chado'); // use chado database
  1683. $lib_rset = db_query($sql);
  1684. db_set_active($previous_db); // now use drupal database
  1685. $libraries = array();
  1686. $libraries[''] = '';
  1687. while($library = db_fetch_object($lib_rset)){
  1688. $libraries[$library->library_id] = "$library->name ($library->genus $library->species)";
  1689. }
  1690. $form['#attributes']['enctype'] = 'multipart/form-data';
  1691. $form['organism'] = array (
  1692. '#title' => t('Organism'),
  1693. '#type' => t('select'),
  1694. '#description' => t("Choose the organism with which these sequences are associated "),
  1695. '#required' => TRUE,
  1696. '#default_vaule' => '',
  1697. '#options' => $organisms,
  1698. '#weight' => 1,
  1699. );
  1700. $form['library'] = array (
  1701. '#title' => t('Library'),
  1702. '#type' => t('select'),
  1703. '#description' => t("Choose the library with from which these sequences are derived. Leave blank if not applicable."),
  1704. '#required' => FALSE,
  1705. '#default_vaule' => '',
  1706. '#options' => $libraries,
  1707. '#weight' => 2,
  1708. );
  1709. $form['ftype'] = array (
  1710. '#title' => t('Feature Type'),
  1711. '#type' => t('select'),
  1712. '#description' => t("Choose the category of sequences you are uploading. All sequences in the FASTA file will be imported as this type"),
  1713. '#required' => TRUE,
  1714. '#default_vaule' => '',
  1715. '#options' => $ftypes,
  1716. '#weight' => 3,
  1717. );
  1718. $form['fasta_file'] = array(
  1719. '#type' => t('file'),
  1720. '#title' => t('Fasta File'),
  1721. '#description' => t('Upload a FASTA file of sequences. The definition line should contain only the feature name. All other annotations should be removed. The file must not be larger than ' . file_upload_max_size() . ' bytes'),
  1722. '#weight' => 4,
  1723. );
  1724. $form['upload'] = array(
  1725. '#type' => 'submit',
  1726. '#value' => t('Upload File'),
  1727. '#weight' => 2,
  1728. '#executes_submit_callback' => TRUE,
  1729. '#weight' => 5,
  1730. );
  1731. return $form;
  1732. }
  1733. /************************************************************************
  1734. *
  1735. */
  1736. function tripal_feature_load_fasta_form_validate($form, &$form_state){
  1737. // TODO !!! check that the fasta file is valid
  1738. global $user;
  1739. // we need a path within the drupal installation to temporarily use as the destination
  1740. // after we upload, we'll move the file to analysis directory
  1741. $upload_url = file_directory_path() . "/chado_feature_bulk_upload/$user->uid";
  1742. // create the download directory. We do it this way rather than the
  1743. // file_check_directory because we don't want a drupal message presented
  1744. // the user when the directory is created.
  1745. if (!is_dir($upload_url)) {
  1746. mkdir($upload_url,0775,TRUE);
  1747. }
  1748. // upload the file and copy it to the proper location
  1749. $validators = array(); // we don't have any validators
  1750. if($file = file_save_upload('fasta_file',$validators,$upload_url)){
  1751. drupal_set_message("File $file->name uploaded succesfully");
  1752. } else {
  1753. form_set_error('fasta_file',t('Upload Failed'));
  1754. }
  1755. }
  1756. /************************************************************************
  1757. *
  1758. */
  1759. function tripal_feature_load_fasta_form_submit($form, &$form_state){
  1760. global $user;
  1761. // add a job to be executed
  1762. tripal_add_job ($job_name,$type,$callback,$uid);
  1763. }
  1764. /************************************************************************
  1765. *
  1766. */
  1767. function tripal_feature_return_fasta($feature,$desc){
  1768. $fasta = ">" . variable_get('chado_feature_accession_prefix','ID') . "$feature->feature_id|$feature->name";
  1769. $fasta .= " $desc\n";
  1770. $fasta .= wordwrap($feature->residues, 50, "\n", true);
  1771. $fasta .= "\n\n";
  1772. return $fasta;
  1773. }