syncFeatures.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. print "syncing feature $feature_id\n";
  17. tripal_feature_sync_feature($feature_id);
  18. }
  19. else{
  20. print "syncing all features...\n";
  21. tripal_feature_sync_features();
  22. }
  23. }
  24. /**
  25. *
  26. */
  27. function tripal_feature_set_urls($job_id = NULL){
  28. // first get the list of features that have been synced
  29. $sql = "SELECT * FROM {chado_feature}";
  30. $nodes = db_query($sql);
  31. while($node = db_fetch_object($nodes)){
  32. // now get the feature details
  33. $feature_arr = tripal_core_chado_select('feature',
  34. array('feature_id','name','uniquename'),
  35. array('feature_id' => $node->feature_id));
  36. $feature = $feature_arr[0];
  37. tripal_feature_set_feature_url($node,$feature);
  38. }
  39. }
  40. /**
  41. *
  42. */
  43. function tripal_feature_set_feature_url($node,$feature){
  44. // determine which URL alias to use
  45. $alias_type = variable_get('chado_feature_url','internal ID');
  46. $aprefix = variable_get('chado_feature_accession_prefix','ID');
  47. switch ($alias_type) {
  48. case 'feature name':
  49. $url_alias = $feature->name;
  50. break;
  51. case 'feature unique name':
  52. $url_alias = $feature->uniquename;
  53. break;
  54. default:
  55. $url_alias = "$aprefix$feature->feature_id";
  56. }
  57. print "Setting $alias_type as URL alias for $feature->name: node/$node->nid => $url_alias\n";
  58. // remove any previous alias
  59. db_query("DELETE FROM {url_alias} WHERE src = '%s'", "node/$node->nid");
  60. // add the new alias
  61. path_set_alias("node/$node->nid",$url_alias);
  62. }
  63. /**
  64. *
  65. *
  66. * @ingroup tripal_feature
  67. */
  68. function tripal_feature_sync_features ($max_sync = 0, $job_id = NULL){
  69. //print "Syncing features (max of $max_sync)\n";
  70. $i = 0;
  71. // get the list of available sequence ontology terms for which
  72. // we will build drupal pages from features in chado. If a feature
  73. // is not one of the specified typse we won't build a node for it.
  74. $allowed_types = variable_get('chado_feature_types','EST contig');
  75. $allowed_types = preg_replace("/[\s\n\r]+/"," ",$allowed_types);
  76. $so_terms = split(' ',$allowed_types);
  77. $where_cvt = "";
  78. foreach ($so_terms as $term){
  79. $where_cvt .= "CVT.name = '$term' OR ";
  80. }
  81. $where_cvt = substr($where_cvt,0,strlen($where_cvt)-3); # strip trailing 'OR'
  82. // get the list of organisms that are synced and only include features from
  83. // those organisms
  84. $orgs = organism_get_synced();
  85. $where_org = "";
  86. foreach($orgs as $org){
  87. $where_org .= "F.organism_id = $org->organism_id OR ";
  88. }
  89. $where_org = substr($where_org,0,strlen($where_org)-3); # strip trailing 'OR'
  90. // use this SQL statement to get the features that we're going to upload
  91. $sql = "SELECT feature_id ".
  92. "FROM {FEATURE} F ".
  93. " INNER JOIN Cvterm CVT ON F.type_id = CVT.cvterm_id ".
  94. "WHERE ($where_cvt) AND ($where_org) ".
  95. "ORDER BY feature_id";
  96. // get the list of features
  97. $previous_db = tripal_db_set_active('chado'); // use chado database
  98. $results = db_query($sql);
  99. tripal_db_set_active($previous_db); // now use drupal database
  100. // load into ids array
  101. $count = 0;
  102. $ids = array();
  103. while($id = db_fetch_object($results)){
  104. $ids[$count] = $id->feature_id;
  105. $count++;
  106. }
  107. // make sure our vocabularies are set before proceeding
  108. tripal_feature_set_vocabulary();
  109. // pre-create the SQL statement that will be used to check
  110. // if a feature has already been synced. We skip features
  111. // that have been synced
  112. $sql = "SELECT * FROM {chado_feature} WHERE feature_id = %d";
  113. // Iterate through features that need to be synced
  114. $interval = intval($count * 0.01);
  115. foreach($ids as $feature_id){
  116. // update the job status every 1% features
  117. if($job_id and $i % $interval == 0){
  118. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  119. }
  120. // if we have a maximum number to sync then stop when we get there
  121. // if not then just continue on
  122. if($max_sync and $i == $max_sync){
  123. return '';
  124. }
  125. if(!db_fetch_object(db_query($sql,$feature_id))){
  126. # parsing all the features can cause memory overruns
  127. # we are not sure why PHP does not clean up the memory as it goes
  128. # to avoid this problem we will call this script through an
  129. # independent system call
  130. $cmd = "php " . drupal_get_path('module', 'tripal_feature') . "/syncFeatures.php -f $feature_id ";
  131. system($cmd);
  132. }
  133. $i++;
  134. }
  135. return '';
  136. }
  137. /**
  138. *
  139. *
  140. * @ingroup tripal_feature
  141. */
  142. function tripal_feature_sync_feature ($feature_id){
  143. // print "\tfeature $feature_id\n";
  144. $mem = memory_get_usage(TRUE);
  145. $mb = $mem/1048576;
  146. // print "$mb mb\n";
  147. global $user;
  148. $create_node = 1; // set to 0 if the node exists and we just sync and not create
  149. // get the accession prefix
  150. $aprefix = variable_get('chado_feature_accession_prefix','ID');
  151. // if we don't have a feature_id then return
  152. if(!$feature_id){
  153. drupal_set_message(t("Please provide a feature_id to sync"));
  154. return '';
  155. }
  156. // get information about this feature
  157. $fsql = "SELECT F.feature_id, F.name, F.uniquename,O.genus, ".
  158. " O.species,CVT.name as cvname,F.residues,F.organism_id ".
  159. "FROM {FEATURE} F ".
  160. " INNER JOIN Cvterm CVT ON F.type_id = CVT.cvterm_id ".
  161. " INNER JOIN Organism O ON F.organism_id = O.organism_ID ".
  162. "WHERE F.feature_id = %d";
  163. $previous_db = tripal_db_set_active('chado'); // use chado database
  164. $feature = db_fetch_object(db_query($fsql,$feature_id));
  165. tripal_db_set_active($previous_db); // now use drupal database
  166. // get the synonyms for this feature
  167. $synsql = "SELECT S.name ".
  168. "FROM {feature_synonym} FS ".
  169. " INNER JOIN {synonym} S on FS.synonym_id = S.synonym_id ".
  170. "WHERE FS.feature_id = %d";
  171. $previous_db = tripal_db_set_active('chado'); // use chado database
  172. $synonyms = db_query($synsql,$feature_id);
  173. tripal_db_set_active($previous_db); // now use drupal database
  174. // now add these synonyms to the feature object as a single string
  175. $synstring = '';
  176. while($synonym = db_fetch_object($synonyms)){
  177. $synstring .= "$synonym->name\n";
  178. }
  179. $feature->synonyms = $synstring;
  180. // check to make sure that we don't have any nodes with this feature name as a title
  181. // but without a corresponding entry in the chado_feature table if so then we want to
  182. // clean up that node. (If a node is found we don't know if it belongs to our feature or
  183. // not since features can have the same name/title.)
  184. $tsql = "SELECT * FROM {node} N ".
  185. "WHERE title = '%s'";
  186. $cnsql = "SELECT * FROM {chado_feature} ".
  187. "WHERE nid = %d";
  188. $nodes = db_query($tsql,$feature->name);
  189. // cycle through all nodes that may have this title
  190. while($node = db_fetch_object($nodes)){
  191. $feature_nid = db_fetch_object(db_query($cnsql,$node->nid));
  192. if(!$feature_nid){
  193. drupal_set_message(t("$feature_id: A node is present but the chado_feature entry is missing... correcting"));
  194. node_delete($node->nid);
  195. }
  196. }
  197. // check if this feature already exists in the chado_feature table.
  198. // if we have a chado feature, we want to check to see if we have a node
  199. $cfsql = "SELECT * FROM {chado_feature} ".
  200. "WHERE feature_id = %d";
  201. $nsql = "SELECT * FROM {node} ".
  202. "WHERE nid = %d";
  203. $chado_feature = db_fetch_object(db_query($cfsql,$feature->feature_id));
  204. if($chado_feature){
  205. drupal_set_message(t("$feature_id: A chado_feature entry exists"));
  206. $node = db_fetch_object(db_query($nsql,$chado_feature->nid));
  207. if(!$node){
  208. // if we have a chado_feature but not a node then we have a problem and
  209. // need to cleanup
  210. drupal_set_message(t("$feature_id: The node is missing, but has a chado_feature entry... correcting"));
  211. $df_sql = "DELETE FROM {chado_feature} WHERE feature_id = %d";
  212. db_query($df_sql,$feature_id);
  213. } else {
  214. drupal_set_message(t("$feature_id: A corresponding node exists"));
  215. $create_node = 0;
  216. }
  217. }
  218. // if we've encountered an error then just return.
  219. if($error_msg = db_error()){
  220. //print "$error_msg\n";
  221. return '';
  222. }
  223. // if a drupal node does not exist for this feature then we want to
  224. // create one. Note that the node_save call in this block
  225. // will call the hook_submit function which
  226. if($create_node){
  227. // get the organism for this feature
  228. $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
  229. $organism = db_fetch_object(db_query($sql,$feature->organism_id));
  230. drupal_set_message(t("$feature_id: Creating node $feature->name"));
  231. $new_node = new stdClass();
  232. $new_node->type = 'chado_feature';
  233. $new_node->uid = $user->uid;
  234. $new_node->title = "$feature->name, $feature->uniquename ($feature->cvname) $organism->genus $organism->species";
  235. $new_node->fname = "$feature->name";
  236. $new_node->uniquename = "$feature->uniquename";
  237. $new_node->feature_id = $feature->feature_id;
  238. $new_node->residues = $feature->residues;
  239. $new_node->organism_id = $feature->organism_id;
  240. $new_node->feature_type = $feature->cvname;
  241. $new_node->synonyms = $feature->synonyms;
  242. // validate the node and if okay then submit
  243. node_validate($new_node);
  244. if ($errors = form_get_errors()) {
  245. foreach($errors as $key => $msg){
  246. drupal_set_message($msg);
  247. }
  248. return $errors;
  249. } else {
  250. $node = node_submit($new_node);
  251. node_save($node);
  252. }
  253. }
  254. else {
  255. $node = $chado_feature;
  256. }
  257. // set the taxonomy for this node
  258. drupal_set_message(t("$feature_id ($node->nid): setting taxonomy"));
  259. tripal_feature_set_taxonomy($node,$feature_id);
  260. // reindex the node
  261. // drupal_set_message(t("$feature_id( $node->nid): indexing"));
  262. // tripal_feature_index_feature ($feature_id,$node->nid);
  263. // set the URL alias for this node
  264. tripal_feature_set_feature_url($node,$feature);
  265. return '';
  266. }
  267. /**
  268. * Returns a list of organisms that are currently synced with Drupal
  269. *
  270. * @ingroup tripal_feature
  271. */
  272. function organism_get_synced() {
  273. // use this SQL for getting synced organisms
  274. $dsql = "SELECT * FROM {chado_organism}";
  275. $orgs = db_query($dsql);
  276. // use this SQL statement for getting the organisms
  277. $csql = "SELECT * FROM {Organism} ".
  278. "WHERE organism_id = %d";
  279. $org_list = array();
  280. // iterate through the organisms and build an array of those that are synced
  281. while($org = db_fetch_object($orgs)){
  282. $previous_db = tripal_db_set_active('chado'); // use chado database
  283. $info = db_fetch_object(db_query($csql,$org->organism_id));
  284. tripal_db_set_active($previous_db); // now use drupal database
  285. $org_list[] = $info;
  286. }
  287. return $org_list;
  288. }
  289. ?>