override public IEnumerable <TrueCollection> Collection() { Set <Hla> assignmentAsSet = CreateAssignmentAsSet(QmmrModelOnePeptide.BestHlaAssignmentSoFar.Champ); foreach (Hla originalTrueHla in QmmrModelOnePeptide.BestHlaAssignmentSoFar.Champ) { if (QmmrModelOnePeptide.QmrrModelMissingAssignment.KnownHlaSet.Contains(originalTrueHla)) { continue; } assignmentAsSet.Remove(originalTrueHla); foreach (Hla originalFalseHla in QmmrModelOnePeptide.QmrrModelMissingAssignment.SwitchableHlasOfRespondingPatients) { if (!assignmentAsSet.Contains(originalFalseHla)) { assignmentAsSet.AddNew(originalFalseHla); Debug.Assert(assignmentAsSet.Count == QmmrModelOnePeptide.BestHlaAssignmentSoFar.Champ.Count); // real assert yield return(TrueCollection.GetInstance(assignmentAsSet)); assignmentAsSet.Remove(originalFalseHla); } } assignmentAsSet.AddNew(originalTrueHla); Debug.Assert(assignmentAsSet.Count == QmmrModelOnePeptide.BestHlaAssignmentSoFar.Champ.Count); // real assert } }
private void ReportPerHla(OptimizationParameterList qmrrParams, Dictionary <string, BestSoFar <double, TrueCollection> > peptideToBestHlaAssignmentSoFar, string directory, string name) { string fileName = string.Format(@"{0}\NoisyOr.PeptideHlaProbability.{1}.new.txt", directory, name); using (StreamWriter output = File.CreateText(fileName)) { output.WriteLine(SpecialFunctions.CreateTabString("Peptide", "HLA", "LogOdds", "Probability")); foreach (QmrrPartialModel qmrrPartialModel in QmrrPartialModelCollection) { QmrrModelMissingAssignment aQmrrModelMissingAssignment = QmrrModelMissingAssignment.GetInstance(ModelLikelihoodFactories, qmrrPartialModel, qmrrParams); BestSoFar <double, TrueCollection> bestHlaAssignment = peptideToBestHlaAssignmentSoFar[qmrrPartialModel.Peptide]; Set <Hla> trueCollectionFullAsSet = new Set <Hla>(bestHlaAssignment.Champ); double loglikelihoodFull = bestHlaAssignment.ChampsScore; foreach (Hla hla in trueCollectionFullAsSet) { bool known = qmrrPartialModel.KnownHlaSet.Contains(hla); if (!known) { Set <Hla> allLessOne = trueCollectionFullAsSet.SubtractElement(hla); TrueCollection trueCollectionWithout = TrueCollection.GetInstance(allLessOne); double loglikelihoodWithout = aQmrrModelMissingAssignment.LogLikelihoodOfCompleteModelConditionedOnKnownHlas(trueCollectionWithout); double logOdds = loglikelihoodFull - loglikelihoodWithout; double probabilityFull = Math.Exp(loglikelihoodFull); double probabilityWithout = Math.Exp(loglikelihoodWithout); double probability = probabilityFull / (probabilityFull + probabilityWithout); Debug.Assert(logOdds >= 0); // real assert output.WriteLine(SpecialFunctions.CreateTabString(qmrrPartialModel.Peptide, hla, logOdds, probability)); } } } } }
internal void CreateNoSwitchablesHlaAssignment() { BestHlaAssignmentSoFar = BestSoFar <double, TrueCollection> .GetInstance(SpecialFunctions.DoubleGreaterThan); TrueCollection trueCollection = TrueCollection.GetInstance(QmrrModelMissingAssignment.KnownHlaSet); double scoreOnNoSwitchables = QmrrModelMissingAssignment.LogLikelihoodOfCompleteModelConditionedOnKnownHlas(trueCollection); BestHlaAssignmentSoFar.Compare(scoreOnNoSwitchables, trueCollection); }
internal static Set <Hla> CreateAssignmentAsSet(TrueCollection startingAssignment) { Set <Hla> assignmentAsSet = Set <Hla> .GetInstance(); foreach (Hla hla in startingAssignment) { assignmentAsSet.AddNew(hla); } return(assignmentAsSet); }
//!!!similar code is used in NumberOfPositiveHlas private int FitTrueCount(Set <Hla> fitUniverse, TrueCollection trueCollection) { int fitTrueCount = 0; foreach (Hla hla in trueCollection) { if (fitUniverse.Contains(hla)) { ++fitTrueCount; } } return(fitTrueCount); }
internal static TrueCollection GetInstance(params IEnumerable <Hla>[] hlaCollectionCollection) { TrueCollection aTrueCollection = new TrueCollection(); foreach (IEnumerable <Hla> hlaCollection in hlaCollectionCollection) { foreach (Hla hla in hlaCollection) { aTrueCollection.Add(hla); } } return(aTrueCollection); }
override public IEnumerable <TrueCollection> Collection() { Set <Hla> assignmentAsSet = CreateAssignmentAsSet(QmmrModelOnePeptide.BestHlaAssignmentSoFar.Champ); SubtractKnownHlas(ref assignmentAsSet); EverySubsetBySize aEverySubsetBySize = EverySubsetBySize.GetInstance(QmmrModelOnePeptide.QmrrModelMissingAssignment.SwitchableHlasOfRespondingPatients.Count, Depth, 1); foreach (List <int> indexList in aEverySubsetBySize.Collection()) { FlipListedBits(assignmentAsSet, indexList); yield return(TrueCollection.GetInstance(QmmrModelOnePeptide.QmrrModelMissingAssignment.KnownHlaSet, assignmentAsSet)); FlipListedBits(assignmentAsSet, indexList); } }
private void ReportPerHlaAssignment(Dictionary <string, BestSoFar <double, TrueCollection> > peptideToBestHlaAssignmentSoFar, string directory, string name) { string fileName = string.Format(@"{0}\NoisyOr.HlasPerPeptide.{1}.new.txt", directory, name); using (StreamWriter output = File.CreateText(fileName)) { output.WriteLine(SpecialFunctions.CreateTabString("Peptide", "HLAAssignment", "LogLikelihood")); foreach (QmrrPartialModel qmrrPartialModel in QmrrPartialModelCollection) { BestSoFar <double, TrueCollection> bestHlaAssignment = peptideToBestHlaAssignmentSoFar[qmrrPartialModel.Peptide]; TrueCollection trueCollectionFull = bestHlaAssignment.Champ; double loglikelihoodFull = bestHlaAssignment.ChampsScore; output.WriteLine(SpecialFunctions.CreateTabString(qmrrPartialModel.Peptide, trueCollectionFull, loglikelihoodFull)); } } }
internal static HlaAssignmentWithResponses GetInstance(IEnumerable <Hla> trueCollection, Quickscore <Hla, int> quickscore, List <Hla> hlaListFromRepondingPatients, Dictionary <Hla, List <int> > hlaToRespondingPatients) { HlaAssignmentWithResponses aHlaAssignment = new HlaAssignmentWithResponses(); aHlaAssignment.TrueCollection = TrueCollection.GetInstance(trueCollection); aHlaAssignment.AsDictionary = new Dictionary <Hla, bool>(); aHlaAssignment.UnexplainedPatients = new List <int>(); Dictionary <int, bool> patientToIsExplained = new Dictionary <int, bool>(); aHlaAssignment.HlaToRespondingPatients = hlaToRespondingPatients; foreach (Hla hla in trueCollection) { aHlaAssignment.AsDictionary.Add(hla, true); foreach (int patient in hlaToRespondingPatients[hla]) { patientToIsExplained[patient] = true; } } foreach (Hla hla in quickscore.CauseList()) { if (!aHlaAssignment.AsDictionary.ContainsKey(hla)) { aHlaAssignment.AsDictionary.Add(hla, false); if (hlaToRespondingPatients.ContainsKey(hla)) { foreach (int patient in hlaToRespondingPatients[hla]) { if (!patientToIsExplained.ContainsKey(patient)) { patientToIsExplained.Add(patient, false); aHlaAssignment.UnexplainedPatients.Add(patient); } } } } } return(aHlaAssignment); }
override public IEnumerable <TrueCollection> Collection() { Set <Hla> assignmentAsSet = CreateAssignmentAsSet(QmmrModelOnePeptide.BestHlaAssignmentSoFar.Champ); foreach (Hla hla in QmmrModelOnePeptide.QmrrModelMissingAssignment.SwitchableHlasOfRespondingPatients) { if (assignmentAsSet.Contains(hla)) { assignmentAsSet.Remove(hla); yield return(TrueCollection.GetInstance(assignmentAsSet)); assignmentAsSet.AddNew(hla); } else { assignmentAsSet.AddNew(hla); yield return(TrueCollection.GetInstance(assignmentAsSet)); assignmentAsSet.Remove(hla); } } }
internal static TrueCollection GetInstance() { TrueCollection aTrueCollection = new TrueCollection(); return(aTrueCollection); }
virtual public MissingParametersDelegate MissingParametersDelegateFactory(QmrrPartialModel qmrrPartialModel, TrueCollection trueCollection) { PartialModelDelegate LogLikelihoodOfCompleteModelConditionedOnKnownHlas = PartialModelDelegateFactory(qmrrPartialModel); return(delegate(OptimizationParameterList parameterList) { return LogLikelihoodOfCompleteModelConditionedOnKnownHlas(trueCollection, parameterList); }); }
private double LogLikelihoodOfCompleteModelConditionedOnKnownHlas(QmrrPartialModel qmrrPartialModel, TrueCollection trueCollection, OptimizationParameterList qmrrParams) { Set <Hla> trueHlaSet = trueCollection.CreateHlaAssignmentAsSet(); int reactionsCoveredCount = CountReactionsCovered(qmrrPartialModel, trueHlaSet); SpecialFunctions.CheckCondition(reactionsCoveredCount < 1000); int trueCount = trueCollection.Count; SpecialFunctions.CheckCondition(trueCount < 1000); int falseCount = qmrrPartialModel.HlaList.Count - trueCollection.Count; SpecialFunctions.CheckCondition(falseCount < 1000); string llAsString = string.Format("{0:000}.{1:000}{2:000}", reactionsCoveredCount, falseCount, trueCount); double logLikelihood = double.Parse(llAsString); return(logLikelihood); }
protected override double LogLikelihoodOfCausesConditionedOnKnownHlas(QmrrPartialModel qmrrPartialModel, TrueCollection trueCollection, OptimizationParameterList qmrrParams) { //!!!This could be calculated during the construction of qmrrPartialModel Set <Hla> fitUniverse = SpecialFunctions.GetValueOrDefault(PeptideToFitUniverse, qmrrPartialModel.Peptide); //Compute with priors double unfitCausePrior = qmrrParams["causePrior"].Value; double fitFactor = qmrrParams["fitFactor"].Value; double fitCausePrior = unfitCausePrior * fitFactor; double logUnfitCausePrior = Math.Log(unfitCausePrior); double logOneLessUnfitCausePrior = Math.Log(1.0 - unfitCausePrior); double logFitCausePrior = Math.Log(fitCausePrior); double logOneLessFitCausePrior = Math.Log(1.0 - fitCausePrior); //Tabulate counts int unfitTotalCount = qmrrPartialModel.HlaList.Count - fitUniverse.Count; int trueCountLessKnown = trueCollection.Count - qmrrPartialModel.KnownHlaSet.Count; int falseCount = qmrrPartialModel.HlaList.Count - trueCollection.Count; int knownFitCount = KnownFitCount(fitUniverse, qmrrPartialModel.KnownHlaSet); //!!!could be pretabulated int knownUnfitCount = qmrrPartialModel.KnownHlaSet.Count - knownFitCount; int fitTrueCountLessKnown = FitTrueCount(fitUniverse, trueCollection) - knownFitCount; int unfitTrueCountLessKnown = trueCountLessKnown - fitTrueCountLessKnown; int fitFalseCount = fitUniverse.Count - fitTrueCountLessKnown - knownFitCount; int unfitFalseCount = unfitTotalCount - unfitTrueCountLessKnown - knownUnfitCount; //Compute logLikelihood double logLikelihood = (double)unfitTrueCountLessKnown * logUnfitCausePrior + (double)unfitFalseCount * logOneLessUnfitCausePrior + (double)fitTrueCountLessKnown * logFitCausePrior + (double)fitFalseCount * logOneLessFitCausePrior; return(logLikelihood); }
virtual protected double LogLikelihoodOfCompleteModelConditionedOnKnownHlas(QmrrPartialModel qmrrPartialModel, TrueCollection trueCollection, OptimizationParameterList qmrrParams) { double logLikelihood = LogLikelihoodOfCausesConditionedOnKnownHlas(qmrrPartialModel, trueCollection, qmrrParams) + LogLikelihoodOfEffects(qmrrPartialModel, trueCollection, qmrrParams); return(logLikelihood); }
virtual protected double LogLikelihoodOfCausesConditionedOnKnownHlas(QmrrPartialModel qmrrPartialModel, TrueCollection trueCollection, OptimizationParameterList qmrrParams) { double logCausePrior = Math.Log(qmrrParams["causePrior"].Value); double logOneLessCausePrior = Math.Log(1.0 - qmrrParams["causePrior"].Value); int trueCountLessKnown = trueCollection.Count - qmrrPartialModel.KnownHlaSet.Count; Debug.Assert(trueCountLessKnown >= 0); // real assert int falseCount = qmrrPartialModel.HlaList.Count - trueCollection.Count; double logLikelihood = (double)trueCountLessKnown * logCausePrior + (double)falseCount * logOneLessCausePrior; return(logLikelihood); }
virtual protected double LogLikelihoodOfEffects(QmrrPartialModel qmrrPartialModel, TrueCollection trueCollection, OptimizationParameterList qmrrParams) { double logOneLessLink = Math.Log(1.0 - qmrrParams["link"].Value); double logOneLessLeakProbability = Math.Log(1.0 - qmrrParams["leakProbability"].Value); double logLikelihood = 0.0; Set <Hla> trueCollectionAsSet = trueCollection.CreateHlaAssignmentAsSet(); foreach (KeyValuePair <string, Set <Hla> > patientAndHlaList in qmrrPartialModel.PatientList) { double logLikelihoodNoReactionInThisPatient = logOneLessLeakProbability + NumberOfPositiveHlas(patientAndHlaList.Value, trueCollectionAsSet) * logOneLessLink; bool didReact = qmrrPartialModel.PatientToAnyReaction.ContainsKey(patientAndHlaList.Key); logLikelihood += LogLikelihoodOfThisPatient(logLikelihoodNoReactionInThisPatient, didReact); } return(logLikelihood); }
override protected double LogLikelihoodOfEffects(QmrrPartialModel qmrrPartialModel, TrueCollection trueCollection, OptimizationParameterList qmrrParams) { Set <Hla> trueCollectionAsSet = trueCollection.CreateHlaAssignmentAsSet(); double logOneLessLeakProbability = Math.Log(1.0 - qmrrParams["leakProbability"].Value); Dictionary <Hla, double> hlaToLogOneLessLink = new Dictionary <Hla, double>(); foreach (Hla hla in trueCollectionAsSet) { double logOneLessLink = Math.Log(1.0 - qmrrParams["link" + hla].Value); hlaToLogOneLessLink.Add(hla, logOneLessLink); } double logLikelihood = 0.0; foreach (KeyValuePair <string, Set <Hla> > patientAndHlaList in qmrrPartialModel.PatientList) { double logLikelihoodNoReactionInThisPatient = logOneLessLeakProbability; foreach (Hla hla in patientAndHlaList.Value) { if (trueCollectionAsSet.Contains(hla)) { double logOneLessLink = hlaToLogOneLessLink[hla]; logLikelihoodNoReactionInThisPatient += logOneLessLink; } } bool didReact = qmrrPartialModel.PatientToAnyReaction.ContainsKey(patientAndHlaList.Key); logLikelihood += LogLikelihoodOfThisPatient(logLikelihoodNoReactionInThisPatient, didReact); } return(logLikelihood); }