// add psm to corresponding peptide
        private void AddPsmToTreeView(ProteinForTreeView protein, PsmFromTsv psm)
        {
            PeptideForTreeView peptide = null;

            if (protein.AllPeptides.ContainsKey(psm.BaseSeq)) // retrieve corresponding peptide
            {
                peptide = protein.AllPeptides[psm.BaseSeq];
            }
            else // psm doesnt match any peptide, create new peptide
            {
                peptide = new PeptideForTreeView(psm.BaseSeq, protein);
                protein.Children.Add(peptide);
                protein.AllPeptides.Add(psm.BaseSeq, peptide);
            }

            int i = 0;

            while (i < peptide.Children.Count) // O(N)
            {
                // add to sorted collection
                if (psm.Ms2ScanNumber < peptide.Children[i].ScanNo)
                {
                    peptide.Children.Insert(i, new PsmForTreeView(psm.Ms2ScanNumber, psm, peptide));
                    return;
                }
                ++i;
            }

            peptide.Children.Add(new PsmForTreeView(psm.Ms2ScanNumber, psm, peptide));
        }
        private void OnSelectionChanged()
        {
            // can select protein or peptide only at a time (not both)
            if (proteinTreeView.SelectedItem == null)
            {
                return;
            }

            // draw the selected PSM
            propertyView.Clear();

            // differentiate item click protein VS psm
            var            selection    = proteinTreeView.SelectedItem.GetType().Name;
            PsmForTreeView psmToDisplay = null;

            switch (selection)
            {
            case "ProteinForTreeView":
            {
                ProteinForTreeView selectedItem = (ProteinForTreeView)proteinTreeView.SelectedItem;
                itemsControlSampleViewModel.Data.Clear();

                if (proteinGroups[selectedItem.Accession] != null)
                {
                    DrawSequenceCoverageMap(selectedItem);
                    ChangeMapScrollViewVisibility();
                }
            }
            break;

            case "PeptideForTreeView":
            {
                // find psm with best score
                PeptideForTreeView selectedItem = (PeptideForTreeView)proteinTreeView.SelectedItem;
                psmToDisplay = selectedItem.Children.OrderByDescending(psm => psm.Psm.Score).First();
                goto case "display";
            }

            case "PsmForTreeView":
            {
                psmToDisplay = (PsmForTreeView)proteinTreeView.SelectedItem;
                goto case "display";
            }

            case "display":
            {
                mapViewer.Visibility = Visibility.Collapsed;
                legend.Visibility    = Visibility.Collapsed;
                displayPsm(psmToDisplay);
            }
            break;
            }
        }
        private void LoadProteinGroups(string filename)
        {
            try
            {
                proteinGroups.Clear();
                proteinGroupsForTreeView = new Dictionary <string, ProteinForTreeView>();

                int lineCount = 0;
                foreach (string line in File.ReadLines(filename))
                {
                    ++lineCount;
                    if (lineCount == 1)
                    {
                        continue;
                    }

                    var    spl            = line.Split('\t');
                    string accession      = spl[0];
                    string name           = spl[3];
                    string sequence       = spl[11];
                    var    uniquePeptides = spl[6].Trim().Length > 0 ? spl[6].Split('|') : new string[0];
                    var    sharedPeptides = spl[7].Trim().Length > 0 ? spl[7].Split('|') : new string[0];

                    var ptv = CreateNewProtein(accession, sequence, name, new Dictionary <string, PeptideForTreeView>(), new Dictionary <string, PeptideForTreeView>());

                    // store unique peptides
                    foreach (string pep in uniquePeptides)
                    {
                        var pepTV = new PeptideForTreeView(pep, ptv);
                        ptv.Children.Add(pepTV);
                        ptv.UniquePeptides.Add(pep, pepTV);
                        ptv.AllPeptides.Add(pep, pepTV);
                    }

                    // store shared peptides
                    foreach (string pep in sharedPeptides)
                    {
                        var pepTV = new PeptideForTreeView(pep, ptv);
                        ptv.Children.Add(pepTV);
                        ptv.SharedPeptides.Add(pep, pepTV);
                        ptv.AllPeptides.Add(pep, pepTV);
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Could not open protein groups file:\n" + e.Message);
            }
        }
        // search through psm list based on user input
        private ProteinForTreeView searchPsm(ProteinForTreeView prot, PeptideForTreeView peptide, string txt)
        {
            // create copy of peptide
            var pep = new PeptideForTreeView(peptide.DisplayName, peptide.Parent);

            foreach (var psm in peptide.Children)
            {
                if (psm.ScanNo.ToString().StartsWith(txt))
                {
                    pep.Expanded = true;
                    pep.Children.Add(psm);
                }
            }

            if (pep.Children.Count() > 0)
            {
                prot.Children.Add(pep);
            }

            return(prot);
        }
 public PsmForTreeView(int scan, PsmFromTsv psm, PeptideForTreeView parent)
 {
     Parent = parent;
     Psm    = psm;
     ScanNo = scan;
 }
        private void PDFButton_Click(object sender, RoutedEventArgs e)
        {
            object temp       = null;
            var    selections = new List <object>();

            // select best scoring psm for any peptide selections
            foreach (object selectedItem in proteinTreeView.SelectedItems)
            {
                if (selectedItem.GetType().Name.Equals("PeptideForTreeView"))
                {
                    PeptideForTreeView peptide        = (PeptideForTreeView)selectedItem;
                    PsmForTreeView     bestScoringPsm = peptide.Children.OrderByDescending(psm => psm.Psm.Score).First();
                    selections.Add(bestScoringPsm);
                    continue;
                }

                selections.Add(selectedItem);
            }

            if (proteinTreeView.SelectedItems.Count == 0)
            {
                MessageBox.Show("Please select at least one scan or coverage map to export");
            }
            else
            {
                int numberOfScansToExport = selections.Distinct().Count();

                foreach (object selectedItem in selections.Distinct())
                {
                    if (temp == null)
                    {
                        temp = selectedItem;
                    }

                    var selection = selectedItem.GetType().Name;
                    switch (selection)
                    {
                    case "ProteinForTreeView":
                    {
                        ProteinForTreeView protein  = (ProteinForTreeView)selectedItem;
                        string             myString = illegalInFileName.Replace(protein.DisplayName, "");
                        myString = myString.Length > 30 ? myString.Substring(0, 30) : myString;

                        string filePath = Path.Combine(Path.GetDirectoryName(psmFilePath), "MetaDrawExport", myString.Trim(), myString + ".pdf");
                        string dir      = Path.GetDirectoryName(filePath);

                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        DrawPdfCoverageMap(protein, mapGrid, filePath);
                    }
                    break;

                    case "PsmForTreeView":
                    {
                        PsmForTreeView psm           = (PsmForTreeView)selectedItem;
                        var            proteinFolder = illegalInFileName.Replace(psm.Parent.Parent.DisplayName, "");
                        proteinFolder = proteinFolder.Length > 30 ? proteinFolder.Substring(0, 30) : proteinFolder;

                        string peptideFolder = illegalInFileName.Replace(psm.Parent.DisplayName, "");
                        peptideFolder = peptideFolder.Length > 30 ? peptideFolder.Substring(0, 30) : peptideFolder;

                        string filePath = Path.Combine(Path.GetDirectoryName(psmFilePath), "MetaDrawExport", proteinFolder.Trim(), peptideFolder.Trim(), psm.ScanNo + ".pdf");
                        string dir      = Path.GetDirectoryName(filePath);

                        MsDataScan msDataScanToDraw = MsDataFile.GetOneBasedScan(psm.ScanNo);

                        if (!Directory.Exists(dir))
                        {
                            Directory.CreateDirectory(dir);
                        }

                        DrawPdfAnnotatedBaseSequence(psm.Psm, canvas, filePath);         // captures the annotation for the pdf
                        mainViewModel.DrawPeptideSpectralMatchPdf(msDataScanToDraw, psm.Psm, filePath, numberOfScansToExport > 1);
                    }
                    break;
                    }
                }

                RestoreDefault(temp);
                MessageBox.Show(string.Format("{0} PDFs exported", numberOfScansToExport));
            }
        }