private static void GetBalanceData(List <QuantitationPeak> argQuantPeaks)
        {
            QuantitationPeak processingGlycan = null;

            try
            {
                int minScanCount = 0;
                List <QuantitationPeak> qPeaks = argQuantPeaks.Where(x => x.ProtonatedPeaks.Count > 0).ToList();
                if (qPeaks.Count > 0)
                {
                    minScanCount = qPeaks.Min(x => x.ProtonatedPeaks.Count);
                    foreach (QuantitationPeak qPeak in argQuantPeaks)
                    {
                        processingGlycan = qPeak;
                        while (qPeak.ProtonatedPeaks.Count > minScanCount)
                        {
                            if (qPeak.ProtonatedPeaks[0].Item2 > qPeak.ProtonatedPeaks[qPeak.ProtonatedPeaks.Count - 1].Item2)
                            {
                                qPeak.ProtonatedPeaks.RemoveAt(qPeak.ProtonatedPeaks.Count - 1);
                            }
                            else
                            {
                                qPeak.ProtonatedPeaks.RemoveAt(0);
                            }
                        }
                    }
                }
            }
            catch
            {
                throw new InvalidDataException("GetBalanceData exception " + processingGlycan.GlycanKey);
            }
        }
        public static void MergeFullList(List<string> argFiles, string argOutputFile, bool argProtonatedResult = false)
        {
            List<string> ResultFiles = argFiles;
            List<string> AllGlycans = new List<string>();
            Dictionary<string, List<QuantitationPeak>> AllResult = new Dictionary<string, List<QuantitationPeak>>();
            for (int i = 0; i < ResultFiles.Count; i++)
            {
                //Glycan, Adduct
                Dictionary<string, Dictionary<string, List<Tuple<float, float>>>> Result = ReadFullResultCSV(ResultFiles[i]);
                //Impute data

                ImputationData(Result);
                foreach (string glycanKey in Result.Keys)
                {
                    if (!AllGlycans.Contains(glycanKey))
                    {
                        AllGlycans.Add(glycanKey);
                    }
                    if (!AllResult.ContainsKey(glycanKey))
                    {
                        AllResult.Add(glycanKey, new List<QuantitationPeak>());
                    }
                    QuantitationPeak qPeak = new QuantitationPeak(Path.GetFileNameWithoutExtension(ResultFiles[i]).Replace("_FullList", ""), glycanKey);
                    qPeak.AssignPeaks(Result[glycanKey]);

                    if (!AllResult.ContainsKey(glycanKey))
                    {
                        AllResult.Add(glycanKey, new List<QuantitationPeak>());
                    }
                    AllResult[glycanKey].Add(qPeak);
                }
            }

            //Export File
            using (StreamWriter sw = new StreamWriter(argOutputFile))
            {
                string tmpStr = "";
                tmpStr = "Glycan,";
                //Title
                foreach (string filename in ResultFiles)
                {
                    tmpStr = tmpStr + Path.GetFileNameWithoutExtension(filename) + ",";
                }
                sw.WriteLine(tmpStr);

                foreach (string GlycanKey in AllResult.Keys)
                {
                    GetBalanceData(AllResult[GlycanKey]);
                    //Protonated
                    tmpStr = GlycanKey + " Protonated,";
                    int ZeroAndNACount = 0;
                    double smallestIntensity = 9999999999999;
                    int fileIdx = 0;
                    for (int i = 0; i < AllResult[GlycanKey].Count; i++)
                    {
                        while (AllResult[GlycanKey][i].DataSetName != Path.GetFileNameWithoutExtension(ResultFiles[fileIdx]).Replace("_FullList", ""))
                        {
                            tmpStr = tmpStr + "N/A,";
                            fileIdx++;
                            ZeroAndNACount++;
                        }
                        tmpStr = tmpStr + AllResult[GlycanKey][i].TotalProtonatedIntensity.ToString("0.00") + ",";
                        if (AllResult[GlycanKey][i].TotalProtonatedIntensity == 0)
                        {
                            ZeroAndNACount++;
                        }
                        fileIdx++;

                        if (smallestIntensity >= AllResult[GlycanKey][i].TotalProtonatedIntensity && AllResult[GlycanKey][i].TotalProtonatedIntensity > 0)
                        {
                            smallestIntensity = AllResult[GlycanKey][i].TotalProtonatedIntensity;
                        }
                    }
                    while (tmpStr.Count(t => t == ',') < ResultFiles.Count + 1)
                    {
                        tmpStr += "N/A,";
                        ZeroAndNACount++;
                    }
                    if (ZeroAndNACount == ResultFiles.Count)
                    {
                        continue;
                    }
                    sw.WriteLine(tmpStr);
                    tmpStr = GlycanKey + " Protonated Ratio,";
                    fileIdx = 0;
                    for (int i = 0; i < AllResult[GlycanKey].Count; i++)
                    {
                        while (AllResult[GlycanKey][i].DataSetName != Path.GetFileNameWithoutExtension(ResultFiles[fileIdx]).Replace("_FullList", ""))
                        {
                            tmpStr = tmpStr + "N/A,";
                            fileIdx++;
                        }
                        tmpStr = tmpStr + (AllResult[GlycanKey][i].TotalProtonatedIntensity / smallestIntensity).ToString("0.00") + ",";
                        fileIdx++;
                    }
                    while (tmpStr.Count(t => t == ',') < ResultFiles.Count + 1)
                    {
                        tmpStr += "N/A,";
                    }
                    sw.WriteLine(tmpStr);
                    //All
                    tmpStr = GlycanKey + " All Adducts,";

                    smallestIntensity = 9999999999999;
                    fileIdx = 0;
                    for (int i = 0; i < AllResult[GlycanKey].Count; i++)
                    {
                        while (AllResult[GlycanKey][i].DataSetName != Path.GetFileNameWithoutExtension(ResultFiles[fileIdx]).Replace("_FullList", ""))
                        {
                            tmpStr = tmpStr + "N/A,";
                            fileIdx++;
                        }
                        tmpStr = tmpStr + AllResult[GlycanKey][i].TotalIntensity.ToString("0.00") + ",";
                        fileIdx++;

                        if (smallestIntensity >= AllResult[GlycanKey][i].TotalIntensity && AllResult[GlycanKey][i].TotalIntensity > 0)
                        {
                            smallestIntensity = AllResult[GlycanKey][i].TotalIntensity;
                        }
                    }
                    while (tmpStr.Count(t => t == ',') < ResultFiles.Count + 1)
                    {
                        tmpStr += "N/A,";
                    }
                    sw.WriteLine(tmpStr);
                    tmpStr = GlycanKey + " All Ratio,";
                    fileIdx = 0;
                    for (int i = 0; i < AllResult[GlycanKey].Count; i++)
                    {
                        while (AllResult[GlycanKey][i].DataSetName != Path.GetFileNameWithoutExtension(ResultFiles[fileIdx]).Replace("_FullList", ""))
                        {
                            tmpStr = tmpStr + "N/A,";
                            fileIdx++;
                        }
                        tmpStr = tmpStr + (AllResult[GlycanKey][i].TotalIntensity / smallestIntensity).ToString("0.00") + ",";
                        fileIdx++;
                    }
                    while (tmpStr.Count(t => t == ',') < ResultFiles.Count + 1)
                    {
                        tmpStr += "N/A,";
                    }
                    sw.WriteLine(tmpStr);
                }
            }
            //Debug
            if (argProtonatedResult)
            {
                StreamWriter swDebug = new StreamWriter(Path.GetDirectoryName(argOutputFile) + "\\MergeResult_Debug.csv");
                string tmpDebugStr = "";
                tmpDebugStr = "Glycan,";
                //Title
                foreach (string filename in ResultFiles)
                {
                    tmpDebugStr = tmpDebugStr + Path.GetFileNameWithoutExtension(filename) + "_Time," + Path.GetFileNameWithoutExtension(filename) + "_Intensity,";
                }
                swDebug.WriteLine(tmpDebugStr);
                foreach (string GlycanKey in AllResult.Keys)
                {
                    GetBalanceData(AllResult[GlycanKey]);
                    swDebug.WriteLine(GlycanKey + " Protonated");
                    int ProtonatedPeakCount = AllResult[GlycanKey][0].ProtonatedPeaks.Count;

                    for (int i = 0; i < ProtonatedPeakCount; i++)
                    {
                        tmpDebugStr = ",";
                        int fileIdx = 0;
                        for (int j = 0; j < ResultFiles.Count; j++)
                        {
                            if (AllResult[GlycanKey].Count <= fileIdx || AllResult[GlycanKey][fileIdx].DataSetName != Path.GetFileNameWithoutExtension(ResultFiles[fileIdx]).Replace("_FullList", "") || AllResult[GlycanKey][fileIdx].ProtonatedPeaks.Count == 0)
                            {
                                tmpDebugStr += "N/A,N/A,";
                            }
                            else
                            {
                                tmpDebugStr += AllResult[GlycanKey][fileIdx].ProtonatedPeaks[i].Item1.ToString("0.00") + "," + AllResult[GlycanKey][fileIdx].ProtonatedPeaks[i].Item2.ToString("0.00") + ",";
                                fileIdx++;
                            }
                        }
                        swDebug.WriteLine(tmpDebugStr);
                    }
                }
                swDebug.Close();
            }
        }
        public static void MergeFullList(List <string> argFiles, string argOutputFile, bool argProtonatedResult = false)
        {
            List <string> ResultFiles = argFiles;
            List <string> AllGlycans  = new List <string>();
            Dictionary <string, List <QuantitationPeak> > AllResult = new Dictionary <string, List <QuantitationPeak> >();

            for (int i = 0; i < ResultFiles.Count; i++)
            {
                //Glycan, Adduct
                Dictionary <string, Dictionary <string, List <Tuple <float, float> > > > Result = ReadFullResultCSV(ResultFiles[i]);
                //Impute data

                ImputationData(Result);
                foreach (string glycanKey in Result.Keys)
                {
                    if (!AllGlycans.Contains(glycanKey))
                    {
                        AllGlycans.Add(glycanKey);
                    }
                    if (!AllResult.ContainsKey(glycanKey))
                    {
                        AllResult.Add(glycanKey, new List <QuantitationPeak>());
                    }
                    QuantitationPeak qPeak = new QuantitationPeak(Path.GetFileNameWithoutExtension(ResultFiles[i]).Replace("_FullList", ""), glycanKey);
                    qPeak.AssignPeaks(Result[glycanKey]);

                    if (!AllResult.ContainsKey(glycanKey))
                    {
                        AllResult.Add(glycanKey, new List <QuantitationPeak>());
                    }
                    AllResult[glycanKey].Add(qPeak);
                }
            }



            //Export File
            using (StreamWriter sw = new StreamWriter(argOutputFile))
            {
                string tmpStr = "";
                tmpStr = "Glycan,";
                //Title
                foreach (string filename in ResultFiles)
                {
                    tmpStr = tmpStr + Path.GetFileNameWithoutExtension(filename) + ",";
                }
                sw.WriteLine(tmpStr);

                foreach (string GlycanKey in AllResult.Keys)
                {
                    GetBalanceData(AllResult[GlycanKey]);
                    //Protonated
                    tmpStr = GlycanKey + " Protonated,";
                    int    ZeroAndNACount    = 0;
                    double smallestIntensity = 9999999999999;
                    int    fileIdx           = 0;
                    for (int i = 0; i < AllResult[GlycanKey].Count; i++)
                    {
                        while (AllResult[GlycanKey][i].DataSetName != Path.GetFileNameWithoutExtension(ResultFiles[fileIdx]).Replace("_FullList", ""))
                        {
                            tmpStr = tmpStr + "N/A,";
                            fileIdx++;
                            ZeroAndNACount++;
                        }
                        tmpStr = tmpStr + AllResult[GlycanKey][i].TotalProtonatedIntensity.ToString("0.00") + ",";
                        if (AllResult[GlycanKey][i].TotalProtonatedIntensity == 0)
                        {
                            ZeroAndNACount++;
                        }
                        fileIdx++;

                        if (smallestIntensity >= AllResult[GlycanKey][i].TotalProtonatedIntensity && AllResult[GlycanKey][i].TotalProtonatedIntensity > 0)
                        {
                            smallestIntensity = AllResult[GlycanKey][i].TotalProtonatedIntensity;
                        }
                    }
                    while (tmpStr.Count(t => t == ',') < ResultFiles.Count + 1)
                    {
                        tmpStr += "N/A,";
                        ZeroAndNACount++;
                    }
                    if (ZeroAndNACount == ResultFiles.Count)
                    {
                        continue;
                    }
                    sw.WriteLine(tmpStr);
                    tmpStr  = GlycanKey + " Protonated Ratio,";
                    fileIdx = 0;
                    for (int i = 0; i < AllResult[GlycanKey].Count; i++)
                    {
                        while (AllResult[GlycanKey][i].DataSetName != Path.GetFileNameWithoutExtension(ResultFiles[fileIdx]).Replace("_FullList", ""))
                        {
                            tmpStr = tmpStr + "N/A,";
                            fileIdx++;
                        }
                        tmpStr = tmpStr + (AllResult[GlycanKey][i].TotalProtonatedIntensity / smallestIntensity).ToString("0.00") + ",";
                        fileIdx++;
                    }
                    while (tmpStr.Count(t => t == ',') < ResultFiles.Count + 1)
                    {
                        tmpStr += "N/A,";
                    }
                    sw.WriteLine(tmpStr);
                    //All
                    tmpStr = GlycanKey + " All Adducts,";

                    smallestIntensity = 9999999999999;
                    fileIdx           = 0;
                    for (int i = 0; i < AllResult[GlycanKey].Count; i++)
                    {
                        while (AllResult[GlycanKey][i].DataSetName != Path.GetFileNameWithoutExtension(ResultFiles[fileIdx]).Replace("_FullList", ""))
                        {
                            tmpStr = tmpStr + "N/A,";
                            fileIdx++;
                        }
                        tmpStr = tmpStr + AllResult[GlycanKey][i].TotalIntensity.ToString("0.00") + ",";
                        fileIdx++;

                        if (smallestIntensity >= AllResult[GlycanKey][i].TotalIntensity && AllResult[GlycanKey][i].TotalIntensity > 0)
                        {
                            smallestIntensity = AllResult[GlycanKey][i].TotalIntensity;
                        }
                    }
                    while (tmpStr.Count(t => t == ',') < ResultFiles.Count + 1)
                    {
                        tmpStr += "N/A,";
                    }
                    sw.WriteLine(tmpStr);
                    tmpStr  = GlycanKey + " All Ratio,";
                    fileIdx = 0;
                    for (int i = 0; i < AllResult[GlycanKey].Count; i++)
                    {
                        while (AllResult[GlycanKey][i].DataSetName != Path.GetFileNameWithoutExtension(ResultFiles[fileIdx]).Replace("_FullList", ""))
                        {
                            tmpStr = tmpStr + "N/A,";
                            fileIdx++;
                        }
                        tmpStr = tmpStr + (AllResult[GlycanKey][i].TotalIntensity / smallestIntensity).ToString("0.00") + ",";
                        fileIdx++;
                    }
                    while (tmpStr.Count(t => t == ',') < ResultFiles.Count + 1)
                    {
                        tmpStr += "N/A,";
                    }
                    sw.WriteLine(tmpStr);
                }
            }
            //Debug
            if (argProtonatedResult)
            {
                StreamWriter swDebug     = new StreamWriter(Path.GetDirectoryName(argOutputFile) + "\\MergeResult_Debug.csv");
                string       tmpDebugStr = "";
                tmpDebugStr = "Glycan,";
                //Title
                foreach (string filename in ResultFiles)
                {
                    tmpDebugStr = tmpDebugStr + Path.GetFileNameWithoutExtension(filename) + "_Time," + Path.GetFileNameWithoutExtension(filename) + "_Intensity,";
                }
                swDebug.WriteLine(tmpDebugStr);
                foreach (string GlycanKey in AllResult.Keys)
                {
                    GetBalanceData(AllResult[GlycanKey]);
                    swDebug.WriteLine(GlycanKey + " Protonated");
                    int ProtonatedPeakCount = AllResult[GlycanKey][0].ProtonatedPeaks.Count;


                    for (int i = 0; i < ProtonatedPeakCount; i++)
                    {
                        tmpDebugStr = ",";
                        int fileIdx = 0;
                        for (int j = 0; j < ResultFiles.Count; j++)
                        {
                            if (AllResult[GlycanKey].Count <= fileIdx || AllResult[GlycanKey][fileIdx].DataSetName != Path.GetFileNameWithoutExtension(ResultFiles[fileIdx]).Replace("_FullList", "") || AllResult[GlycanKey][fileIdx].ProtonatedPeaks.Count == 0)
                            {
                                tmpDebugStr += "N/A,N/A,";
                            }
                            else
                            {
                                tmpDebugStr += AllResult[GlycanKey][fileIdx].ProtonatedPeaks[i].Item1.ToString("0.00") + "," + AllResult[GlycanKey][fileIdx].ProtonatedPeaks[i].Item2.ToString("0.00") + ",";
                                fileIdx++;
                            }
                        }
                        swDebug.WriteLine(tmpDebugStr);
                    }
                }
                swDebug.Close();
            }
        }