public static Linkdis GetInstance(string ethnicityName, int combinationLimit)
        {
            Linkdis linkdis = new Linkdis();

            linkdis.Ethnicity        = Ethnicity.GetInstance(ethnicityName);
            linkdis.CombinationLimit = combinationLimit;
            return(linkdis);
        }
Beispiel #2
0
        internal void LoadTable(string resourceName, string targetClass)
        {
            using (TextReader textReader = Linkdis.OpenResource(resourceName))
            {
                List <HlaMsr1> targetHlaList = CreateTargetHlaList(textReader, targetClass, resourceName);

                AddToAbstractHlaToGroundHlaList(targetHlaList);

                CreatePredictorNameOrInterceptToTargetToWeight(textReader, targetHlaList, resourceName);
            }
        }
        internal void Prenormalize(PidAndHlaSet pidAndHlaSet, Linkdis linkdis)
        {
            PhaseToLogProb       = new Dictionary <UOPair <LinkedList1 <HlaMsr1> >, double>();
            UnphaseToLogProb     = new Dictionary <LinkedList1 <UOPair <HlaMsr1> >, double>();
            LogTotal             = double.NegativeInfinity;
            BadHlaMsr1NameOrNull = null;
            UsedLowerResModel    = false;

            //CounterWithMessages abstractPhaseCounter = CounterWithMessages.GetInstance("\tabstract phase index = {0}", 1, null);

            try
            {
                foreach (var phaseAbstract in pidAndHlaSet.GetPhasedEnumeration())
                {
                    //abstractPhaseCounter.Increment();

                    var firstHlaListToProb  = linkdis.CreateHlaListToProb(phaseAbstract.First);
                    var secondHlaListToProb = linkdis.CreateHlaListToProb(phaseAbstract.Second);
                    if (firstHlaListToProb.Count * secondHlaListToProb.Count > linkdis.CombinationLimit)
                    {
                        throw new CombinationLimitException("The combinationLimit was exceeded. " + linkdis.CombinationLimit.ToString());
                    }

                    CounterWithMessages groundPhaseCounter = CounterWithMessages.GetInstance("\t\tground phase index = {0}", 1000, null);
                    foreach (var firstHlaListAndProb in firstHlaListToProb)
                    {
                        foreach (var secondHlaListAndProb in secondHlaListToProb)
                        {
                            groundPhaseCounter.Increment();

                            var phaseGrounded = UOPair <LinkedList1 <HlaMsr1> > .GetInstance(firstHlaListAndProb.Key, secondHlaListAndProb.Key);

                            var unphasedGrounded = MakeUnphased(phaseGrounded);

                            double prob = firstHlaListAndProb.Value.Key * secondHlaListAndProb.Value.Key;
                            UsedLowerResModel |= firstHlaListAndProb.Value.Value || secondHlaListAndProb.Value.Value;
                            double logProb = Math.Log(prob);


                            LogSum(PhaseToLogProb, phaseGrounded, logProb);
                            LogSum(UnphaseToLogProb, unphasedGrounded, logProb);
                            LogTotal = SpecialFunctions.LogSum(LogTotal, logProb);
                        }
                    }
                }
            }
            catch (HlaNotInModelException e)
            {
                CreateNoAnswerAnswer(pidAndHlaSet, e);
            }
        }
