//First method, run supervised learning with composition hypothesis.
        public MS1ResultsViewer(OpenFileDialog oFDDeconData, String ComHypoLink)
        {
            InitializeComponent();
            DeconData = oFDDeconData;
            CompositionHypothesisTabbedForm ct = new CompositionHypothesisTabbedForm();
            CompositionHypothesisList = ct.getCompHypo(ComHypoLink);

            //This part is used to show the "Please Wait" box while running the code.
            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);

            //set lable and your waiting text in this form
            try
            {
                bw.RunWorkerAsync();//this will run the DoWork code at background thread
                WaitForm.ShowDialog();//show the please wait box
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            var elementsAndMolecules = GroupingResults.GetElementsAndMolecules(ResultGroups[0]);
            ElementIDs = elementsAndMolecules.Item1;
            MoleculeNames = elementsAndMolecules.Item2;
            //ElementIDs.Clear();
            //MoleculeNames.Clear();
            //for (int i = 0; i < ResultGroups[0].Count(); i++)
            //{
            //    if (ResultGroups[0][i].PredictedComposition.ElementNames.Count() > 0)
            //    {
            //        for (int j = 0; j < ResultGroups[0][i].PredictedComposition.ElementNames.Count(); j++)
            //        {
            //            ElementIDs.Add(ResultGroups[0][i].PredictedComposition.ElementNames[j]);
            //        }
            //        for (int j = 0; j < ResultGroups[0][i].PredictedComposition.MoleculeNames.Count(); j++)
            //        {
            //            MoleculeNames.Add(ResultGroups[0][i].PredictedComposition.MoleculeNames[j]);
            //        }
            //    }
            //}

            comboBox1.DataSource = oFDDeconData.FileNames;
            comboBox1.SelectedIndex = 0;
            VolumeHistogramFileSelector.DataSource = oFDDeconData.FileNames;
            VolumeHistogramFileSelector.SelectedIndex = 0;
            ResultsGridView.DataSource = ToDataTable(ResultGroups, 0);
            ResultsGridView.ReadOnly = true;
        }
 private void oFDComposition_FileOk(object sender, CancelEventArgs e)
 {
     //First, get the path to the file.
     String currentpath = oFDComposition.FileName;
     //Second, populate the Form.
     CompositionHypothesisTabbedForm comp = new CompositionHypothesisTabbedForm();
     this.populateForm(currentpath);
 }
        private List<CompositionHypothesisEntry> obtainFalse(GlycanHypothesisCombinatorialGenerator GD, int numofLines, OpenFileDialog oFDPPMSD)
        {
            //obtain a random list with the correct bounds but the elemental composition randomized:
            List<GlycanCompositionTable> randomCT = randomList(GD.comTable);
            CompositionHypothesisTabbedForm comp = new CompositionHypothesisTabbedForm();
            GD.comTable = randomCT;
            List<CompositionHypothesisEntry> CTAns = comp.GenerateHypothesis(GD);
            List<int> rangeone = Enumerable.Range(0, CTAns.Count()).ToList();
            rangeone.Shuffle();
            //restricting them to numperlist per list, which is half of the number of lines of the original composition hypothesis divided by numofFile            Int32 numperlist = (numofLines / 2) / numofFiles;
            List<CompositionHypothesisEntry> finalAns = new List<CompositionHypothesisEntry>();
            for (int j = 0; j < numofLines; j++)
            {
                finalAns.Add(CTAns[rangeone[j]]);
            }
            Int32 Length = finalAns.Count();
            //Next, mix it with Protein Prospector MS-digest file, if it exists.
            Stream mystream = null;
            try
            {
                if ((mystream = oFDPPMSD.OpenFile()) != null)
                {
                    if (!String.IsNullOrEmpty(oFDPPMSD.FileName))
                    {
                        List<CompositionHypothesisEntry> FinalAns = getPPhypo(finalAns, finalAns, genFalsePPMSD(oFDPPMSD.FileName));
                        List<int> rangethree = Enumerable.Range(0, FinalAns.Count).ToList();
                        rangethree.Shuffle();
                        finalAns.Clear();
                        for (int i = 0; i < Length; i++)
                        {
                            finalAns.Add(FinalAns[rangethree[i]]);
                        }
                    }
                }
            }
            catch
            {
            }
            for (int i = 0; i < finalAns.Count(); i++)
            {
                finalAns[i].IsDecoy = false;
            }

            return finalAns;
        }
        private List<Peptide> genFalsePPMSD(String path)
        {
            Features fea = new Features();
            List<Peptide> PP = fea.readtablim(path);
            CompositionHypothesisTabbedForm comp = new CompositionHypothesisTabbedForm();
            String sequence = comp.GetSequenceFromCleavedPeptides(PP);
            List<int> forRandom = new List<int>
            {
               {0},{1},{2}
            };
            List<int> forlength = new List<int>
            {
               {4},{5},{6},{7},{8},{9},{10},{11}
            };

            List<Peptide> finalAns = new List<Peptide>();
            Int32 StartAA = 0;
            Int32 EndAA = 0;
            while (EndAA != sequence.Count())
            {
                forRandom.Shuffle();
                if (forRandom[0] == 1)
                {
                    //add in this fragment
                    Peptide Ans = new Peptide();
                    forlength.Shuffle();
                    Int32 length = forlength[0];
                    EndAA = StartAA + length;
                    if (EndAA > sequence.Count())
                        EndAA = sequence.Count();
                    String Fra = "";
                    Ans.StartAA = Convert.ToInt32(StartAA + 1);
                    Ans.EndAA = Convert.ToInt32(EndAA + 1);
                    for (int i = StartAA; i < EndAA; i++)
                    {
                        Fra = Fra + sequence[i];
                    }
                    StartAA = StartAA + length;
                    Ans.Selected = true;
                    Ans.PeptideIndex = 1;
                    Ans.Mass = getFragmentMass(Fra);
                    Ans.Charge = 0;
                    Ans.Modifications = "";
                    Ans.MissedCleavages = 0;
                    Ans.PreviousAA = "";
                    Ans.NextAA = "";
                    forRandom.Shuffle();
                    Ans.NumGlycosylations = Convert.ToInt32(forRandom[0]);
                    finalAns.Add(Ans);
                }
                else
                {
                    //Skip this fragment
                    forlength.Shuffle();
                    Int32 length = forlength[0];
                    EndAA = StartAA + length;
                    if (EndAA > sequence.Count())
                        EndAA = sequence.Count();
                    StartAA = StartAA + length;
                }
                if (EndAA == sequence.Count() && finalAns.Count() == 0)
                {
                    EndAA = 0;
                    StartAA = 0;
                }
            }
            return finalAns;
        }
        //This function draws the ROC curve by the TP and FP rates calculated from the score.
        private void scoreBasedGraph()
        {
            CompositionHypothesisTabbedForm comp = new CompositionHypothesisTabbedForm();
            SupervisedLearner SL = new SupervisedLearner();
            Feature Fea = readFeature(oFDAddFeatureFiles.FileName);
            //Create the list of random composition hypotesis for testing FDR.
            //ObtainTrue Position data.
            List<CompositionHypothesisEntry> CH = comp.getCompHypo(oFDCompositionTest.FileName);
            List<ResultsGroup>[] TrueDATA = SL.EvaluateFeature(oFDTest, CH, Fea);

            this.drawGraph(TrueDATA, " Loaded Features");

            //Checkbox1 is default features.####################################
            List<ResultsGroup>[] DefaultFeature = new List<ResultsGroup>[oFDTest.FileNames.Count()];
            String path = Application.StartupPath + "\\FeatureDefault.fea";
            Feature DeFea = readFeature(path);
            if (checkBox1.Checked == true)
            {
                List<ResultsGroup>[] TrueDATADefault = SL.EvaluateFeature(oFDTest, CH, DeFea);

                this.drawGraph(TrueDATADefault, " Default Features");
            }

            //################################################
            //Checkbox2 is unsupervised Learning. It is a bit different from supervised learning, so it is hard-coded here.

            if (checkBox2.Checked == true)
            {
                UnsupervisedLearner UL = new UnsupervisedLearner();
                List<ResultsGroup>[] USLTrueDATA = UL.evaluate(oFDTest, Fea);
                //ROC curve needs match to perform, so we will use the match list from Supervised learning and apply them to USLDATA.
                for (int i = 0; i < oFDTest.FileNames.Count(); i++)
                {
                    USLTrueDATA[i] = USLTrueDATA[i].OrderByDescending(b => b.DeconRow.MonoisotopicMassWeight).ToList();
                    int USllasttruematch = 0;
                    for (int j = 0; j < TrueDATA[i].Count; j++)
                    {
                        if (TrueDATA[i][j].Match == true)
                        {
                            for (int k = USllasttruematch; k < USLTrueDATA[i].Count; k++)
                            {
                                if (USLTrueDATA[i][k].DeconRow.MonoisotopicMassWeight < TrueDATA[i][j].DeconRow.MonoisotopicMassWeight)
                                {
                                    USllasttruematch = k;
                                    break;
                                }
                                if (USLTrueDATA[i][k].DeconRow.MonoisotopicMassWeight == TrueDATA[i][j].DeconRow.MonoisotopicMassWeight)
                                {
                                    USLTrueDATA[i][k].Match = true;
                                    USLTrueDATA[i][k].PredictedComposition = TrueDATA[i][j].PredictedComposition;
                                    USllasttruematch = k + 1;
                                    break;
                                }
                                if (USLTrueDATA[i][k].DeconRow.MonoisotopicMassWeight > TrueDATA[i][j].DeconRow.MonoisotopicMassWeight)
                                {
                                    USLTrueDATA[i][k].Match = false;
                                }
                            }
                        }
                    }
                }

                //Now that both of the data got their matchs, draw the graph
                this.drawGraph(USLTrueDATA, " Unsupervised Learning + Loaded Features");
            }
            //#############################unsupervised learning part ends#################

            //Finally populate the Resulting datagridview and the combobox1

            comboBox1.Invoke(new MethodInvoker(delegate
            {
                for (int i = 0; i < TF.Count; i++)
                {
                    comboBox1.Items.Add(TF[i].TableName);
                }
                comboBox1.SelectedIndex = 0;
            }));
            dataGridView1.Invoke(new MethodInvoker(delegate
            {
                dataGridView1.DataSource = TF[0];
            }));
        }
        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            //Obtain Feature
            Feature Ans = new Feature();
            SupervisedLearner SL = new SupervisedLearner();
            CompositionHypothesisTabbedForm comp = new CompositionHypothesisTabbedForm();
            List<CompositionHypothesisEntry> comhy = comp.getCompHypo(oFDComposition.FileName);
            Ans = SL.obtainFeatures(oFDTrain, comhy);

            //Write Features to File
            String path = Application.StartupPath + "\\FeatureCurrent.fea";
            FileStream FS = new FileStream(path, FileMode.Create, FileAccess.Write);
            StreamWriter Write1 = new StreamWriter(FS);
            Write1.Write(Ans.Initial + "," + Ans.numChargeStates + "," + Ans.ScanDensity + "," + Ans.numModiStates + "," + Ans.totalVolume + "," + Ans.ExpectedA + "," + Ans.CentroidScan + "," + Ans.numOfScan + "," + Ans.avgSigNoise);
            Write1.Flush();
            Write1.Close();
            FS.Close();
        }
 //Generate GlycanCompositions Hypothesis Button
 private void button5_Click(object sender, EventArgs e)
 {
     CompositionHypothesisTabbedForm opencompo = new CompositionHypothesisTabbedForm();
     opencompo.Show();
 }
 private void oFDGenerator_FileOk(object sender, CancelEventArgs e)
 {
     CompositionHypothesisTabbedForm comp = new CompositionHypothesisTabbedForm();
     button3.Enabled = true;
 }
        //This function draws the ROC curve by the TP and FP rates calculated from the score.
        private void scoreBasedGraph()
        {
            CompositionHypothesisTabbedForm comp = new CompositionHypothesisTabbedForm();

            Features FT = new Features();
            String currentpath = Application.StartupPath + "\\FeatureCurrent.fea";
            Feature Fea = FT.readFeature(currentpath);
            //Create the list of random composition hypotesis for testing FDR.
            //ObtainTrue Position data.
            this.drawGraph(AllFinalResults, "");

            //Finally populate the Resulting datagridview and the combobox1

            comboBox2.Invoke(new MethodInvoker(delegate
            {
                for (int i = 0; i < TF.Count; i++)
                {
                    comboBox2.Items.Add(TF[i].TableName);
                }
                comboBox2.SelectedIndex = 0;
            }));
            dataGridView2.Invoke(new MethodInvoker(delegate
            {
                dataGridView2.DataSource = TF[0];
            }));
        }
        //This function draws the ROC curve by the TP and FP rates calculated from the false data set
        private void DecoyDataROC()
        {
            CompositionHypothesisTabbedForm comp = new CompositionHypothesisTabbedForm();
            SupervisedLearner SL = new SupervisedLearner();
            Features featuresMenu = new Features();
            String currentpath = Application.StartupPath + "\\FeatureCurrent.fea";
            Feature currentFeatures = featuresMenu.readFeature(currentpath);
            //Create the list of random composition hypotesis for testing FDR.
            //ObtainTrue Position data.

            falseDataset fD = new falseDataset();
            List<CompositionHypothesisEntry> falseCH = fD.genFalse(oFDcposTest.FileName, CompositionHypothesisList, oFDPPMSD);
            List<ResultsGroup>[] decoyData = SL.EvaluateFeature(DeconData, falseCH, currentFeatures);

            this.drawGraph(decoyData, " ", 0);

            //Checkbox1 is default features.####################################
            List<ResultsGroup>[] DefaultFeature = new List<ResultsGroup>[DeconData.FileNames.Count()];
            String path = Application.StartupPath + "\\FeatureDefault.fea";
            Feature DeFea = featuresMenu.readFeature(path);
            if (checkBox1.Checked == true)
            {
                List<ResultsGroup>[] decoyDataDefault = SL.EvaluateFeature(DeconData, falseCH, DeFea);
                this.drawGraph(decoyDataDefault, " Default Features", 0);
            }

            //################################################
            //Checkbox2 is unsupervised Learning. It is a bit different from supervised learning, so it is hard-coded here.
            UnsupervisedLearner UL = new UnsupervisedLearner();
            if (checkBox2.Checked == true)
            {
                List<ResultsGroup>[] USLFalseDATA = UL.evaluate(DeconData, currentFeatures);
                //ROC curve needs match to perform, so we will use the match list from Supervised learning and apply them to USLDATA.
                for (int i = 0; i < DeconData.FileNames.Count(); i++)
                {
                    decoyData[i] = decoyData[i].OrderByDescending(a => a.DeconRow.MonoisotopicMassWeight).ToList();
                    USLFalseDATA[i] = USLFalseDATA[i].OrderByDescending(b => b.DeconRow.MonoisotopicMassWeight).ToList();
                    int USllasttruematch = 0;
                    for (int j = 0; j < decoyData[i].Count; j++)
                    {
                        if (decoyData[i][j].Match == true)
                        {
                            for (int k = USllasttruematch; k < USLFalseDATA[i].Count; k++)
                            {
                                if (USLFalseDATA[i][k].DeconRow.MonoisotopicMassWeight < decoyData[i][j].DeconRow.MonoisotopicMassWeight)
                                {
                                    USllasttruematch = k;
                                    break;
                                }
                                if (USLFalseDATA[i][k].DeconRow.MonoisotopicMassWeight == decoyData[i][j].DeconRow.MonoisotopicMassWeight)
                                {
                                    USLFalseDATA[i][k].Match = true;
                                    USLFalseDATA[i][k].PredictedComposition = decoyData[i][j].PredictedComposition;
                                    USllasttruematch = k + 1;
                                    break;
                                }
                                if (USLFalseDATA[i][k].DeconRow.MonoisotopicMassWeight > decoyData[i][j].DeconRow.MonoisotopicMassWeight)
                                {
                                    USLFalseDATA[i][k].Match = false;
                                }
                            }
                        }
                    }
                }

                //Now that both of the data got their matchs, draw the graph
                this.drawGraph(USLFalseDATA, " Unsupervised Learning", 0);
            }
            //#############################unsupervised learning part ends#################

            //Finally populate the Resulting datagridview and the combobox1

            comboBox2.Invoke(new MethodInvoker(delegate
            {
                for (int i = 0; i < TF.Count; i++)
                {
                    comboBox2.Items.Add(TF[i].TableName);
                }
                comboBox2.SelectedIndex = 0;
            }));
            dataGridView2.Invoke(new MethodInvoker(delegate
            {
                dataGridView2.DataSource = TF[0];
            }));
        }