<commit_before>/*
 * Copyright 2006-2020 The MZmine Development Team
 * 
 * This file is part of MZmine.
 * 
 * MZmine is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * MZmine is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with MZmine; if not,
 * write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
 * USA
 */
package io.github.mzmine.modules.dataprocessing.id_formulapredictionpeaklist;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.openscience.cdk.formula.MolecularFormulaGenerator;
import org.openscience.cdk.formula.MolecularFormulaRange;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.interfaces.IMolecularFormula;
import org.openscience.cdk.silent.SilentChemObjectBuilder;
import org.openscience.cdk.tools.manipulator.MolecularFormulaManipulator;
import com.google.common.collect.Range;
import io.github.mzmine.datamodel.Feature;
import io.github.mzmine.datamodel.IonizationType;
import io.github.mzmine.datamodel.IsotopePattern;
import io.github.mzmine.datamodel.MassList;
import io.github.mzmine.datamodel.PeakIdentity;
import io.github.mzmine.datamodel.PeakList;
import io.github.mzmine.datamodel.PeakListRow;
import io.github.mzmine.datamodel.RawDataFile;
import io.github.mzmine.datamodel.Scan;
import io.github.mzmine.datamodel.identities.MolecularFormulaIdentity;
import io.github.mzmine.main.MZmineCore;
import io.github.mzmine.modules.dataprocessing.id_formula_sort.FormulaSortParameters;
import io.github.mzmine.modules.dataprocessing.id_formulaprediction.restrictions.elements.ElementalHeuristicChecker;
import io.github.mzmine.modules.dataprocessing.id_formulaprediction.restrictions.rdbe.RDBERestrictionChecker;
import io.github.mzmine.modules.tools.isotopepatternscore.IsotopePatternScoreCalculator;
import io.github.mzmine.modules.tools.isotopepatternscore.IsotopePatternScoreParameters;
import io.github.mzmine.modules.tools.isotopeprediction.IsotopePatternCalculator;
import io.github.mzmine.modules.tools.msmsscore.MSMSScore;
import io.github.mzmine.modules.tools.msmsscore.MSMSScoreCalculator;
import io.github.mzmine.modules.tools.msmsscore.MSMSScoreParameters;
import io.github.mzmine.parameters.ParameterSet;
import io.github.mzmine.parameters.parametertypes.tolerances.MZTolerance;
import io.github.mzmine.taskcontrol.AbstractTask;
import io.github.mzmine.taskcontrol.TaskStatus;
import io.github.mzmine.util.FormulaUtils;

public class FormulaPredictionPeakListTask extends AbstractTask {

  private Logger logger = Logger.getLogger(this.getClass().getName());
  private Range<Double> massRange;
  private MolecularFormulaRange elementCounts;
  private MolecularFormulaGenerator generator;
  private IonizationType ionType;
  private double searchedMass;
  private int charge;
  private PeakList peakList;
  private boolean checkIsotopes, checkMSMS, checkRatios, checkRDBE;
  private ParameterSet isotopeParameters, msmsParameters, ratiosParameters, rdbeParameters;
  private MZTolerance mzTolerance;
  private String message;
  private int totalRows, finishedRows;
  private int maxBestFormulasPerPeak;
  private final double minScore;
  private final double minMSMSScore;
  private Boolean isSorting;
  private Double sortPPMFactor = 20d;
  private Double sortIsotopeFactor = 0d;
  private Double sortMSMSFactor = 0d;

