public void TestExperimentalDesignParserSampleNumbers()
        {
            RunProteinSignificanceClassifier runProteinSignificanceClassifier = new RunProteinSignificanceClassifier();

            int expectedNumberSamples = 8;

            int resultNumberSamples = 0;
            Dictionary <string, List <string> > resultSamplefileConditionRelation =
                runProteinSignificanceClassifier.ExpermientalDesignParser("TestExperimentalDesign.tsv", ref resultNumberSamples);

            Assert.AreEqual(expectedNumberSamples, resultNumberSamples);
        }
        public void TestResizePermutedArray()
        {
            List <double> expectedResizedArray = new List <double>()
            {
                0, 1, 2, 3, 5, 6, 7, 9, 10, 12, 13, 14, 16, 17, 18
            };

            List <double> outputResizedArray = new List <double>();

            for (int i = 0; i < 20; i++)
            {
                outputResizedArray.Add(i);
            }
            int sizeDifference = 5;
            RunProteinSignificanceClassifier runProteinSignificanceClassifier = new RunProteinSignificanceClassifier();

            runProteinSignificanceClassifier.ResizePermutedArray(outputResizedArray, sizeDifference);

            CollectionAssert.AreEqual(expectedResizedArray, outputResizedArray);
        }
        public void TestRPCGenerateAllCombinationsOfTwoConditions()
        {
            RunProteinSignificanceClassifier runProteinSignificanceClassifier = new RunProteinSignificanceClassifier();
            List <string> testConditions = new List <string>();

            testConditions.Add("condition1");
            testConditions.Add("condition2");
            testConditions.Add("condition3");
            testConditions.Add("condition4");

            List <List <string> > expectedConditionPairs = new List <List <string> >();

            expectedConditionPairs.Add(new List <string>()
            {
                "condition1", "condition2"
            });
            expectedConditionPairs.Add(new List <string>()
            {
                "condition1", "condition3"
            });
            expectedConditionPairs.Add(new List <string>()
            {
                "condition1", "condition4"
            });
            expectedConditionPairs.Add(new List <string>()
            {
                "condition2", "condition3"
            });
            expectedConditionPairs.Add(new List <string>()
            {
                "condition2", "condition4"
            });
            expectedConditionPairs.Add(new List <string>()
            {
                "condition3", "condition4"
            });

            List <List <string> > resultConditionPairs = runProteinSignificanceClassifier.GenerateAllCombinationsOfTwoConditions(testConditions);

            CollectionAssert.AreEqual(expectedConditionPairs, resultConditionPairs);
        }
        public void TestProteinDataParserSampleFileNames()
        {
            List <string> expectedSampleFileNames = new List <string>();

            expectedSampleFileNames.Add("Intensity_B02_06_161103_A1_HCD_OT_4ul");
            expectedSampleFileNames.Add("Intensity_B02_07_161103_A2_HCD_OT_4ul");
            expectedSampleFileNames.Add("Intensity_B02_16_161103_A3_HCD_OT_4ul");
            expectedSampleFileNames.Add("Intensity_B02_17_161103_A4_HCD_OT_4ul");
            expectedSampleFileNames.Add("Intensity_B02_24_161103_C1_HCD_OT_4ul");
            expectedSampleFileNames.Add("Intensity_B02_09_161103_C2_HCD_OT_4ul");
            expectedSampleFileNames.Add("Intensity_B02_14_161103_C3_HCD_OT_4ul");
            expectedSampleFileNames.Add("Intensity_B02_19_161103_C4_HCD_OT_4ul");

            int maxAllowedMissingValues = 2;
            List <ProteinRowInfo>            outputAllProteinInfo             = new List <ProteinRowInfo>();
            List <string>                    outputSamplesFileNames           = new List <string>();
            RunProteinSignificanceClassifier runProteinSignificanceClassifier = new RunProteinSignificanceClassifier();

            runProteinSignificanceClassifier.ProteinDataParser(outputAllProteinInfo, maxAllowedMissingValues,
                                                               outputSamplesFileNames, "TestQuantifiedProteins.tsv");

            CollectionAssert.AreEqual(expectedSampleFileNames, outputSamplesFileNames);
        }
        public void TestExperimentalDesignParserConditionSampleMapping()
        {
            RunProteinSignificanceClassifier runProteinSignificanceClassifier = new RunProteinSignificanceClassifier();

            Dictionary <string, List <string> > expectedSamplefileConditionRelation = new Dictionary <string, List <string> >();

            expectedSamplefileConditionRelation.Add("Ecoli 1x", new List <string>()
            {
                "Intensity_B02_06_161103_A1_HCD_OT_4ul",
                "Intensity_B02_07_161103_A2_HCD_OT_4ul", "Intensity_B02_16_161103_A3_HCD_OT_4ul", "Intensity_B02_17_161103_A4_HCD_OT_4ul"
            });
            expectedSamplefileConditionRelation.Add("Ecoli 2x", new List <string>()
            {
                "Intensity_B02_09_161103_C2_HCD_OT_4ul",
                "Intensity_B02_14_161103_C3_HCD_OT_4ul", "Intensity_B02_19_161103_C4_HCD_OT_4ul", "Intensity_B02_24_161103_C1_HCD_OT_4ul"
            });

            int resultNumberSamples = 0;
            Dictionary <string, List <string> > resultSamplefileConditionRelation =
                runProteinSignificanceClassifier.ExpermientalDesignParser("TestExperimentalDesign.tsv", ref resultNumberSamples);

            CollectionAssert.AreEqual(expectedSamplefileConditionRelation, resultSamplefileConditionRelation);
        }
        public static void Main(string[] args)
        {
            RunProteinSignificanceClassifier proteinBasedSignificance = new RunProteinSignificanceClassifier();
            int numberSamples = 0;
            // Parse the ExperimentalDesign File to get info of samples and conditions they belong to
            Dictionary <string, List <string> > samplefileConditionRelation = proteinBasedSignificance.ExpermientalDesignParser("C:/Users/Anay/Desktop/UW Madison/Smith Lab/Spectra Data/ExperimentalDesign.tsv"
                                                                                                                                , ref numberSamples);
            // get all conditions and pair them up for Significance classification
            List <string>         allConditions = new List <string>(samplefileConditionRelation.Keys);
            List <List <string> > allTwoConditionCombinations = proteinBasedSignificance.GenerateAllCombinationsOfTwoConditions(allConditions);

            foreach (List <string> conditionPair in allTwoConditionCombinations)
            {
                string firstCondition      = conditionPair[0];
                string secondCondition     = conditionPair[1];
                double sOValue             = 0.1;
                double meanFraction        = 0.1;
                int    maxSignificantCount = 0;

                while (meanFraction < 1)
                {
                    while (sOValue < 1)
                    {
                        for (int k = 0; k < numberSamples; k++)
                        {
                            proteinBasedSignificance = new RunProteinSignificanceClassifier();
                            //Declaring variables which will be generated after parsing QuantifiedPeptides file
                            List <ProteinRowInfo> allProteinInfo   = new List <ProteinRowInfo>();
                            List <string>         samplesFileNames = new List <string>();
                            int maxInvalidIntensityValues          = k;
                            proteinBasedSignificance.ProteinDataParser(allProteinInfo, maxInvalidIntensityValues,
                                                                       samplesFileNames, "C:/Users/Anay/Desktop/UW Madison/Smith Lab/Spectra Data/FlashLFQ_2020-04-26-17-39-35/QuantifiedProteins.tsv");

                            // imputes missing intensity values for each protein
                            ImputationProcess imputationProcess = new ImputationProcess();
                            imputationProcess.RunImputationProcess(allProteinInfo, samplesFileNames, meanFraction);

                            // Declaring variables which will be generated after T-Tests and Permutation Tests
                            List <double>    observedNValues  = new List <double>(); // will store observed N values
                            List <double>    permutedNValues  = new List <double>(); // will store permuted N values
                            StatisticalTests statisticalTests = new StatisticalTests();

                            // contains proteins and their observed N value, P Value and Log Fold Change
                            Dictionary <string, List <double> > allProteinObservedStatistics = new Dictionary <string, List <double> >();
                            // Creating threads for Parallelizing code
                            ThreadPool.GetMaxThreads(out int workerThreadsCount, out int ioThreadsCount);
                            int[] threads = Enumerable.Range(0, workerThreadsCount).ToArray();
                            Parallel.ForEach(threads, (i) =>
                            {
                                // Compute observed and permuted N Values for each protein using T Tests and Permutation Testing
                                for (; i < allProteinInfo.Count; i += workerThreadsCount)
                                {
                                    ProteinRowInfo proteinRowInfo = allProteinInfo[i];
                                    Dictionary <string, double> samplesintensityData = proteinRowInfo.SamplesIntensityData;

                                    List <string> firstConditionAssociatedSamples       = samplefileConditionRelation.GetValueOrDefault(firstCondition);
                                    List <string> secondConditionAssociatedSamples      = samplefileConditionRelation.GetValueOrDefault(secondCondition);
                                    List <double> proteinFirstConditionIntensityValues  = new List <double>();
                                    List <double> proteinSecondConditionIntensityValues = new List <double>();

                                    // get the protein's intensity values corresponding to the chosen pair of conditions
                                    foreach (string sampleFileName in samplesFileNames)
                                    {
                                        if (firstConditionAssociatedSamples.Contains(sampleFileName))
                                        {
                                            proteinFirstConditionIntensityValues.Add(samplesintensityData[sampleFileName]);
                                        }
                                        if (secondConditionAssociatedSamples.Contains(sampleFileName))
                                        {
                                            proteinSecondConditionIntensityValues.Add(samplesintensityData[sampleFileName]);
                                        }
                                    }

                                    // Compute observed N Values with the chosen pair of conditions using T-Tests and
                                    // store in observedNValues array
                                    List <double> proteinStatistics = statisticalTests.GetNValueUsingTTest(proteinFirstConditionIntensityValues, proteinSecondConditionIntensityValues,
                                                                                                           sOValue, false);

                                    // Compute permuted N Values with the chosen pair of conditions using T-Tests and
                                    // store in permutedNValues array
                                    List <double> proteinPermutedNavlues = statisticalTests.GetNValueUsingPermutationtests(proteinFirstConditionIntensityValues,
                                                                                                                           proteinSecondConditionIntensityValues, sOValue);

                                    // add computed original and permuted statistics for the protein
                                    lock (allProteinObservedStatistics)
                                    {
                                        // add protein and its observed N value, P Value and Log Fold Change in that order
                                        allProteinObservedStatistics.Add(proteinRowInfo.ProteinID, new List <double>()
                                        {
                                            proteinStatistics[0],
                                            proteinStatistics[1], proteinStatistics[2]
                                        });
                                        observedNValues.Add(proteinStatistics[0]);
                                        foreach (double permutedNValue in proteinPermutedNavlues)
                                        {
                                            permutedNValues.Add(permutedNValue);
                                        }
                                    }
                                }
                            });

                            // makes the permuted N values list and the observed N Values list of the same size
                            proteinBasedSignificance.ResizePermutedArray(permutedNValues, permutedNValues.Count() - observedNValues.Count());

                            // get the threshold at which we will filter out the significant proteins
                            double nValueThreshold = statisticalTests.calculateNvaluethreshold(observedNValues, permutedNValues, 0.05);

                            // determine number of signifcant proteins detected
                            int newSignificantCount = observedNValues.Count(x => x >= nValueThreshold);
                            if (newSignificantCount > maxSignificantCount)
                            {
                                maxSignificantCount = newSignificantCount;
                                proteinBasedSignificance.PrintSignificantProtein(allProteinInfo, nValueThreshold, allProteinObservedStatistics,
                                                                                 "C:/Users/Anay/Desktop/UW Madison/Smith Lab/Project 1/ConsoleApp1/ProteinBasedSignificanceModified.csv");
                                Console.WriteLine("Sig Count - " + maxSignificantCount + "meanFraction - " + meanFraction + "sOValue - "
                                                  + sOValue + "k - " + k);
                            }
                        }
                        sOValue = sOValue + 0.1;
                    }
                    sOValue      = 0.1;
                    meanFraction = meanFraction + 0.3;
                }
            }
        }
        public void TestProteinDataParserProteinIntensityMapping()
        {
            List <string> sampleFileNames = new List <string>();

            sampleFileNames.Add("Intensity_B02_06_161103_A1_HCD_OT_4ul");
            sampleFileNames.Add("Intensity_B02_07_161103_A2_HCD_OT_4ul");
            sampleFileNames.Add("Intensity_B02_16_161103_A3_HCD_OT_4ul");
            sampleFileNames.Add("Intensity_B02_17_161103_A4_HCD_OT_4ul");
            sampleFileNames.Add("Intensity_B02_24_161103_C1_HCD_OT_4ul");
            sampleFileNames.Add("Intensity_B02_09_161103_C2_HCD_OT_4ul");
            sampleFileNames.Add("Intensity_B02_14_161103_C3_HCD_OT_4ul");
            sampleFileNames.Add("Intensity_B02_19_161103_C4_HCD_OT_4ul");

            List <ProteinRowInfo> expectedAllProteinInfo = new List <ProteinRowInfo>();
            ProteinRowInfo        sampleprotein1         = new ProteinRowInfo();

            sampleprotein1.ProteinID = "A0A023KES1";
            sampleprotein1.SamplesIntensityData["Intensity_B02_06_161103_A1_HCD_OT_4ul"] = 22.192;
            sampleprotein1.SamplesIntensityData["Intensity_B02_07_161103_A2_HCD_OT_4ul"] = 22.729;
            sampleprotein1.SamplesIntensityData["Intensity_B02_16_161103_A3_HCD_OT_4ul"] = 22.347;
            sampleprotein1.SamplesIntensityData["Intensity_B02_17_161103_A4_HCD_OT_4ul"] = 22.397;
            sampleprotein1.SamplesIntensityData["Intensity_B02_24_161103_C1_HCD_OT_4ul"] = 23.489;
            sampleprotein1.SamplesIntensityData["Intensity_B02_09_161103_C2_HCD_OT_4ul"] = 23.180;
            sampleprotein1.SamplesIntensityData["Intensity_B02_14_161103_C3_HCD_OT_4ul"] = 23.293;
            sampleprotein1.SamplesIntensityData["Intensity_B02_19_161103_C4_HCD_OT_4ul"] = 23.271;

            ProteinRowInfo sampleprotein2 = new ProteinRowInfo();

            sampleprotein2.ProteinID = "A0A024R4E5";
            sampleprotein2.SamplesIntensityData["Intensity_B02_06_161103_A1_HCD_OT_4ul"] = 25.535;
            sampleprotein2.SamplesIntensityData["Intensity_B02_07_161103_A2_HCD_OT_4ul"] = 25.482;
            sampleprotein2.SamplesIntensityData["Intensity_B02_16_161103_A3_HCD_OT_4ul"] = 25.308;
            sampleprotein2.SamplesIntensityData["Intensity_B02_17_161103_A4_HCD_OT_4ul"] = 25.373;
            sampleprotein2.SamplesIntensityData["Intensity_B02_24_161103_C1_HCD_OT_4ul"] = 25.370;
            sampleprotein2.SamplesIntensityData["Intensity_B02_09_161103_C2_HCD_OT_4ul"] = 25.368;
            sampleprotein2.SamplesIntensityData["Intensity_B02_14_161103_C3_HCD_OT_4ul"] = 25.359;
            sampleprotein2.SamplesIntensityData["Intensity_B02_19_161103_C4_HCD_OT_4ul"] = 25.251;

            ProteinRowInfo sampleprotein3 = new ProteinRowInfo();

            sampleprotein3.ProteinID = "A0A087WTI9";
            sampleprotein3.SamplesIntensityData["Intensity_B02_06_161103_A1_HCD_OT_4ul"] = 0;
            sampleprotein3.SamplesIntensityData["Intensity_B02_07_161103_A2_HCD_OT_4ul"] = 23.763;
            sampleprotein3.SamplesIntensityData["Intensity_B02_16_161103_A3_HCD_OT_4ul"] = 20.783;
            sampleprotein3.SamplesIntensityData["Intensity_B02_17_161103_A4_HCD_OT_4ul"] = 20.165;
            sampleprotein3.SamplesIntensityData["Intensity_B02_24_161103_C1_HCD_OT_4ul"] = 21.7858;
            sampleprotein3.SamplesIntensityData["Intensity_B02_09_161103_C2_HCD_OT_4ul"] = 0;
            sampleprotein3.SamplesIntensityData["Intensity_B02_14_161103_C3_HCD_OT_4ul"] = 21.564;
            sampleprotein3.SamplesIntensityData["Intensity_B02_19_161103_C4_HCD_OT_4ul"] = 20.469;

            expectedAllProteinInfo.Add(sampleprotein1);
            expectedAllProteinInfo.Add(sampleprotein2);
            expectedAllProteinInfo.Add(sampleprotein3);


            int maxAllowedMissingValues = 2;
            List <ProteinRowInfo>            outputAllProteinInfo             = new List <ProteinRowInfo>();
            List <string>                    outputSamplesFileNames           = new List <string>();
            RunProteinSignificanceClassifier runProteinSignificanceClassifier = new RunProteinSignificanceClassifier();

            runProteinSignificanceClassifier.ProteinDataParser(outputAllProteinInfo, maxAllowedMissingValues,
                                                               outputSamplesFileNames, "TestQuantifiedProteins.tsv");


            foreach (ProteinRowInfo outputProtein in outputAllProteinInfo)
            {
                if (outputProtein.ProteinID.Equals(sampleprotein1.ProteinID))
                {
                    Assert.That(sampleprotein1.SamplesIntensityData,
                                Is.EqualTo(outputProtein.SamplesIntensityData).Within(0.001));
                }
                else if (outputProtein.ProteinID.Equals(sampleprotein2.ProteinID))
                {
                    Assert.That(sampleprotein2.SamplesIntensityData,
                                Is.EqualTo(outputProtein.SamplesIntensityData).Within(0.001));
                }
                else if (outputProtein.ProteinID.Equals(sampleprotein3.ProteinID))
                {
                    Assert.That(sampleprotein3.SamplesIntensityData,
                                Is.EqualTo(outputProtein.SamplesIntensityData).Within(0.001));
                }
                else
                {
                    Assert.Fail();
                }
            }
        }