Пример #1
0
  public boolean imp() {
    boolean res = false;

    DbVariable dbVariable = null;
    String fullFileName = "";
    String errMessage = null;
    DbImportFile dbInFile = new DbImportFile();
    try {
      dbVariable = new DbVariable();

      // Store the file on server filesystem
      fullFileName = dbInFile.storeImportFileBLOB(conn_viss, ifid);

      FileParser fileParser = new FileParser(fullFileName);
      fileParser.Parse(
          FileTypeDefinitionList.matchingDefinitions(
              FileTypeDefinition.VARIABLE, FileTypeDefinition.LIST));

      dbVariable.CreateVariables(fileParser, connection, sampleUnitId, Integer.parseInt(userId));

      errMessage = dbVariable.getErrorMessage();
      Assertion.assertMsg(errMessage == null || errMessage.trim().equals(""), errMessage);

      dbInFile.setStatus(conn_viss, ifid, "IMPORTED");
      // dbInFile.UpdateImportFile(connection,null,null,"Done",Integer.parseInt(ifid),Integer.parseInt(userId));

      // Add a message to the log
      dbInFile.addErrMsg(
          conn_viss,
          ifid,
          "File imported to sampling unit "
              + DbSamplingUnit.getSUName(conn_viss, Integer.toString(sampleUnitId))
              + "Note: Markers is always imported in Create mode.");
      res = true;
    } catch (Exception e) {
      Errors.logError("ImportVariables.imp(...)" + e.getMessage());

      dbInFile.setStatus(conn_viss, ifid, "ERROR");
      // dbInFile.UpdateImportFile(connection,null,null,e.getMessage(),Integer.parseInt(ifid),Integer.parseInt(userId));

      // Add a message to the log
      dbInFile.addErrMsg(conn_viss, ifid, e.getMessage());

      e.printStackTrace(System.err);
      if (errMessage == null) {
        errMessage = e.getMessage();
      }
    } finally {
      try {
        /*
         * Delete files uploaded
         */
        File tmp = new File(fullFileName);
        tmp.delete();
      } catch (Exception ignore) {
      }
    }

    return res;
  }
Пример #2
0
  /**
   * Check the headers of the Phenotype matrix file
   *
   * @param titles
   * @param errorMessages
   * @param suid
   * @return
   */
  private boolean checkMatrixTitles(String[] titles, Vector errorMessages) {
    boolean errorFound = false;
    String errorStr = null;
    try {

      if (titles.length < 2) errorFound = true;
      else if (!(titles[0].equals("IDENTITY") || titles[0].equals("ALIAS"))) {
        errorFound = true;
      }
      if (errorFound) {
        errorStr =
            " Illegal headers.\n"
                + "# Required file headers: IDENTITY/ALIAS VARIABLE1 VARIABLE2 ...\n"
                + "# Headers found in file: ";
        for (int j = 0; j < titles.length; j++) {
          errorStr = errorStr + " " + titles[j];
        }
        errorMessages.addElement(errorStr);
      }

      // now we check that the variables in header exists
      for (int i = 1; i < titles.length; i++) {
        if (db.isVariableUnique(titles[i])) {
          errorFound = true;
          errorStr = "Variable " + titles[i] + " does not exist.";
          errorMessages.addElement(errorStr);
        }
      }
    } catch (Exception e) {
      e.printStackTrace(System.err);
    }
    return errorFound;
  }
