private SrmDocument EnsureAnnotation(SrmDocument document, string annotationName, bool addAnnotation,
                                             IEnumerable <string> annotationNames)
        {
            var containsQAnnotation = annotationNames.Contains(annotationName);

            if (!containsQAnnotation && addAnnotation)
            {
                var annotationTargets =
                    AnnotationDef.AnnotationTargetSet.OfValues(AnnotationDef.AnnotationTarget.precursor_result);
                var newAnnotationDef = new AnnotationDef(annotationName, annotationTargets, AnnotationDef.AnnotationType.number,
                                                         new string[0]);
                AnnotationDef existingAnnotationDef;
                // CONSIDER: Throw error instead of overwriting?
                if (!Settings.Default.AnnotationDefList.TryGetValue(annotationName, out existingAnnotationDef) &&
                    !Equals(existingAnnotationDef, newAnnotationDef))
                {
                    Settings.Default.AnnotationDefList.SetValue(newAnnotationDef);
                }
                else
                {
                    // Use the existing annotation
                    newAnnotationDef = existingAnnotationDef;
                }

                document = document.ChangeSettings(Document.Settings.ChangeAnnotationDefs(defs =>
                {
                    var defsNew = defs.ToList();
                    defsNew.Add(newAnnotationDef);
                    return(defsNew);
                }));
            }
            else if (containsQAnnotation && !AddAnnotation)
            {
                document = document.ChangeSettings(Document.Settings.ChangeAnnotationDefs(defs =>
                {
                    var defsNew = defs.ToList();
                    defsNew.RemoveAll(def => Equals(def.Name, annotationName));
                    return(defsNew);
                }));
                var annotationNamesToKeep = document.Settings.DataSettings.AnnotationDefs.Select(def => def.Name).ToList();
                document = (SrmDocument)document.StripAnnotationValues(annotationNamesToKeep);
            }
            return(document);
        }
        public void GenerateComparison(SrmDocument docOriginal, IProgressMonitor progressMonitor)
        {
            DocOriginal = docOriginal;
            _docCompare = docOriginal;
            if (IsModel)
            {
                var handler = new MProphetResultsHandler(DocOriginal, PeakScoringModel)
                {
                    AddAnnotation  = true,
                    AddMAnnotation = true
                };
                handler.ScoreFeatures(progressMonitor);
                _docCompare = handler.ChangePeaks(progressMonitor);
            }
            else
            {
                // Add in the necessary annotation definitions so that ImportPeakBoundaries can read them
                _docCompare = AddAnnotationIfMissing(_docCompare, MProphetResultsHandler.AnnotationName);
                _docCompare = AddAnnotationIfMissing(_docCompare, MProphetResultsHandler.MAnnotationName);
                _docCompare = AddAnnotationIfMissing(_docCompare, APEX_ANNOTATION);
                // But strip the values of these annotations if there were any
                var annotationNamesToStrip = new List <string>
                {
                    MProphetResultsHandler.AnnotationName,
                    MProphetResultsHandler.MAnnotationName,
                    APEX_ANNOTATION
                };
                var annotationNamesToKeep = _docCompare.Settings.DataSettings.AnnotationDefs
                                            .Where(def => !annotationNamesToStrip.Contains(def.Name))
                                            .Select(def => def.Name).ToList();
                _docCompare = (SrmDocument)_docCompare.StripAnnotationValues(annotationNamesToKeep);
                Importer    = new PeakBoundaryImporter(_docCompare);
                long lineCount = Helpers.CountLinesInFile(FilePath);
                // Peek at the file to see if it has a column called "Apex"
                using (var reader = new StreamReader(FilePath))
                {
                    var line       = reader.ReadLine();
                    var fieldNames = PeakBoundaryImporter.FIELD_NAMES.ToList();
                    fieldNames.Add(new [] { APEX_ANNOTATION });
                    int   fieldsTotal;
                    int[] fieldIndices;
                    var   separator = PeakBoundaryImporter.DetermineCorrectSeparator(line, fieldNames,
                                                                                     PeakBoundaryImporter.REQUIRED_NO_CHROM, out fieldIndices, out fieldsTotal);
                    ApexPresent = separator != null && fieldIndices.Last() != -1;
                }
                _docCompare = Importer.Import(FilePath, progressMonitor, lineCount, true, !ApexPresent);
            }
            var matches          = new List <PeakBoundsMatch>();
            var truePeptides     = DocOriginal.Molecules.ToList();
            var pickedPeptides   = _docCompare.Molecules.ToList();
            int nTruePeptides    = truePeptides.Count;
            var chromatogramSets = DocOriginal.Settings.MeasuredResults.Chromatograms;

            for (int i = 0; i < chromatogramSets.Count; i++)
            {
                var set = chromatogramSets[i];
                for (int j = 0; j < nTruePeptides; j++)
                {
                    var truePeptide   = truePeptides[j];
                    var pickedPeptide = pickedPeptides[j];
                    // We don't care about peak picking performance for decoys
                    if (truePeptide.IsDecoy)
                    {
                        continue;
                    }
                    // We don't care about peak picking performance for standard peptides
                    if (truePeptide.GlobalStandardType != null)
                    {
                        continue;
                    }
                    var trueGroups   = truePeptide.TransitionGroups.ToList();
                    var pickedGroups = pickedPeptide.TransitionGroups.ToList();
                    for (int k = 0; k < trueGroups.Count; k++)
                    {
                        var trueGroup     = trueGroups[k];
                        var pickedGroup   = pickedGroups[k];
                        var trueResults   = trueGroup.Results[i];
                        var pickedResults = pickedGroup.Results[i];
                        if (trueResults == null)
                        {
                            continue;
                        }
                        int nChromInfos = trueResults.Count;
                        for (int m = 0; m < nChromInfos; m++)
                        {
                            var trueInfo   = trueResults[m];
                            var pickedInfo = pickedResults[m];
                            var fileInfo   = set.GetFileInfo(trueInfo.FileId);
                            var filePath   = fileInfo.FilePath;
                            var key        = new MatchKey(trueGroup.Id.GlobalIndex, trueInfo.FileId.GlobalIndex);
                            matches.Add(new PeakBoundsMatch(trueInfo, pickedInfo, key, filePath, trueGroup,
                                                            HasNoQValues, HasNoScores, ApexPresent));
                        }
                    }
                }
            }
            Matches = matches;
            // Detect if the apex time is in seconds, and if so adjust it to minutes
            if (ApexPresent)
            {
                double maxTime = matches.Where(match => match.PickedApex.HasValue)
                                 .Select(match => match.PickedApex.Value).Max();
                if (maxTime > PeakBoundaryImporter.GetMaxRt(DocOriginal))
                {
                    foreach (var match in matches.Where(match => match.PickedApex.HasValue))
                    {
                        match.PickedApex = match.PickedApex / 60;
                    }
                }
            }
        }
