Ejemplo n.º 1
0
        public void Tabulate(string directory, string caseName, List <KeyValuePair <int, int> > firstNullAndLastNullList)
        {
            List <Dictionary <string, string> > realRowCollectionToSort = new List <Dictionary <string, string> >();
            List <double> nullDiffCollectionToBeSorted = new List <double>();

            //!!!move VirusCount.PhyloTree.RangeCollection to a more general place
            RangeCollection rangeCollection = RangeCollection.GetInstance();
            int             minFirstNull    = int.MaxValue;
            int             maxLastNull     = int.MinValue;


            foreach (KeyValuePair <int, int> firstNullAndLastNull in firstNullAndLastNullList)
            {
                int firstNull = firstNullAndLastNull.Key;
                minFirstNull = Math.Min(minFirstNull, firstNull);
                int lastNull = firstNullAndLastNull.Value;
                maxLastNull = Math.Max(maxLastNull, lastNull);
                //!!!string repeated elsewere
                //!!!what "new" or not?
                string inputFileName = string.Format(@"{0}\{1}.{2}.{3}-{4}.pValues.new.txt", directory, SelectionName, CaseName, firstNull, lastNull); //!!!const
                int    maxNullSeen   = int.MinValue;

                //foreach (Dictionary<string, string> row in SpecialFunctions.TabFileTable(inputFileName, true))
                foreach (Dictionary <string, string> row in SpecialFunctions.TabFileTable(inputFileName, PValueDetails.Header, true))
                {
                    int nullIndex = int.Parse(row["NullIndex"]);
                    rangeCollection.TryAdd(nullIndex);
                    if (nullIndex == -1)
                    {
                        realRowCollectionToSort.Add(row);
                    }
                    else
                    {
                        double value = AccessPValueFromRow(row);
                        nullDiffCollectionToBeSorted.Add(value);
                        maxNullSeen = Math.Max(maxNullSeen, nullIndex);
                    }
                }
            }
            SpecialFunctions.CheckCondition(realRowCollectionToSort.Count != 0);
            SpecialFunctions.CheckCondition(rangeCollection.IsComplete(minFirstNull, maxLastNull));
            int nullCount = maxLastNull - Math.Max(0, minFirstNull) + 1;


            Dictionary <Dictionary <string, string>, double>
            qValueList = SpecialFunctions.ComputeQValues(ref realRowCollectionToSort, AccessPValueFromRow, ref nullDiffCollectionToBeSorted, nullCount);

            string outputFile = string.Format(@"{0}\{1}.{2}.{3}.qValues.new.txt", directory, SelectionName, CaseName, nullCount); //!!!const

            using (StreamWriter outputStream = File.CreateText(outputFile))
            {
                outputStream.WriteLine(SpecialFunctions.CreateTabString(PValueDetails.Header, "qValue"));
                foreach (Dictionary <string, string> row in realRowCollectionToSort)
                {
                    double qValue = qValueList[row];
                    outputStream.WriteLine(SpecialFunctions.CreateTabString(row[""], qValue));
                }
            }
        }
