syncFeatures.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. # This script can be run as a stand-alone script to sync all the features from chado to drupal
  3. // Parameter f specifies the feature_id to sync
  4. // -f 0 will sync all features
  5. $arguments = getopt("f:");
  6. if(isset($arguments['f'])){
  7. $drupal_base_url = parse_url('http://www.example.com');
  8. $_SERVER['HTTP_HOST'] = $drupal_base_url['host'];
  9. $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'];
  10. $_SERVER['REMOTE_ADDR'] = NULL;
  11. $_SERVER['REQUEST_METHOD'] = NULL;
  12. require_once 'includes/bootstrap.inc';
  13. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  14. $feature_id = $arguments['f'];
  15. if($feature_id > 0 ){
  16. tripal_feature_sync_feature($feature_id);
  17. }
  18. else{
  19. print "syncing all features...\n";
  20. tripal_feature_sync_features();
  21. }
  22. }
  23. /**
  24. *
  25. */
  26. function tripal_feature_sync_form (){
  27. $form['description'] = array(
  28. '#type' => 'item',
  29. '#value' => t("Add feature types, optionally select an organism and ".
  30. "click the 'Sync all Features' button to create Drupal ".
  31. "content for features in chado. Only features of the types listed ".
  32. "below in the Feature Types box will be synced. You may limit the ".
  33. "features to be synced by a specific organism. Depending on the ".
  34. "number of features in the chado database this may take a long ".
  35. "time to complete. "),
  36. );
  37. $form['feature_types'] = array(
  38. '#title' => t('Feature Types'),
  39. '#type' => 'textarea',
  40. '#description' => t('Enter the names of the sequence types that the ".
  41. "site will support with independent pages. Pages for these data ".
  42. "types will be built automatically for features that exist in the ".
  43. "chado database. The names listed here should be spearated by ".
  44. "spaces or entered separately on new lines. The names must match ".
  45. "exactly (spelling and case) with terms in the sequence ontology'),
  46. '#required' => TRUE,
  47. '#default_value' => variable_get('chado_feature_types','EST contig'),
  48. );
  49. // get the list of organisms
  50. $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  51. $orgs = tripal_organism_get_synced();
  52. $organisms[] = '';
  53. foreach($orgs as $organism){
  54. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  55. }
  56. $form['organism_id'] = array (
  57. '#title' => t('Organism'),
  58. '#type' => t('select'),
  59. '#description' => t("Choose the organism for which features will be deleted."),
  60. '#options' => $organisms,
  61. );
  62. $form['button'] = array(
  63. '#type' => 'submit',
  64. '#value' => t('Sync all Features'),
  65. '#weight' => 3,
  66. );
  67. return $form;
  68. }
  69. /**
  70. *
  71. */
  72. function tripal_feature_sync_form_validate ($form, &$form_state){
  73. $organism_id = $form_state['values']['organism_id'];
  74. $feature_types = $form_state['values']['feature_types'];
  75. // nothing to do
  76. }
  77. /**
  78. *
  79. */
  80. function tripal_feature_sync_form_submit ($form, &$form_state){
  81. global $user;
  82. $organism_id = $form_state['values']['organism_id'];
  83. $feature_types = $form_state['values']['feature_types'];
  84. $job_args = array(0,$organism_id,$feature_types);
  85. if($organism_id){
  86. $organism = tripal_core_chado_select('organism',array('genus','species'),array('organism_id' => $organism_id));
  87. $title = "Sync all features for " . $organism[0]->genus . " " . $organism[0]->species;
  88. } else {
  89. $title = t('Sync all features for all synced organisms');
  90. }
  91. variable_get('chado_feature_types',$feature_types);
  92. tripal_add_job($title,'tripal_feature',
  93. 'tripal_feature_sync_features',$job_args,$user->uid);
  94. }
  95. /**
  96. *
  97. */
  98. function tripal_feature_set_urls($job_id = NULL){
  99. // first get the list of features that have been synced
  100. $sql = "SELECT * FROM {chado_feature}";
  101. $nodes = db_query($sql);
  102. while($node = db_fetch_object($nodes)){
  103. // now get the feature details
  104. $feature_arr = tripal_core_chado_select('feature',
  105. array('feature_id','name','uniquename'),
  106. array('feature_id' => $node->feature_id));
  107. $feature = $feature_arr[0];
  108. tripal_feature_set_feature_url($node,$feature);
  109. }
  110. }
  111. /**
  112. *
  113. */
  114. function tripal_feature_set_feature_url($node,$feature){
  115. // determine which URL alias to use
  116. $alias_type = variable_get('chado_feature_url','internal ID');
  117. $aprefix = variable_get('chado_feature_accession_prefix','ID');
  118. switch ($alias_type) {
  119. case 'feature name':
  120. $url_alias = $feature->name;
  121. break;
  122. case 'feature unique name':
  123. $url_alias = $feature->uniquename;
  124. break;
  125. default:
  126. $url_alias = "$aprefix$feature->feature_id";
  127. }
  128. print "Setting $alias_type as URL alias for $feature->name: node/$node->nid => $url_alias\n";
  129. // remove any previous alias
  130. db_query("DELETE FROM {url_alias} WHERE src = '%s'", "node/$node->nid");
  131. // add the new alias
  132. path_set_alias("node/$node->nid",$url_alias);
  133. }
  134. /**
  135. *
  136. *
  137. * @ingroup tripal_feature
  138. */
  139. function tripal_feature_sync_features ($max_sync = 0, $organism_id = NULL,
  140. $feature_types = NULL, $job_id = NULL)
  141. {
  142. //print "Syncing features (max of $max_sync)\n";
  143. $i = 0;
  144. // get the list of available sequence ontology terms for which
  145. // we will build drupal pages from features in chado. If a feature
  146. // is not one of the specified typse we won't build a node for it.
  147. if(!$feature_types){
  148. $allowed_types = variable_get('chado_feature_types','EST contig');
  149. } else {
  150. $allowed_types = $feature_types;
  151. }
  152. $allowed_types = preg_replace("/[\s\n\r]+/"," ",$allowed_types);
  153. print "Looking for features of type: $allowed_types\n";
  154. $so_terms = split(' ',$allowed_types);
  155. $where_cvt = "";
  156. foreach ($so_terms as $term){
  157. $where_cvt .= "CVT.name = '$term' OR ";
  158. }
  159. $where_cvt = substr($where_cvt,0,strlen($where_cvt)-3); # strip trailing 'OR'
  160. // get the list of organisms that are synced and only include features from
  161. // those organisms
  162. $orgs = tripal_organism_get_synced();
  163. $where_org = "";
  164. foreach($orgs as $org){
  165. if($organism_id){
  166. if($org->organism_id and $org->organism_id == $organism_id){
  167. $where_org .= "F.organism_id = $org->organism_id OR ";
  168. }
  169. }
  170. else {
  171. if($org->organism_id){
  172. $where_org .= "F.organism_id = $org->organism_id OR ";
  173. }
  174. }
  175. }
  176. $where_org = substr($where_org,0,strlen($where_org)-3); # strip trailing 'OR'
  177. // use this SQL statement to get the features that we're going to upload
  178. $sql = "SELECT feature_id ".
  179. "FROM {FEATURE} F ".
  180. " INNER JOIN Cvterm CVT ON F.type_id = CVT.cvterm_id ".
  181. " INNER JOIN CV on CV.cv_id = CVT.cv_id ".
  182. "WHERE ($where_cvt) AND ($where_org) AND CV.name = 'sequence' ".
  183. "ORDER BY feature_id";
  184. // get the list of features
  185. $previous_db = tripal_db_set_active('chado'); // use chado database
  186. $results = db_query($sql);
  187. tripal_db_set_active($previous_db); // now use drupal database
  188. // load into ids array
  189. $count = 0;
  190. $ids = array();
  191. while($id = db_fetch_object($results)){
  192. $ids[$count] = $id->feature_id;
  193. $count++;
  194. }
  195. // make sure our vocabularies are set before proceeding
  196. tripal_feature_set_vocabulary();
  197. // pre-create the SQL statement that will be used to check
  198. // if a feature has already been synced. We skip features
  199. // that have been synced
  200. $sql = "SELECT * FROM {chado_feature} WHERE feature_id = %d";
  201. // Iterate through features that need to be synced
  202. $interval = intval($count * 0.01);
  203. if($interval > 1){
  204. $interval = 1;
  205. }
  206. $num_ids = sizeof($ids);
  207. $i = 0;
  208. foreach($ids as $feature_id){
  209. // update the job status every 1% features
  210. if($job_id and $i % $interval == 0){
  211. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  212. }
  213. // if we have a maximum number to sync then stop when we get there
  214. // if not then just continue on
  215. if($max_sync and $i == $max_sync){
  216. return '';
  217. }
  218. if(!db_fetch_object(db_query($sql,$feature_id))){
  219. # parsing all the features can cause memory overruns
  220. # we are not sure why PHP does not clean up the memory as it goes
  221. # to avoid this problem we will call this script through an
  222. # independent system call
  223. print "$i of $num_ids Syncing feature id: $feature_id\n";
  224. $cmd = "php " . drupal_get_path('module', 'tripal_feature') . "/syncFeatures.php -f $feature_id ";
  225. system($cmd);
  226. }
  227. $i++;
  228. }
  229. return '';
  230. }
  231. /**
  232. *
  233. *
  234. * @ingroup tripal_feature
  235. */
  236. function tripal_feature_sync_feature ($feature_id){
  237. // print "\tSyncing feature $feature_id\n";
  238. $mem = memory_get_usage(TRUE);
  239. $mb = $mem/1048576;
  240. // print "$mb mb\n";
  241. global $user;
  242. $create_node = 1; // set to 0 if the node exists and we just sync and not create
  243. // get the accession prefix
  244. $aprefix = variable_get('chado_feature_accession_prefix','ID');
  245. // if we don't have a feature_id then return
  246. if(!$feature_id){
  247. drupal_set_message(t("Please provide a feature_id to sync"));
  248. return '';
  249. }
  250. // get information about this feature
  251. $fsql = "SELECT F.feature_id, F.name, F.uniquename,O.genus, ".
  252. " O.species,CVT.name as cvname,F.residues,F.organism_id ".
  253. "FROM {FEATURE} F ".
  254. " INNER JOIN Cvterm CVT ON F.type_id = CVT.cvterm_id ".
  255. " INNER JOIN Organism O ON F.organism_id = O.organism_ID ".
  256. "WHERE F.feature_id = %d";
  257. $previous_db = tripal_db_set_active('chado'); // use chado database
  258. $feature = db_fetch_object(db_query($fsql,$feature_id));
  259. tripal_db_set_active($previous_db); // now use drupal database
  260. // get the synonyms for this feature
  261. $synsql = "SELECT S.name ".
  262. "FROM {feature_synonym} FS ".
  263. " INNER JOIN {synonym} S on FS.synonym_id = S.synonym_id ".
  264. "WHERE FS.feature_id = %d";
  265. $previous_db = tripal_db_set_active('chado'); // use chado database
  266. $synonyms = db_query($synsql,$feature_id);
  267. tripal_db_set_active($previous_db); // now use drupal database
  268. // now add these synonyms to the feature object as a single string
  269. $synstring = '';
  270. while($synonym = db_fetch_object($synonyms)){
  271. $synstring .= "$synonym->name\n";
  272. }
  273. $feature->synonyms = $synstring;
  274. // check to make sure that we don't have any nodes with this feature name as a title
  275. // but without a corresponding entry in the chado_feature table if so then we want to
  276. // clean up that node. (If a node is found we don't know if it belongs to our feature or
  277. // not since features can have the same name/title.)
  278. $tsql = "SELECT * FROM {node} N ".
  279. "WHERE title = '%s'";
  280. $cnsql = "SELECT * FROM {chado_feature} ".
  281. "WHERE nid = %d";
  282. $nodes = db_query($tsql,$feature->name);
  283. // cycle through all nodes that may have this title
  284. while($node = db_fetch_object($nodes)){
  285. $feature_nid = db_fetch_object(db_query($cnsql,$node->nid));
  286. if(!$feature_nid){
  287. drupal_set_message(t("$feature_id: A node is present but the chado_feature entry is missing... correcting"));
  288. node_delete($node->nid);
  289. }
  290. }
  291. // check if this feature already exists in the chado_feature table.
  292. // if we have a chado feature, we want to check to see if we have a node
  293. $cfsql = "SELECT * FROM {chado_feature} ".
  294. "WHERE feature_id = %d";
  295. $nsql = "SELECT * FROM {node} ".
  296. "WHERE nid = %d";
  297. $chado_feature = db_fetch_object(db_query($cfsql,$feature->feature_id));
  298. if($chado_feature){
  299. drupal_set_message(t("$feature_id: A chado_feature entry exists"));
  300. $node = db_fetch_object(db_query($nsql,$chado_feature->nid));
  301. if(!$node){
  302. // if we have a chado_feature but not a node then we have a problem and
  303. // need to cleanup
  304. drupal_set_message(t("$feature_id: The node is missing, but has a chado_feature entry... correcting"));
  305. $df_sql = "DELETE FROM {chado_feature} WHERE feature_id = %d";
  306. db_query($df_sql,$feature_id);
  307. } else {
  308. drupal_set_message(t("$feature_id: A corresponding node exists"));
  309. $create_node = 0;
  310. }
  311. }
  312. // if we've encountered an error then just return.
  313. if($error_msg = db_error()){
  314. //print "$error_msg\n";
  315. return '';
  316. }
  317. // if a drupal node does not exist for this feature then we want to
  318. // create one. Note that the node_save call in this block
  319. // will call the hook_submit function which
  320. if($create_node){
  321. // get the organism for this feature
  322. $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
  323. $organism = db_fetch_object(db_query($sql,$feature->organism_id));
  324. drupal_set_message(t("$feature_id: Creating node $feature->name"));
  325. $new_node = new stdClass();
  326. $new_node->type = 'chado_feature';
  327. $new_node->uid = $user->uid;
  328. $new_node->title = "$feature->name, $feature->uniquename ($feature->cvname) $organism->genus $organism->species";
  329. $new_node->fname = "$feature->name";
  330. $new_node->uniquename = "$feature->uniquename";
  331. $new_node->feature_id = $feature->feature_id;
  332. $new_node->residues = $feature->residues;
  333. $new_node->organism_id = $feature->organism_id;
  334. $new_node->feature_type = $feature->cvname;
  335. $new_node->synonyms = $feature->synonyms;
  336. // validate the node and if okay then submit
  337. node_validate($new_node);
  338. if ($errors = form_get_errors()) {
  339. foreach($errors as $key => $msg){
  340. drupal_set_message($msg);
  341. }
  342. return $errors;
  343. } else {
  344. $node = node_submit($new_node);
  345. node_save($node);
  346. }
  347. }
  348. else {
  349. $node = $chado_feature;
  350. }
  351. // set the taxonomy for this node
  352. drupal_set_message(t("$feature_id ($node->nid): setting taxonomy"));
  353. tripal_feature_set_taxonomy($node,$feature_id);
  354. // reindex the node
  355. // drupal_set_message(t("$feature_id( $node->nid): indexing"));
  356. // tripal_feature_index_feature ($feature_id,$node->nid);
  357. // set the URL alias for this node
  358. tripal_feature_set_feature_url($node,$feature);
  359. return '';
  360. }
  361. ?>