  /**
   *
   * @param parameters
   * @param peakList
   * @param peakListRow
   * @param peak
   */
  FormulaPredictionPeakListTask(PeakList peakList, ParameterSet parameters) {

    /*
     * searchedMass = parameters.getParameter(
     * FormulaPredictionPeakListParameters.neutralMass).getValue();
     */
    this.peakList = peakList;
    charge = parameters.getParameter(FormulaPredictionPeakListParameters.charge).getValue();
    ionType = parameters.getParameter(FormulaPredictionPeakListParameters.ionization).getValue();
    mzTolerance =
        parameters.getParameter(FormulaPredictionPeakListParameters.mzTolerance).getValue();
    elementCounts =
        parameters.getParameter(FormulaPredictionPeakListParameters.elements).getValue();

    checkIsotopes =
        parameters.getParameter(FormulaPredictionPeakListParameters.isotopeFilter).getValue();
    isotopeParameters = parameters.getParameter(FormulaPredictionPeakListParameters.isotopeFilter)
        .getEmbeddedParameters();

    checkMSMS = parameters.getParameter(FormulaPredictionPeakListParameters.msmsFilter).getValue();
    msmsParameters = parameters.getParameter(FormulaPredictionPeakListParameters.msmsFilter)
        .getEmbeddedParameters();
    if (checkMSMS) {
      // Only get the value if the MSMS checking is activated, otherwise we might get a NPE
      minMSMSScore = msmsParameters.getParameter(MSMSScoreParameters.msmsMinScore).getValue();
    } else {
      minMSMSScore = 0d;
    }

    checkRDBE =
        parameters.getParameter(FormulaPredictionPeakListParameters.rdbeRestrictions).getValue();
    rdbeParameters = parameters.getParameter(FormulaPredictionPeakListParameters.rdbeRestrictions)
        .getEmbeddedParameters();

    checkRatios =
        parameters.getParameter(FormulaPredictionPeakListParameters.elementalRatios).getValue();
    ratiosParameters = parameters.getParameter(FormulaPredictionPeakListParameters.elementalRatios)
        .getEmbeddedParameters();

    maxBestFormulasPerPeak = parameters
        .getParameter(FormulaPredictionPeakListParameters.maxBestFormulasPerPeak).getValue();


    minScore = isotopeParameters
        .getParameter(IsotopePatternScoreParameters.isotopePatternScoreThreshold).getValue();



    // get sorting parameters
    isSorting = parameters.getParameter(FormulaPredictionPeakListParameters.sorting).getValue();
    if (isSorting) {
      FormulaSortParameters sortParam = parameters
          .getParameter(FormulaPredictionPeakListParameters.sorting).getEmbeddedParameters();
      sortPPMFactor = sortParam.getParameter(FormulaSortParameters.MAX_PPM_WEIGHT).getValue();
      sortMSMSFactor = sortParam.getParameter(FormulaSortParameters.MSMS_SCORE_WEIGHT).getValue();
      sortIsotopeFactor =
          sortParam.getParameter(FormulaSortParameters.ISOTOPE_SCORE_WEIGHT).getValue();
    }
    message = "Formula Prediction";
  }

  /**
   * @see io.github.mzmine.taskcontrol.Task#getFinishedPercentage()
   */
  @Override
  public double getFinishedPercentage() {
    if (totalRows == 0)
      return 0.0;
    return (double) finishedRows / (double) totalRows;
  }

