public void TestPrefixAndSuffixMatches()
 {
     string[] peptides =
     {
         "LATQSNEITIPVTFESRAqLGGPEAAK",
         "LATqSNEITIPVTFESRAQLGGPEAAK",
         "LATQSnEITIPVTFESRAQLGGPEAAK",
         "LATQSnEITIPVTFESRAQLGGPEAAKSDETAAK",
         "LATQSNEITIPVTFESRAQLGGPEAAK",
         "LATQSNEITIPVTFESRAQLGGPEAAKSDETAAK",
         "LATQSNEITIPVTFESRAqLGGPEAAKSDETAAK",
     };
     var uniquePrefixGenerator = new UniquePrefixGenerator(peptides.Select(
         sequence => new Tuple<string, bool>(sequence, true)), 3);
     Dictionary<string, string> peptideAbbreviations = new Dictionary<string, string>();
     foreach (string peptide in peptides)
     {
         string abbreviation = uniquePrefixGenerator.GetUniquePrefix(peptide, true);
         Assert.IsTrue(abbreviation.Length <= peptide.Length);
         peptideAbbreviations.Add(peptide, abbreviation);
     }
     CollectionAssert.AllItemsAreUnique(peptideAbbreviations.Values);
 }
 private void UpdateGraph()
 {
     if (!IsHandleCreated)
     {
         return;
     }
     zedGraphControl.GraphPane.GraphObjList.Clear();
     zedGraphControl.GraphPane.CurveList.Clear();
     _barGraph = null;
     _rows = null;
     var points = new PointPairList();
     var groupComparisonModel = FoldChangeBindingSource.GroupComparisonModel;
     var groupComparisonDef = groupComparisonModel.GroupComparisonDef;
     var document = groupComparisonModel.Document;
     var sequences = new List<Tuple<string, bool>>();
     foreach (var nodePep in document.Molecules)
         sequences.Add(new Tuple<string, bool>(nodePep.RawTextId, nodePep.IsProteomic));
     var uniquePrefixGenerator = new UniquePrefixGenerator(sequences, 3);
     var textLabels = new List<string>();
     var rows = _bindingListSource.OfType<RowItem>()
         .Select(rowItem => rowItem.Value)
         .OfType<FoldChangeBindingSource.FoldChangeRow>()
         .ToArray();
     bool showLabelType = rows.Select(row => row.IsotopeLabelType).Distinct().Count() > 1;
     bool showMsLevel = rows.Select(row => row.MsLevel).Distinct().Count() > 1;
     bool showGroup = rows.Select(row => row.Group).Distinct().Count() > 1;
     foreach (var row in rows)
     {
         var foldChangeResult = row.FoldChangeResult;
         double error = Math.Log(foldChangeResult.MaxFoldChange/foldChangeResult.FoldChange, 2.0);
         var point = MeanErrorBarItem.MakePointPair(points.Count, foldChangeResult.Log2FoldChange, error);
         points.Add(point);
         string label;
         if (null != row.Peptide)
         {
             label = uniquePrefixGenerator.GetUniquePrefix(row.Peptide.GetDocNode().RawTextId, row.Peptide.GetDocNode().IsProteomic);
         }
         else
         {
             label = row.Protein.Name;
         }
         if (showMsLevel && row.MsLevel.HasValue)
         {
             label += " MS" + row.MsLevel; // Not L10N;
         }
         if (showLabelType && row.IsotopeLabelType != null)
         {
             label += " (" + row.IsotopeLabelType.Title + ")"; // Not L10N
         }
         if (showGroup && !Equals(row.Group, default(GroupIdentifier)))
         {
             label += " " + row.Group; // Not L10N
         }
         textLabels.Add(label);
         if (IsSelected(row))
         {
             double y, height;
             if (foldChangeResult.Log2FoldChange >= 0)
             {
                 y = foldChangeResult.Log2FoldChange + error;
                 height = y;
             }
             else
             {
                 y = 0;
                 height = error - foldChangeResult.Log2FoldChange;
             }
             zedGraphControl.GraphPane.GraphObjList.Add(new BoxObj(point.X + .5, y, .99, height)
             {
                 ZOrder = ZOrder.E_BehindCurves,
                 IsClippedToChartRect = true
             });
         }
     }
     zedGraphControl.GraphPane.XAxis.Title.Text = groupComparisonDef.PerProtein ? GroupComparisonStrings.FoldChangeBarGraph_UpdateGraph_Protein : GroupComparisonStrings.FoldChangeBarGraph_UpdateGraph_Peptide;
     zedGraphControl.GraphPane.YAxis.Title.Text = GroupComparisonStrings.FoldChangeBarGraph_UpdateGraph_Log_2_Fold_Change;
     var barGraph = new MeanErrorBarItem(null, points, Color.Black, Color.Blue);
     zedGraphControl.GraphPane.CurveList.Add(barGraph);
     zedGraphControl.GraphPane.XAxis.Type = AxisType.Text;
     zedGraphControl.GraphPane.XAxis.Scale.TextLabels = textLabels.ToArray();
     _axisLabelScaler.ScaleAxisLabels();
     zedGraphControl.GraphPane.AxisChange();
     zedGraphControl.Invalidate();
     _barGraph = barGraph;
     _rows = rows;
 }