Пример #3
0
 /** Creates a new instance of ImportVariables */
 public ImportVariables() {
   CREATE = true;
   SUID = true;
   try {
     headers = new ArrayList<FileHeader>();
     headers.add(new FileHeader("VARIABLE", "LIST", 1, '\t'));
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Пример #4
0
  /** Compares all values to whats already in the database returns the number of errors found. */
  private void checkCreate(
      String id_or_alias,
      String ind,
      String variable,
      String value,
      String date,
      String ref,
      String comm,
      int suid) {

    // ResultSet rset;
    int nrErrors = 0;
    String identity = null;
    try {
      // First if no IDENTITY was sent, we need to get it through alias!

      if (id_or_alias.equalsIgnoreCase("ALIAS")
          && (ind != null)
          && (!ind.trim().equalsIgnoreCase(""))) {
        identity = db.getIdentity(ind);
      } else {
        identity = ind;
      }

      // compare to database, is this unique? (can it be inserted?)
      if (!db.isPhenotypeUnique(variable, identity)) {
        // the phenotype exists
        String Message =
            " The Phenotype [V="
                + variable
                + ", I="
                + identity
                + "] already exists, cannot be created.";
        errorList.add(Message);
        nrErrors++;
      }

      // Does the individual exist?
      if (db.isIndividualUnique(identity)) {
        // the Individual does not exist
        String Message = " The Individual with " + id_or_alias + " " + ind + " does not exist.";
        errorList.add(Message);
      }

      // does variable exist?
      if (db.isVariableUnique(variable)) {
        // the variable does not exist
        String Message = " Variable " + variable + " does not exist";
        errorList.add(Message);
      }
    } catch (Exception e) {
      // Flag for error and set the errMessage if it has not been set
      e.printStackTrace(System.err);
    }
  }
Пример #5
0
  private void writeListErrors(
      FileWriter fileOut,
      Vector deviationMessages,
      Vector databaseValues,
      String ind,
      char delimeter,
      String variable,
      String value,
      String date,
      String ref,
      String comm) {

    try {

      if (errorList.size() > 0 || deviationMessages.size() > 0 || warningList.size() > 0) {
        fileOut.write("#--------------------------------------------------\n");
      }
      if (errorList.size() > 0) {
        for (int i = 0; i < errorList.size(); i++) {
          fileOut.write("#" + (String) errorList.get(i) + "\n");
        }
      }
      if (warningList.size() > 0) {
        for (int i = 0; i < warningList.size(); i++) {
          fileOut.write("#" + (String) warningList.get(i) + "\n");
        }
      }

      if (deviationMessages.size() > 0) {
        for (int i = 0; i < deviationMessages.size(); i++) {
          fileOut.write("#" + (String) deviationMessages.elementAt(i) + "\n");
        }
        // write old values
        fileOut.write("#" + databaseValues.elementAt(0) + "\n");
      }
      // if there are errors, the string is "Outcommented"
      if (errorList.size() > 0) {
        fileOut.write("#");
      }

      // write original string
      fileOut.write(
          ind + delimeter + variable + delimeter + value + delimeter + date + delimeter + ref
              + delimeter + comm + "\n");

      if (errorList.size() > 0 || deviationMessages.size() > 0 || warningList.size() > 0) {
        fileOut.write("#--------------------------------------------------\n");
      }

    } // try
    catch (Exception e) {
      e.printStackTrace(System.err);
    }
  }
Пример #6
0
  /**
   * Checks if the genotype exists, that alleles exists etc. Makes certain the genotype can be
   * updated
   */
  private void checkCreateOrUpdate(
      String id_or_alias,
      String ind,
      String variable,
      String value,
      String date,
      String ref,
      String comm,
      int suid,
      Vector deviationMessages,
      Vector databaseValues,
      char delim) {
    // ResultSet rset;
    String identity = null;
    boolean match = false;

    try {

      // First if no IDENTITY was sent, we need to get it through alias!
      if (id_or_alias.equalsIgnoreCase("ALIAS")
          && ind != null
          && !ind.trim().equalsIgnoreCase("")) {
        identity = db.getIdentity(ind);
      } else identity = ind;

      // compare to database, does genotype exist? can it be updated..
      if (!db.isPhenotypeUnique(variable, identity)) {
        // Genotype exists -- check for update

        checkUpdate(
            id_or_alias,
            ind,
            variable,
            value,
            date,
            ref,
            comm,
            suid,
            deviationMessages,
            databaseValues,
            delim);
      } else {
        // Genotype doesn't exist
        checkCreate(id_or_alias, ind, variable, value, date, ref, comm, suid);
        //  checkCreate(id_or_alias, ind, variable, value, date, ref, comm, suid,
        //    errorMessages,warningMessages);
      }
    } // try
    catch (Exception e) {
      // Flag for error and set the errMessage if it has not been set
      e.printStackTrace(System.err);
    }
  }
Пример #7
0
 /** Creates a new instance of ImportPhenotypes */
 public ImportPhenotypes() {
   CREATE = true;
   UPDATE = true;
   CREATE_OR_UPDATE = true;
   SUID = true;
   LEVEL = true;
   try {
     headers = new ArrayList<FileHeader>();
     headers.add(new FileHeader("PHENOTYPE", "LIST", 1, '\t'));
     headers.add(new FileHeader("PHENOTYPE", "MATRIX", 1, '\t'));
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Пример #8
0
  /**
   * @param fileOut
   * @param errorMessages
   * @param warningMessages
   * @param deviationMessages
   * @param databaseValues
   * @param ind
   * @param delimeter
   * @param marker
   * @param allele1
   * @param allele2
   * @param raw1
   * @param raw2
   * @param ref
   * @param comm
   */
  private void writeTitleErrors(FileWriter fileOut, Vector titleError) {

    try {

      if (titleError.size() > 0) {
        fileOut.write("#--------------------------------------------------\n");
        for (int i = 0; i < titleError.size(); i++) {
          fileOut.write("#" + (String) titleError.elementAt(i) + "\n");
        }
        fileOut.write("#--------------------------------------------------\n");
      }
    } catch (Exception e) {
      e.printStackTrace(System.err);
    }
  }
Пример #9
0
  public boolean check() {
    Errors.logDebug("CheckVariables started");
    boolean res = false;
    DbImportFile dbInFile = new DbImportFile();
    String fullFileName = "";
    String checkFileName = "";
    String errMessage = null;

    try {
      // Create the variable
      DbVariable dbVariable = new DbVariable();

      dbInFile.setStatus(conn_viss, ifid, "0%");

      // Store the file on server filesystem
      fullFileName = dbInFile.storeImportFileBLOB(conn_viss, ifid);
      checkFileName = fullFileName + "_checked";

      FileHeader header = FileParser.scanFileHeader(fullFileName);
      String type = header.formatTypeName().toUpperCase();
      char delimiter = header.delimiter().charValue();

      FileParser fileParser = new FileParser(fullFileName);
      fileParser.Parse(
          FileTypeDefinitionList.matchingDefinitions(
              FileTypeDefinition.VARIABLE, FileTypeDefinition.LIST));

      // Write out the result to a new file
      FileWriter fileOut = new FileWriter(checkFileName);
      fileOut.write(
          header.objectTypeName()
              + "/"
              + header.formatTypeName()
              + "/"
              + header.version()
              + "/"
              + header.delimiter()
              + "\n");

      String titles[] = fileParser.columnTitles();
      for (int j = 0; j < titles.length; j++) {
        fileOut.write(titles[j] + delimiter);
      }
      fileOut.write("\n");

      String errorMsg = "";
      errorMsg = checkList(fileParser, fileOut, delimiter);
      fileOut.close();

      /*
       * Save the file to database
       */
      dbInFile.saveCheckedFile(conn_viss, ifid, checkFileName);

      if (errorMsg.startsWith("ERROR:")) {
        res = false;
        dbInFile.setStatus(conn_viss, ifid, "ERROR");

        // Add a message to the log
        dbInFile.addErrMsg(
            conn_viss,
            ifid,
            "File checked failed for sampling unit "
                + DbSamplingUnit.getSUName(conn_viss, Integer.toString(sampleUnitId))
                + ". <br>"
                + errorMsg);
        Errors.logDebug("ErrorMsg=" + errorMsg);
      } else if (errorMsg.startsWith("WARNING:")) {
        dbInFile.setStatus(conn_viss, ifid, "WARNING");
        res = true;

        // Add a message to the log
        dbInFile.addErrMsg(conn_viss, ifid, "Warnings exists: " + errorMsg);
      } else {
        dbInFile.setStatus(conn_viss, ifid, "CHECKED");
        res = true;

        // Add a message to the log
        dbInFile.addErrMsg(
            conn_viss,
            ifid,
            "File checked for sampling unit "
                + DbSamplingUnit.getSUName(conn_viss, Integer.toString(sampleUnitId))
                + ".<br>"
                + errorMsg);
      }

      /*
      if (errorMsg.length()>0)
      {
          res = false;
          dbInFile.setStatus(conn_viss,ifid,"ERROR");
          // Add a message to the log
          dbInFile.addErrMsg(conn_viss,ifid,"File failed the check for sampling unit "+DbSamplingUnit.getSUName(conn_viss,Integer.toString(sampleUnitId)) +"<br>"+errorMsg);
      }
      else
      {
          res = true;
          dbInFile.setStatus(conn_viss,ifid,"CHECKED");
          // Add a message to the log
          dbInFile.addErrMsg(conn_viss,ifid,"File checked for sampling unit "+DbSamplingUnit.getSUName(conn_viss,Integer.toString(sampleUnitId)) +"<br>"+errorMsg);
      }*/
    } catch (Exception e) {
      dbInFile.setStatus(conn_viss, ifid, "ERROR");

      // Add a message to the log
      dbInFile.addErrMsg(conn_viss, ifid, e.getMessage());

      e.printStackTrace(System.err);
      if (errMessage == null) {
        errMessage = e.getMessage();
      }
    } finally {
      try {
        /*
         * Delete files uploaded
         */
        File tmp = new File(checkFileName);
        tmp.delete();

        tmp = new File(fullFileName);
        tmp.delete();
      } catch (Exception ignore) {
      }
    }

    Errors.logDebug("CheckVariables completed");
    return res;
  }
Пример #10
0
  public boolean imp() {
    boolean res = false;
    String errMessage = null;

    DbImportFile dbInFile = new DbImportFile();
    DbPhenotype dbp = new DbPhenotype();
    String fullFileName = null;

    try {
      Errors.logInfo("CheckPhenotype started");
      // connection.setAutoCommit(false);
      dbInFile.setStatus(conn_viss, ifid, "0%");

      fullFileName = dbInFile.storeImportFileBLOB(conn_viss, ifid);

      FileHeader header = FileParser.scanFileHeader(fullFileName);
      FileParser fileParser = new FileParser(fullFileName);

      // Set status
      dbInFile.setStatus(conn_viss, ifid, "10%");

      // Ensure file format is list or matrix
      Assertion.assertMsg(
          header.formatTypeName().equalsIgnoreCase(FileTypeDefinition.LIST)
              || header.formatTypeName().equalsIgnoreCase(FileTypeDefinition.MATRIX),
          "Format type name should be list or matrix "
              + "but found found "
              + header.formatTypeName());

      // If file is a list
      if (header.formatTypeName().equalsIgnoreCase(FileTypeDefinition.LIST)) {
        fileParser.Parse(
            FileTypeDefinitionList.matchingDefinitions(
                FileTypeDefinition.PHENOTYPE, FileTypeDefinition.LIST));
        dbInFile.setStatus(conn_viss, ifid, "20%");

        if (updateMethod.equals("CREATE")) {
          dbp.CreatePhenotypesList(
              fileParser, connection, sampleUnitId, Integer.valueOf(userId).intValue());
        } else if (updateMethod.equals("UPDATE")) {
          dbp.UpdatePhenotypesList(
              fileParser, connection, sampleUnitId, Integer.valueOf(userId).intValue());
        } else if (updateMethod.equals("CREATE_OR_UPDATE")) {
          dbp.CreateOrUpdatePhenotypesList(
              fileParser, connection, sampleUnitId, Integer.valueOf(userId).intValue());
        }
      }

      // If file is a matrix
      else if (header.formatTypeName().equalsIgnoreCase(FileTypeDefinition.MATRIX)) {
        fileParser.Parse(
            FileTypeDefinitionList.matchingDefinitions(
                FileTypeDefinition.PHENOTYPE, FileTypeDefinition.MATRIX));
        dbInFile.setStatus(conn_viss, ifid, "20%");

        if (updateMethod.equals("CREATE")) {
          dbp.CreatePhenotypesMatrix(
              fileParser, connection, sampleUnitId, Integer.valueOf(userId).intValue());
        } else if (updateMethod.equals("UPDATE")) {
          dbp.UpdatePhenotypesMatrix(
              fileParser, connection, sampleUnitId, Integer.valueOf(userId).intValue());
        } else if (updateMethod.equals("CREATE_OR_UPDATE")) {
          dbp.CreateOrUpdatePhenotypesMatrix(
              fileParser, connection, sampleUnitId, Integer.valueOf(userId).intValue());
        }
      }
      errMessage = dbp.getErrorMessage();

      Assertion.assertMsg(errMessage == null || errMessage.trim().equals(""), errMessage);

      dbInFile.setStatus(conn_viss, ifid, "IMPORTED");

      // Add a message to the log
      dbInFile.addErrMsg(
          conn_viss,
          ifid,
          "File imported for sampling unit "
              + DbSamplingUnit.getSUName(conn_viss, Integer.toString(sampleUnitId)));
      res = true;

      Errors.logInfo("Check Phenotype ended");
    } catch (Exception e) {
      // Flag for error and set the errMessage if it has not been set
      // isOk = false;
      dbInFile.setStatus(conn_viss, ifid, "ERROR");

      // Add a message to the log
      dbInFile.addErrMsg(conn_viss, ifid, e.getMessage());

      e.printStackTrace(System.err);
      if (errMessage == null) {
        errMessage = e.getMessage();
      }
    }

    return res;
  }
Пример #11
0
  /**
   * @param fileOut
   * @param errorMessages
   * @param warningMessages
   * @param deviationMessages
   * @param databaseValues
   * @param newAlleles
   * @param ind
   * @param delim
   * @param marker
   * @param allele1
   * @param allele2
   */
  private void writeMatrixErrors(
      FileWriter fileOut,
      Vector deviationMessages,
      Vector databaseValues,
      Vector values,
      String ind,
      char delim,
      String variable,
      String value) {
    try {
      // if row contains comments
      if (errorList.size() > 0 || deviationMessages.size() > 0 || warningList.size() > 0) {
        fileOut.write("#--------------------------------------------------------\n");
      }
      if (errorList.size() > 0) {
        for (int i = 0; i < errorList.size(); i++) {
          fileOut.write("#" + (String) errorList.get(i) + "\n");
        }
      }

      if (warningList.size() > 0) {
        for (int i = 0; i < warningList.size(); i++) {
          fileOut.write("#" + (String) warningList.get(i) + "\n");
        }
      }

      if (deviationMessages.size() > 0) {
        for (int i = 0; i < deviationMessages.size(); i++) {
          fileOut.write("#" + (String) deviationMessages.elementAt(i) + "\n");
        }
        // write database values
        fileOut.write("#" + ind);
        for (int i = 0; i < databaseValues.size(); i++) {
          fileOut.write(delim + (String) databaseValues.elementAt(i));
        }
        fileOut.write("\n");
      }
      // write row from file:
      if (errorList.size() > 0) {
        fileOut.write("#");
      }
      fileOut.write(ind);

      for (int i = 0; i < values.size(); i++) {
        fileOut.write(delim + (String) values.elementAt(i));
      }
      fileOut.write("\n");

      /*
      for (int i=0;i<newAlleles.size();i++)
      {
          fileOut.write(delim+(String)newAlleles.elementAt(i));
      }
      fileOut.write("\n");
      */

      // fileOut.write(delim+allele1);
      // fileOut.write(delim+allele2);
      // fileOut.write("\n");

      if (errorList.size() > 0 || deviationMessages.size() > 0 || warningList.size() > 0) {
        fileOut.write("#--------------------------------------------------------\n");
      }
    } // try
    catch (Exception e) {
      e.printStackTrace(System.err);
    }
  }
Пример #12
0
  public boolean check() {
    Errors.logDebug("CheckPhenotype started");

    boolean res = false;

    String errMessage = null;
    FileWriter fileOut = null;
    DbImportFile dbInFile = new DbImportFile();

    String fullFileName = "";
    String checkFileName = "";
    try {
      dbInFile.setStatus(conn_viss, ifid, "0%");

      fullFileName = dbInFile.storeImportFileBLOB(conn_viss, ifid);
      checkFileName = fullFileName + "_checked";

      // Create the Phenotype
      DbPhenotype dbPhenotype = new DbPhenotype();

      FileHeader header = FileParser.scanFileHeader(fullFileName);
      String type = header.formatTypeName().toUpperCase();
      char delimiter = header.delimiter().charValue();

      // AbstractValueFileParser fp = null;
      FileParser fp = null;

      if (type.equals("LIST")) {
        fp = new FileParser(fullFileName);
        fp.Parse(
            FileTypeDefinitionList.matchingDefinitions(
                FileTypeDefinition.PHENOTYPE, FileTypeDefinition.LIST));
      } else if (type.equals("MATRIX")) {
        fp = new FileParser(fullFileName);
        fp.Parse(
            FileTypeDefinitionList.matchingDefinitions(
                FileTypeDefinition.PHENOTYPE, FileTypeDefinition.MATRIX));
      }

      // Write out the result to a new file
      fileOut = new FileWriter(checkFileName);
      fileOut.write(
          header.objectTypeName()
              + "/"
              + header.formatTypeName()
              + "/"
              + header.version()
              + "/"
              + header.delimiter()
              + "\n");

      String titles[] = fp.columnTitles();
      for (int j = 0; j < titles.length; j++) {
        fileOut.write(titles[j] + delimiter);
      }
      fileOut.write("\n");

      // Garbage collect the unused variables
      header = null;
      // fullFileName = null;
      // checkFileName = null;

      // Fix to upper case
      updateMethod = updateMethod.toUpperCase();

      Vector fatalErrors = new Vector();

      if (type.equals("LIST")) checkListTitles(titles, fatalErrors);
      else checkMatrixTitles(titles, fatalErrors);

      writeTitleErrors(fileOut, fatalErrors);

      String indId;

      if (titles[0].equals("IDENTITY")) indId = "IDENTITY";
      else indId = "Alias";

      String errMsg = "";
      if (type.equals("LIST")) errMsg = checkList((FileParser) fp, fileOut, delimiter, indId);
      // errMsg = checkList((FileParser)fp, fatalErrors,fileOut,delimiter,indId);
      else if (type.equals("MATRIX"))
        errMsg = checkMatrix((FileParser) fp, fatalErrors, fileOut, delimiter, indId);

      // Close the file
      fileOut.close();

      /*
       * Save the file to database
       */
      dbInFile.saveCheckedFile(conn_viss, ifid, checkFileName);

      // Get the error message from the database object. If it is set an
      // error occured during the operation so an error is thrown.
      // errMessage = dbIndividual.getErrorMessage();
      // Assertion.assertMsg(errMessage == null ||
      //                     errMessage.trim().equals(""), errMessage);

      if (errMsg.startsWith("ERROR:")) {
        dbInFile.setStatus(conn_viss, ifid, "ERROR");
        res = false;
      } else if (errMsg.startsWith("WARNING:")) {
        dbInFile.setStatus(conn_viss, ifid, "WARNING");
        res = true;
      } else {
        dbInFile.setStatus(conn_viss, ifid, "CHECKED");
        res = true;
      }

      // Add a message to the log
      dbInFile.addErrMsg(
          conn_viss,
          ifid,
          "File checked for sampling unit "
              + DbSamplingUnit.getSUName(conn_viss, Integer.toString(sampleUnitId))
              + "<br>\n"
              + errMsg);

    } catch (Exception e) {
      // Flag for error and set the errMessage if it has not been set
      // isOk = false;
      dbInFile.setStatus(conn_viss, ifid, "ERROR");
      // dbInFile.UpdateImportFile(connection,null,null,e.getMessage(),Integer.parseInt(ifid),Integer.parseInt(userId));

      // Add a message to the log
      dbInFile.addErrMsg(conn_viss, ifid, e.getMessage());

      e.printStackTrace(System.err);
      if (errMessage == null) {
        errMessage = e.getMessage();
      }
    } finally {
      try {

        /*
         * Delete temporary file
         */
        File tmp = new File(fullFileName);
        tmp.delete();
        tmp = null;

        tmp = new File(checkFileName);
        tmp.delete();
        tmp = null;
      } catch (Exception ignore) {
      }
    }

    Errors.logDebug("CheckPhenotype completed");

    return res;
  }