Tripal v1.0 (6.x-1.0)
tripal_library.admin.inc
Go to the documentation of this file.
00001 <?php 
00002 
00003 
00009 function tripal_library_admin() {
00010   $form = array();
00011 
00012   // before proceeding check to see if we have any
00013   // currently processing jobs. If so, we don't want
00014   // to give the opportunity to sync libraries
00015   $active_jobs = FALSE;
00016   if (tripal_get_module_active_jobs('tripal_library')) {
00017     $active_jobs = TRUE;
00018   }
00019 
00020   // add the field set for syncing libraries
00021   if (!$active_jobs) {
00022     get_tripal_library_admin_form_sync_set($form);
00023     get_tripal_library_admin_form_reindex_set($form);
00024     get_tripal_library_admin_form_taxonomy_set($form);
00025     get_tripal_library_admin_form_cleanup_set($form);
00026   }
00027   else {
00028     $form['notice'] = array(
00029      '#type' => 'fieldset',
00030      '#title' => t('Library Management Temporarily Unavailable')
00031     );
00032     $form['notice']['message'] = array(
00033         '#value' => t('Currently, library management jobs are waiting or are running. . Managemment features have been hidden until these jobs complete.  Please check back later once these jobs have finished.  You can view the status of pending jobs in the Tripal jobs page.'),
00034     );
00035   }
00036 
00037   return system_settings_form($form);
00038 }
00039 
00040 
00046 function get_tripal_library_admin_form_cleanup_set(&$form) {
00047   $form['cleanup'] = array(
00048     '#type' => 'fieldset',
00049     '#title' => t('Clean Up')
00050   );
00051   $form['cleanup']['description'] = array(
00052      '#type' => 'item',
00053      '#value' => t("With Drupal and chado residing in different databases ".
00054         "it is possible that nodes in Drupal and libraries in Chado become ".
00055         "\"orphaned\".  This can occur if an library node in Drupal is ".
00056         "deleted but the corresponding chado library is not and/or vice ".
00057         "versa. Click the button below to resolve these discrepancies."),
00058      '#weight' => 1,
00059   );
00060   $form['cleanup']['button'] = array(
00061     '#type' => 'submit',
00062     '#value' => t('Clean up orphaned libraries'),
00063     '#weight' => 2,
00064   );
00065 }
00066 
00072 function get_tripal_library_admin_form_taxonomy_set(&$form) {
00073   $form['taxonify'] = array(
00074     '#type' => 'fieldset',
00075     '#title' => t('Assign Drupal Taxonomy to Library Features')
00076   );
00077 
00078   // get the list of libraries
00079   $sql = "SELECT * FROM {Library} ORDER BY uniquename";
00080   $lib_rset = chado_query($sql);
00081 
00082   // iterate through all of the libraries
00083   $lib_boxes = array();
00084   while ($library = db_fetch_object($lib_rset)) {
00085     $lib_boxes[$library->library_id] = "$library->name";
00086   }
00087 
00088   $form['taxonify']['description'] = array(
00089      '#type' => 'item',
00090      '#value' => t("Drupal allows for assignment of \"taxonomy\" or catagorical terms to " .
00091                    "nodes. These terms allow for advanced filtering during searching. This option allows ".
00092                    "for setting taxonomy only for features that belong to the selected libraries below.  All other features will be unaffected.  To set taxonomy for all features in the site see the Feature Administration page."),
00093    '#weight' => 1,
00094   );
00095 
00096   $form['taxonify']['tx-libraries'] = array(
00097    '#title'       => t('Libraries'),
00098    '#type'        => t('checkboxes'),
00099    '#description' => t("Check the libraries whose features you want to reset taxonomy.  Note: this list contains all libraries, even those that may not be synced."),
00100    '#required'    => FALSE,
00101    '#prefix'      => '<div id="lib_boxes">',
00102    '#suffix'      => '</div>',
00103    '#options'     => $lib_boxes,
00104    '#weight'      => 2
00105   );
00106   $form['taxonify']['tx-button'] = array(
00107     '#type' => 'submit',
00108     '#value' => t('Set Feature Taxonomy'),
00109     '#weight'      => 3
00110   );
00111 }
00116 function get_tripal_library_admin_form_reindex_set(&$form) {
00117    // define the fieldsets
00118   $form['reindex'] = array(
00119     '#type' => 'fieldset',
00120     '#title' => t('Reindex Library Features')
00121   );
00122 
00123   // get the list of libraries
00124   $sql = "SELECT * FROM {Library} ORDER BY uniquename";
00125   $lib_rset = chado_query($sql);
00126 
00127   // iterate through all of the libraries
00128   $lib_boxes = array();
00129   while ($library = db_fetch_object($lib_rset)) {
00130     $lib_boxes[$library->library_id] = "$library->name";
00131   }
00132   $form['reindex']['description'] = array(
00133      '#type' => 'item',
00134      '#value' => t("This option allows for reindexing of only those features that belong to the selected libraries below. All other features will be unaffected.  To reindex all features in the site see the Feature Administration page."),
00135    '#weight' => 1,
00136   );
00137 
00138   $form['reindex']['re-libraries'] = array(
00139    '#title'       => t('Libraries'),
00140    '#type'        => t('checkboxes'),
00141    '#description' => t("Check the libraries whoee features you want to reindex. Note: this list contains all libraries, even those that may not be synced."),
00142    '#required'    => FALSE,
00143    '#prefix'      => '<div id="lib_boxes">',
00144    '#suffix'      => '</div>',
00145    '#options'     => $lib_boxes,
00146    '#weight' => 2,
00147   );
00148   $form['reindex']['re-button'] = array(
00149     '#type' => 'submit',
00150     '#value' => t('Reindex Features'),
00151     '#weight' => 3,
00152   );
00153 }
00158 function get_tripal_library_admin_form_sync_set(&$form) {
00159    // define the fieldsets
00160   $form['sync'] = array(
00161     '#type' => 'fieldset',
00162     '#title' => t('Sync Libraries')
00163   );
00164 
00165 
00166   // get the list of libraries
00167   $sql = "SELECT * FROM {Library} ORDER BY uniquename";
00168   $lib_rset = chado_query($sql);
00169 
00170   // if we've added any libraries to the list that can be synced
00171   // then we want to build the form components to allow the user
00172   // to select one or all of them.  Otherwise, just present
00173   // a message stating that all libraries are currently synced.
00174   $lib_boxes = array();
00175   $added = 0;
00176   while ($library = db_fetch_object($lib_rset)) {
00177     // check to see if the library is already present as a node in drupal.
00178     // if so, then skip it.
00179     $sql = "SELECT * FROM {chado_library} WHERE library_id = %d";
00180     if (!db_fetch_object(db_query($sql, $library->library_id))) {
00181       $lib_boxes[$library->library_id] = "$library->name";
00182       $added++;
00183     }
00184   }
00185 
00186   // if we have libraries we need to add to the checkbox then
00187   // build that form element
00188   if ($added > 0) {
00189     $lib_boxes['all'] = "All Libraries";
00190 
00191     $form['reindex']['description'] = array(
00192      '#type' => 'item',
00193      '#value' => t("This option allows for the creation of Drupal content for libraries in chado. Only the selected libraries will be synced."),
00194    '#weight' => 1,
00195     );
00196 
00197 
00198     $form['sync']['libraries'] = array(
00199       '#title'       => t('Available Libraries'),
00200       '#type'        => t('checkboxes'),
00201       '#description' => t("Check the libraries you want to sync.  Drupal content will be created for each of the libraries listed above.  Select 'All Libraries' to sync all of them."),
00202       '#required'    => FALSE,
00203       '#prefix'      => '<div id="lib_boxes">',
00204       '#suffix'      => '</div>',
00205       '#options'     => $lib_boxes,
00206     '#weight' => 2,
00207     );
00208     $form['sync']['button'] = array(
00209        '#type' => 'submit',
00210        '#value' => t('Sync Libraries'),
00211      '#weight' => 3,
00212     );
00213   }
00214    // we don't have any libraries to select from
00215   else {
00216     $form['sync']['value'] = array(
00217        '#value' => t('All libraries in Chado are currently synced with Drupal.')
00218     );
00219   }
00220 }
00225 function tripal_library_admin_validate($form, &$form_state) {
00226   global $user;  // we need access to the user info
00227   $job_args = array();
00228 
00229   // Submit the Sync Job if selected
00230   if ($form_state['values']['op'] == t('Sync Libraries')) {
00231 
00232     // check to see if the user wants to sync chado and drupal.  If
00233     // so then we need to register a job to do so with tripal
00234     $libraries = $form_state['values']['libraries'];
00235     $do_all = FALSE;
00236     $to_sync = array();
00237 
00238   foreach ($libraries as $library_id) {
00239     if (preg_match("/^all$/i", $library_id)) {
00240       $do_all = TRUE;
00241     }
00242     if ($library_id and preg_match("/^\d+$/i", $library_id)) {
00243       // get the library info
00244       $sql = "SELECT * FROM {Library} WHERE library_id = %d";
00245       $library = db_fetch_object(chado_query($sql, $library_id));
00246       $to_sync[$library_id] = $library->name;
00247     }
00248   }
00249 
00250   // submit the job to the tripal job manager
00251   if ($do_all) {
00252     tripal_add_job('Sync all libraries', 'tripal_library', 'tripal_library_sync_libraries', $job_args, $user->uid);
00253   }
00254   else{
00255     foreach ($to_sync as $library_id => $name) {
00256       $job_args[0] = $library_id;
00257       tripal_add_job("Sync library: $name", 'tripal_library', 'tripal_library_sync_libraries', $job_args, $user->uid);
00258       }
00259     }
00260   }
00261 
00262   // -------------------------------------
00263   // Submit the Reindex Job if selected
00264   if ($form_state['values']['op'] == t('Reindex Features')) {
00265     $libraries = $form_state['values']['re-libraries'];
00266     foreach ($libraries as $library_id) {
00267       if ($library_id and preg_match("/^\d+$/i", $library_id)) {
00268         // get the library info
00269         $sql = "SELECT * FROM {Library} WHERE library_id = %d";
00270         $library = db_fetch_object(chado_query($sql, $library_id));
00271         $job_args[0] = $library_id;
00272         tripal_add_job("Reindex features for library: $library->name", 'tripal_library',
00273          'tripal_library_reindex_features', $job_args, $user->uid);
00274       }
00275     }
00276   }
00277 
00278   // -------------------------------------
00279   // Submit the Taxonomy Job if selected
00280   if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
00281     $libraries = $form_state['values']['tx-libraries'];
00282     foreach ($libraries as $library_id) {
00283       if ($library_id and preg_match("/^\d+$/i", $library_id)) {
00284         // get the library info
00285         $sql = "SELECT * FROM {Library} WHERE library_id = %d";
00286         $library = db_fetch_object(chado_query($sql, $library_id));
00287         $job_args[0] = $library_id;
00288         tripal_add_job("Set taxonomy for features in library: $library->name", 'tripal_library',
00289          'tripal_library_taxonify_features', $job_args, $user->uid);
00290       }
00291     }
00292   }
00293     // -------------------------------------
00294     // Submit the Cleanup Job if selected
00295     if ($form_state['values']['op'] == t('Clean up orphaned libraries')) {
00296       tripal_add_job('Cleanup orphaned libraries', 'tripal_library',
00297          'tripal_library_cleanup', $job_args, $user->uid);
00298     }
00299 }
00300 
00301 
00307 function tripal_library_add_taxonomy($node, $library_id) {
00308 
00309     //include the file containing the required functions.  We only have to
00310     // do this because Drupal 6 fails to do this globally for us and
00311     // the drupal_execute function below won't work
00312     module_load_include('inc', 'taxonomy', 'taxonomy.admin');
00313 
00314    /*   // get the vocabulary id
00315     $vocabularies = taxonomy_get_vocabularies();
00316     $vid = NULL;
00317     foreach($vocabularies as $vocab){
00318     if($vocab->name == 'DNA Libraries'){
00319     $vid = $vocab->vid;
00320     }
00321     }
00322 
00323     if(!$vid){  */
00324    // add the vocabulary
00325   $vocab_form['values']['name'] = 'DNA Libraries';
00326   $vocab_form['values']['description'] = 'Allows for associating/searching of library features by library name';
00327   $vocab_form['values']['help'] = '';
00328   $vocab_form['values']['module'] = 'taxonomy';
00329   drupal_execute('taxonomy_form_vocabulary', $vocab_form);
00330   return;
00331    //   }
00332 
00333    // make sure this term doesn't already exist.  If it doesn't then add it
00334   if ($vid) {
00335     $tree = taxonomy_get_tree($vid);
00336     $found = 0;
00337     foreach ($tree as $term) {
00338       if ($term->name == $node->title) {
00339         $found = 1;
00340       }
00341     }
00342 
00343       // add the term to the vocabulary
00344     if (!$found) {
00345       $form_state = array();
00346       $form_state['values']['name'] = $node->title;
00347       $form_state['values']['description'] = $library_id;
00348       drupal_execute('taxonomy_form_term', $form_state, $vid);
00349     }
00350   }
00351 }
00352 
00353 
00359 function tripal_library_sync_libraries($library_id = NULL, $job_id = NULL) {
00360 
00361   global $user;
00362   $page_content = '';
00363 
00364   // get the list of libraries and create new nodes
00365   if (!$library_id) {
00366     $sql = "SELECT * FROM {Library} L";
00367     $results = chado_query($sql);
00368   }
00369   else {
00370     $sql = "SELECT * FROM {Library} L WHERE library_id = %d";
00371     $results = chado_query($sql, $library_id);
00372   }
00373 
00374   // We'll use the following SQL statement for checking if the library
00375   // already exists as a drupal node.
00376   $sql = "SELECT * FROM {chado_library} ".
00377         "WHERE library_id = %d";
00378 
00379   while ($library = db_fetch_object($results)) {
00380 
00381     // check if this library already exists in the drupal database. if it
00382     // does then skip this library and go to the next one.
00383     if (!db_fetch_object(db_query($sql, $library->library_id))) {
00384 
00385     $new_node = new stdClass();
00386     $new_node->type = 'chado_library';
00387     $new_node->uid = $user->uid;
00388     $new_node->title = "$library->name";
00389     $new_node->library_id = $library->library_id;
00390     $new_node->organism_id = $library->organism_id;
00391     $new_node->uniquename = $library->uniquename;
00392     $new_node->type_id = $library->type_id;
00393 
00394     node_validate($new_node);
00395     $errors = form_get_errors();
00396     if (!$errors) {
00397       $node = node_submit($new_node);
00398       node_save($node);
00399       if ($node->nid) {
00400         print "Added " . $library->name . "\n";
00401       }
00402       else {
00403         print "ERROR: Unable to create " . $library->name . "\n";
00404       }
00405     }
00406     else {
00407       print "ERROR: Unable to create " . $library->name . "\n" . print_r($errors, TRUE) . "\n";
00408     }
00409     }
00410     else {
00411       print "Skipped " . $library->name . "\n";
00412     }
00413   }
00414   return $page_content;
00415 }
00416 
00417 
00422 function tripal_library_feature_set_taxonomy($library_id = NULL) {
00423 
00424    //TO DO : return usable error if vocabs don't exist
00425    // get the list of vocabularies and find our two vocabularies of interest
00426   $vocabularies = taxonomy_get_vocabularies();
00427   $vid = NULL;
00428   foreach ($vocabularies as $vocab) {
00429     if ($vocab->name == 'Library') {
00430       $vid = $vocab->vid;
00431     }
00432   }
00433   if (!$vid) {
00434     return;
00435   }
00436 
00437   // We'll use the following SQL statement for getting the node info
00438   if ($library_id) {
00439     print "Finding features for library with ID: $library_id\n";
00440     $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
00441            "FROM {library_feature} LF ".
00442            "INNER JOIN Library L ON LF.library_id = L.library_id ".
00443            "WHERE L.library_id = $library_id ".
00444            "ORDER BY LF.feature_id";
00445     $features = chado_query($sql);
00446   }
00447   else {
00448     print "Finding features for all libraries\n";
00449     $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
00450            "FROM {library_feature} LF ".
00451            "INNER JOIN Library L ON LF.library_id = L.library_id ".
00452            "ORDER BY LF.feature_id";
00453     $features = chado_query($sql);
00454   }
00455 
00456   $node_sql = "SELECT * FROM {chado_feature} CF ".
00457              "  INNER JOIN {node} N ON CF.nid = N.nid ".
00458              "WHERE feature_id = %d";
00459 
00460   // iterate through the features and add the taxonomy
00461   while ($feature = db_fetch_object($features)) {
00462     $node = db_fetch_object(db_query($node_sql, $feature->feature_id));
00463     $tags["$vid"] = $feature->libname;
00464     $terms['tags'] = $tags;
00465     taxonomy_node_save($node, $terms);
00466     print "Updated $feature->feature_id as $feature->libname\n";
00467   }
00468 }
00473 function tripal_library_reindex_features($library_id = NULL, $job_id = NULL) {
00474   $i = 0;
00475 
00476   // if the caller provided a library_id then get all of the features
00477   // associated with the library. Otherwise get all sequences assoicated
00478   // with all libraries.
00479   if ($library_id) {
00480     $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
00481            " FROM {library_feature} LF ".
00482            "  INNER JOIN Library L ON LF.library_id = L.library_id ".
00483            "WHERE L.library_id = $library_id ".
00484            "ORDER BY LF.feature_id";
00485     $results = chado_query($sql);
00486   }
00487   else {
00488     $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
00489            " FROM {library_feature} LF ".
00490            "  INNER JOIN Library L ON LF.library_id = L.library_id ".
00491            "ORDER BY LF.feature_id";
00492     $results = chado_query($sql);
00493   }
00494 
00495    // load into ids array
00496   $count = 0;
00497   $ids = array();
00498   while ($id = db_fetch_object($results)) {
00499     $ids[$count] = $id->feature_id;
00500     $count++;
00501   }
00502 
00503   $interval = intval($count * 0.01);
00504   foreach ($ids as $feature_id) {
00505     // update the job status every 1% features
00506     if ($job_id and $i % interval == 0) {
00507       tripal_job_set_progress($job_id, intval(($i/$count)*100));
00508     }
00509     tripal_feature_sync_feature($feature_id);
00510     $i++;
00511   }
00512 }
00517 function tripal_library_taxonify_features($library_id = NULL, $job_id = NULL) {
00518   $i = 0;
00519 
00520   // if the caller provided a library_id then get all of the features
00521   // associated with the library. Otherwise get all sequences assoicated
00522   // with all libraries.
00523   if ($library_id) {
00524     $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
00525            " FROM {library_feature} LF ".
00526            "  INNER JOIN Library L ON LF.library_id = L.library_id ".
00527            "WHERE L.library_id = $library_id ".
00528            "ORDER BY LF.feature_id";
00529     $results = chado_query($sql);
00530   }
00531   else {
00532     $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
00533            " FROM {library_feature} LF ".
00534            "  INNER JOIN Library L ON LF.library_id = L.library_id ".
00535            "ORDER BY LF.feature_id";
00536     $results = chado_query($sql);
00537   }
00538 
00539   // load into ids array
00540   $count = 0;
00541   $ids = array();
00542   while ($id = db_fetch_object($results)) {
00543     $ids[$count] = $id->feature_id;
00544     $count++;
00545   }
00546 
00547   // make sure our vocabularies are set before proceeding
00548   tripal_feature_set_vocabulary();
00549 
00550   // use this SQL for getting the nodes
00551   $nsql =  "SELECT * FROM {chado_feature} CF ".
00552           "  INNER JOIN {node} N ON N.nid = CF.nid ".
00553           "WHERE feature_id = %d";
00554 
00555    // iterate through the features and set the taxonomy
00556   $interval = intval($count * 0.01);
00557   foreach ($ids as $feature_id) {
00558     // update the job status every 1% features
00559     if ($job_id and $i % interval == 0) {
00560       tripal_job_set_progress($job_id, intval(($i/$count)*100));
00561     }
00562       $node = db_fetch_object(db_query($nsql, $feature_id));
00563       tripal_feature_set_taxonomy($node, $feature_id);
00564       $i++;
00565   }
00566 }
00567 
00568 
00579 function tripal_library_cleanup($dummy = NULL, $job_id = NULL) {
00580 
00581   return tripal_core_clean_orphaned_nodes('library', $job_id);
00582   
00583 }
00584 
 All Classes Files Functions Variables