public static InternalStandard GetClosestInternalStandard(QuantPoint qPt, List <InternalStandard> internalStandards)
 {
     if (internalStandards.Count == 0)
     {
         return(null);
     }
     if (internalStandards.Count == 1)
     {
         return(internalStandards.First());
     }
     if (internalStandards.Count > 1)
     {
         double           timeDiff     = double.MaxValue;
         InternalStandard holdStandard = null;
         foreach (var standard in internalStandards)
         {
             var currDiff = Math.Abs(standard.ApexRT - qPt.ApexRT);
             if (currDiff < timeDiff)
             {
                 timeDiff     = currDiff;
                 holdStandard = standard;
             }
         }
         return(holdStandard);
     }
     return(null);
 }
        public static void GetInternalStandards(Dictionary <int, Batch> batchDict, SQLiteConnection conn)
        {
            List <int> returnList   = new List <int>();
            var        queryText    = "SELECT s.GroupID FROM GCMaster_FeatureGroup_Table s WHERE s.IsInternalStandard=1";
            var        queryCommand = new SQLiteCommand(queryText, conn);
            var        reader       = queryCommand.ExecuteReader();

            while (reader.Read())
            {
                var id = int.Parse(reader["GroupID"].ToString());
                returnList.Add(id);
            }
            List <double> internalStandardIntensities = new List <double>();

            foreach (var id in returnList)
            {
                //go find that id in each replicate and make a new internal standard, also add the intensity to a list
                foreach (var batch in batchDict.Values)
                {
                    foreach (var rep in batch.replicates)
                    {
                        if (rep.quantDictionary.ContainsKey(id))
                        {
                            var internalStandardFeature = rep.quantDictionary[id];
                            internalStandardIntensities.Add(internalStandardFeature.ApexIntensity);
                            var newInternalStandard = new InternalStandard();
                            newInternalStandard.ApexIntensity    = rep.quantDictionary[id].ApexIntensity;
                            newInternalStandard.ApexRT           = internalStandardFeature.ApexRT;
                            newInternalStandard.GCMasterGroup_ID = id;
                            newInternalStandard.ReplicateName    = rep.name;
                            newInternalStandard.Feature_ID       = internalStandardFeature.FeatureID;
                            rep.internalStandards.Add(newInternalStandard);
                        }
                    }
                }
                internalStandardIntensities = internalStandardIntensities.OrderBy(x => x).ToList();
                var median = internalStandardIntensities.Median();
                foreach (var batch in batchDict.Values)
                {
                    foreach (var rep in batch.replicates)
                    {
                        foreach (var internalStandard in rep.internalStandards)
                        {
                            if (internalStandard.GCMasterGroup_ID == id)
                            {
                                internalStandard.CorrectionFactor = median / internalStandard.ApexIntensity;
                            }
                        }
                    }
                }
            }

            foreach (var batch in batchDict.Values)
            {
                foreach (var rep in batch.replicates)
                {
                    foreach (var key in rep.quantDictionary.Keys)
                    {
                        var qPt             = rep.quantDictionary[key];
                        var nearestStandard = GetClosestInternalStandard(qPt, rep.internalStandards);
                        if (nearestStandard != null)
                        {
                            qPt.ApexIntensity_Normalized = qPt.ApexIntensity * nearestStandard.CorrectionFactor;
                        }
                        else
                        {
                        }
                    }
                }
            }

            foreach (var batch in batchDict.Values)
            {
                HashSet <int> keys = new HashSet <int>();
                foreach (var rep in batch.replicates)
                {
                    foreach (var key in rep.quantDictionary.Keys)
                    {
                        keys.Add(key);
                    }
                }
                foreach (var key in keys)
                {
                    var           currAvgPt     = batch.avgQuantDict[key];
                    List <double> normalizedPts = new List <double>();
                    foreach (var rep in batch.replicates)
                    {
                        if (rep.quantDictionary.ContainsKey(key))
                        {
                            var normalizedPt = rep.quantDictionary[key].ApexIntensity_Normalized;
                            if (normalizedPt != 0)
                            {
                                normalizedPts.Add(normalizedPt);
                            }
                        }
                    }
                    if (normalizedPts.Count > 0)
                    {
                        currAvgPt.Allintensities_Normalized      = normalizedPts;
                        currAvgPt.AvgIntensity_Normalized        = normalizedPts.Average();
                        currAvgPt.AvgIntensity_Normalized_StdDev = normalizedPts.StandardDeviation();
                    }
                }
            }

            foreach (var batch in batchDict.Values)
            {
                if (batch.control != null)
                {
                    var        controlBatch = batch.control;
                    List <int> validKeys    = controlBatch.avgQuantDict.Keys.Where(x => batch.avgQuantDict.ContainsKey(x)).ToList();
                    foreach (var key in validKeys)
                    {
                        var avgValLessControl = batch.avgQuantDict[key].AvgIntensity_Normalized - controlBatch.avgQuantDict[key].AvgIntensity_Normalized;
                        batch.avgQuantDict[key].AvgIntensity_Normalized_LessControl = avgValLessControl;
                        // batch.avgQuantDict[key].AvgIntensity_LessControl_PValue = Statistics.GetWelchsTTestPValue(batch.avgQuantDict[key].AllIntensities, controlBatch.avgQuantDict[key].AllIntensities);
                        var pVal = Statistics.GetWelchsTTestPValue(batch.avgQuantDict[key].Allintensities_Normalized, controlBatch.avgQuantDict[key].Allintensities_Normalized);
                        pVal = Math.Pow(10, -pVal);
                        batch.avgQuantDict[key].AvgIntensity_Normalized_LessControl_PValue = pVal;
                    }
                }
            }
        }