Exemple #3
0
        public void GenerateComparison(SrmDocument docOriginal, IProgressMonitor progressMonitor)
        {
            DocOriginal = docOriginal;
            _docCompare = docOriginal;
            if (IsModel)
            {
                var handler = new MProphetResultsHandler(DocOriginal, PeakScoringModel);
                handler.ScoreFeatures(progressMonitor, true);
                _docCompare = handler.ChangePeaks(progressMonitor);
            }
            else
            {
                // Add in the necessary annotation definitions so that ImportPeakBoundaries can read them
                var annotationNamesToStrip = new List <string>
                {
                    MProphetResultsHandler.AnnotationName,
                    MProphetResultsHandler.MAnnotationName,
                    APEX_ANNOTATION,
                    START_TIME_ANNOTATION,
                    END_TIME_ANNOTATION
                };
                foreach (var annotationName in annotationNamesToStrip)
                {
                    _docCompare = AddAnnotationIfMissing(_docCompare, annotationName);
                }
                // But strip the values of these annotations if there were any
                var annotationNamesToKeep = _docCompare.Settings.DataSettings.AnnotationDefs
                                            .Where(def => !annotationNamesToStrip.Contains(def.Name))
                                            .Select(def => def.Name).ToList();
                _docCompare = (SrmDocument)_docCompare.StripAnnotationValues(annotationNamesToKeep);
                Importer    = new PeakBoundaryImporter(_docCompare);
                long lineCount = Helpers.CountLinesInFile(FilePath);
                // Peek at the file to see if it has a column called "Apex"
                using (var reader = new StreamReader(FilePath))
                {
                    var   line       = reader.ReadLine();
                    var   fieldNames = PeakBoundaryImporter.FIELD_NAMES.ToList();
                    int   fieldsTotal;
                    int[] fieldIndices;
                    var   separator = PeakBoundaryImporter.DetermineCorrectSeparator(line, fieldNames,
                                                                                     PeakBoundaryImporter.REQUIRED_NO_CHROM, out fieldIndices, out fieldsTotal);
                    ApexPresent = separator != null && fieldIndices[(int)PeakBoundaryImporter.Field.apex_time] != -1;
                }
                _docCompare = Importer.Import(FilePath, progressMonitor, lineCount, true, !ApexPresent);
                if (progressMonitor.IsCanceled)
                {
                    return;
                }
            }
            var matches          = new List <PeakBoundsMatch>();
            var truePeptides     = DocOriginal.Molecules.ToList();
            var pickedPeptides   = _docCompare.Molecules.ToList();
            int nTruePeptides    = truePeptides.Count;
            var chromatogramSets = DocOriginal.Settings.MeasuredResults.Chromatograms;

            for (int i = 0; i < chromatogramSets.Count; i++)
            {
                var chromSet = chromatogramSets[i];
                var set      = chromatogramSets[i];
                for (int j = 0; j < nTruePeptides; j++)
                {
                    var truePeptide   = truePeptides[j];
                    var pickedPeptide = pickedPeptides[j];
                    // We don't care about peak picking performance for decoys
                    if (truePeptide.IsDecoy)
                    {
                        continue;
                    }
                    // We don't care about peak picking performance for standard peptides
                    if (truePeptide.GlobalStandardType != null)
                    {
                        continue;
                    }
                    var trueGroups   = truePeptide.TransitionGroups.ToList();
                    var pickedGroups = pickedPeptide.TransitionGroups.ToList();
                    for (int k = 0; k < trueGroups.Count; k++)
                    {
                        var trueGroup     = trueGroups[k];
                        var pickedGroup   = pickedGroups[k];
                        var trueResults   = trueGroup.Results[i];
                        var pickedResults = pickedGroup.Results[i];
                        if (trueResults.IsEmpty)
                        {
                            continue;
                        }
                        int nChromInfos = trueResults.Count;
                        for (int m = 0; m < nChromInfos; m++)
                        {
                            var trueInfo   = trueResults[m];
                            var pickedInfo = pickedResults[m];
                            // For TRIC testing at the moment
//                            double? qvaluePicked = PeakBoundsMatch.GetScoreValue(pickedInfo,
//                                MProphetResultsHandler.AnnotationName, ci => pickedInfo.QValue);
//                            if (qvaluePicked.HasValue && qvaluePicked.Value > 0.01)
//                                continue;
                            var fileInfo = set.GetFileInfo(trueInfo.FileId);
                            var filePath = fileInfo.FilePath;
                            var key      = new MatchKey(trueGroup.Id.GlobalIndex, trueInfo.FileId.GlobalIndex);
                            matches.Add(new PeakBoundsMatch(trueInfo, pickedInfo, key, chromSet, filePath,
                                                            trueGroup, truePeptide, HasNoQValues, HasNoScores, ApexPresent));
                        }
                    }
                }
            }
            Matches = matches;
            // Detect if the apex time is in seconds, and if so adjust it to minutes
            if (ApexPresent)
            {
                // ReSharper disable PossibleMultipleEnumeration
                var    matchesWithApex = matches.Where(match => match.PickedApex.HasValue).ToArray();
                double maxTime         = 0;
                if (matchesWithApex.Length > 0)
                {
                    maxTime = matchesWithApex.Select(match => match.PickedApex.Value).Max();
                }
                if (maxTime > PeakBoundaryImporter.GetMaxRt(DocOriginal))
                {
                    foreach (var match in matchesWithApex)
                    {
                        // If the end is greater than the Apex, then it must also be in seconds (otherwise minutes)
                        if (match.PickedEndBoundary.HasValue && match.PickedEndBoundary > match.PickedApex)
                        {
                            match.PickedEndBoundary = match.PickedEndBoundary / 60;
                        }
                        match.PickedApex = match.PickedApex / 60;
                        // If the start is now greater than the apex, then it must also be in seconds (otherwise minutes)
                        if (match.PickedStartBoundary.HasValue && match.PickedStartBoundary > match.PickedApex)
                        {
                            match.PickedStartBoundary = match.PickedStartBoundary / 60;
                        }
                    }
                }
                // ReSharper restore PossibleMultipleEnumeration
            }
        }
        private SrmDocument EnsureAnnotation(SrmDocument document, string annotationName, bool addAnnotation,
            IEnumerable<string> annotationNames)
        {
            var containsQAnnotation = annotationNames.Contains(annotationName);
            if (!containsQAnnotation && addAnnotation)
            {
                var annotationTargets =
                    AnnotationDef.AnnotationTargetSet.OfValues(AnnotationDef.AnnotationTarget.precursor_result);
                var newAnnotationDef = new AnnotationDef(annotationName, annotationTargets, AnnotationDef.AnnotationType.number,
                    new string[0]);
                AnnotationDef existingAnnotationDef;
                // CONSIDER: Throw error instead of overwriting?
                if (!Settings.Default.AnnotationDefList.TryGetValue(annotationName, out existingAnnotationDef) &&
                    !Equals(existingAnnotationDef, newAnnotationDef))
                {
                    Settings.Default.AnnotationDefList.SetValue(newAnnotationDef);
                }
                else
                {
                    // Use the existing annotation
                    newAnnotationDef = existingAnnotationDef;
                }

                document = document.ChangeSettings(Document.Settings.ChangeAnnotationDefs(defs =>
                {
                    var defsNew = defs.ToList();
                    defsNew.Add(newAnnotationDef);
                    return defsNew;
                }));
            }
            else if (containsQAnnotation && !AddAnnotation)
            {
                document = document.ChangeSettings(Document.Settings.ChangeAnnotationDefs(defs =>
                {
                    var defsNew = defs.ToList();
                    defsNew.RemoveAll(def => Equals(def.Name, annotationName));
                    return defsNew;
                }));
                var annotationNamesToKeep = document.Settings.DataSettings.AnnotationDefs.Select(def => def.Name).ToList();
                document = (SrmDocument) document.StripAnnotationValues(annotationNamesToKeep);
            }
            return document;
        }
 public void GenerateComparison(SrmDocument docOriginal, IProgressMonitor progressMonitor)
 {
     DocOriginal = docOriginal;
     _docCompare = docOriginal;
     if (IsModel)
     {
         var handler = new MProphetResultsHandler(DocOriginal, PeakScoringModel)
         {
             AddAnnotation = true,
             AddMAnnotation = true
         };
         handler.ScoreFeatures(progressMonitor);
         _docCompare = handler.ChangePeaks(progressMonitor);
     }
     else
     {
         // Add in the necessary annotation definitions so that ImportPeakBoundaries can read them
         _docCompare = AddAnnotationIfMissing(_docCompare, MProphetResultsHandler.AnnotationName);
         _docCompare = AddAnnotationIfMissing(_docCompare, MProphetResultsHandler.MAnnotationName);
         _docCompare = AddAnnotationIfMissing(_docCompare, APEX_ANNOTATION);
         // But strip the values of these annotations if there were any
         var annotationNamesToStrip = new List<string>
         {
             MProphetResultsHandler.AnnotationName,
             MProphetResultsHandler.MAnnotationName,
             APEX_ANNOTATION
         };
         var annotationNamesToKeep = _docCompare.Settings.DataSettings.AnnotationDefs
             .Where(def => !annotationNamesToStrip.Contains(def.Name))
             .Select(def => def.Name).ToList();
         _docCompare = (SrmDocument)_docCompare.StripAnnotationValues(annotationNamesToKeep);
         Importer = new PeakBoundaryImporter(_docCompare);
         long lineCount = Helpers.CountLinesInFile(FilePath);
         // Peek at the file to see if it has a column called "Apex"
         using (var reader = new StreamReader(FilePath))
         {
             var line = reader.ReadLine();
             var fieldNames = PeakBoundaryImporter.FIELD_NAMES.ToList();
             fieldNames.Add(new [] {APEX_ANNOTATION});
             int fieldsTotal;
             int[] fieldIndices;
             var separator = PeakBoundaryImporter.DetermineCorrectSeparator(line, fieldNames,
                 PeakBoundaryImporter.REQUIRED_NO_CHROM, out fieldIndices, out fieldsTotal);
             ApexPresent = separator != null && fieldIndices.Last() != -1;
         }
         _docCompare = Importer.Import(FilePath, progressMonitor, lineCount, true, !ApexPresent);
     }
     var matches = new List<PeakBoundsMatch>();
     var truePeptides = DocOriginal.Molecules.ToList();
     var pickedPeptides = _docCompare.Molecules.ToList();
     int nTruePeptides = truePeptides.Count;
     var chromatogramSets = DocOriginal.Settings.MeasuredResults.Chromatograms;
     for (int i = 0; i < chromatogramSets.Count; i++)
     {
         var set = chromatogramSets[i];
         for (int j = 0; j < nTruePeptides; j++)
         {
             var truePeptide = truePeptides[j];
             var pickedPeptide = pickedPeptides[j];
             // We don't care about peak picking performance for decoys
             if (truePeptide.IsDecoy)
                 continue;
             // We don't care about peak picking performance for standard peptides
             if (truePeptide.GlobalStandardType != null)
                 continue;
             var trueGroups = truePeptide.TransitionGroups.ToList();
             var pickedGroups = pickedPeptide.TransitionGroups.ToList();
             for (int k = 0; k < trueGroups.Count; k++)
             {
                 var trueGroup = trueGroups[k];
                 var pickedGroup = pickedGroups[k];
                 var trueResults = trueGroup.Results[i];
                 var pickedResults = pickedGroup.Results[i];
                 if (trueResults == null)
                     continue;
                 int nChromInfos = trueResults.Count;
                 for (int m = 0; m < nChromInfos; m++)
                 {
                     var trueInfo = trueResults[m];
                     var pickedInfo = pickedResults[m];
                     var fileInfo = set.GetFileInfo(trueInfo.FileId);
                     var filePath = fileInfo.FilePath;
                     var key = new MatchKey(trueGroup.Id.GlobalIndex, trueInfo.FileId.GlobalIndex);
                     matches.Add(new PeakBoundsMatch(trueInfo, pickedInfo, key, filePath, trueGroup,
                         HasNoQValues, HasNoScores, ApexPresent));
                 }
             }
         }
     }
     Matches = matches;
     // Detect if the apex time is in seconds, and if so adjust it to minutes
     if (ApexPresent)
     {
         double maxTime = matches.Where(match => match.PickedApex.HasValue)
                                 .Select(match => match.PickedApex.Value).Max();
         if (maxTime > PeakBoundaryImporter.GetMaxRt(DocOriginal))
         {
             foreach (var match in matches.Where(match => match.PickedApex.HasValue))
             {
                 match.PickedApex = match.PickedApex / 60;
             }
         }
     }
 }