Ejemplo n.º 2
0
        //!!!!somehow combine with Tabulate
        static public void TabulateForTwo(LrtForHla lrtForHlaA, string directoryA, string caseNameA, List <KeyValuePair <int, int> > firstNullAndLastNullListA,
                                          LrtForHla lrtForHlaB, string directoryB, string caseNameB, List <KeyValuePair <int, int> > firstNullAndLastNullListB)
        {
            SpecialFunctions.CheckCondition(lrtForHlaA.SelectionName == lrtForHlaB.SelectionName);
            List <Dictionary <string, string> > realRowCollectionToSort = new List <Dictionary <string, string> >();
            List <double> nullDiffCollectionToBeSorted = new List <double>();

            //!!!move VirusCount.PhyloTree.RangeCollection to a more general place
            RangeCollection rangeCollection = RangeCollection.GetInstance();
            int             minFirstNullx   = int.MaxValue;
            int             maxLastNullx    = int.MinValue;

            foreach (char which in new char[] { 'A', 'B' })
            {
                List <KeyValuePair <int, int> > firstNullAndLastNullList;
                string    directory;
                LrtForHla lrtForHla;
                if (which == 'A')
                {
                    firstNullAndLastNullList = firstNullAndLastNullListA;
                    directory = directoryA;
                    lrtForHla = lrtForHlaA;
                }
                else
                {
                    Debug.Assert(which == 'B');
                    firstNullAndLastNullList = firstNullAndLastNullListB;
                    directory = directoryB;
                    lrtForHla = lrtForHlaB;
                }

                int minFirstNull = int.MaxValue;
                int maxLastNull  = int.MinValue;
                foreach (KeyValuePair <int, int> firstNullAndLastNull in firstNullAndLastNullList)
                {
                    int firstNull = firstNullAndLastNull.Key;
                    minFirstNull = Math.Min(minFirstNull, firstNull);
                    int lastNull = firstNullAndLastNull.Value;
                    maxLastNull = Math.Max(maxLastNull, lastNull);
                    //!!!string repeated elsewere
                    //!!!what "new" or not?

                    string inputFileName = string.Format(@"{0}\{1}.{2}.{3}-{4}.pValues.new.txt", directory, lrtForHla.SelectionName, lrtForHla.CaseName, firstNull, lastNull); //!!!const
                    int    maxNullSeen   = int.MinValue;

                    //foreach (Dictionary<string, string> row in SpecialFunctions.TabFileTable(inputFileName, true))
                    foreach (Dictionary <string, string> row in SpecialFunctions.TabFileTable(inputFileName, PValueDetails.Header, true))
                    {
                        int nullIndex = int.Parse(row["NullIndex"]);
                        rangeCollection.TryAdd(nullIndex);
                        if (nullIndex == -1)
                        {
                            realRowCollectionToSort.Add(row);
                        }
                        else
                        {
                            double value = AccessPValueFromRow(row);
                            nullDiffCollectionToBeSorted.Add(value);
                            maxNullSeen = Math.Max(maxNullSeen, nullIndex);
                        }
                    }
                }

                if (minFirstNullx == int.MaxValue)
                {
                    minFirstNullx = minFirstNull;
                    Debug.Assert(maxLastNullx == int.MinValue);
                    maxLastNullx = maxLastNull;
                }
                else
                {
                    SpecialFunctions.CheckCondition(minFirstNullx == minFirstNull);
                    SpecialFunctions.CheckCondition(maxLastNullx == maxLastNull);
                }
            }
            SpecialFunctions.CheckCondition(realRowCollectionToSort.Count != 0);
            SpecialFunctions.CheckCondition(rangeCollection.IsComplete(minFirstNullx, maxLastNullx));
            int nullCount = maxLastNullx - Math.Max(0, minFirstNullx) + 1;

            //Dictionary<string,string> realRowCollectionToSortNAAs0 = new List<double>();
            //foreach (Dictionary<string,string> row in realRowCollectionToSort)
            //{
            //    double r = AccessPValueFromRow(row);
            //    if (double.IsNaN(r))
            //    {
            //        Dictionary<string, string> row2 = new Dictionary<string, string>();
            //        foreach (KeyValuePair<string, string> keyAndValue in row)
            //        {
            //        }
            //        realRowCollectionToSortNAAs0.Add(0.0);
            //    }
            //    else
            //    {
            //        realRowCollectionToSortNAAs0.Add(row);
            //    }
            //}

            Dictionary <Dictionary <string, string>, double>
            qValueList = SpecialFunctions.ComputeQValues(ref realRowCollectionToSort, AccessPValueFromRow, ref nullDiffCollectionToBeSorted, nullCount);

            string outputFile = string.Format(@"{0}\{1}.{2}-{3}.{4}.qValues.new.txt", directoryB, lrtForHlaA.SelectionName, lrtForHlaA.CaseName, lrtForHlaB.CaseName, nullCount); //!!!const

            using (StreamWriter outputStream = File.CreateText(outputFile))
            {
                outputStream.WriteLine(SpecialFunctions.CreateTabString(PValueDetails.Header, "qValue"));
                foreach (Dictionary <string, string> row in realRowCollectionToSort)
                {
                    double qValue = qValueList[row];
                    outputStream.WriteLine(SpecialFunctions.CreateTabString(row[""], qValue));
                }
            }
        }
