private Dictionary <object, BestSoFarWithTies <double, Prediction> > ForEachKeyFindTheBestResults( ShowBy showBy, string inputPeptide, MerLength merLength, int?dOfCenter, HlaSetSpecification hlaSetSpecification, string hlaOrSupertypeOrNull, bool modelOnly) { Dictionary <object, BestSoFarWithTies <double, Prediction> > keyToBest = new Dictionary <object, BestSoFarWithTies <double, Prediction> >(); foreach (Prediction prediction in PredictionEnumeration(inputPeptide, merLength, dOfCenter, hlaSetSpecification, hlaOrSupertypeOrNull, modelOnly)) { object key = prediction.GroupByKey(showBy); //!!!would be nice if this were typed, rather than just using 'string' BestSoFarWithTies <double, Prediction> bestSoFarWithTies = SpecialFunctions.GetValueOrDefault(keyToBest, key, BestSoFarWithTies <double, Prediction> .GetInstance(SpecialFunctions.DoubleGreaterThan)); bestSoFarWithTies.Compare(prediction.WeightOfEvidence, prediction); } return(keyToBest); }
private Set <Hla> HlaSet(HlaSetSpecification hlaSetSpecification, string hlaOrSupertypeOrNull) { Set <Hla> hlaSet = hlaSetSpecification.HlaSet(hlaOrSupertypeOrNull, _hlaSet, _supertypeMap); return(hlaSet); }
////!!! this could be moved into a class //private object CreateKey(Prediction prediction, Best display) //{ // switch (display) // { // case Best.overall: // return "best"; // case Best.perHla: // return prediction.Hla; // case Best.perPrediction: // return prediction; // case Best.perLength: // return prediction.K; // case Best.perHlaAndLength: // return new Pair<Hla, int>(prediction.Hla, prediction.K); // default: // SpecialFunctions.CheckCondition(false, "Don't know how to display " + display.ToString()); // return null; // } //} /// <summary> /// HlaSetSpecification class choices: /// HlaSetSpecification.Singleton – Means that an Hla will be given and it is the only hla to be considered /// HlaSetSpecification.Supertype – Means that a supertype will be given and it’s hlas should be considered /// HlaSetSpecification.All – Means to consider all known hlas /// </summary> /// <param name="inputPeptide">a string of amino acids</param> /// <param name="merLength">A value from the MerLength enum, which includes MerLength.scan, MerLength.given, MerLength.Eight, etc</param> /// <param name="hlaSetSpecification">A predefined HlaSetSpecification class.</param> /// <param name="hlaOrSupertypeOrNull">The hla or supertype required by HlaSetSpecification, or null for HlaSetSpecification.All</param> /// <param name="modelOnly">If should report the probability from the model, even when the epitope is on a source list.</param> /// <returns></returns> public IEnumerable <Prediction> PredictionEnumeration(string inputPeptide, MerLength merLength, int?dOfCenter, HlaSetSpecification hlaSetSpecification, string hlaOrSupertypeOrNull, bool modelOnly) { Set <Hla> hlaSet = HlaSet(hlaSetSpecification, hlaOrSupertypeOrNull); foreach (int eLength in KEnumeration(merLength, inputPeptide.Length)) { Predictor predictor = KToPredictor[eLength]; Dictionary <Hla, double> hlaToPriorLogOdds = KToHlaToPriorLogOdds[eLength]; int necLength = NCLength + eLength + NCLength; foreach (int startIndex in StartIndexEnumeration(inputPeptide.Length, necLength, dOfCenter)) { string peptide = inputPeptide.Substring(startIndex, necLength); NEC nec = NEC.GetInstance(peptide, NCLength, eLength, NCLength); foreach (Hla hla in hlaSet) { Hla hlaForNormalization = HlaForNormalization(hla); double priorLogOddsOfThisLengthAndHla; if (!hlaToPriorLogOdds.TryGetValue(hlaForNormalization, out priorLogOddsOfThisLengthAndHla)) { SpecialFunctions.CheckCondition(!RaiseErrorIfNotFoundInNormalizationTable, string.Format("Hla '{0}' (which is '{1}' for the purposes of normalization) and is not found in the normalization table", hla, hlaForNormalization)); priorLogOddsOfThisLengthAndHla = SpecialFunctions.LogOdds(RatioOfTrueToFalseTrainingExample); } string source; double originalP = predictor.Predict(nec, hla, modelOnly, out source); double originalLogOdds = SpecialFunctions.LogOdds(originalP); double correctedLogOdds = originalLogOdds + priorLogOddsOfThisLengthAndHla; double posteriorProbability = SpecialFunctions.InverseLogOdds(correctedLogOdds); double weightOfEvidence = correctedLogOdds - SpecialFunctions.LogOdds(RatioOfTrueToFalseTrainingExample); Prediction prediction = Prediction.GetInstance(inputPeptide, hla, posteriorProbability, weightOfEvidence, nec, startIndex + NCLength + 1, startIndex + NCLength + eLength, source); yield return(prediction); } } } }
/// <summary> /// Returns a list with one item per unique "GroupBy" key (e.g. each hla, each length, etc). Each item is a list of the predictions /// that tie for maximum probability. /// /// Most of the inputs are the same as for PredictionEnumeration. /// </summary> /// <param name="showBy"></param> /// <param name="inputPeptide"></param> /// <param name="merLength"></param> /// <param name="hlaSetSpecification"></param> /// <param name="hlaOrSupertypeOrNull"></param> /// <param name="modelOnly"></param> /// <returns></returns> public Dictionary <object, List <Prediction> > MaxProbabilityPredictions(ShowBy showBy, string inputPeptide, MerLength merLength, int?dOfCenter, HlaSetSpecification hlaSetSpecification, string hlaOrSupertypeOrNull, bool modelOnly) { Dictionary <object, BestSoFarWithTies <double, Prediction> > keyToBest = ForEachKeyFindTheBestResults(showBy, inputPeptide, merLength, dOfCenter, hlaSetSpecification, hlaOrSupertypeOrNull, modelOnly); Dictionary <object, List <Prediction> > maxProbabilityPredictionsPerKey = FindResultsPerKey(keyToBest); return(maxProbabilityPredictionsPerKey); }