public PeptideTerminalAAResult calculateForAnalysis(int analysisId) {

    DAOFactory daoFactory = DAOFactory.instance();

    MsRunSearchAnalysisDAO rsaDao = daoFactory.getMsRunSearchAnalysisDAO();
    List<Integer> runSearchAnalysisIds = rsaDao.getRunSearchAnalysisIdsForAnalysis(analysisId);

    PeptideTerminalAAResult result =
        calculateForRunSearchAnalysisIds(analysisId, runSearchAnalysisIds);
    result.setEnzyme(enzyme);
    return result;
  }
 private int getSearchDatabaseId(int searchId) {
   MsSearchDAO searchDao = DAOFactory.instance().getMsSearchDAO();
   MsSearch search = searchDao.loadSearch(searchId);
   List<MsSearchDatabase> db = search.getSearchDatabases();
   if (db.size() == 0) return 0;
   return NrSeqLookupUtil.getDatabaseId(db.get(0).getDatabaseFileName());
 }
  public void convertToSqt(int runSearchId, String outputFile) throws IOException {

    try {
      outFile = new BufferedWriter(new FileWriter(outputFile));

      SQTRunSearchDAO searchDao = DAOFactory.instance().getSqtRunSearchDAO();
      SQTRunSearch runSearch = searchDao.loadRunSearch(runSearchId);
      if (runSearch == null) {
        System.err.println("No run search found with id: " + runSearchId);
        return;
      }

      int searchDatabaseId = getSearchDatabaseId(runSearch.getSearchId());

      printSqtHeader(runSearch);
      outFile.write("\n");
      SearchFileFormat origFileType = runSearch.getSearchFileFormat();
      if (origFileType == SearchFileFormat.SQT_SEQ) {
        printSequestSQTData(runSearch, searchDatabaseId, outFile);
      } else if (origFileType == SearchFileFormat.SQT_PLUCID) {
        // TODO
      }

      outFile.flush();
    } finally {
      if (outFile != null) outFile.close();
    }
  }
  public PsmRetTimeDistribution getDistribution() {

    MsSearchAnalysis analysis = DAOFactory.instance().getMsSearchAnalysisDAO().load(analysisId);
    if (analysis.getAnalysisProgram() == Program.PERCOLATOR) {
      return getPercolatorResults();
    } else if (analysis.getAnalysisProgram() == Program.PEPTIDE_PROPHET) {
      return getPeptideProphetResults();
    }

    return null;
  }
  public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {

    // User making this request
    User user = UserUtils.getUser(request);
    if (user == null) {
      ActionErrors errors = new ActionErrors();
      errors.add("username", new ActionMessage("error.login.notloggedin"));
      saveErrors(request, errors);
      return mapping.findForward("authenticate");
    }

    // Restrict access to researchers who are members of a group
    Groups groupMan = Groups.getInstance();
    if (!groupMan.isInAGroup(user.getResearcher().getID())) {
      ActionErrors errors = new ActionErrors();
      errors.add("access", new ActionMessage("error.access.invalidgroup"));
      saveErrors(request, errors);
      return mapping.findForward("standardHome");
    }

    UploadMSDataForm newForm = new UploadMSDataForm();
    if (groupMan.isMember(user.getResearcher().getID(), "administrators")) {
      newForm.setPipeline(Pipeline.TPP);
      newForm.setDataServer("local");
    } else if (groupMan.isMember(user.getResearcher().getID(), "MacCoss")) {
      newForm.setPipeline(Pipeline.MACOSS);
      newForm.setDataServer("local");
    } else {
      newForm.setPipeline(Pipeline.TPP);
      newForm.setDataServer("local");
    }

    try {
      int projectID = Integer.parseInt(request.getParameter("projectID"));
      newForm.setProjectID(projectID);
    } catch (Exception e) {
      newForm.setProjectID(0);
    }

    request.setAttribute("uploadMSDataForm", newForm);

    // get a list of projects on which this researcher is listed
    List<ProjectLite> projects =
        ProjectLiteDAO.instance().getResearcherWritableProjects(user.getResearcher().getID());
    Collections.sort(
        projects,
        new Comparator<ProjectLite>() {
          @Override
          public int compare(ProjectLite o1, ProjectLite o2) {
            return Integer.valueOf(o2.getId()).compareTo(o1.getId());
          }
        });

    request.getSession().setAttribute("researcherProjects", projects);

    // get a list of instruments available
    List<MsInstrument> instruments = DAOFactory.instance().getInstrumentDAO().loadAllInstruments();
    request.getSession().setAttribute("instrumentList", instruments);

    // Kick it to the view page
    return mapping.findForward("Success");
  }
  private PsmRetTimeDistribution getPercolatorResults() {

    List<PercolatorFilteredPsmResult> filteredResults = null;
    if (scoreCutoff == 0.01) {
      // Look in the database first for pre-calculated results
      PercolatorFilteredPsmResultDAO dao =
          DAOFactory.instance().getPrecolatorFilteredPsmResultDAO();
      filteredResults = dao.loadForAnalysis(analysisId);
    }

    if (filteredResults == null || filteredResults.size() == 0) {

      // Results were not found in the database; calculate now
      PercolatorFilteredPsmDistributionCalculator calc =
          new PercolatorFilteredPsmDistributionCalculator(analysisId, scoreCutoff);
      calc.calculate();
      filteredResults = calc.getFilteredResults();
    }

    if (filteredResults == null || filteredResults.size() == 0) {
      log.error("No results for searchAnalysisID: " + analysisId);
      return null;
    }

    PercolatorFilteredPsmResultDAO statsDao =
        DAOFactory.instance().getPrecolatorFilteredPsmResultDAO();
    double populationMax = RoundingUtils.getInstance().roundOne(statsDao.getPopulationMax());
    double populationMin = RoundingUtils.getInstance().roundOne(statsDao.getPopulationMin());
    double populationMean =
        RoundingUtils.getInstance().roundOne(statsDao.getPopulationAvgFilteredPercent());
    double populationStddev =
        RoundingUtils.getInstance().roundOne(statsDao.getPopulationStdDevFilteredPercent());

    int[] allPsmCounts = null;
    int[] filteredPsmCounts = null;

    int maxPsmCount = 0;
    double maxRt = 0;

    List<FileStats> fileStats = new ArrayList<FileStats>(filteredResults.size());
    MsRunSearchAnalysisDAO rsaDao = DAOFactory.instance().getMsRunSearchAnalysisDAO();

    maxRt = getMaxRtForPercolatorResults(filteredResults.get(0).getBinnedResults());
    int binIncr = getBinIncrement(maxRt);
    int numBins = (int) Math.ceil(maxRt / binIncr);

    for (PercolatorFilteredPsmResult res : filteredResults) {

      int runSearchAnalysisId = res.getRunSearchAnalysisId();
      String filename = rsaDao.loadFilenameForRunSearchAnalysis(runSearchAnalysisId);
      FileStats stats = new FileStats(res.getRunSearchAnalysisId(), filename);
      stats.setGoodCount(res.getFiltered());
      stats.setTotalCount(res.getTotal());
      if (scoreCutoff == 0.01) {
        stats.setPopulationMean(populationMean);
        stats.setPopulationStandardDeviation(populationStddev);
        stats.setPopulationMin(populationMin);
        stats.setPopulationMax(populationMax);
      }

      fileStats.add(stats);

      List<PercolatorBinnedPsmResult> binnedResults = res.getBinnedResults();
      Collections.sort(
          binnedResults,
          new Comparator<PercolatorBinnedPsmResult>() {
            @Override
            public int compare(PercolatorBinnedPsmResult o1, PercolatorBinnedPsmResult o2) {
              return Double.valueOf(o1.getBinStart()).compareTo(o2.getBinStart());
            }
          });

      if (allPsmCounts == null) allPsmCounts = new int[numBins];
      if (filteredPsmCounts == null) filteredPsmCounts = new int[numBins];

      maxRt = Math.max(maxRt, binnedResults.get(binnedResults.size() - 1).getBinEnd());

      int idx = 0;
      for (int j = 0; j < binnedResults.size(); j++) {
        PercolatorBinnedPsmResult binned = binnedResults.get(j);
        allPsmCounts[idx] += binned.getTotal();
        filteredPsmCounts[idx] += binned.getFiltered();
        maxPsmCount = Math.max(allPsmCounts[idx], maxPsmCount);
        if (j > 0 && j % binIncr == 0) idx++;
      }
    }

    PsmRetTimeDistribution distribution = new PsmRetTimeDistribution(Program.PERCOLATOR);
    distribution.setScoreCutoff(this.scoreCutoff);
    distribution.setNumBins(allPsmCounts.length);
    distribution.setMaxRT(maxRt);
    distribution.setMaxPsmCount(maxPsmCount);
    distribution.setFilteredPsmCounts(filteredPsmCounts);
    distribution.setAllPsmCounts(allPsmCounts);
    distribution.setBinSize(binIncr);
    distribution.setFileStatsList(fileStats);

    return distribution;
  }
  private PsmRetTimeDistribution getPeptideProphetResults() {

    List<ProphetFilteredPsmResult> filteredResults = null;
    if (scoreCutoff == QCStatsGetter.PEPPROPHET_ERR_RATE_DEFAULT) {
      // Look in the database first for pre-calculated results
      ProphetFilteredPsmResultDAO dao = DAOFactory.instance().getProphetFilteredPsmResultDAO();
      filteredResults = dao.loadForAnalysis(analysisId);
    }

    if (filteredResults == null || filteredResults.size() == 0) {

      // Results were not found in the database; calculate now

      PeptideProphetRocDAO rocDao = DAOFactory.instance().getPeptideProphetRocDAO();
      PeptideProphetROC roc = rocDao.loadRoc(analysisId);
      double probability = roc.getMinProbabilityForError(scoreCutoff);
      log.info("Probability for error rate of " + scoreCutoff + " is: " + probability);

      ProphetFilteredPsmDistributionCalculator calc =
          new ProphetFilteredPsmDistributionCalculator(analysisId, probability);
      calc.calculate();
      filteredResults = calc.getFilteredResults();
    }

    if (filteredResults == null || filteredResults.size() == 0) {
      log.error("No results for searchAnalysisID: " + analysisId);
      return null;
    }

    ProphetFilteredPsmResultDAO statsDao = DAOFactory.instance().getProphetFilteredPsmResultDAO();
    double populationMax = RoundingUtils.getInstance().roundOne(statsDao.getPopulationMax());
    double populationMin = RoundingUtils.getInstance().roundOne(statsDao.getPopulationMin());
    double populationMean =
        RoundingUtils.getInstance().roundOne(statsDao.getPopulationAvgFilteredPercent());
    double populationStddev =
        RoundingUtils.getInstance().roundOne(statsDao.getPopulationStdDevFilteredPercent());

    int[] allPsmCounts = null;
    int[] filteredPsmCounts = null;

    int maxPsmCount = 0;
    double maxRt = 0;

    List<FileStats> fileStats = new ArrayList<FileStats>(filteredResults.size());
    MsRunSearchAnalysisDAO rsaDao = DAOFactory.instance().getMsRunSearchAnalysisDAO();

    maxRt = getMaxRtForProphetResults(filteredResults.get(0).getBinnedResults());
    int binIncr = getBinIncrement(maxRt);
    int numBins = (int) Math.ceil(maxRt / binIncr);

    double actualScoreCutoff =
        -1.0; // actual probability cutoff used (that corresponds to the given error rate)

    for (ProphetFilteredPsmResult res : filteredResults) {

      actualScoreCutoff = res.getProbability();

      int runSearchAnalysisId = res.getRunSearchAnalysisId();
      String filename = rsaDao.loadFilenameForRunSearchAnalysis(runSearchAnalysisId);
      FileStats stats = new FileStats(res.getRunSearchAnalysisId(), filename);
      stats.setGoodCount(res.getFiltered());
      stats.setTotalCount(res.getTotal());
      if (scoreCutoff == 0.01) {
        stats.setPopulationMean(populationMean);
        stats.setPopulationStandardDeviation(populationStddev);
        stats.setPopulationMin(populationMin);
        stats.setPopulationMax(populationMax);
      }

      fileStats.add(stats);

      List<ProphetBinnedPsmResult> binnedResults = res.getBinnedResults();
      Collections.sort(
          binnedResults,
          new Comparator<ProphetBinnedPsmResult>() {
            @Override
            public int compare(ProphetBinnedPsmResult o1, ProphetBinnedPsmResult o2) {
              return Double.valueOf(o1.getBinStart()).compareTo(o2.getBinStart());
            }
          });

      if (allPsmCounts == null) allPsmCounts = new int[numBins];
      if (filteredPsmCounts == null) filteredPsmCounts = new int[numBins];

      maxRt = Math.max(maxRt, binnedResults.get(binnedResults.size() - 1).getBinEnd());

      int idx = 0;
      for (int j = 0; j < binnedResults.size(); j++) {
        ProphetBinnedPsmResult binned = binnedResults.get(j);
        allPsmCounts[idx] += binned.getTotal();
        filteredPsmCounts[idx] += binned.getFiltered();
        maxPsmCount = Math.max(allPsmCounts[idx], maxPsmCount);
        if (j > 0 && j % binIncr == 0) idx++;
      }
    }

    PsmRetTimeDistribution distribution = new PsmRetTimeDistribution(Program.PEPTIDE_PROPHET);
    distribution.setScoreCutoff(actualScoreCutoff);
    distribution.setNumBins(allPsmCounts.length);
    distribution.setMaxRT(maxRt);
    distribution.setMaxPsmCount(maxPsmCount);
    distribution.setFilteredPsmCounts(filteredPsmCounts);
    distribution.setAllPsmCounts(allPsmCounts);
    distribution.setBinSize(binIncr);
    distribution.setFileStatsList(fileStats);

    return distribution;
  }
  public PeptideTerminalAAResult calculateForRunSearchAnalysis(
      int analysisId, int runSearchAnalysisId) {

    if (lookAtUniqPeptides) {
      if (this.peptides == null) {
        peptides = new HashSet<String>();
      }
    }

    PercolatorResultDAO percDao = DAOFactory.instance().getPercolatorResultDAO();

    PercolatorResultFilterCriteria filterCriteria = new PercolatorResultFilterCriteria();
    filterCriteria.setMaxQValue(scoreCutoff);

    long s = System.currentTimeMillis();

    PeptideTerminalAAResultBuilder builder = new PeptideTerminalAAResultBuilder(analysisId, enzyme);
    builder.setScoreCutoff(this.scoreCutoff);
    builder.setScoreType("PERC_PSM_QVAL");

    int totalResults = 0;

    List<Integer> percResultIds =
        percDao.loadIdsForRunSearchAnalysis(runSearchAnalysisId, filterCriteria, null);

    log.info(
        "Found "
            + percResultIds.size()
            + " Percolator results at qvalue <= "
            + scoreCutoff
            + " for runSearchAnalysisID "
            + runSearchAnalysisId);

    for (Integer percResultId : percResultIds) {

      PercolatorResult pres = percDao.loadForPercolatorResultId(percResultId);
      MsSearchResultPeptide peptide = pres.getResultPeptide();
      String seq = peptide.getPeptideSequence();

      if (this.lookAtUniqPeptides) {
        String fullseq = peptide.getPreResidue() + "." + seq + "." + peptide.getPostResidue();
        if (peptides.contains(fullseq)) continue;
        else peptides.add(fullseq);
      }

      totalResults++;

      char ntermMinusOne = peptide.getPreResidue(); // nterm - 1 residue
      builder.addNtermMinusOneCount(ntermMinusOne);

      char nterm = seq.charAt(0); // nterm residue
      builder.addNtermCount(nterm);

      char cterm = seq.charAt(seq.length() - 1); // cterm residue
      builder.addCtermCount(cterm);

      char ctermPlusOne = peptide.getPostResidue(); // cterm + 1 residue
      builder.addCtermPlusOneCount(ctermPlusOne);

      int numEnzTerm = 0;
      if (this.rule != null)
        numEnzTerm = rule.getNumEnzymaticTermini(seq, ntermMinusOne, ctermPlusOne);
      builder.addEnzymaticTerminiCount(numEnzTerm);
    }

    long e = System.currentTimeMillis();

    log.info(
        "Time to get results for runSearchAnalysisId "
            + runSearchAnalysisId
            + " "
            + TimeUtils.timeElapsedSeconds(s, e)
            + " seconds");
    log.info("# filtered results: " + totalResults);

    builder.setTotalResultCount(totalResults);

    return builder.getResult();
  }
  private void printSequestSQTData(
      SQTRunSearch runSearch, int searchDatabaseId, BufferedWriter outFile) throws IOException {

    List<MsResidueModification> dynaResidueModsDb =
        getDynaResidueModsForSearch(runSearch.getSearchId());

    SQTSearchScanDAO scanDao = DAOFactory.instance().getSqtSpectrumDAO();

    SequestSearchResultDAO resultDao = DAOFactory.instance().getSequestResultDAO();
    List<Integer> resultIds = resultDao.loadResultIdsForRunSearch(runSearch.getId());
    int currCharge = -1;
    int currScanId = -1;
    SearchScan currScan = null;
    for (Integer resultId : resultIds) {
      SequestSearchResult result = resultDao.load(resultId);
      if (result.getScanId() != currScanId || result.getCharge() != currCharge) {
        if (currScan != null) {
          outFile.write(currScan.toString());
          outFile.write("\n");
        }
        currScanId = result.getScanId();
        currCharge = result.getCharge();
        SQTSearchScan scanDb =
            scanDao.load(runSearch.getId(), currScanId, currCharge, result.getObservedMass());
        currScan = makeScanResult(scanDb);
      }
      List<MsResidueModificationIn> dynaResidueMods = new ArrayList<MsResidueModificationIn>();
      for (MsResidueModification modDb : dynaResidueModsDb) {
        dynaResidueMods.add(modDb);
      }
      SequestResult peptResult = new SequestResult();
      peptResult.setResultPeptide(result.getResultPeptide());
      SequestResultData data = result.getSequestResultData();
      peptResult.setCharge(result.getCharge());
      peptResult.setObservedMass(result.getObservedMass());
      peptResult.setDeltaCN(data.getDeltaCN());
      peptResult.setCalculatedMass(data.getCalculatedMass());
      peptResult.setMatchingIons(data.getMatchingIons());
      peptResult.setPredictedIons(data.getPredictedIons());
      peptResult.setOriginalPeptideSequence(
          reconstructSequestPeptideSequence(runSearch.getSearchId(), result));
      peptResult.setScanNumber(currScan.getScanNumber());
      peptResult.setSp(data.getSp());
      peptResult.setSpRank(data.getSpRank());
      peptResult.setValidationStatus(result.getValidationStatus().getStatusChar());
      peptResult.setxCorr(data.getxCorr());
      peptResult.setxCorrRank(data.getxCorrRank());
      peptResult.setEvalue(data.getEvalue());

      List<MsSearchResultProtein> proteins = getProteinsForResultId(resultId);
      for (MsSearchResultProtein pr : proteins) {
        peptResult.addMatchingLocus(pr.getAccession(), null);
      }
      // currScan.addPeptideResult(peptResult);
    }
    // print the last one
    if (currScan != null) {
      outFile.write(currScan.toString());
      outFile.write("\n");
    }
  }
 private MsScan getScanForId(int scanId) {
   MsScanDAO scanDao = DAOFactory.instance().getMsScanDAO();
   return scanDao.load(scanId);
 }
 private List<MsTerminalModification> getDynaTermModsForSearch(int dbSearchId) {
   MsSearchModificationDAO modDao = DAOFactory.instance().getMsSearchModDAO();
   List<MsTerminalModification> dynaMods = modDao.loadDynamicTerminalModsForSearch(dbSearchId);
   return dynaMods;
 }
 private List<MsSearchResultProtein> getProteinsForResultId(Integer resultId) {
   MsSearchResultProteinDAO proteinDao = DAOFactory.instance().getMsProteinMatchDAO();
   return proteinDao.loadResultProteins(resultId);
 }