Ejemplo n.º 3
0
        //Similar to the other tabulators, but can work with multiple sets of pValues files
        //!!!would be better if could cut off really bad pValues to save memory
        //!!! also would be nice to have filters
        public static void CreateTabulateReport(ICollection <string> inputFilePatternCollection, string outputFileName,
                                                KeepTest <Dictionary <string, string> > keepTest, double maxPValue, bool auditRowIndexValues)
        {
            //SpecialFunctions.CheckCondition(!File.Exists(outputFileName), "Output file already exists: " + outputFileName);
            using (TextWriter textWriter = File.CreateText(outputFileName)) // Do this early so that if it fails, well know
            {
                List <Dictionary <string, string> > realRowCollectionToSort = new List <Dictionary <string, string> >();
                List <double> nullValueCollectionToBeSorted = new List <double>();

                string headerSoFar = null;

                Set <int> broadRealAndNullIndexSetSoFar = null;

                foreach (string broadInputFilePattern in inputFilePatternCollection)
                {
                    Set <int> narrowRealAndNullIndexSetSetSoFar = Set <int> .GetInstance();

                    foreach (string narrowInputFilePattern in broadInputFilePattern.Split('+'))
                    {
                        Set <int> realAndNullIndexSet =
                            CreateTabulateReportInternal(narrowInputFilePattern, keepTest, maxPValue, auditRowIndexValues,
                                                         ref realRowCollectionToSort, ref nullValueCollectionToBeSorted, ref headerSoFar);

                        //Instead of throwing an error, we could filter out the duplicated null indexes
                        SpecialFunctions.CheckCondition(narrowRealAndNullIndexSetSetSoFar.IntersectionIsEmpty(realAndNullIndexSet),
                                                        string.Format("Within inputFilePattern {0}, multiple '+'-connected parts cover the same nullIndex(s), {1}",
                                                                      broadInputFilePattern,
                                                                      narrowRealAndNullIndexSetSetSoFar.Intersection(realAndNullIndexSet)));

                        narrowRealAndNullIndexSetSetSoFar.AddNewRange(realAndNullIndexSet);
                    }

                    SpecialFunctions.CheckCondition(!auditRowIndexValues || narrowRealAndNullIndexSetSetSoFar.Contains(-1),
                                                    string.Format("The 'null' index -1 for the real data was not seen in {0}", broadInputFilePattern));


                    if (broadRealAndNullIndexSetSoFar == null)
                    {
                        broadRealAndNullIndexSetSoFar = narrowRealAndNullIndexSetSetSoFar;
                    }
                    else
                    {
                        SpecialFunctions.CheckCondition(broadRealAndNullIndexSetSoFar.Equals(narrowRealAndNullIndexSetSetSoFar),
                                                        string.Format("The broad inputFilePattern {0} covers a different set of nullIndexes ({1}) than its predecessors ({2})",
                                                                      broadInputFilePattern, narrowRealAndNullIndexSetSetSoFar, broadRealAndNullIndexSetSoFar));
                    }
                }

                double numberOfRandomizationRuns = broadRealAndNullIndexSetSoFar.Count - 1;
                Console.WriteLine("Detected {0} randomized runs relative to the number of real runs.", numberOfRandomizationRuns);
                Dictionary <Dictionary <string, string>, double> qValueList = SpecialFunctions.ComputeQValues(ref realRowCollectionToSort, AccessPValueFromPhylotreeRow, ref nullValueCollectionToBeSorted, numberOfRandomizationRuns);

                //!!!this code is repeated elsewhere
                textWriter.WriteLine(SpecialFunctions.CreateTabString(headerSoFar, "qValue"));
                foreach (Dictionary <string, string> row in realRowCollectionToSort)
                {
                    double qValue = qValueList[row];
                    textWriter.WriteLine(SpecialFunctions.CreateTabString(row[""], qValue));
                }
            }
        }