static public bool?HasHla(Hla hlaGoal, Dictionary <string, string> row, HlaResolution hlaResolution) { bool hasNull = false; for (int i = 1; i <= 2; ++i) { string column = hlaGoal.ToString().Substring(0, 1) + i.ToString(); HlaToLength hlaToLengthOrNull = HlaAssignmentParams.GetHlaToLengthValueOrNull(row, column, hlaResolution); if (hlaToLengthOrNull == null || hlaToLengthOrNull.HlaNumberToLength >= 9000) { hasNull = true; continue; } if (hlaGoal.ToString() == hlaToLengthOrNull.ToString()) { return(true); } } if (hasNull) { return(null); } else { return(false); } }
public static void BioQuickTestInternal(Quickscore <Hla, int> quickscore, string fileName, string header) { List <int> patientList = quickscore.EffectList(); List <QmrJob <Hla, int> > jobList = new List <QmrJob <Hla, int> >(); foreach (List <Dictionary <string, string> > tableByPeptide in HlaAssignmentParams.QuickScoreOptimalsGroupByPeptide(fileName, header)) { Debug.Assert(tableByPeptide.Count > 0); // real assert string peptide = tableByPeptide[0]["peptide"]; List <int> patientsWhoDoNotRespond; List <int> patientsWhoRespond; QmrAlgorithms.FindPatientsWhoRespondAndWhoDoNot(patientList, tableByPeptide, out patientsWhoRespond, out patientsWhoDoNotRespond); QmrJob <Hla, int> aQuickScoreJob = QmrJob <Hla, int> .GetInstance(peptide, patientsWhoRespond, patientsWhoDoNotRespond, quickscore); jobList.Add(aQuickScoreJob); } jobList.Sort(delegate(QmrJob <Hla, int> x, QmrJob <Hla, int> y) { return(x.PresentEffectCollection.Count.CompareTo(y.PresentEffectCollection.Count)); }); foreach (QmrJob <Hla, int> job in jobList) { Debug.WriteLine(job); } foreach (QmrJob <Hla, int> job in jobList) { //if (job.Name != "RIRTWKSLVK") //{ // continue; //} Console.WriteLine(job); //job.Quickscore.Probability("B58", job.PresentEffectCollection, job.AbsentEffectCollection); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Dictionary <Hla, double> posteriorOfEveryCause = job.PosteriorOfEveryCause(); stopwatch.Stop(); Console.WriteLine("{0}\t{1}\t{2}\t{3}", job.Name, job.PresentEffectCollection.Count, job.AbsentEffectCollection.Count, stopwatch.Elapsed); foreach (KeyValuePair <Hla, double> causeAndPosterior in posteriorOfEveryCause) { Debug.Assert(0 <= causeAndPosterior.Value && causeAndPosterior.Value <= 1); Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", job.Name, job.PresentEffectCollection.Count, job.AbsentEffectCollection.Count, stopwatch.Elapsed , causeAndPosterior.Key, causeAndPosterior.Value); } } }
public static void FindPatientsWhoRespondAndWhoDoNot(IList <int> patientList, List <Dictionary <string, string> > tableByPeptide, out List <int> patientsWhoRespond, out List <int> patientsWhoDoNotRespond) { Dictionary <int, bool> patientsWhoRespondDictionary = new Dictionary <int, bool>(); foreach (Dictionary <string, string> row in tableByPeptide) { int patient = HlaAssignmentParams.GetPatient(row); patientsWhoRespondDictionary[patient] = true; } patientsWhoDoNotRespond = new List <int>(); foreach (int patient in patientList) { if (!patientsWhoRespondDictionary.ContainsKey(patient)) { patientsWhoDoNotRespond.Add(patient); } } patientsWhoRespond = new List <int>(patientsWhoRespondDictionary.Keys); }
static public IEnumerable <Hla> FindAllHla(List <Dictionary <string, string> > expandedTable, HlaResolution hlaResolution, string header) { Qmrr.HlaFactory hlaFactory = Qmrr.HlaFactory.GetFactory("noConstraint"); Dictionary <Hla, bool> seenIt = new Dictionary <Hla, bool>(); foreach (Dictionary <string, string> row in expandedTable) { foreach (string column in HlaAssignmentParams.CreateHlaColumns(header)) { HlaToLength hlaToLengthOrNull = HlaAssignmentParams.GetHlaToLengthValueOrNull(row, column, hlaResolution); if (hlaToLengthOrNull == null || hlaToLengthOrNull.HlaNumberToLength >= 9000) { continue; } Hla hla = hlaFactory.GetGroundInstance(hlaToLengthOrNull.ToString()); if (!seenIt.ContainsKey(hla)) { seenIt.Add(hla, true); yield return(hla); } } } }
public static void SearchHlaAssignmentsRelaxation(string fileName, string header, string solutionFileName, string solutionHeader, double causePrior, double linkProbability, double leakProbability, int howManyBest, string outputFilename, HlaResolution hlaResolution) { Debug.Assert(Math.Exp(double.NegativeInfinity) == 0); //Create the structure of patients and their HLAs Quickscore <Hla, int> quickscore = HlaAssignmentParams.CreateQuickscore(fileName, header, causePrior, linkProbability, leakProbability, hlaResolution); // Get the list of patients, Hlas, and patient weights List <int> patientList = quickscore.EffectList(); IEnumerable <Hla> hlaList = quickscore.CauseList(); //The structure file can contain a "weight" column. If it does, weight the patients, otherwise give them all weight 1.0 Dictionary <int, double> patientWeightTable = HlaAssignmentParams.CreatePatientWeightTable(fileName, header); //The cause assignment table is a mapping from // peptide to HLA assignments for that peptide Dictionary <string, List <Dictionary <string, string> > > causeAssignmentTable = CreateCauseAssignmentTable(solutionFileName, solutionHeader); //We start the report using (StreamWriter streamwriterOutputFile = File.CreateText(outputFilename)) { //The report is the same as the input "solution" except that we now list new HLA, a note, and a p(assignment) streamwriterOutputFile.WriteLine(SpecialFunctions.CreateTabString(solutionHeader, "newHLA", "rank", "p(assignment)", "note", "Peptide", "LogLikelihood", "TrueHlas.Count", "TrueHlas", "TrueHlasAndRespondingPatients", "UnexplainedPatients.Count")); //Now we go through the model file, one peptide at a time. For each peptide, we know the patientID (did) of // every patient who responded. (It also tells the HLAs of the patient, but we already have that info in the structure) /* * peptide did a1 a2 b1 b2 c1 c2 * * ACQGVGGPGHK 10 2 11 38 44 12 16 * ACQGVGGPGHK 14 2 24 1517 58 3 7 * ACQGVGGPGHK 41 11 33 35 40 6 7 * ACQGVGGPGHK 102 2 11 35 44 4 5 * * AENLWVTVY 25 24 66 35 39 4 12 * AENLWVTVY 36 23 32 8 44 7 7 * AENLWVTVY 45 3 31 39 44 5 12 * AENLWVTVY 46 2 29 41 52 16 16 * AENLWVTVY 59 2 33 7 44 3 15 * * [...] */ foreach (List <Dictionary <string, string> > tableByPeptide in HlaAssignmentParams.QuickScoreOptimalsGroupByPeptide(fileName, header)) { Debug.Assert(tableByPeptide.Count > 0); // real assert string peptide = tableByPeptide[0]["peptide"]; //Create lists of patients who responded and assume everyone else didn't. List <int> patientsWhoRespond; List <int> patientsWhoDoNotRespond; FindPatientsWhoRespondAndWhoDoNot(patientList, tableByPeptide, out patientsWhoRespond, out patientsWhoDoNotRespond); //If we get a peptide with no row in the solution file, skip it. if (!causeAssignmentTable.ContainsKey(peptide)) { streamwriterOutputFile.WriteLine("{0}\t\t{1}", peptide, "explained by noise"); continue; } //We assign every HLA mentioned by the solution file with this peptide to TRUE and all others to FALSE //An assignment of TRUE means that this HLA is a cause of this response. List <Dictionary <string, string> > rowsOfThisPeptide = causeAssignmentTable[peptide]; HlaAssignmentWithResponses hlaAssignmentBase = CreateHlaAssignment(rowsOfThisPeptide, hlaList, hlaResolution, quickscore, patientsWhoRespond); //Find the likelihood of this structure, with these patient responses, and the solution's HLA assignment. double baseLogLikelihood = quickscore.LogLikelihoodOfModelWithCompleteAssignments(patientsWhoRespond, patientsWhoDoNotRespond, hlaAssignmentBase.AsDictionary, patientWeightTable); //Consider each HLA assigned to TRUE (for this peptide) by the solution foreach (Dictionary <string, string> row in rowsOfThisPeptide) { Hla hla = GetHlaFromRow(row, hlaResolution); //If we already really know that this HLA is a cause of this reponse, just report that. if (IsKnown(row)) { streamwriterOutputFile.WriteLine("{0}\t{1}\t{2}", row[""], hla, "known"); continue; } //Set it to false as a way to see measure the probability that it is true. HlaAssignmentWithResponses hlaAssignmentL0 = CreateHlaAssignment(rowsOfThisPeptide, hlaList, hlaResolution, quickscore, patientsWhoRespond, hla, null); double logL0 = quickscore.LogLikelihoodOfModelWithCompleteAssignments(patientsWhoRespond, patientsWhoDoNotRespond, hlaAssignmentL0.AsDictionary, patientWeightTable); Debug.Assert(hlaAssignmentL0.TrueCount + 1 == hlaAssignmentBase.TrueCount); // real assert string noteNote = ""; if (logL0 > baseLogLikelihood) { noteNote = string.Format("\tlogL0 > baseLogLikelihood ({0}>{1})", logL0, baseLogLikelihood); } double probability = Math.Exp(baseLogLikelihood - SpecialFunctions.LogSum(baseLogLikelihood, logL0)); streamwriterOutputFile.WriteLine(SpecialFunctions.CreateTabString( row[""], hla, "1 best", probability, noteNote, peptide, baseLogLikelihood, hlaAssignmentBase.TrueCount, hlaAssignmentBase.TrueToString(), hlaAssignmentBase.TrueToListString(), hlaAssignmentBase.UnexplainedPatients.Count)); streamwriterOutputFile.WriteLine(SpecialFunctions.CreateTabString( row[""], hla, "remove 1 best", "", noteNote, peptide, logL0, hlaAssignmentL0.TrueCount, hlaAssignmentL0.TrueToString(), hlaAssignmentL0.TrueToListString(), hlaAssignmentL0.UnexplainedPatients.Count)); //Also, while we're in the world were it is false, let's measure how well each of the currently-set-to-false HLAs would do instead. //!!!should this be one dictionary to a new class instead of three dictionaries? List <KeyValuePair <Hla, double> > listOfhla1AndProbability1 = new List <KeyValuePair <Hla, double> >(); Dictionary <Hla, double> loglikelihoodCollection = new Dictionary <Hla, double>(); Dictionary <Hla, HlaAssignmentWithResponses> hla1ToHlaAssignment = new Dictionary <Hla, HlaAssignmentWithResponses>(); foreach (Hla hla1 in hlaList) { if (hla1 == hla || hlaAssignmentBase.AsDictionary[hla1]) { continue; } HlaAssignmentWithResponses hlaAssignmentL1 = CreateHlaAssignment(rowsOfThisPeptide, hlaList, hlaResolution, quickscore, patientsWhoRespond, hla, hla1); if (hlaAssignmentL1.TrueCount == hlaAssignmentBase.TrueCount) { double logL1 = quickscore.LogLikelihoodOfModelWithCompleteAssignments(patientsWhoRespond, patientsWhoDoNotRespond, hlaAssignmentL1.AsDictionary, patientWeightTable); double probability1 = Math.Exp(logL1 - SpecialFunctions.LogSum(logL1, logL0)); loglikelihoodCollection[hla1] = logL1; hla1ToHlaAssignment.Add(hla1, hlaAssignmentL1); listOfhla1AndProbability1.Add(new KeyValuePair <Hla, double>(hla1, probability1)); } } //Report on the best of these listOfhla1AndProbability1.Sort(delegate(KeyValuePair <Hla, double> x, KeyValuePair <Hla, double> y) { return(y.Value.CompareTo(x.Value)); }); for (int i = 0; i < howManyBest - 1; ++i) { Hla hla1 = listOfhla1AndProbability1[i].Key; streamwriterOutputFile.WriteLine(SpecialFunctions.CreateTabString( row[""], hla1, string.Format("{0} best", i + 2), listOfhla1AndProbability1[i].Value, "", peptide, loglikelihoodCollection[hla1], hla1ToHlaAssignment[hla1].TrueCount, hla1ToHlaAssignment[hla1].TrueToString(), hla1ToHlaAssignment[hla1].TrueToListString(), hla1ToHlaAssignment[hla1].UnexplainedPatients.Count)); } } } } }
public static HlaAssignmentParams GetInstance(string nameForNumbers, string prefix, int limit, bool doPlus, bool doOutput) { HlaAssignmentParams aHlaAssignmentParams = new HlaAssignmentParams(); aHlaAssignmentParams.Limit = limit; aHlaAssignmentParams.DoPlus = doPlus; aHlaAssignmentParams.FileName = string.Format(@"{0}-model.txt", prefix); //aHlaAssignmentParams.Header = @"peptide did a1 a2 b1 b2 c1 c2"; aHlaAssignmentParams.OutputFileOrNull = doOutput ? string.Format(@"exhaustivePlus{0}-abinitio.{1}.{2}.new.txt", prefix, limit, nameForNumbers) : null; //!!!const //string solutionFileName = string.Format(@"{0}-solution-abinitio-leak0.003.txt", prefix); //string solutionHeader = @"peptide HLA p(assignment) isKnown knownHLAs"; if (nameForNumbers == "") { if (prefix == "HIVOptimals") { aHlaAssignmentParams.CausePrior = 0.011498; aHlaAssignmentParams.LinkProbability = 0.48795; aHlaAssignmentParams.LeakProbability = 0.051818; } else { SpecialFunctions.CheckCondition(prefix == "EBVOptimals"); aHlaAssignmentParams.CausePrior = 0.0068892; aHlaAssignmentParams.LinkProbability = 0.4597; aHlaAssignmentParams.LeakProbability = 0.042766; } } //else if (nameForNumbers == "1in300") //{ // if (prefix == "HIVOptimals") // { // linkProbability = 0.30816; // causePrior = 0.040854; // leakProbability = 0.0033333; // } // else // { // CheckCondition(prefix == "EBVOptimals"); // linkProbability = 0.30056; // causePrior = 0.028322; // leakProbability = 0.0033333; // } //} else if (nameForNumbers == "leak0") { if (prefix == "HIVOptimals") { aHlaAssignmentParams.CausePrior = 0.041551; aHlaAssignmentParams.LinkProbability = 0.33478; aHlaAssignmentParams.LeakProbability = 1.0 / 300.0; } else { SpecialFunctions.CheckCondition(prefix == "EBVOptimals"); aHlaAssignmentParams.CausePrior = 0.028628; aHlaAssignmentParams.LinkProbability = 0.29874; aHlaAssignmentParams.LeakProbability = 1.0 / 300.0; } } else { aHlaAssignmentParams.CausePrior = double.NaN; aHlaAssignmentParams.LinkProbability = double.NaN; aHlaAssignmentParams.LeakProbability = double.NaN; } aHlaAssignmentParams.HlaResolution = HlaResolution.ABMixed; return(aHlaAssignmentParams); }