Skip to content

Commit

Permalink
Reverted everythin to a static method
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Sol committed Sep 12, 2024
1 parent ae3b811 commit 7c45323
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 34 deletions.
3 changes: 2 additions & 1 deletion MetaMorpheus/EngineLayer/FdrAnalysis/FdrAnalysisEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ public void Compute_PEPValue(FdrAnalysisResults myAnalysisResults, List<Spectral
{
searchType = "crosslink";
}
myAnalysisResults.BinarySearchTreeMetrics = new PepAnalysisEngine(psms, searchType, FileSpecificParameters, OutputFolder).ComputePEPValuesForAllPSMs();
PepAnalysisEngine.SetVariables(psms, searchType, FileSpecificParameters, OutputFolder);
myAnalysisResults.BinarySearchTreeMetrics = PepAnalysisEngine.ComputePEPValuesForAllPSMs();

}

Expand Down
82 changes: 49 additions & 33 deletions MetaMorpheus/EngineLayer/FdrAnalysis/PEPAnalysisEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@

namespace EngineLayer
{
public class PepAnalysisEngine
public static class PepAnalysisEngine
{
private int _randomSeed = 42;
private static int _randomSeed = 42;

/// <summary>
/// This method contains the hyper-parameters that will be used when training the machine learning model
/// </summary>
/// <returns> Options object to be passed in to the FastTree constructor </returns>
public Microsoft.ML.Trainers.FastTree.FastTreeBinaryTrainer.Options BGDTreeOptions =>
public static Microsoft.ML.Trainers.FastTree.FastTreeBinaryTrainer.Options BGDTreeOptions =>
new Microsoft.ML.Trainers.FastTree.FastTreeBinaryTrainer.Options
{
NumberOfThreads = 1,
Expand All @@ -54,24 +54,24 @@ public class PepAnalysisEngine
//The value of the dictionary is another dictionary that profiles the hydrophobicity behavior.
//Each key is a retention time rounded to the nearest minute.
//The value Tuple is the average and standard deviation, respectively, of the predicted hydrophobicities of the observed peptides eluting at that rounded retention time.
public Dictionary<string, Dictionary<int, Tuple<double, double>>> FileSpecificTimeDependantHydrophobicityAverageAndDeviation_unmodified { get; private set; }
public Dictionary<string, Dictionary<int, Tuple<double, double>>> FileSpecificTimeDependantHydrophobicityAverageAndDeviation_modified { get; private set; }
public Dictionary<string, Dictionary<int, Tuple<double, double>>> FileSpecificTimeDependantHydrophobicityAverageAndDeviation_CZE { get; private set; }
public static Dictionary<string, Dictionary<int, Tuple<double, double>>> FileSpecificTimeDependantHydrophobicityAverageAndDeviation_unmodified { get; private set; }
public static Dictionary<string, Dictionary<int, Tuple<double, double>>> FileSpecificTimeDependantHydrophobicityAverageAndDeviation_modified { get; private set; }
public static Dictionary<string, Dictionary<int, Tuple<double, double>>> FileSpecificTimeDependantHydrophobicityAverageAndDeviation_CZE { get; private set; }

/// <summary>
/// A dictionary which stores the chimeric ID string in the key and the number of chimeric identifications as the vale
/// </summary>
private Dictionary<string, int> chimeraCountDictionary = new Dictionary<string, int>();
public Dictionary<string, float> FileSpecificMedianFragmentMassErrors { get; private set; }
public Dictionary<string, CommonParameters> FileSpecificParametersDictionary { get; private set; }
public int ChargeStateMode { get; private set; }

public double QValueCutoff { get; }
public bool UsePeptideLevelQValueForTraining = true;
public string[] TrainingVariables { get; }
public string OutputFolder { get; }
public List<SpectralMatch> AllPsms { get; }
public string SearchType { get; }
private static Dictionary<string, int> chimeraCountDictionary = new Dictionary<string, int>();
public static Dictionary<string, float> FileSpecificMedianFragmentMassErrors { get; private set; }
public static Dictionary<string, CommonParameters> FileSpecificParametersDictionary { get; private set; }
public static int ChargeStateMode { get; private set; }

public static double QValueCutoff { get; private set; }
public static bool UsePeptideLevelQValueForTraining = true;
public static string[] TrainingVariables { get; private set; }
public static string OutputFolder { get; private set; }
public static List<SpectralMatch> AllPsms { get; private set; }
public static string SearchType { get; private set; }

/// <summary>
/// This method is used to compute the PEP values for all PSMs in a dataset.
Expand All @@ -81,12 +81,28 @@ public class PepAnalysisEngine
/// <param name="fileSpecificParameters"></param>
/// <param name="outputFolder"></param>
/// <returns></returns>
public void SetFileSpecificParameters(List<(string fileName, CommonParameters fileSpecificParameters)> fileSpecificParameters)
public static void SetFileSpecificParameters(List<(string fileName, CommonParameters fileSpecificParameters)> fileSpecificParameters)
{
FileSpecificParametersDictionary = fileSpecificParameters.ToDictionary(p => Path.GetFileName(p.fileName), p => p.fileSpecificParameters);
}

public PepAnalysisEngine(List<SpectralMatch> psms, string searchType, List<(string fileName, CommonParameters fileSpecificParameters)> fileSpecificParameters, string outputFolder)
//public PepAnalysisEngine(List<SpectralMatch> psms, string searchType, List<(string fileName, CommonParameters fileSpecificParameters)> fileSpecificParameters, string outputFolder)
//{
// // This creates a new list of PSMs, but does not clone the Psms themselves.
// // This allows the PSMs to be modified and the order to be preserved
// AllPsms = psms.OrderByDescending(p => p).ToList();
// TrainingVariables = PsmData.trainingInfos[searchType];
// OutputFolder = outputFolder;
// SearchType = searchType;
// SetFileSpecificParameters(fileSpecificParameters);
// BuildFileSpecificDictionaries(psms, TrainingVariables);
// QValueCutoff = Math.Max(fileSpecificParameters.Select(t => t.fileSpecificParameters.QValueCutoffForPepCalculation).Min(), 0.005);

// // If we have more than 100 peptides, we will train on the peptide level. Otherwise, we will train on the PSM level
// UsePeptideLevelQValueForTraining = psms.Select(psm => psm.FullSequence).Distinct().Count(seq => seq.IsNotNullOrEmpty()) >= 100;
//}

public static void SetVariables(List<SpectralMatch> psms, string searchType, List<(string fileName, CommonParameters fileSpecificParameters)> fileSpecificParameters, string outputFolder)
{
// This creates a new list of PSMs, but does not clone the Psms themselves.
// This allows the PSMs to be modified and the order to be preserved
Expand All @@ -102,7 +118,7 @@ public PepAnalysisEngine(List<SpectralMatch> psms, string searchType, List<(stri
UsePeptideLevelQValueForTraining = psms.Select(psm => psm.FullSequence).Distinct().Count(seq => seq.IsNotNullOrEmpty()) >= 100;
}

public string ComputePEPValuesForAllPSMs()
public static string ComputePEPValuesForAllPSMs()
{
//List<PeptideMatchGroup> peptideGroups = UsePeptideLevelQValueForTraining
// ? PeptideMatchGroup.GroupByBaseSequence(AllPsms)
Expand Down Expand Up @@ -171,7 +187,7 @@ public string ComputePEPValuesForAllPSMs()
/// </summary>
/// <param name="trainingData"> The PSMs that will be used for training </param>
/// <param name="trainingVariables"> An array of training variables from PsmData.trainingInfos dictionary </param>
public void BuildFileSpecificDictionaries(List<SpectralMatch> trainingData, string[] trainingVariables)
public static void BuildFileSpecificDictionaries(List<SpectralMatch> trainingData, string[] trainingVariables)
{
FileSpecificMedianFragmentMassErrors = GetFileSpecificMedianFragmentMassError(trainingData);
ChargeStateMode = GetChargeStateMode(trainingData);
Expand Down Expand Up @@ -242,7 +258,7 @@ static List<List<int>> DivideListIntoGroups(List<int> list, int numGroups)
return groups;
}

public IEnumerable<PsmData> CreatePsmData(string searchType,
public static IEnumerable<PsmData> CreatePsmData(string searchType,
List<PeptideMatchGroup> peptideGroups, List<int> peptideGroupIndices)
{
object psmDataListLock = new object();
Expand Down Expand Up @@ -387,7 +403,7 @@ public static string AggregateMetricsForOutput(List<CalibratedBinaryClassificati
return s.ToString();
}

public int Compute_PSM_PEP(List<PeptideMatchGroup> peptideGroups,
public static int Compute_PSM_PEP(List<PeptideMatchGroup> peptideGroups,
List<int> peptideGroupIndices,
MLContext mLContext, TransformerChain<BinaryPredictionTransformer<Microsoft.ML.Calibrators.CalibratedModelParametersBase<Microsoft.ML.Trainers.FastTree.FastTreeBinaryModelParameters, Microsoft.ML.Calibrators.PlattCalibrator>>> trainedModel, string searchType, string outputFolder)
{
Expand Down Expand Up @@ -469,7 +485,7 @@ public int Compute_PSM_PEP(List<PeptideMatchGroup> peptideGroups,
return ambiguousPeptidesResolved;
}

public PsmData CreateOnePsmDataEntry(string searchType, SpectralMatch psm, IBioPolymerWithSetMods selectedPeptide, int notchToUse, bool label)
public static PsmData CreateOnePsmDataEntry(string searchType, SpectralMatch psm, IBioPolymerWithSetMods selectedPeptide, int notchToUse, bool label)
{
double normalizationFactor = selectedPeptide.BaseSequence.Length;
float totalMatchingFragmentCount = 0;
Expand Down Expand Up @@ -530,11 +546,11 @@ public PsmData CreateOnePsmDataEntry(string searchType, SpectralMatch psm, IBioP
complementaryIonCount = (float)Math.Round(SpectralMatch.GetCountComplementaryIons(psm.BioPolymersWithSetModsToMatchingFragments, selectedPeptide) / normalizationFactor * multiplier, 0);
isVariantPeptide = PeptideIsVariant(selectedPeptide);
spectralAngle = (float)psm.SpectralAngle;
if (chimeraCountDictionary.TryGetValue(psm.ChimeraIdString, out int val))
chimeraCount = val;
peaksInPrecursorEnvelope = psm.PrecursorScanEnvelopePeakCount;
mostAbundantPrecursorPeakIntensity = (float)Math.Round((float)psm.PrecursorScanIntensity / normalizationFactor * multiplier, 0);
fractionalIntensity = (float)psm.PrecursorFractionalIntensity;
//if (chimeraCountDictionary.TryGetValue(psm.ChimeraIdString, out int val))
// chimeraCount = val;
//peaksInPrecursorEnvelope = psm.PrecursorScanEnvelopePeakCount;
//mostAbundantPrecursorPeakIntensity = (float)Math.Round((float)psm.PrecursorScanIntensity / normalizationFactor * multiplier, 0);
//fractionalIntensity = (float)psm.PrecursorFractionalIntensity;

if (PsmHasSpectralAngle(psm))
{
Expand Down Expand Up @@ -692,12 +708,12 @@ public static void GetIndiciesOfPeptidesToRemove(List<int> indiciesOfPeptidesToR
/// <summary>
/// Here we're getting the most common charge state for precursors that are Targets with q<=0.01.

public int GetChargeStateMode(List<SpectralMatch> psms)
public static int GetChargeStateMode(List<SpectralMatch> psms)
{
return psms.Where(p => p.IsDecoy != true && p.GetFdrInfo(UsePeptideLevelQValueForTraining).QValue <= 0.01).Select(p => p.ScanPrecursorCharge).GroupBy(n => n).OrderByDescending(g => g.Count()).Select(g => g.Key).FirstOrDefault();
}

public Dictionary<string, Dictionary<int, Tuple<double, double>>> ComputeHydrophobicityValues(List<SpectralMatch> psms, bool computeHydrophobicitiesforModifiedPeptides)
public static Dictionary<string, Dictionary<int, Tuple<double, double>>> ComputeHydrophobicityValues(List<SpectralMatch> psms, bool computeHydrophobicitiesforModifiedPeptides)
{
SSRCalc3 calc = new SSRCalc3("SSRCalc 3.0 (300A)", SSRCalc3.Column.A300);

Expand Down Expand Up @@ -802,7 +818,7 @@ public Dictionary<string, Dictionary<int, Tuple<double, double>>> ComputeHydroph
return rtHydrophobicityAvgDev;
}

public Dictionary<string, Dictionary<int, Tuple<double, double>>> ComputeMobilityValues(List<SpectralMatch> psms)
public static Dictionary<string, Dictionary<int, Tuple<double, double>>> ComputeMobilityValues(List<SpectralMatch> psms)
{
Dictionary<string, Dictionary<int, Tuple<double, double>>> rtMobilityAvgDev = new Dictionary<string, Dictionary<int, Tuple<double, double>>>();

Expand Down Expand Up @@ -939,7 +955,7 @@ private static float GetSSRCalcHydrophobicityZScore(SpectralMatch psm, IBioPolym
return (float)hydrophobicityZscore;
}

private float GetMobilityZScore(SpectralMatch psm, IBioPolymerWithSetMods selectedPeptide)
private static float GetMobilityZScore(SpectralMatch psm, IBioPolymerWithSetMods selectedPeptide)
{
double mobilityZScore = double.NaN;

Expand Down

0 comments on commit 7c45323

Please sign in to comment.