  /**
   * @see io.github.mzmine.taskcontrol.Task#getTaskDescription()
   */
  @Override
  public String getTaskDescription() {<commit_msg>Fixed another NPE in formula prediction<commit_after>/*
 * Copyright 2006-2020 The MZmine Development Team
 * 
 * This file is part of MZmine.
 * 
 * MZmine is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * MZmine is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with MZmine; if not,
 * write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
 * USA
 */
package io.github.mzmine.modules.dataprocessing.id_formulapredictionpeaklist;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.openscience.cdk.formula.MolecularFormulaGenerator;
import org.openscience.cdk.formula.MolecularFormulaRange;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.interfaces.IMolecularFormula;
import org.openscience.cdk.silent.SilentChemObjectBuilder;
import org.openscience.cdk.tools.manipulator.MolecularFormulaManipulator;
import com.google.common.collect.Range;
import io.github.mzmine.datamodel.Feature;
import io.github.mzmine.datamodel.IonizationType;
import io.github.mzmine.datamodel.IsotopePattern;
import io.github.mzmine.datamodel.MassList;
import io.github.mzmine.datamodel.PeakIdentity;
import io.github.mzmine.datamodel.PeakList;
import io.github.mzmine.datamodel.PeakListRow;
import io.github.mzmine.datamodel.RawDataFile;
import io.github.mzmine.datamodel.Scan;
import io.github.mzmine.datamodel.identities.MolecularFormulaIdentity;
import io.github.mzmine.main.MZmineCore;
import io.github.mzmine.modules.dataprocessing.id_formula_sort.FormulaSortParameters;
import io.github.mzmine.modules.dataprocessing.id_formulaprediction.restrictions.elements.ElementalHeuristicChecker;
import io.github.mzmine.modules.dataprocessing.id_formulaprediction.restrictions.rdbe.RDBERestrictionChecker;
import io.github.mzmine.modules.tools.isotopepatternscore.IsotopePatternScoreCalculator;
import io.github.mzmine.modules.tools.isotopepatternscore.IsotopePatternScoreParameters;
import io.github.mzmine.modules.tools.isotopeprediction.IsotopePatternCalculator;
import io.github.mzmine.modules.tools.msmsscore.MSMSScore;
import io.github.mzmine.modules.tools.msmsscore.MSMSScoreCalculator;
import io.github.mzmine.modules.tools.msmsscore.MSMSScoreParameters;
import io.github.mzmine.parameters.ParameterSet;
import io.github.mzmine.parameters.parametertypes.tolerances.MZTolerance;
import io.github.mzmine.taskcontrol.AbstractTask;
import io.github.mzmine.taskcontrol.TaskStatus;
import io.github.mzmine.util.FormulaUtils;

public class FormulaPredictionPeakListTask extends AbstractTask {

  private Logger logger = Logger.getLogger(this.getClass().getName());
  private Range<Double> massRange;
  private MolecularFormulaRange elementCounts;
  private MolecularFormulaGenerator generator;
  private IonizationType ionType;
  private double searchedMass;
  private int charge;
  private PeakList peakList;
  private boolean checkIsotopes, checkMSMS, checkRatios, checkRDBE;
  private ParameterSet isotopeParameters, msmsParameters, ratiosParameters, rdbeParameters;
  private MZTolerance mzTolerance;
  private String message;
  private int totalRows, finishedRows;
  private int maxBestFormulasPerPeak;
  private final double minScore;
  private final double minMSMSScore;
  private Boolean isSorting;
  private Double sortPPMFactor = 20d;
  private Double sortIsotopeFactor = 0d;
  private Double sortMSMSFactor = 0d;

  /**
   *
   * @param parameters
   * @param peakList
   * @param peakListRow
   * @param peak
   */
  FormulaPredictionPeakListTask(PeakList peakList, ParameterSet parameters) {

    /*
     * searchedMass = parameters.getParameter(
     * FormulaPredictionPeakListParameters.neutralMass).getValue();
     */
    this.peakList = peakList;
    charge = parameters.getParameter(FormulaPredictionPeakListParameters.charge).getValue();
    ionType = parameters.getParameter(FormulaPredictionPeakListParameters.ionization).getValue();
    mzTolerance =
        parameters.getParameter(FormulaPredictionPeakListParameters.mzTolerance).getValue();
    elementCounts =
        parameters.getParameter(FormulaPredictionPeakListParameters.elements).getValue();

    checkIsotopes =
        parameters.getParameter(FormulaPredictionPeakListParameters.isotopeFilter).getValue();
    isotopeParameters = parameters.getParameter(FormulaPredictionPeakListParameters.isotopeFilter)
        .getEmbeddedParameters();
    if (checkIsotopes) {
      // Only get the value if the isotope checking is activated, otherwise we might get a NPE
      minScore = isotopeParameters
        .getParameter(IsotopePatternScoreParameters.isotopePatternScoreThreshold).getValue();
    } else {
      minScore = 0d;
    }
    
    checkMSMS = parameters.getParameter(FormulaPredictionPeakListParameters.msmsFilter).getValue();
    msmsParameters = parameters.getParameter(FormulaPredictionPeakListParameters.msmsFilter)
        .getEmbeddedParameters();
    if (checkMSMS) {
      // Only get the value if the MSMS checking is activated, otherwise we might get a NPE
      minMSMSScore = msmsParameters.getParameter(MSMSScoreParameters.msmsMinScore).getValue();
    } else {
      minMSMSScore = 0d;
    }

    checkRDBE =
        parameters.getParameter(FormulaPredictionPeakListParameters.rdbeRestrictions).getValue();
    rdbeParameters = parameters.getParameter(FormulaPredictionPeakListParameters.rdbeRestrictions)
        .getEmbeddedParameters();

    checkRatios =
        parameters.getParameter(FormulaPredictionPeakListParameters.elementalRatios).getValue();
    ratiosParameters = parameters.getParameter(FormulaPredictionPeakListParameters.elementalRatios)
        .getEmbeddedParameters();

    maxBestFormulasPerPeak = parameters
        .getParameter(FormulaPredictionPeakListParameters.maxBestFormulasPerPeak).getValue();

    // get sorting parameters
    isSorting = parameters.getParameter(FormulaPredictionPeakListParameters.sorting).getValue();
    if (isSorting) {
      FormulaSortParameters sortParam = parameters
          .getParameter(FormulaPredictionPeakListParameters.sorting).getEmbeddedParameters();
      sortPPMFactor = sortParam.getParameter(FormulaSortParameters.MAX_PPM_WEIGHT).getValue();
      sortMSMSFactor = sortParam.getParameter(FormulaSortParameters.MSMS_SCORE_WEIGHT).getValue();
      sortIsotopeFactor =
          sortParam.getParameter(FormulaSortParameters.ISOTOPE_SCORE_WEIGHT).getValue();
    }
    message = "Formula Prediction";
  }

  /**
   * @see io.github.mzmine.taskcontrol.Task#getFinishedPercentage()
   */
  @Override
  public double getFinishedPercentage() {
    if (totalRows == 0)
      return 0.0;
    return (double) finishedRows / (double) totalRows;
  }

  /**
   * @see io.github.mzmine.taskcontrol.Task#getTaskDescription()
   */
  @Override
  public String getTaskDescription() {
{'fraction_non_alphanumeric': 0.08080878265703169, 'fraction_numerical': 0.00396053362979433, 'mean_word_length': 5.190537634408602, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1925319', 'n_tokens_mistral': 4360, 'n_tokens_neox': 4159, 'n_words': 804}