function tripal_stock_sync_stocks

1.x tripal_stock.sync_stocks.inc tripal_stock_sync_stocks($max_sync = 0, $organism_id = NULL, $stock_types = NULL, $job_id = NULL)

Related topics

1 call to tripal_stock_sync_stocks()
tripal_stock.sync_stocks.inc in tripal_stock/includes/tripal_stock.sync_stocks.inc
@todo Add file header description
2 string references to 'tripal_stock_sync_stocks'
drush_tripal_core_tripal_node_sync in tripal_core/tripal_core.drush.inc
Sync's chado records with drupal creating nodes for the given chado-centric module.
tripal_stock_sync_form_submit in tripal_stock/includes/tripal_stock.sync_stocks.inc

File

tripal_stock/includes/tripal_stock.sync_stocks.inc, line 298
@todo Add file header description

Code

function tripal_stock_sync_stocks($max_sync = 0, $organism_id = NULL, 
$stock_types = NULL, $job_id = NULL) {
  //print "Syncing stocks (max of $max_sync)\n";
  $i = 0;

  // get the list of available sequence ontology terms for which
  // we will build drupal pages from stocks in chado.  If a stock
  // is not one of the specified typse we won't build a node for it.
  if (!$stock_types) {
    $allowed_types = variable_get('chado_sync_stock_types', '');
  }
  else {
    $allowed_types = $stock_types;
  }

  if ($allowed_types) {
    $allowed_types = preg_replace("/[\s\n\r]+/", " ", $allowed_types);
    print "Looking for stocks of type: $allowed_types\n";

    $so_terms = split(' ', $allowed_types);
    $where_cvt = "";
    foreach ($so_terms as $term) {
      $where_cvt .= "CVT.name = '$term' OR ";
    }
    $where_cvt = drupal_substr($where_cvt, 0, drupal_strlen($where_cvt) -3); # strip trailing 'OR'
  }
  else {
    $where_cvt = '1=1';
  }

  // get the list of organisms that are synced and only include stocks from
  // those organisms
  $orgs = tripal_organism_get_synced();
  $where_org = "";
  foreach ($orgs as $org) {
    if ($organism_id) {
      if ($org->organism_id and $org->organism_id == $organism_id) {
        $where_org .= "S.organism_id = $org->organism_id OR ";
      }
    }
    else {
      if ($org->organism_id) {
        $where_org .= "S.organism_id = $org->organism_id OR ";
      }
    }
  }
  $where_org = drupal_substr($where_org, 0, drupal_strlen($where_org) -3); # strip trailing 'OR'

  // use this SQL statement to get the stocks that we're going to upload
  $sql = "SELECT stock_id " .
    "FROM {stock} S " .
    "  INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id " .
    "WHERE ($where_cvt) AND ($where_org) " .
    "ORDER BY stock_id";

  // get the list of stocks
  $results = chado_query($sql);

  // load into ids array
  $count = 0;
  $ids = array();
  while ($id = db_fetch_object($results)) {
    $ids[$count] = $id->stock_id;
    $count++;
  }

  // make sure our vocabularies are set before proceeding
  //  tripal_stock_set_vocabulary();

  // pre-create the SQL statement that will be used to check
  // if a stock has already been synced.  We skip stocks
  // that have been synced
  $sql = "SELECT * FROM {chado_stock} WHERE stock_id = %d";

  // Iterate through stocks that need to be synced
  $interval = intval($count * 0.01);
  if ($interval < 1) {
    $interval = 1;
  }
  $num_ids = sizeof($ids);
  $i = 0;
  foreach ($ids as $stock_id) {
    // update the job status every 1% stocks
    if ($job_id and $i % $interval == 0) {
      tripal_job_set_progress($job_id, intval(($i / $count) * 100));
    }
    // if we have a maximum number to sync then stop when we get there
    // if not then just continue on
    if ($max_sync and $i == $max_sync) {
      return '';
    }
    if (!db_fetch_object(db_query($sql, $stock_id))) {

      # parsing all the stocks can cause memory overruns
      # we are not sure why PHP does not clean up the memory as it goes
      # to avoid this problem we will call this script through an
      # independent system call
      print ($i + 1) . " of $num_ids Syncing stock id: $stock_id\n";
      $cmd = "php " . drupal_get_path('module', 'tripal_stock') . "/includes/tripal_stock.sync_stocks.inc -f $stock_id -t chado_stock";
      print "$cmd\n";
      system($cmd);

    }
    $i++;
  }

  return '';
}