Beispiel #4
0
        static private Dictionary <string, Ethnicity> Init()
        {
            Dictionary <string, Ethnicity> ethnicityNameToEthnicity = new Dictionary <string, Ethnicity>();

            using (TextReader textReader = Linkdis.OpenResource("datafileList.txt"))
            {
                foreach (var row in SpecialFunctions.ReadDelimitedFile(textReader, new { Ethnicity = "", Class = "", HlaLengthList = "", FileName = "" }, new char[] { '\t' }, true))
                {
                    Ethnicity ethnicity          = ethnicityNameToEthnicity.GetValueOrDefault(row.Ethnicity.ToLowerInvariant());
                    EClass    eclass             = ethnicity.HlaClassNameToEClass.GetValueOrDefault(row.Class);
                    var       hlaLengthListQuery =
                        from hlaLengthAsString in row.HlaLengthList.Split(' ')
                        select int.Parse(hlaLengthAsString);

                    LinkedList1 <int> hlaLengthList = LinkedList1 <int> .GetInstanceFromList(hlaLengthListQuery.ToList());


                    TableInfo tableInfo = eclass.HlaLengthListToTableInfo.GetValueOrDefault(hlaLengthList);
                    tableInfo.HlaMsr1Factory = HlaMsr1Factory.GetFactory(hlaLengthList);
                    tableInfo.LoadTable(row.FileName, row.Class);
                }
            }
            return(ethnicityNameToEthnicity);
        }
        static void Main(string[] args)
        {
            //HlaMsr1Factory.UnitTest();

            try
            {
                ArgCollection argCollection = ArgCollection.GetInstance(args);

                string ethnicityName = argCollection.ExtractOptional <string>("ethnicity", "").ToLowerInvariant();
                SpecialFunctions.CheckCondition(Linkdis.EthnicityNameLowerList().Contains(ethnicityName), string.Format("'-ethnicity ETHNICITY' is required, where ETHNICITY is " + Linkdis.EthnicityNameMixedList().StringJoin(", ")));
                int  outputLineLimit  = argCollection.ExtractOptional <int>("outputLineLimit", 100000);
                int  combinationLimit = argCollection.ExtractOptional <int>("combinationLimit", 10000);
                bool isSparse         = argCollection.ExtractOptionalFlag("sparse");

                argCollection.CheckNoMoreOptions(3);

                string inputFileName          = argCollection.ExtractNext <string>("inputFile");
                string phasedOutputFileName   = argCollection.ExtractNext <string>("phasedOutputFile");
                string unphasedOutputFileName = argCollection.ExtractNext <string>("unphasedOutputFile");
                argCollection.CheckThatEmpty();

                Linkdis linkdis = Linkdis.GetInstance(ethnicityName, combinationLimit);

                string versionName = string.Format("MSCompBio HLA Completion v. {0}", GetVersionString());


                CounterWithMessages pidCounter = CounterWithMessages.GetInstance("Pid index = {0}", 1, null);

                int outputLineIndex = -1;
                using (TextWriter phasedTextWriter = File.CreateText(phasedOutputFileName),
                       unphasedTextWriter = File.CreateText(unphasedOutputFileName))
                {
                    phasedTextWriter.WriteLine(versionName + "\n");
                    unphasedTextWriter.WriteLine(versionName + "\n");

                    phasedTextWriter.WriteLine("pid" + "\t" + PhasedExpansion.Header);
                    unphasedTextWriter.WriteLine("pid" + "\t" + UnphasedExpansion.Header);
                    outputLineIndex += 6;

                    HashSet <string> warningSet = new HashSet <string>();
                    using (TextReader textReader = File.OpenText(inputFileName))
                    {
                        foreach (PidAndHlaSet pidAndHlaSet in isSparse ? PidAndHlaSet.GetEnumerationSparse(textReader) : PidAndHlaSet.GetEnumerationDense(textReader))
                        {
                            pidCounter.Increment();
                            warningSet.UnionWith(pidAndHlaSet.WarningSet);

                            ExpansionCollection expansionCollectionOrNull = linkdis.ExpandOrNullIfTooMany(pidAndHlaSet);

                            if (null == expansionCollectionOrNull)
                            {
                                phasedTextWriter.WriteLine(pidAndHlaSet.Pid + "\t" + PhasedExpansion.TooManyCombinationsMessage());
                                unphasedTextWriter.WriteLine(pidAndHlaSet.Pid + "\t" + UnphasedExpansion.TooManyCombinationsMessage());
                                warningSet.Add(string.Format("Error: Too many combinations, case {0} skipped", pidAndHlaSet.Pid));
                                outputLineIndex += 2;
                                if (outputLineIndex > outputLineLimit)
                                {
                                    goto TOOMANYLINES;
                                }
                            }
                            else
                            {
                                foreach (PhasedExpansion phasedExpansion in expansionCollectionOrNull.Phased())
                                {
                                    string phasedLine = pidAndHlaSet.Pid + "\t" + phasedExpansion.ToString();
                                    phasedTextWriter.WriteLine(phasedLine);
                                    if (phasedExpansion.BadHlaNameOrNull != null)
                                    {
                                        warningSet.Add(phasedLine);
                                    }
                                    ++outputLineIndex;
                                    if (outputLineIndex > outputLineLimit)
                                    {
                                        goto TOOMANYLINES;
                                    }
                                }

                                foreach (UnphasedExpansion unphasedExpansion in expansionCollectionOrNull.Unphased())
                                {
                                    string unphasedLine = pidAndHlaSet.Pid + "\t" + unphasedExpansion.ToString();
                                    unphasedTextWriter.WriteLine(unphasedLine);
                                    if (unphasedExpansion.BadHlaNameOrNull != null)
                                    {
                                        warningSet.Add(unphasedLine);
                                    }

                                    ++outputLineIndex;
                                    if (outputLineIndex > outputLineLimit)
                                    {
                                        goto TOOMANYLINES;
                                    }
                                }
                            }
                        }
                    }

                    goto INANYCASE;
TOOMANYLINES:
                    string tooManyLinesMessage = string.Format("ERROR: The line limit of {0} was reached and output was ended early", outputLineLimit);
                    phasedTextWriter.WriteLine(tooManyLinesMessage);
                    unphasedTextWriter.WriteLine(tooManyLinesMessage);
                    warningSet.Add(tooManyLinesMessage);
INANYCASE:
                    Console.Error.WriteLine(warningSet.StringJoin("\n"));
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
                if (exception.InnerException != null)
                {
                    Console.WriteLine(exception.InnerException.Message);
                }

                Console.Error.WriteLine(@"
 
USAGE 

HlaCompletion -ethnicity ETHNICITY [-outputLineLimit 100000] [-sparse] [-combinationLimit 10000] inputFile phaseFile unphaseFile 
where ETHNICITY is {0}
'outputLineLimit' limits the total lines of output. If it is reached, a warning message is written as the last line of the output.
'combinationLimit' limits the number of combinations of HLAs consider in one phase for one case.
        It is is reached, an error message is output for that case in place of results.
'-sparse' reads files in sparse format
 
", Linkdis.EthnicityNameMixedList().StringJoin(", "));

                System.Environment.Exit(-1);
            }
        }