private IEnumerable <int> KEnumeration(MerLength merLength, int inputPeptideLength)
        {
            switch (merLength)
            {
            case MerLength.scan:
                for (int k = (int)MerLength.firstLength; k <= (int)MerLength.lastLength; ++k)
                {
                    yield return(k);
                }
                break;

            case MerLength.given:
                int inputEpitopeLength = inputPeptideLength - NCLength - NCLength;
                SpecialFunctions.CheckCondition((int)MerLength.firstLength <= inputEpitopeLength && inputEpitopeLength <= (int)MerLength.lastLength,
                                                string.Format("Given peptide length {0} is out of range", inputPeptideLength));
                yield return(inputEpitopeLength);

                break;

            default:
                SpecialFunctions.CheckCondition(MerLength.firstLength <= merLength && merLength <= MerLength.lastLength,
                                                string.Format("Given peptide length {0} is out of range", (int)merLength));
                yield return((int)merLength);

                break;
            }
        }
Example #2
0
        private static List <string> ProcessLine(ShowBy showBy, PredictorCollection predictorCollection, string line,
                                                 MerLength merLength, int?dOfCenter, HlaSetSpecification hlaSetSpecification, bool modelOnly)
        {
            try
            {
                string hlaOrSupertypeOrNull;
                string inputPeptide = ExtractInputs(hlaSetSpecification, line, predictorCollection, out hlaOrSupertypeOrNull);

                List <string> output = new List <string>();
                foreach (List <Prediction> predictionList in predictorCollection.MaxProbabilityPredictions(showBy, inputPeptide, merLength, dOfCenter, hlaSetSpecification, hlaOrSupertypeOrNull, modelOnly).Values)
                {
                    string outputLine = InsertMaterial(line, hlaSetSpecification.InputHeaderCollection().Length, Prediction.CollectionToString(predictionList, false, hlaSetSpecification.IncludeHlaInOutput()));
                    output.Add(outputLine);
                }
                return(output);
            }
            catch (Exception exception)
            {
                string errorString = SpecialFunctions.CreateTabString(
                    line,
                    string.Format("Error: {0}{1}", exception.Message, exception.InnerException == null ? "" : string.Format(" ({0})", exception.InnerException)));
                List <string> output = new List <string>();
                output.Add(errorString);
                return(output);
            }
        }
        /// <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);
        }
        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);
        }
Example #5
0
        public static void Main(string[] args)
        {
            try
            {
                // Parse arguments
                ArgCollection argCollection = ArgCollection.GetInstance(args);

                if (argCollection.ExtractOptionalFlag("Help"))
                {
                    Console.WriteLine(HelpString);
                    return;
                }

                string              modelName           = argCollection.ExtractOptional("model", "LANLIEDB03062007");
                bool                modelOnly           = argCollection.ExtractOptionalFlag("modelOnly");
                bool                inputHasHeader      = argCollection.ExtractOptionalFlag("inputHasHeader");
                MerLength           merLength           = argCollection.ExtractOptional <MerLength>("merLength", MerLength.scan);
                int?                dOfCenter           = argCollection.ExtractOptional <int?>("withinDOfCenter", null);
                ShowBy              showBy              = argCollection.ExtractOptional <ShowBy>("showBy", ShowBy.all);
                string              hlaSetName          = argCollection.ExtractOptional <string>("hlaSet", "singleton");
                HlaSetSpecification hlaSetSpecification = HlaSetSpecification.GetInstance(hlaSetName);

                argCollection.CheckNoMoreOptions();

                string inputFileName  = argCollection.ExtractNext <string>("inputFile");
                string outputFileName = argCollection.ExtractNext <string>("outputFile");
                argCollection.CheckThatEmpty();


                ReadInputCreatingOutput(showBy, modelName, inputHasHeader, merLength, dOfCenter, hlaSetSpecification, modelOnly, inputFileName, outputFileName);
            }
            catch (Exception exception)
            {
                Console.WriteLine("");
                Console.WriteLine(exception.Message);
                if (exception.InnerException != null)
                {
                    Console.WriteLine(exception.InnerException.Message);
                }

                Console.Error.WriteLine("");
                Console.Error.WriteLine(@"For more help:
Epipred -help");

                System.Environment.Exit(-1);
            }
        }
        ////!!! 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);
                    }
                }
            }
        }
Example #7
0
        private static void ReadInputCreatingOutput(ShowBy showBy, string modelName,
                                                    bool inputHasHeader, MerLength merLength, int?dOfCenter,
                                                    HlaSetSpecification hlaSetSpecification, bool modelOnly, string inputFileName, string outputFileName)
        {
            using (TextReader textReader = File.OpenText(inputFileName))
            {
                using (TextWriter textWriter = File.CreateText(outputFileName))
                {
                    string header = CreateHeader(hlaSetSpecification, textReader, inputHasHeader);
                    textWriter.WriteLine(header);

                    PredictorCollection predictorCollection = PredictorCollection.GetInstance(modelName);

                    foreach (string line in SpecialFunctions.ReadEachLine(textReader))
                    {
                        foreach (string outputLine in ProcessLine(showBy, predictorCollection, line, merLength, dOfCenter, hlaSetSpecification, modelOnly))
                        {
                            textWriter.WriteLine(outputLine);
                            textWriter.Flush();
                        }
                    }
                }
            }
        }