Browse Source

FASTA loader now working

Stephen Ficklin 8 years ago
parent
commit
18e55b1d2f

+ 6 - 2
tripal/api/tripal.importer.api.inc

@@ -120,7 +120,9 @@ function tripal_run_importer($import_id, TripalJob $job = NULL) {
         $loader->preRun($details);
         $loader->run($details);
         $loader->postRun($details);
-        $job->logMessage("Done");
+        if ($job) {
+          $job->logMessage("Done");
+        }
         print "\nDone\n";
 
         // Check for new fields and notify the user.
@@ -129,7 +131,9 @@ function tripal_run_importer($import_id, TripalJob $job = NULL) {
       }
       catch (Exception $e) {
         $transaction->rollback();
-        throw new Exception('Cannot run loader, ' . $loader_name . ': ' .  $e->getMessage());
+        if ($job) {
+          $job->logMessage($e->getMessage(), array(), TRIPAL_ERROR);
+        }
       }
     }
   }

+ 3 - 2
tripal/api/tripal.jobs.api.inc

@@ -363,7 +363,8 @@ function tripal_launch_job($do_parallel = 0, $job_id = NULL, $max_jobs = -1, $si
       WHERE
         TJ.start_time IS NULL AND
         TJ.end_time IS NULL AND
-        TJ.job_id = :job_id
+        TJ.job_id = :job_id AND
+        TJ.status =! 'Cancelled'
       ORDER BY priority ASC, job_id ASC
     ";
     $jobs = db_query($sql, array(':job_id' => $job_id));
@@ -412,7 +413,7 @@ function tripal_launch_job($do_parallel = 0, $job_id = NULL, $max_jobs = -1, $si
       $job->run();
     }
     catch (Exception $e) {
-      tripal_report_error('tripal', TRIPAL_ERROR, $e->getMessage());
+      $job->logMessage($e->getMessage(), array(), TRIPAL_ERROR);
       drupal_set_message($e->getMessage(), 'error');
     }
 

+ 6 - 0
tripal/includes/TripalImporter.inc

@@ -62,6 +62,12 @@ class TripalImporter {
    */
   public static $require_analysis = TRUE;
 
+  /**
+   * Text that should appear on the button at the bottom of the importer
+   * form.
+   */
+  public static $button_text = 'Import File';
+
 
   // --------------------------------------------------------------------------
   //                  PRIVATE MEMBERS -- DO NOT EDIT

+ 0 - 6
tripal/includes/TripalJob.inc

@@ -464,11 +464,5 @@ class TripalJob {
     if ($severity == TRIPAL_CRITICAL or $severity == TRIPAL_ERROR) {
       tripal_report_error('tripal_job', $severity, $message, $variables);
     }
-    if ($severity == TRIPAL_WARNING) {
-      drupal_set_message($tmessage, 'warning');
-    }
-    else {
-      drupal_set_message($tmessage, 'status');
-    }
   }
 }

+ 1 - 1
tripal/includes/tripal.importer.inc

@@ -65,7 +65,7 @@ function tripal_get_importer_form($form, &$form_state, $class) {
 
   $form['button'] = array(
     '#type' => 'submit',
-    '#value' => t('Import ' . $class),
+    '#value' => t($class::$button_text),
     '#weight' => 10,
   );
   return $form;

+ 50 - 48
tripal_chado/includes/TripalImporter/FASTAImporter.inc

@@ -35,7 +35,15 @@ class FASTAImporter extends TripalImporter {
    */
   public static $upload_title = 'FASTA Upload';
 
+  /**
+   * Text that should appear on the button at the bottom of the importer
+   * form.
+   */
+  public static $button_text = 'Import FASTA file';
 
+  /**
+   * @see TripalImporter::form()
+   */
   public function form($form, &$form_state) {
 
     // get the list of organisms
@@ -218,7 +226,6 @@ class FASTAImporter extends TripalImporter {
    * @see TripalImporter::formValidate()
    */
   public function formValidate($form, &$form_state) {
-    $fasta_file = trim($form_state['values']['fasta_file']);
     $organism_id = $form_state['values']['organism_id'];
     $type = trim($form_state['values']['seqtype']);
     $method = trim($form_state['values']['method']);
@@ -257,18 +264,6 @@ class FASTAImporter extends TripalImporter {
       form_set_error('re_name', t("You must provide a regular expression to identify the sequence name"));
     }
 
-    // check to see if the file is located local to Drupal
-    $fasta_file = trim($fasta_file);
-    $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $fasta_file;
-    if (!file_exists($dfile)) {
-      // if not local to Drupal, the file must be someplace else, just use
-      // the full path provided
-      $dfile = $fasta_file;
-    }
-    if (!file_exists($dfile)) {
-      form_set_error('fasta_file', t("Cannot find the file on the system. Check that the file exists or that the web server has permissions to read the file."));
-    }
-
     // make sure if a relationship is specified that all fields are provided.
     if (($rel_type or $parent_type) and !$re_subject) {
       form_set_error('re_subject', t("Please provide a regular expression for the parent"));
@@ -309,9 +304,6 @@ class FASTAImporter extends TripalImporter {
         form_set_error('parent_type', t("The Sequence Ontology (SO) term selected for the parent relationship is not available in the database. Please check spelling or select another."));
       }
     }
-
-    // check to make sure the 'relationship' and 'sequence' ontologies are loaded
-    $form_state['storage']['dfile'] = $dfile;
   }
   /**
    * @see TripalImporter::run()
@@ -331,10 +323,31 @@ class FASTAImporter extends TripalImporter {
     $rel_type = $arguments['rel_type'];
     $re_subject = $arguments['re_subject'];
     $parent_type = $arguments['parent_type'];
+    $method = $arguments['method'];
     $analysis_id = $arguments['analysis_id'];
+    $match_type = $arguments['match_type'];
 
-    $this->load($dfile, $organism_id, $type, $re_name, $re_uname, $re_accession,
-      $db_id, $rel_type, $re_subject, $parent_type, $method, $uid, $analysis_id,
+
+    if ($method == 0) {
+      $method = 'Insert only';
+    }
+    if ($method == 1) {
+      $method = 'Update only';
+    }
+    if ($method == 2) {
+      $method = 'Insert and update';
+    }
+
+    if ($match_type == 0) {
+      $match_type = 'Name';
+    }
+
+    if ($match_type == 1) {
+      $match_type = 'Unique name';
+    }
+
+    $this->loadFasta($dfile, $organism_id, $type, $re_name, $re_uname, $re_accession,
+      $db_id, $rel_type, $re_subject, $parent_type, $method, $analysis_id,
       $match_type);
   }
   /**
@@ -366,17 +379,15 @@ class FASTAImporter extends TripalImporter {
    * @param $method
    *   The method of feature adding. (ie: 'Insert only', 'Update only',
    *   'Insert and update').
-   * @param $uid
-   *   The user id of the user who submitted the job.
    * @param $analysis_id
    *   The analysis_id to associate the features in this fasta file with.
    * @param $match_type
    *  Whether to match existing features based on the 'Name' or 'Unique name'.
    */
   private function loadFasta($dfile, $organism_id, $type, $re_name, $re_uname, $re_accession,
-      $db_id, $rel_type, $re_subject, $parent_type, $method, $uid, $analysis_id, $match_type) {
+      $db_id, $rel_type, $re_subject, $parent_type, $method, $analysis_id, $match_type) {
 
-    // First get the type for this sequence.
+      // First get the type for this sequence.
     $cvtermsql = "
       SELECT CVT.cvterm_id
       FROM {cvterm} CVT
@@ -417,15 +428,11 @@ class FASTAImporter extends TripalImporter {
     $feature_tbl = chado_get_schema('feature');
     $dbxref_tbl = chado_get_schema('dbxref');
 
-    $this->logMessage(t("Step 1: finding sequences\n"));
+    $this->logMessage(t("Step 1: Finding sequences..."));
     $filesize = filesize($dfile);
-    $this->setTotalItems($filesize);
     $fh = fopen($dfile, 'r');
     if (!$fh) {
-      $this->logMessage("Cannot open file: %dfile", array(
-        '%dfile' => $dfile
-      ), TRIPAL_ERROR);
-      return 0;
+      throw new Exception(t("Cannot open file: %dfile", array('%dfile' => $dfile)));
     }
     $num_read = 0;
 
@@ -459,6 +466,7 @@ class FASTAImporter extends TripalImporter {
             $name = trim($matches[1]);
           }
         }
+
         // If the match_type is name and no regular expression was provided
         // then use the first word as the name, otherwise we don't set the name.
         elseif (strcmp($match_type, 'Name') == 0) {
@@ -537,24 +545,20 @@ class FASTAImporter extends TripalImporter {
     $seqs[$num_seqs - 1]['seq_end'] = $num_read - strlen($line);
 
     // Now that we know where the sequences are in the file we need to add them.
-    $this->logMessage("\nStep 2: Importing sequences\n");
+    $this->logMessage("Step 2: Importing sequences...");
+    $this->setTotalItems($num_seqs);
     for ($i = 0; $i < $num_seqs; $i++) {
       $seq = $seqs[$i];
-      $this->logMessage("Importing " . ($i + 1) . " of $num_seqs. ");
-      if ($name) {
-        $this->logMessage("Current feature: " . $seq['name'] . ".\n");
-      }
-      else {
-        print "Current feature: " . $seq['uname'] . ".\n";
-      }
-
-      loadFastaFeature($fh, $seq['name'], $seq['uname'], $db_id, $seq['accession'], $seq['subject'], $rel_type, $parent_type, $analysis_id, $organism_id, $cvterm, $source, $method, $re_name, $match_type, $parentcvterm, $relcvterm, $seq['seq_start'], $seq['seq_end']);
-    }
-    if ($this->job) {
-      tripal_set_job_progress($this->job->job_id, 100);
+      //$this->logMessage("  importing: @name", array('@name' => $seq['name']));
+      $this->loadFastaFeature($fh, $seq['name'], $seq['uname'], $db_id,
+          $seq['accession'], $seq['subject'], $rel_type, $parent_type,
+          $analysis_id, $organism_id, $cvterm, $source, $method, $re_name,
+          $match_type, $parentcvterm, $relcvterm, $seq['seq_start'],
+          $seq['seq_end']);
+      $this->setItemsHandled($i);
     }
     fclose($fh);
-
+    $this->setItemsHandled($i);
   }
 
   /**
@@ -651,20 +655,18 @@ class FASTAImporter extends TripalImporter {
       }
 
       // Add the residues for this feature
-      loadFastaResidues($fh, $feature->feature_id, $seq_start, $seq_end);
+      $this->loadFastaResidues($fh, $feature->feature_id, $seq_start, $seq_end);
     }
 
     // if we don't have a feature and the user wants to do an update then fail
-    if (!$feature and (strcmp($method, 'Update only') == 0 or
-        drupal_strcmp($method, 'Insert and update') == 0)) {
+    if (!$feature and (strcmp($method, 'Update only') == 0 or strcmp($method, 'Insert and update') == 0)) {
       $this->logMessage("Failed to find feature '%name' ('%uname') while matching on " . drupal_strtolower($match_type) . ".",
           array('%name' => $name,'%uname' => $uname), TRIPAL_ERROR);
       return 0;
     }
 
     // if we do have a feature and this is an update then proceed with the update
-    if ($feature and !$inserted and (strcmp($method, 'Update only') == 0 or
-        strcmp($method, 'Insert and update') == 0)) {
+    if ($feature and !$inserted and (strcmp($method, 'Update only') == 0 or strcmp($method, 'Insert and update') == 0)) {
 
       // if the user wants to match on the Name field
       if (strcmp($match_type, 'Name') == 0) {
@@ -731,7 +733,7 @@ class FASTAImporter extends TripalImporter {
     }
 
     // Update the residues for this feature
-    loadFastaResidues($fh, $feature->feature_id, $seq_start, $seq_end);
+    $this->loadFastaResidues($fh, $feature->feature_id, $seq_start, $seq_end);
 
     // add in the analysis link
     if ($analysis_id) {

+ 7 - 0
tripal_chado/includes/TripalImporter/GFF3Importer.inc

@@ -36,6 +36,13 @@ class GFF3Importer extends TripalImporter {
    * The title that should appear above the upload button.
    */
   public static $upload_title = 'GFF3 File';
+
+  /**
+   * Text that should appear on the button at the bottom of the importer
+   * form.
+   */
+  public static $button_text = 'Import GFF3 file';
+
   /**
    * @see TripalImporter::form()
    */