syncFeatures.php 10 KB

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