syncFeatures.php 9.7 KB

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