//!!!Yikes tons of repeated code and stuff re-run
        internal static HlaAssignmentWithResponses CreateHlaAssignment(Quickscore <Hla, int> quickscore, string hlaListAsString, ICollection <int> patientsWhoRespond)
        {
            Qmrr.HlaFactory hlaFactory = Qmrr.HlaFactory.GetFactory("noConstraint");

            //!!!this code is repeated elsewhere
            Dictionary <Hla, List <int> > hlaToRespondingPatientsUnfiltered = quickscore.CreateCauseToSubsetOfEffects(patientsWhoRespond);
            Dictionary <Hla, List <int> > hlaToRespondingPatients           = Filter9xs(hlaToRespondingPatientsUnfiltered);
            List <Hla> hlaListFromRepondingPatients = new List <Hla>(hlaToRespondingPatients.Keys);

            Dictionary <Hla, bool> hlaAssignmentDicationary = new Dictionary <Hla, bool>();

            foreach (string hlaName in hlaListAsString.Split(';'))
            {
                Hla hla = hlaFactory.GetGroundInstance(hlaName);
                hlaAssignmentDicationary[hla] = true;
            }

            List <int> indexCollection = new List <int>();

            for (int hlaIndex = 0; hlaIndex < hlaListFromRepondingPatients.Count; ++hlaIndex)
            {
                Hla hla = hlaListFromRepondingPatients[hlaIndex];
                if (hlaAssignmentDicationary.ContainsKey(hla))
                {
                    indexCollection.Add(hlaIndex);
                }
            }

            HlaAssignmentWithResponses hlaAssignment = HlaAssignmentWithResponses.GetInstance(quickscore, hlaListFromRepondingPatients,
                                                                                              indexCollection, hlaToRespondingPatients);

            return(hlaAssignment);
        }
Example #2
0
        internal static HlaAssignmentWithResponses GetInstance(Quickscore <Hla, int> quickscore, List <Hla> hlaListFromRepondingPatients, List <int> indexCollection,
                                                               Dictionary <Hla, List <int> > hlaToRespondingPatients)
        {
            List <Hla> trueCollection = new List <Hla>();

            foreach (int index in indexCollection)
            {
                Hla hla = hlaListFromRepondingPatients[index];
                trueCollection.Add(hla);
            }
            return(HlaAssignmentWithResponses.GetInstance(trueCollection, quickscore, hlaListFromRepondingPatients, hlaToRespondingPatients));
        }
Example #3
0
        public static bool BetterAtExplainingReactions(HlaAssignmentWithResponses champ, HlaAssignmentWithResponses challenger)
        {
            if (challenger.UnexplainedPatients.Count < champ.UnexplainedPatients.Count)
            {
                return(true);
            }
            if (champ.UnexplainedPatients.Count < challenger.UnexplainedPatients.Count)
            {
                return(false);
            }

            return(challenger.TrueCount < champ.TrueCount);
        }
        //private Dictionary<string, bool> GetAList(Dictionary<string, string> row)
        //{
        //    Dictionary<string, bool> alist = new Dictionary<string, bool>();
        //    string alistValue = row["HLA In Fixed List"];
        //    foreach (string hla in alistValue.Split(' '))
        //    {
        //        Debug.Assert((hla.Length == 3 && !hla.StartsWith("B15")) || (hla.Length == 5 && hla.StartsWith("B15")));
        //        alist.Add(hla, true);
        //    }
        //    return alist;
        //}

        public static HlaAssignmentWithResponses CreateHlaAssignment(List <Dictionary <string, string> > rowsOfThisPeptide, IEnumerable <Hla> hlaList, HlaResolution hlaResolution, Quickscore <Hla, int> quickscore, List <int> patientsWhoRespond)
        {
            //!!!this code is repeated elsewhere
            Dictionary <Hla, List <int> > hlaToRespondingPatientsUnfiltered = quickscore.CreateCauseToSubsetOfEffects(patientsWhoRespond);
            Dictionary <Hla, List <int> > hlaToRespondingPatients           = Filter9xs(hlaToRespondingPatientsUnfiltered);
            List <Hla> hlaListFromRepondingPatients = new List <Hla>(hlaToRespondingPatients.Keys);

            Dictionary <Hla, bool> hlaAssignmentDicationary = new Dictionary <Hla, bool>();

            foreach (Dictionary <string, string> causeAssignmentRow in rowsOfThisPeptide)
            {
                Hla hla = GetHlaFromRow(causeAssignmentRow, hlaResolution);
                hlaAssignmentDicationary[hla] = true;
            }

            HlaAssignmentWithResponses hlaAssignment = HlaAssignmentWithResponses.GetInstance(hlaAssignmentDicationary.Keys, quickscore, hlaListFromRepondingPatients,
                                                                                              hlaToRespondingPatients);

            return(hlaAssignment);
        }
Example #5
0
        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);
        }
        //!!!Yikes tons of repeated code and stuff re-run
        public static HlaAssignmentWithResponses CreateHlaAssignment(List <Dictionary <string, string> > rowsOfThisPeptide, IEnumerable <Hla> hlaList, HlaResolution hlaResolution, Quickscore <Hla, int> quickscore, List <int> patientsWhoRespond, Hla hlaToReplace, Hla hlaWithWhichToReplaceItOrNull)
        {
            //!!!this code is repeated elsewhere
            Dictionary <Hla, List <int> > hlaToRespondingPatientsUnfiltered = quickscore.CreateCauseToSubsetOfEffects(patientsWhoRespond);
            Dictionary <Hla, List <int> > hlaToRespondingPatients           = Filter9xs(hlaToRespondingPatientsUnfiltered);
            List <Hla> hlaListFromRepondingPatients = new List <Hla>(hlaToRespondingPatients.Keys);

            Dictionary <Hla, bool> hlaAssignmentDicationary = new Dictionary <Hla, bool>();

            foreach (Dictionary <string, string> causeAssignmentRow in rowsOfThisPeptide)
            {
                Hla hla = GetHlaFromRow(causeAssignmentRow, hlaResolution);
                hlaAssignmentDicationary[hla] = true;
            }
            hlaAssignmentDicationary.Remove(hlaToReplace);
            if (hlaWithWhichToReplaceItOrNull != null)
            {
                hlaAssignmentDicationary.Add(hlaWithWhichToReplaceItOrNull, true);
            }

            List <int> indexCollection = new List <int>();

            for (int hlaIndex = 0; hlaIndex < hlaListFromRepondingPatients.Count; ++hlaIndex)
            {
                Hla hla = hlaListFromRepondingPatients[hlaIndex];
                if (hlaAssignmentDicationary.ContainsKey(hla))
                {
                    indexCollection.Add(hlaIndex);
                }
            }

            HlaAssignmentWithResponses hlaAssignment = HlaAssignmentWithResponses.GetInstance(quickscore, hlaListFromRepondingPatients,
                                                                                              indexCollection, hlaToRespondingPatients);

            return(hlaAssignment);
        }
        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));
                        }
                    }
                }
            }
        }