Exemple #3
0
            // ReSharper disable PossibleMultipleEnumeration
            protected GraphData(SrmDocument document, TransitionGroupDocNode selectedGroup, PeptideGroupDocNode selectedProtein,
                                int?iResult, DisplayTypeChrom displayType, GraphValues.IRetentionTimeTransformOp retentionTimeTransformOp,
                                PaneKey paneKey)
            {
                RetentionTimeTransformOp = retentionTimeTransformOp;
                // Determine the shortest possible unique ID for each peptide or molecule
                var sequences = new List <Tuple <string, bool> >();

                foreach (var nodePep in document.Molecules)
                {
                    sequences.Add(new Tuple <string, bool>(nodePep.ModifiedTarget.DisplayName, nodePep.IsProteomic));
                }
                var uniquePrefixGenerator = new UniquePrefixGenerator(sequences, 3);

                int pointListCount = 0;
                var dictTypeToSet  = new Dictionary <IsotopeLabelType, int>();

                bool onePointPerPeptide = PeptideOrder == SummaryPeptideOrder.document &&
                                          null != paneKey.IsotopeLabelType;
                // Figure out how many point lists to create
                bool displayTotals = (displayType == DisplayTypeChrom.total);

                if (displayTotals)
                {
                    foreach (var nodeGroup in document.MoleculeTransitionGroups)
                    {
                        if (!paneKey.IncludesTransitionGroup(nodeGroup))
                        {
                            continue;
                        }
                        IsotopeLabelType labelType = nodeGroup.TransitionGroup.LabelType;
                        if (!dictTypeToSet.ContainsKey(labelType))
                        {
                            dictTypeToSet.Add(labelType, pointListCount++);
                        }
                    }
                }
                else
                {
                    foreach (var nodeGroup in document.MoleculeTransitionGroups)
                    {
                        if (!paneKey.IncludesTransitionGroup(nodeGroup))
                        {
                            continue;
                        }
                        pointListCount = Math.Max(pointListCount, GraphChromatogram.GetDisplayTransitions(nodeGroup, displayType).Count());
                    }
                }

                // Build the list of points to show.
                var listPoints = new List <GraphPointData>();

                foreach (PeptideGroupDocNode nodeGroupPep in document.MoleculeGroups)
                {
                    if (AreaGraphController.AreaScope == AreaScope.protein)
                    {
                        if (!ReferenceEquals(nodeGroupPep, selectedProtein))
                        {
                            continue;
                        }
                    }
                    foreach (PeptideDocNode nodePep in nodeGroupPep.Children)
                    {
                        bool addBlankPoint = onePointPerPeptide &&
                                             !nodePep.TransitionGroups.Any(paneKey.IncludesTransitionGroup);
                        foreach (TransitionGroupDocNode nodeGroup in nodePep.Children)
                        {
                            var path = new IdentityPath(nodeGroupPep.PeptideGroup,
                                                        nodePep.Peptide, nodeGroup.TransitionGroup);
                            var graphPointData = new GraphPointData(nodePep, nodeGroup, path);
                            if (addBlankPoint || paneKey.IncludesTransitionGroup(nodeGroup))
                            {
                                listPoints.Add(graphPointData);
                            }
                            if (addBlankPoint)
                            {
                                break;
                            }
                        }
                    }
                }

                // Sort into correct order
                var peptideOrder = PeptideOrder;

                if (peptideOrder == SummaryPeptideOrder.time)
                {
                    if (displayTotals)
                    {
                        listPoints.Sort(ComparePeptideTimes);
                    }
                    else
                    {
                        listPoints.Sort(CompareGroupTimes);
                    }
                }
                else if (peptideOrder == SummaryPeptideOrder.area)
                {
                    listPoints.Sort(CompareGroupAreas);
                }
                else if (peptideOrder == SummaryPeptideOrder.mass_error)
                {
                    listPoints.Sort(CompareGroupMassErrors);
                }

                // Init calculated values
                var    pointPairLists = new List <PointPairList>();
                var    labels         = new List <string>();
                var    xscalePaths    = new List <IdentityPath>();
                double maxY           = 0;
                double minY           = double.MaxValue;
                int    selectedIndex  = -1;

                for (int i = 0; i < pointListCount; i++)
                {
                    pointPairLists.Add(new PointPairList());
                }

                // Calculate lists and values
                PeptideDocNode nodePepCurrent = null;
                int            chargeCount    = 0;
                var            chargeCurrent  = Adduct.EMPTY;

                foreach (var dataPoint in listPoints)
                {
                    var nodePep   = dataPoint.NodePep;
                    var nodeGroup = dataPoint.NodeGroup;
                    if (!ReferenceEquals(nodePep, nodePepCurrent))
                    {
                        nodePepCurrent = nodePep;

                        chargeCount   = GetChargeCount(nodePep);
                        chargeCurrent = Adduct.EMPTY;
                    }

                    bool addLabel = !displayTotals;
                    if (displayTotals && !Equals(nodeGroup.TransitionGroup.PrecursorAdduct, chargeCurrent))
                    {
                        LevelPointPairLists(pointPairLists);
                        addLabel = true;
                    }
                    chargeCurrent = nodeGroup.TransitionGroup.PrecursorAdduct;

                    var transitionGroup = nodeGroup.TransitionGroup;
                    int iGroup          = labels.Count;

                    if (addLabel)
                    {
                        string label = uniquePrefixGenerator.GetUniquePrefix(nodePep.ModifiedTarget.DisplayName, nodePep.IsProteomic) +
                                       (chargeCount > 1
                                            ? Transition.GetChargeIndicator(transitionGroup.PrecursorAdduct)
                                            : string.Empty);
                        if (!displayTotals && null == paneKey.IsotopeLabelType)
                        {
                            label += transitionGroup.LabelTypeText;
                        }
                        if (peptideOrder == SummaryPeptideOrder.time)
                        {
                            label += string.Format(@" ({0:F01})", displayTotals ?
                                                   dataPoint.TimePepCharge : dataPoint.TimeGroup);
                        }
                        labels.Add(label);
                        xscalePaths.Add(dataPoint.IdentityPath);
                    }

                    double groupMaxY = 0;
                    double groupMinY = double.MaxValue;

                    // ReSharper disable DoNotCallOverridableMethodsInConstructor
                    int?resultIndex = iResult.HasValue && iResult >= 0 ? iResult : null;
                    if (RTLinearRegressionGraphPane.ShowReplicate == ReplicateDisplay.best && nodePep != null)
                    {
                        resultIndex = null;
                        int iBest = nodePep.BestResult;
                        if (iBest != -1)
                        {
                            resultIndex = iBest;
                        }
                    }
                    if (displayTotals)
                    {
                        var labelType = nodeGroup.TransitionGroup.LabelType;
                        if (dictTypeToSet.ContainsKey(labelType))
                        {
                            if (paneKey.IncludesTransitionGroup(nodeGroup))
                            {
                                pointPairLists[dictTypeToSet[labelType]].Add(CreatePointPair(iGroup, nodeGroup,
                                                                                             ref groupMaxY, ref groupMinY,
                                                                                             resultIndex));
                            }
                            else
                            {
                                pointPairLists[dictTypeToSet[labelType]].Add(PointPairMissing(iGroup));
                            }
                        }
                    }
                    else
                    {
                        if (paneKey.IncludesTransitionGroup(nodeGroup))
                        {
                            var nodeTrans = GraphChromatogram.GetDisplayTransitions(nodeGroup, displayType).ToArray();
                            for (int i = 0; i < pointListCount; i++)
                            {
                                var pointPairList = pointPairLists[i];
                                pointPairList.Add(i >= nodeTrans.Length
                                                      ? CreatePointPairMissing(iGroup)
                                                      : CreatePointPair(iGroup, nodeTrans[i], ref groupMaxY,
                                                                        ref groupMinY,
                                                                        resultIndex));
                            }
                        }
                        else
                        {
                            for (int i = 0; i < pointListCount; i++)
                            {
                                pointPairLists[i].Add(CreatePointPairMissing(iGroup));
                            }
                        }
                    }
                    // ReSharper restore DoNotCallOverridableMethodsInConstructor

                    // Save the selected index and its y extent
                    if (ReferenceEquals(selectedGroup, nodeGroup))
                    {
                        selectedIndex = labels.Count - 1;
                        SelectedMaxY  = groupMaxY;
                        SelectedMinY  = groupMinY;
                    }
                    // If multiple groups in the selection, make sure y extent is max of them
                    else if (selectedIndex == labels.Count - 1)
                    {
                        SelectedMaxY = Math.Max(groupMaxY, SelectedMaxY);
                        SelectedMinY = Math.Min(groupMinY, SelectedMinY);
                    }
                    maxY = Math.Max(maxY, groupMaxY);
                    minY = Math.Min(minY, groupMinY);
                }

                PointPairLists = pointPairLists;
                Labels         = labels.ToArray();
                XScalePaths    = xscalePaths.ToArray();
                SelectedIndex  = selectedIndex;
                MaxY           = maxY;
                if (minY != double.MaxValue)
                {
                    MinY = minY;
                }
            }
        public void TestUniquePrefix()
        {
            var testSequences = new[,]
            {
                // Original name on left, expected result on right.
                {null, null},
                {"", ""},
                {"A", "A"},
                {"A", "A"}, // duplicate name (on purpose)
                {"AB", "AB"},
                {"ABC", "ABC"},
                {"ABCD", "ABCD"},
                {"ABCDE", "ABCDE"},
                {"ABCDEF", "ABCDEF"},
                {"ABCDEFG", "ABCDEFG"},
                {"ABCDEFG", "ABCDEFG"}, // duplicate name (on purpose)
                {"ABCDEFGH", "ABC…FGH"},
                {"ABCDEFGHI", "ABC…GHI"},

                {"ABCE", "ABCE"},

                {"ABDEFGHI", "ABD…"},

                {"ABEFGHI", "ABEFGHI"},
                {"ABEFGHIJ", "ABE…HIJ"},
                {"ABEFHI", "ABEFHI"},

                {"ABFFFGHI", "ABF(5)"},
                {"ABFFFFGHI", "ABF(6)"},
                {"ABFFFFAFGHI", "ABF…FA…"},
                {"ABFFFAFFGHI", "ABF…A…"},

                {"ABGAABAABAGHI", "ABG…B…B…"},
                {"ABGAAbAABAGHI", "ABG…b…B…"},
                {"ABGAABAAbAGHI", "ABG…B…b…"},
                {"ABGAAB[80]AAB[99]AGHI", "ABG…b…b…"}
            };
            var testCustomIons = new[,]
            {
                // Original name on left, expected result on right.
                {"C32:0", "C32:0"},
                {"C32:1", "C32:1"},
                {"C32:2", "C32:2"},
                {"C32:2", "C32:2"}, // Duplicated on purpose
                {"C30:0", "C30:0"},
                {"C[30]:0", "C[30]:0"},
                {"C[400]:0", "C[4…"},
                {"C12:0 fish breath", "C12…"},
                {"C15:0 fish breath", "C15(14)"},
                {"C15:0 doggy breath", "C15(15)"},
                {"C16:0 fishy breath", "C16…f…"},
                {"C16:0 doggy breath", "C16…d…"},
                {"C14", "C14"},
                {"C14:1", "C14:1"},
                {"C14:1-OH", "C14:1…"},
                {"C14:2", "C14:2"},
                {"C14:2-OH", "C14:2…"}
            };

            for (var loop = 2; loop > 0; loop--)
            {
                var strings = new List<Tuple<string, bool>>();
                var commontext = (loop == 1) ? String.Empty : "Delta Niner Foxtrot "; // non-peptides should strip leading common text

                for (int i = 0; i < testSequences.GetLength(0); i++)
                {
                    strings.Add(new Tuple<string, bool>(testSequences[i, 0], true));
                }
                for (int i = 0; i < testCustomIons.GetLength(0); i++)
                {
                    strings.Add(new Tuple<string, bool>(commontext + testCustomIons[i, 0], false));
                }
                var prefixGenerator = new UniquePrefixGenerator(strings, 3);
                for (int i = 0; i < testSequences.GetLength(0); i++)
                {
                    var expected = testSequences[i, 1];
                    var uniquePrefix = prefixGenerator.GetUniquePrefix(testSequences[i, 0], true);
                    Assert.AreEqual(expected, uniquePrefix);
                }
                for (int i = 0; i < testCustomIons.GetLength(0); i++)
                {
                    var expected = testCustomIons[i, 1];
                    var uniquePrefix = prefixGenerator.GetUniquePrefix(commontext + testCustomIons[i, 0], false);
                    Assert.AreEqual(expected, uniquePrefix);
                }
            }
        }
        /// <summary>
        /// Display summed transitions for multiple selected peptides.
        /// </summary>
        private void DisplayPeptides(IRegressionFunction timeRegressionFunction,
            ChromatogramSet chromatograms,
            float mzMatchTolerance,
            int countLabelTypes,
            IList<PeptideDocNode> peptideDocNodes,
            ref double bestStartTime,
            ref double bestEndTime,
            out double leftPeakWidth,
            out double rightPeakWidth)
        {
            leftPeakWidth = 0;
            rightPeakWidth = 0;

            // Construct and add graph items for all relevant transition groups.
            float fontSize = FontSize;
            int lineWidth = LineWidth;
            var chromGroupInfos = ChromGroupInfos;
            var lookupChromGroupInfoIndex = new Dictionary<int, int>(_nodeGroups.Length);
            for (int i = 0; i < _nodeGroups.Length; i++)
                lookupChromGroupInfoIndex[_nodeGroups[i].Id.GlobalIndex] = i;

            // Generate a unique short identifier for each peptide.
            var peptideNames = new Tuple<string,bool>[peptideDocNodes.Count];
            for (int i = 0; i < peptideDocNodes.Count; i++)
                peptideNames[i] = new Tuple<string,bool>(peptideDocNodes[i].RawTextId, peptideDocNodes[i].IsProteomic);
            var uniqueNames = new UniquePrefixGenerator(peptideNames, 3);

            var displayPeptides = new List<DisplayPeptide>();
            for (int peptideIndex = 0; peptideIndex < peptideDocNodes.Count; peptideIndex++)
            {
                var peptideDocNode = peptideDocNodes[peptideIndex];
                TransitionChromInfo bestPeakInfo = null;
                ChromatogramInfo sumInfo = null;
                float maxPeakHeight = float.MinValue;

                foreach (var precursor in peptideDocNode.TransitionGroups)
                {
                    int indexInfo;
                    if (!lookupChromGroupInfoIndex.TryGetValue(precursor.Id.GlobalIndex, out indexInfo))
                        continue;
                    var chromGroupInfo = chromGroupInfos[indexInfo];
                    if (chromGroupInfo == null)
                        continue;
                    ChromFileInfoId fileId = chromatograms.FindFile(chromGroupInfo);
                    foreach (var nodeTran in precursor.Transitions)
                    {
                        var info = chromGroupInfo.GetTransitionInfo((float)nodeTran.Mz, mzMatchTolerance);
                        if (info == null)
                            continue;
                        if (sumInfo == null)
                            sumInfo = info;
                        else
                        {
                            float[] sumTimes;
                            float[] sumIntensities;
                            if (!AddTransitions.Add(
                                info.Times, info.Intensities, sumInfo.Times, sumInfo.Intensities,
                                out sumTimes, out sumIntensities))
                                continue;
                            sumInfo = new ChromatogramInfo(sumTimes, sumIntensities);
                        }

                        // Keep track of which chromatogram owns the tallest member of
                        // the peak on the document tree.
                        var transitionChromInfo = GetTransitionChromInfo(nodeTran, _chromIndex, fileId, 0);
                        if (transitionChromInfo == null)
                            continue;

                        if (transitionChromInfo.Height > maxPeakHeight)
                        {
                            maxPeakHeight = transitionChromInfo.Height;
                            bestPeakInfo = transitionChromInfo;
                        }
                    }
                }

                if (sumInfo != null && bestPeakInfo != null)
                {
                    displayPeptides.Add(new DisplayPeptide
                    {
                        PeptideIndex = peptideIndex,
                        SumInfo = sumInfo,
                        BestPeakInfo = bestPeakInfo
                    });
                }
            }

            // Order the peptides by height of best peak.
            displayPeptides = displayPeptides.OrderByDescending(e => e.BestPeakInfo.Height).ToList();

            // Display only the top peptides.
            int lastPeptideIndex = Math.Min(MaxPeptidesDisplayed, displayPeptides.Count);
            var graphItems = new List<ChromGraphItem>();
            for (int i = lastPeptideIndex-1; i >= 0; i--) // smallest peaks first for good z-ordering in graph
            {
                var bestPeakInfo = displayPeptides[i].BestPeakInfo;
                var sumInfo = displayPeptides[i].SumInfo;
                var peptideDocNode = peptideDocNodes[displayPeptides[i].PeptideIndex];

                // Intersect best peak with summed transition.
                if (bestPeakInfo != null && sumInfo.Times.Length > 0)
                {
                    float startRetentionTime = Math.Max(bestPeakInfo.StartRetentionTime, sumInfo.Times[0]);
                    float endRetentionTime = Math.Min(bestPeakInfo.EndRetentionTime, sumInfo.Times[sumInfo.Times.Length - 1]);
                    if (endRetentionTime > startRetentionTime)
                    {
                        if (bestStartTime > startRetentionTime)
                        {
                            bestStartTime = startRetentionTime;
                            leftPeakWidth = endRetentionTime - startRetentionTime;
                        }
                        if (bestEndTime < endRetentionTime)
                        {
                            bestEndTime = endRetentionTime;
                            rightPeakWidth = endRetentionTime - startRetentionTime;
                        }
                        bestPeakInfo = new TransitionChromInfo(startRetentionTime, endRetentionTime);
                    }
                }

                // Get peptide graph color from SequenceTree.
                var peptideGraphInfo = _stateProvider.GetPeptideGraphInfo(peptideDocNode);
                Color color = peptideGraphInfo.Color;

                sumInfo.Transform(Transform);
                bool[] annotateAll = new bool[sumInfo.NumPeaks];
                ChromGraphItem graphItem = new ChromGraphItem(null,
                    null,
                    sumInfo,
                    bestPeakInfo,
                    timeRegressionFunction,
                    annotateAll,
                    null,
                    0,
                    false,
                    false,
                    0,
                    color,
                    fontSize,
                    lineWidth)
                {
                    CurveAnnotation = uniqueNames.GetUniquePrefix(peptideDocNode.RawTextId, peptideDocNode.IsProteomic),
                    IdPath = _groupPaths[displayPeptides[i].PeptideIndex],
                    GraphInfo = peptideGraphInfo
                };
                if (peptideGraphInfo.IsSelected)
                    graphItems.Insert(0, graphItem);
                else
                    graphItems.Add(graphItem);
            }

            foreach (var graphItem in graphItems)
            {
                var curveItem = _graphHelper.AddChromatogram(new PaneKey(), graphItem);
                // Make the fill color under the curve more opaque if the curve is selected.
                var fillAlpha = graphItem.GraphInfo.IsSelected ? 60 : 15;
                ((LineItem)curveItem).Line.Fill = new Fill(Color.FromArgb(fillAlpha, graphItem.GraphInfo.Color));
            }
        }
            protected GraphData(SrmDocument document, TransitionGroupDocNode selectedGroup, PeptideGroupDocNode selectedProtein, 
                int? iResult, DisplayTypeChrom displayType, GraphValues.IRetentionTimeTransformOp retentionTimeTransformOp,
                PaneKey paneKey)
            {
                RetentionTimeTransformOp = retentionTimeTransformOp;
                // Determine the shortest possible unique ID for each peptide or molecule
                var sequences = new List<Tuple<string, bool>>();
                foreach (var nodePep in document.Molecules)
                    sequences.Add(new Tuple<string, bool>(nodePep.RawTextId, nodePep.IsProteomic));
                var uniquePrefixGenerator = new UniquePrefixGenerator(sequences, 3);

                int pointListCount = 0;
                var dictTypeToSet = new Dictionary<IsotopeLabelType, int>();

                bool onePointPerPeptide = PeptideOrder == SummaryPeptideOrder.document &&
                                          null != paneKey.IsotopeLabelType;
                // Figure out how many point lists to create
                bool displayTotals = (displayType == DisplayTypeChrom.total);
                if (displayTotals)
                {
                    foreach (var nodeGroup in document.MoleculeTransitionGroups)
                    {
                        if (!paneKey.IncludesTransitionGroup(nodeGroup))
                        {
                            continue;
                        }
                        IsotopeLabelType labelType = nodeGroup.TransitionGroup.LabelType;
                        if (!dictTypeToSet.ContainsKey(labelType))
                            dictTypeToSet.Add(labelType, pointListCount++);
                    }
                }
                else
                {
                    foreach (var nodeGroup in document.MoleculeTransitionGroups)
                    {
                        if (!paneKey.IncludesTransitionGroup(nodeGroup))
                        {
                            continue;
                        }
                        pointListCount = Math.Max(pointListCount, GraphChromatogram.GetDisplayTransitions(nodeGroup, displayType).Count());
                    }
                }

                // Build the list of points to show.
                var listPoints = new List<GraphPointData>();
                foreach (PeptideGroupDocNode nodeGroupPep in document.MoleculeGroups)
                {
                    if (AreaGraphController.AreaScope == AreaScope.protein)
                    {
                        if (!ReferenceEquals(nodeGroupPep, selectedProtein))
                            continue;
                    }
                    foreach (PeptideDocNode nodePep in nodeGroupPep.Children)
                    {
                        bool addBlankPoint = onePointPerPeptide &&
                                             !nodePep.TransitionGroups.Any(paneKey.IncludesTransitionGroup);
                        foreach (TransitionGroupDocNode nodeGroup in nodePep.Children)
                        {
                            var path = new IdentityPath(nodeGroupPep.PeptideGroup,
                                                        nodePep.Peptide, nodeGroup.TransitionGroup);
                            var graphPointData = new GraphPointData(nodePep, nodeGroup, path);
                            if (addBlankPoint || paneKey.IncludesTransitionGroup(nodeGroup))
                            {
                                listPoints.Add(graphPointData);
                            }
                            if (addBlankPoint)
                            {
                                break;
                            }
                        }
                    }
                }

                // Sort into correct order
                var peptideOrder = PeptideOrder;
                if (peptideOrder == SummaryPeptideOrder.time)
                {
                    if (displayTotals)
                        listPoints.Sort(ComparePeptideTimes);
                    else
                        listPoints.Sort(CompareGroupTimes);
                }
                else if (peptideOrder == SummaryPeptideOrder.area)
                {
                    listPoints.Sort(CompareGroupAreas);
                }

                // Init calculated values
                var pointPairLists = new List<PointPairList>();
                var labels = new List<string>();
                var xscalePaths = new List<IdentityPath>();
                double maxY = 0;
                double minY = double.MaxValue;
                int selectedIndex = -1;

                for (int i = 0; i < pointListCount; i++)
                    pointPairLists.Add(new PointPairList());

                // Calculate lists and values
                PeptideDocNode nodePepCurrent = null;
                int chargeCount = 0, chargeCurrent = 0;
                foreach (var dataPoint in listPoints)
                {
                    var nodePep = dataPoint.NodePep;
                    var nodeGroup = dataPoint.NodeGroup;
                    if (!ReferenceEquals(nodePep, nodePepCurrent))
                    {
                        nodePepCurrent = nodePep;

                        chargeCount = GetChargeCount(nodePep);
                        chargeCurrent = 0;
                    }

                    bool addLabel = !displayTotals;
                    if (displayTotals && nodeGroup.TransitionGroup.PrecursorCharge != chargeCurrent)
                    {
                        LevelPointPairLists(pointPairLists);
                        addLabel = true;
                    }
                    chargeCurrent = nodeGroup.TransitionGroup.PrecursorCharge;

                    var transitionGroup = nodeGroup.TransitionGroup;
                    int iGroup = labels.Count;

                    if (addLabel)
                    {
                        string label = uniquePrefixGenerator.GetUniquePrefix(nodePep.RawTextId, nodePep.IsProteomic) +
                                       (chargeCount > 1
                                            ? Transition.GetChargeIndicator(transitionGroup.PrecursorCharge)
                                            : string.Empty);
                        if (!displayTotals && null == paneKey.IsotopeLabelType)
                            label += transitionGroup.LabelTypeText;
                        if (peptideOrder == SummaryPeptideOrder.time)
                        {
                            label += string.Format(" ({0:F01})", displayTotals ? // Not L10N
                                                                                   dataPoint.TimePepCharge : dataPoint.TimeGroup);
                        }
                        labels.Add(label);
                        xscalePaths.Add(dataPoint.IdentityPath);
                    }

                    double groupMaxY = 0;
                    double groupMinY = double.MaxValue;

                    // ReSharper disable DoNotCallOverridableMethodsInConstructor
                    int? resultIndex = iResult.HasValue && iResult >= 0 ? iResult : null;
                    if (RTLinearRegressionGraphPane.ShowReplicate == ReplicateDisplay.best && nodePep != null)
                    {
                        resultIndex = null;
                        int iBest = nodePep.BestResult;
                        if (iBest !=-1)
                            resultIndex = iBest;
                    }
                    if (displayTotals)
                    {
                        var labelType = nodeGroup.TransitionGroup.LabelType;
                        if (dictTypeToSet.ContainsKey(labelType))
                        {
                            if (paneKey.IncludesTransitionGroup(nodeGroup))
                            {
                                pointPairLists[dictTypeToSet[labelType]].Add(CreatePointPair(iGroup, nodeGroup,
                                                                                             ref groupMaxY, ref groupMinY,
                                                                                             resultIndex));
                            }
                            else
                            {
                                pointPairLists[dictTypeToSet[labelType]].Add(PointPairMissing(iGroup));
                            }
                        }
                    }
                    else
                    {
                        if (paneKey.IncludesTransitionGroup(nodeGroup))
                        {
                            var nodeTrans = GraphChromatogram.GetDisplayTransitions(nodeGroup, displayType).ToArray();
                            for (int i = 0; i < pointListCount; i++)
                            {
                                var pointPairList = pointPairLists[i];
                                pointPairList.Add(i >= nodeTrans.Length
                                                      ? CreatePointPairMissing(iGroup)
                                                      : CreatePointPair(iGroup, nodeTrans[i], ref groupMaxY,
                                                                        ref groupMinY,
                                                                        resultIndex));
                            }
                        }
                        else
                        {
                            for (int i = 0; i < pointListCount; i++)
                            {
                                pointPairLists[i].Add(CreatePointPairMissing(iGroup));
                            }
                        }
                    }
                    // ReSharper restore DoNotCallOverridableMethodsInConstructor

                    // Save the selected index and its y extent
                    if (ReferenceEquals(selectedGroup, nodeGroup))
                    {
                        selectedIndex = labels.Count - 1;
                        SelectedMaxY = groupMaxY;
                        SelectedMinY = groupMinY;
                    }
                        // If multiple groups in the selection, make sure y extent is max of them
                    else if (selectedIndex == labels.Count - 1)
                    {
                        SelectedMaxY = Math.Max(groupMaxY, SelectedMaxY);
                        SelectedMinY = Math.Min(groupMinY, SelectedMinY);
                    }
                    maxY = Math.Max(maxY, groupMaxY);
                    minY = Math.Min(minY, groupMinY);
                }

                PointPairLists = pointPairLists;
                Labels = labels.ToArray();
                XScalePaths = xscalePaths.ToArray();
                SelectedIndex = selectedIndex;
                MaxY = maxY;
                if (minY != double.MaxValue)
                    MinY = minY;
            }