Exemplo n.º 1
0
        public void AgilentFormatsTest()
        {
            var testFilesDir = new TestFilesDir(TestContext, ZIP_FILE);

            string      docPath;
            SrmDocument doc = InitAgilentDocument(testFilesDir, out docPath);

            using (var docContainer = new ResultsTestDocumentContainer(doc, docPath))
            {
                const string replicateName = "AgilentTest";
                string       extRaw        = ExtensionTestContext.ExtAgilentRaw;
                var          chromSets     = new[]
                {
                    new ChromatogramSet(replicateName, new[]
                                        { new MsDataFilePath(testFilesDir.GetTestPath("081809_100fmol-MichromMix-05" + extRaw)), }),
                };
                var docResults = doc.ChangeMeasuredResults(new MeasuredResults(chromSets));
                Assert.IsTrue(docContainer.SetDocument(docResults, doc, true));
                docContainer.AssertComplete();
                docResults = docContainer.Document;
                AssertResult.IsDocumentResultsState(docResults, replicateName,
                                                    doc.PeptideCount, doc.PeptideTransitionGroupCount, 0, doc.PeptideTransitionCount, 0);
            }

            testFilesDir.Dispose();
        }
Exemplo n.º 2
0
        private SrmDocument ChangeStandardConcentrationCount(SrmDocument srmDocument, int pointCount)
        {
            var measuredResults        = srmDocument.Settings.MeasuredResults;
            var standardConcentrations = measuredResults.Chromatograms
                                         .Where(chrom => Equals(chrom.SampleType, SampleType.STANDARD))
                                         .Select(chrom => chrom.AnalyteConcentration)
                                         .OfType <double>()
                                         .Distinct()
                                         .ToArray();

            Array.Sort(standardConcentrations);
            Array.Reverse(standardConcentrations);
            var topConcentrations = new HashSet <double>(standardConcentrations.Take(pointCount));
            var newChromatograms  = measuredResults.Chromatograms.ToArray();

            for (int i = 0; i < newChromatograms.Length; i++)
            {
                var chrom = newChromatograms[i];
                if (Equals(SampleType.STANDARD, chrom.SampleType) && chrom.AnalyteConcentration.HasValue)
                {
                    if (!topConcentrations.Contains(chrom.AnalyteConcentration.Value))
                    {
                        chrom = chrom.ChangeSampleType(SampleType.UNKNOWN);
                    }
                }
                newChromatograms[i] = chrom;
            }
            measuredResults = measuredResults.ChangeChromatograms(newChromatograms);
            return(srmDocument.ChangeMeasuredResults(measuredResults));
        }
Exemplo n.º 3
0
        public void Wiff2ResultsTest()
        {
            TestFilesDir testFilesDir = new TestFilesDir(TestContext, ZIP_FILE);

            string      docPath = testFilesDir.GetTestPath("OnyxTOFMS.sky");
            SrmDocument doc     = ResultsUtil.DeserializeDocument(docPath);

            //AssertEx.IsDocumentState(doc, 0, 1, 1, 4);

            using (var docContainer = new ResultsTestDocumentContainer(doc, docPath))
            {
                const string replicateName = "Wiff2Test";
                string       extRaw        = ExtensionTestContext.ExtAbWiff2;
                string       suffix        = ExtensionTestContext.CanImportAbWiff2 ? "" : "-sample-centroid";
                var          chromSets     = new[]
                {
                    new ChromatogramSet(replicateName, new[]
                                        { new MsDataFilePath(TestFilesDir.GetVendorTestData(TestFilesDir.VendorDir.ABI, "swath.api" + suffix + extRaw)), }),
                };
                var docResults = doc.ChangeMeasuredResults(new MeasuredResults(chromSets));
                Assert.IsTrue(docContainer.SetDocument(docResults, doc, true));
                docContainer.AssertComplete();
                docResults = docContainer.Document;
                //AssertResult.IsDocumentResultsState(docResults, replicateName,
                //    doc.MoleculeCount, doc.MoleculeTransitionGroupCount, 0, doc.MoleculeTransitionCount, 0);
            }

            testFilesDir.Dispose();
        }
Exemplo n.º 4
0
        public void ShimadzuFormatsTest()
        {
            var testFilesDir = new TestFilesDir(TestContext, ZIP_FILE);

            string      docPath;
            SrmDocument doc = InitShimadzuDocument(testFilesDir, out docPath);

            using (var docContainer = new ResultsTestDocumentContainer(doc, docPath))
            {
                const string replicateName = "ShimadzuTest";
                string       extRaw        = ExtensionTestContext.ExtShimadzuRaw;
                var          chromSets     = new[]
                {
                    new ChromatogramSet(replicateName, new[]
                                        { new MsDataFilePath(testFilesDir.GetTestPath("BSA-digest__MRM_optimisation_SL_scheduled_001" + extRaw)), }),
                };
                var docResults = doc.ChangeMeasuredResults(new MeasuredResults(chromSets));
                Assert.IsTrue(docContainer.SetDocument(docResults, doc, true));
                docContainer.AssertComplete();
                docResults = docContainer.Document;
                AssertResult.IsDocumentResultsState(docResults, replicateName,
                                                    doc.PeptideCount, doc.PeptideTransitionGroupCount, 0, doc.PeptideTransitionCount, 0);
            }

            testFilesDir.Dispose();
        }
Exemplo n.º 5
0
        public void TestQcTraces()
        {
            const string testZipPath = @"TestData\PressureTracesTest.zip";

            var testFilesDir = new TestFilesDir(TestContext, testZipPath);

            using (var msDataFile = new MsDataFileImpl(testFilesDir.GetTestPath("PressureTrace1" + ExtensionTestContext.ExtAbWiff)))
            {
                var pressureTraces = msDataFile.GetQcTraces();

                VerifyQcTrace(pressureTraces[0], "Column Pressure (channel 1)", 1148, 0, 9.558333, 1470, 210, MsDataFileImpl.QcTraceQuality.Pressure, MsDataFileImpl.QcTraceUnits.PoundsPerSquareInch);
                VerifyQcTrace(pressureTraces[1], "Pump A Flowrate (channel 2)", 1148, 0, 9.558333, 91590, 89120, MsDataFileImpl.QcTraceQuality.FlowRate, MsDataFileImpl.QcTraceUnits.MicrolitersPerMinute);
                VerifyQcTrace(pressureTraces[2], "Pump B Flowrate (channel 3)", 1148, 0, 9.558333, 0, 840, MsDataFileImpl.QcTraceQuality.FlowRate, MsDataFileImpl.QcTraceUnits.MicrolitersPerMinute);
                VerifyQcTrace(pressureTraces[3], "Column Pressure (channel 4)", 3508, 0, 29.225, 1396, 1322, MsDataFileImpl.QcTraceQuality.Pressure, MsDataFileImpl.QcTraceUnits.PoundsPerSquareInch);
                VerifyQcTrace(pressureTraces[4], "Pump A Flowrate (channel 5)", 3508, 0, 29.225, 7038, 7833, MsDataFileImpl.QcTraceQuality.FlowRate, MsDataFileImpl.QcTraceUnits.MicrolitersPerMinute);
                VerifyQcTrace(pressureTraces[5], "Pump B Flowrate (channel 6)", 3508, 0, 29.225, 680, 151, MsDataFileImpl.QcTraceQuality.FlowRate, MsDataFileImpl.QcTraceUnits.MicrolitersPerMinute);

                string      docPath = testFilesDir.GetTestPath("PressureTrace1.sky");
                SrmDocument doc     = ResultsUtil.DeserializeDocument(docPath);
                AssertEx.IsDocumentState(doc, 0, 1, 3, 9);

                using (var docContainer = new ResultsTestDocumentContainer(doc, docPath))
                {
                    const string replicateName = "PressureTrace1";
                    string       extRaw        = ExtensionTestContext.ExtAbWiff;
                    var          chromSets     = new[]
                    {
                        new ChromatogramSet(replicateName, new[]
                                            { new MsDataFilePath(testFilesDir.GetTestPath("PressureTrace1" + extRaw)), }),
                    };
                    var docResults = doc.ChangeMeasuredResults(new MeasuredResults(chromSets));
                    Assert.IsTrue(docContainer.SetDocument(docResults, doc, true));
                    docContainer.AssertComplete();
                    docResults = docContainer.Document;

                    var chromCache = docResults.Settings.MeasuredResults.GetChromCacheMinimizer(docResults).ChromatogramCache;
                    var tic        = chromCache.LoadAllIonsChromatogramInfo(ChromExtractor.summed, chromSets[0]);
                    var bpc        = chromCache.LoadAllIonsChromatogramInfo(ChromExtractor.base_peak, chromSets[0]);
                    var qc         = chromCache.LoadAllIonsChromatogramInfo(ChromExtractor.qc, chromSets[0]);

                    Assert.AreEqual(0, tic.Count());    // No longer expect these in SRM data
                    Assert.AreEqual(0, bpc.Count());    // No longer expect these in SRM data
                    var qcNames = qc.Select(o => o.TextId).ToArray();
                    Assert.AreEqual(6, qcNames.Length);
                    CollectionAssert.IsSubsetOf(new [] { "Column Pressure (channel 1)",
                                                         "Pump A Flowrate (channel 2)",
                                                         "Pump B Flowrate (channel 3)",
                                                         "Column Pressure (channel 4)",
                                                         "Pump A Flowrate (channel 5)",
                                                         "Pump B Flowrate (channel 6)" },
                                                qcNames);
                }
            }
        }
Exemplo n.º 6
0
        public SrmDocument UpdateDocument(SrmDocument document)
        {
            if (document.Settings.HasResults && _annotationUpdaters.ContainsKey(AnnotationDef.AnnotationTarget.replicate))
            {
                var newMeasuredResults = UpdateReplicateAnnotations(document.MeasuredResults);
                document = document.ChangeMeasuredResults(newMeasuredResults);
            }

            var newChildren = document.MoleculeGroups.Select(UpdateMoleculeGroup).ToArray();

            document = (SrmDocument)document.ChangeChildren(newChildren);
            return(document);
        }
Exemplo n.º 7
0
        public void ThermoMixedPeptidesTest()
        {
            var         testFilesDir = new TestFilesDir(TestContext, ZIP_FILE);
            string      docPath;
            SrmDocument docMixed = InitMixedDocument(testFilesDir, out docPath);

            FileEx.SafeDelete(Path.ChangeExtension(docPath, ChromatogramCache.EXT));
            SrmDocument docUnmixed = InitUnmixedDocument(testFilesDir, out docPath);

            FileEx.SafeDelete(Path.ChangeExtension(docPath, ChromatogramCache.EXT));
            string extRaw            = ExtensionTestContext.ExtThermoRaw;
            var    listChromatograms = new List <ChromatogramSet>
            {
                new ChromatogramSet("rep03", new[]
                {
                    MsDataFileUri.Parse(testFilesDir.GetTestPath(
                                            "Site20_STUDY9P_PHASEII_QC_03" + extRaw))
                }),
                new ChromatogramSet("rep05", new[]
                {
                    MsDataFileUri.Parse(testFilesDir.GetTestPath(
                                            "Site20_STUDY9P_PHASEII_QC_05" + extRaw))
                })
            };
            var docResults        = docMixed.ChangeMeasuredResults(new MeasuredResults(listChromatograms));
            var docContainerMixed = new ResultsTestDocumentContainer(docMixed, docPath);

            Assert.IsTrue(docContainerMixed.SetDocument(docResults, docMixed, true));
            docContainerMixed.AssertComplete();
            docMixed = docContainerMixed.Document;
            SrmDocument  docMixedUnmixed = (SrmDocument)docMixed.ChangeChildren(new DocNode[0]);
            IdentityPath tempPath;

            docMixedUnmixed = docMixedUnmixed.AddPeptideGroups(docUnmixed.PeptideGroups, true, IdentityPath.ROOT,
                                                               out tempPath, out tempPath);

            docResults = docUnmixed.ChangeMeasuredResults(new MeasuredResults(listChromatograms));
            var docContainerUnmixed = new ResultsTestDocumentContainer(docUnmixed, docPath);

            Assert.IsTrue(docContainerUnmixed.SetDocument(docResults, docUnmixed, true));
            docContainerUnmixed.AssertComplete();
            docUnmixed = docContainerUnmixed.Document;
            AssertEx.DocumentCloned(docMixedUnmixed, docUnmixed);

            docContainerMixed.Release();
            docContainerUnmixed.Release();
        }
Exemplo n.º 8
0
        /// <summary>
        /// Filters document chromatograms for all but a selected set of files.
        /// </summary>
        private SrmDocument FilterFiles(SrmDocument doc, Func <ChromFileInfo, bool> selectFilesToKeepFunc)
        {
            if (doc.Settings.MeasuredResults == null)
            {
                return(doc);
            }

            var measuredResultsNew = doc.Settings.MeasuredResults.FilterFiles(selectFilesToKeepFunc);

            // If nothing changed, don't create a new document instance
            if (measuredResultsNew != null &&
                ArrayUtil.ReferencesEqual(measuredResultsNew.Chromatograms, doc.Settings.MeasuredResults.Chromatograms))
            {
                return(doc);
            }

            return(doc.ChangeMeasuredResults(measuredResultsNew));
        }
Exemplo n.º 9
0
        public SrmDocument SetReplicateAnnotations(SrmDocument document, ReplicateRef replicateRef,
                                                   Annotations annotations)
        {
            var measuredResults = document.MeasuredResults;

            if (measuredResults == null)
            {
                throw ElementNotFoundException(replicateRef);
            }
            for (int i = 0; i < measuredResults.Chromatograms.Count; i++)
            {
                var chromSet = measuredResults.Chromatograms[i];
                if (replicateRef.Matches(chromSet))
                {
                    chromSet = chromSet.ChangeAnnotations(annotations);
                    var newChromatograms = measuredResults.Chromatograms.ToArray();
                    newChromatograms[i] = chromSet;
                    return(document.ChangeMeasuredResults(measuredResults.ChangeChromatograms(newChromatograms)));
                }
            }
            throw ElementNotFoundException(replicateRef);
        }
Exemplo n.º 10
0
        public void ThermoCancelImportTest()
        {
            var         testFilesDir = new TestFilesDir(TestContext, ZIP_FILE);
            string      docPath;
            SrmDocument doc          = InitThermoDocument(testFilesDir, out docPath);
            var         docContainer = new ResultsTestDocumentContainer(doc, docPath);
            string      resultsPath  = testFilesDir.GetTestPath("Site20_STUDY9P_PHASEII_QC_03" +
                                                                ExtensionTestContext.ExtThermoRaw);
            string dirPath = Path.GetDirectoryName(resultsPath) ?? "";

            // Remove any existing temp and cache files
            foreach (var path in Directory.GetFiles(dirPath))
            {
                if (IsCacheOrTempFile(path))
                {
                    FileEx.SafeDelete(path);
                }
            }
            string name = Path.GetFileNameWithoutExtension(resultsPath);
            var    listChromatograms = new List <ChromatogramSet> {
                new ChromatogramSet(name, new[] { MsDataFileUri.Parse(resultsPath) })
            };
            var docResults = doc.ChangeMeasuredResults(new MeasuredResults(listChromatograms));

            // Start cache load, but don't wait for completion
            Assert.IsTrue(docContainer.SetDocument(docResults, doc));

            // Wait up to 1 second for the cache to start being written
            for (int i = 0; i < 100; i++)
            {
                if (Directory.GetFiles(dirPath).IndexOf(IsCacheOrTempFile) != -1)
                {
                    break;
                }
                Thread.Sleep(10);
            }

            Assert.IsTrue(Directory.GetFiles(dirPath).IndexOf(IsCacheOrTempFile) != -1, "Failed to create cache file");

            // Cancel by reverting to the original document
            Assert.IsTrue(docContainer.SetDocument(doc, docResults));
            // Wait up to 5 seconds for cancel to occur
            for (int i = 0; i < 50; i++)
            {
                if (docContainer.LastProgress.IsCanceled)
                {
                    break;
                }
                Thread.Sleep(100);
            }
            if (!docContainer.LastProgress.IsCanceled)
            {
                Assert.Fail("Attempt to cancel results load failed. {0}", docContainer.LastProgress.ErrorException != null
                    ? docContainer.LastProgress.ErrorException.Message : string.Empty);
            }

            // Wait up to 20 seconds for the cache to be removed
            for (int i = 0; i < 200; i++)
            {
                if (Directory.GetFiles(dirPath).IndexOf(IsCacheOrTempFile) == -1)
                {
                    break;
                }
                Thread.Sleep(100);
            }
            // Cache file has been removed
            Assert.IsTrue(Directory.GetFiles(dirPath).IndexOf(IsCacheOrTempFile) == -1, "Failed to remove cache file");
            testFilesDir.Dispose();
        }
Exemplo n.º 11
0
        public void DoThermoRatioTest(RefinementSettings.ConvertToSmallMoleculesMode smallMoleculesTestMode)
        {
            TestSmallMolecules = false;  // We do this explicitly

            var         testFilesDir = new TestFilesDir(TestContext, ZIP_FILE);
            string      docPath;
            SrmDocument doc      = InitThermoDocument(testFilesDir, out docPath);
            SrmSettings settings = doc.Settings.ChangePeptideModifications(mods =>
                                                                           mods.ChangeInternalStandardTypes(new[] { IsotopeLabelType.light }));

            doc = doc.ChangeSettings(settings);
            if (smallMoleculesTestMode != RefinementSettings.ConvertToSmallMoleculesMode.none)
            {
                var docOrig = doc;
                var refine  = new RefinementSettings();
                doc = refine.ConvertToSmallMolecules(doc, smallMoleculesTestMode);
                // This is our first example of a converted label doc - check roundtripping
                AssertEx.ConvertedSmallMoleculeDocumentIsSimilar(docOrig, doc);
                AssertEx.Serializable(doc);
            }
            var    docContainer      = new ResultsTestDocumentContainer(doc, docPath);
            string extRaw            = ExtensionTestContext.ExtThermoRaw;
            var    listChromatograms = new List <ChromatogramSet>
            {
                new ChromatogramSet("rep03", new[]
                {
                    MsDataFileUri.Parse(testFilesDir.GetTestPath(
                                            "Site20_STUDY9P_PHASEII_QC_03" + extRaw))
                }),
                new ChromatogramSet("rep05", new[]
                {
                    MsDataFileUri.Parse(testFilesDir.GetTestPath(
                                            "Site20_STUDY9P_PHASEII_QC_05" + extRaw))
                })
            };
            var docResults = doc.ChangeMeasuredResults(new MeasuredResults(listChromatograms));

            Assert.IsTrue(docContainer.SetDocument(docResults, doc, true));
            docContainer.AssertComplete();
            docResults = docContainer.Document;
            // Make sure all groups have at least 5 transitions (of 6) with ratios
            int ratioGroupMissingCount = 0;

            foreach (var nodeGroup in docResults.MoleculeTransitionGroups)
            {
                if (nodeGroup.TransitionGroup.LabelType.IsLight)
                {
                    foreach (var result in nodeGroup.Results)
                    {
                        Assert.IsFalse(result[0].Ratio.HasValue, "Light group found with a ratio");
                    }
                    foreach (TransitionDocNode nodeTran in nodeGroup.Children)
                    {
                        foreach (var resultTran in nodeTran.Results)
                        {
                            Assert.IsFalse(resultTran[0].Ratio.HasValue, "Light transition found with a ratio");
                        }
                    }
                }
                else
                {
                    bool missingRatio = false;
                    foreach (ChromInfoList <TransitionGroupChromInfo> chromInfoList in nodeGroup.Results)
                    {
                        var ratioHeavy = chromInfoList[0].Ratio;
                        if (!ratioHeavy.HasValue)
                        {
                            missingRatio = true;
                        }
                    }
                    int ratioCount1 = 0;
                    int ratioCount2 = 0;
                    foreach (TransitionDocNode nodeTranHeavy in nodeGroup.Children)
                    {
                        float?ratioHeavy = nodeTranHeavy.Results[0][0].Ratio;
                        if (ratioHeavy.HasValue)
                        {
                            Assert.IsFalse(float.IsNaN(ratioHeavy.Value) || float.IsInfinity(ratioHeavy.Value));
                            ratioCount1++;
                        }
                        ratioHeavy = nodeTranHeavy.Results[1][0].Ratio;
                        if (ratioHeavy.HasValue)
                        {
                            Assert.IsFalse(float.IsNaN(ratioHeavy.Value) || float.IsInfinity(ratioHeavy.Value));
                            ratioCount2++;
                        }
                    }
                    Assert.AreEqual(3, ratioCount1);
                    if (ratioCount2 < 2)
                    {
                        ratioGroupMissingCount++;
                    }
                    else
                    {
                        Assert.IsFalse(missingRatio, "Precursor missing ratio when transitions have ratios");
                    }
                }
            }
            // 3 groups with less than 2 transition ratios
            Assert.AreEqual(3, ratioGroupMissingCount);

            // Remove the first light transition, checking that this removes the ratio
            // from the corresponding heavy transition, but not the entire group, until
            // after all light transitions have been removed.
            IdentityPath pathFirstPep = docResults.GetPathTo((int)SrmDocument.Level.Molecules, 0);
            var          nodePep      = (PeptideDocNode)docResults.FindNode(pathFirstPep);

            Assert.AreEqual(2, nodePep.Children.Count);
            var          nodeGroupLight = (TransitionGroupDocNode)nodePep.Children[0];
            IdentityPath pathGroupLight = new IdentityPath(pathFirstPep, nodeGroupLight.TransitionGroup);

            Assert.IsNull(nodeGroupLight.Results[0][0].Ratio, "Light group has ratio");
            var          nodeGroupHeavy = (TransitionGroupDocNode)nodePep.Children[1];
            IdentityPath pathGroupHeavy = new IdentityPath(pathFirstPep, nodeGroupHeavy.TransitionGroup);
            float?       ratioStart     = nodeGroupHeavy.Results[0][0].Ratio;

            Assert.IsTrue(ratioStart.HasValue, "No starting heavy group ratio");
            var expectedValues = new[] { 1.403414, 1.38697791, 1.34598482 };

            for (int i = 0; i < 3; i++)
            {
                var pathLight = docResults.GetPathTo((int)SrmDocument.Level.Transitions, 0);
                var pathHeavy = docResults.GetPathTo((int)SrmDocument.Level.Transitions, 3);
                TransitionDocNode nodeTran  = (TransitionDocNode)docResults.FindNode(pathHeavy);
                float?            ratioTran = nodeTran.Results[0][0].Ratio;
                Assert.IsTrue(ratioTran.HasValue, "Expected transition ratio not found");
                Assert.AreEqual(ratioTran.Value, expectedValues[i], 1.0e-5);
                docResults = (SrmDocument)docResults.RemoveChild(pathLight.Parent, docResults.FindNode(pathLight));
                nodeTran   = (TransitionDocNode)docResults.FindNode(pathHeavy);
                Assert.IsFalse(nodeTran.Results[0][0].Ratio.HasValue, "Unexpected transiton ratio found");
                Assert.AreEqual(pathGroupHeavy, pathHeavy.Parent, "Transition found outside expected group");
//                nodePep = (PeptideDocNode) docResults.FindNode(pathFirstPep);
                nodeGroupHeavy = (TransitionGroupDocNode)docResults.FindNode(pathGroupHeavy);
//                Assert.AreEqual(nodePep.Results[0][0].RatioToStandard, nodeGroupHeavy.Results[0][0].Ratio,
//                                "Peptide and group ratios not equal");
                if (i < 2)
                {
                    float?ratioGroup = nodeGroupHeavy.Results[0][0].Ratio;
                    Assert.IsTrue(ratioGroup.HasValue, "Group ratio removed with transition ratios");
                    Assert.AreEqual(ratioStart.Value, ratioGroup.Value, 0.1,
                                    "Unexpected group ratio change by more than 0.1");
                }
                else
                {
                    Assert.IsFalse(nodeGroupHeavy.Results[0][0].Ratio.HasValue,
                                   "Group ratio still present with no transition ratios");
                }
            }
            bool asSmallMolecules = (smallMoleculesTestMode != RefinementSettings.ConvertToSmallMoleculesMode.none);

            if (!asSmallMolecules) // GetTransitions() doesn't work the same way for small molecules - it only lists existing ones
            {
                bool      firstAdd           = true;
                var       nodeGroupLightOrig = (TransitionGroupDocNode)doc.FindNode(pathGroupLight);
                DocNode[] lightChildrenOrig  = nodeGroupLightOrig.Children.ToArray();
                foreach (var nodeTran in nodeGroupLightOrig.GetTransitions(docResults.Settings,
                                                                           null, nodeGroupLightOrig.PrecursorMz, null, null, null, false))
                {
                    var transition = nodeTran.Transition;
                    if (!firstAdd && lightChildrenOrig.IndexOf(node => Equals(node.Id, transition)) == -1)
                    {
                        continue;
                    }
                    // Add the first transition, and then the original transitions
                    docResults     = (SrmDocument)docResults.Add(pathGroupLight, nodeTran);
                    nodeGroupHeavy = (TransitionGroupDocNode)docResults.FindNode(pathGroupHeavy);
                    if (firstAdd)
                    {
                        Assert.IsNull(nodeGroupHeavy.Results[0][0].Ratio, "Unexpected heavy ratio found");
                    }
                    else
                    {
                        Assert.IsNotNull(nodeGroupHeavy.Results[0][0].Ratio,
                                         "Heavy ratio null after adding light children");
                    }
                    firstAdd = false;
                }
                Assert.AreEqual(ratioStart, nodeGroupHeavy.Results[0][0].Ratio);
            }
            // Release file handles
            docContainer.Release();
            testFilesDir.Dispose();
        }
Exemplo n.º 12
0
        private SrmDocument ImportResults(SrmDocument doc, string nameResult, IEnumerable<MsDataFileUri> dataSources,
            OptimizableRegression optimizationFunction)
        {
            var results = doc.Settings.MeasuredResults;
            var chrom = GetChromatogramByName(nameResult, results);
            if (chrom == null)
            {
                // If the chromatogram, is not in the current set, then delete the cache
                // file to make sure it is not on disk before starting.
                FileEx.SafeDelete(ChromatogramCache.FinalPathForName(DocumentFilePath, nameResult), true);

                chrom = new ChromatogramSet(nameResult, dataSources, Annotations.EMPTY, optimizationFunction);

                if (results == null)
                    results = new MeasuredResults(new[] {chrom});
                else
                {
                    // Add the new result to the end.
                    var listChrom = new List<ChromatogramSet>(results.Chromatograms) {chrom};
                    results = results.ChangeChromatograms(listChrom.ToArray());
                }
            }
            else
            {
                // Append to an existing chromatogram set
                var dataFilePaths = new List<MsDataFileUri>(chrom.MSDataFilePaths);
                foreach (var sourcePath in dataSources)
                {
                    if (!dataFilePaths.Contains(sourcePath))
                        dataFilePaths.Add(sourcePath);
                }
                // If no new paths added, just return without changing.
                if (dataFilePaths.Count == chrom.FileCount)
                    return doc;

                int replaceIndex = results.Chromatograms.IndexOf(chrom);
                var arrayChrom = results.Chromatograms.ToArray();
                arrayChrom[replaceIndex] = chrom.ChangeMSDataFilePaths(dataFilePaths);

                results = results.ChangeChromatograms(arrayChrom);
            }

            if (results != null && Program.DisableJoining)
                results = results.ChangeIsJoiningDisabled(true);

            return doc.ChangeMeasuredResults(results);
        }
Exemplo n.º 13
0
        public SrmDocument ImportResults(SrmDocument doc, List<KeyValuePair<string, MsDataFileUri[]>> namedResults, string optimize)
        {
            OptimizableRegression optimizationFunction = doc.Settings.TransitionSettings.Prediction.GetOptimizeFunction(optimize);

            if (namedResults.Count == 1)
                return ImportResults(doc, namedResults[0].Key, namedResults[0].Value, optimizationFunction);

            // Add all chosen files as separate result sets.
            var results = doc.Settings.MeasuredResults;
            var listChrom = new List<ChromatogramSet>();
            if (results != null)
                listChrom.AddRange(results.Chromatograms);

            foreach (var namedResult in namedResults)
            {
                string nameResult = namedResult.Key;

                // Skip results that have already been loaded.
                if (GetChromatogramByName(nameResult, results) != null)
                    continue;

                // Delete caches that will be overwritten
                FileEx.SafeDelete(ChromatogramCache.FinalPathForName(DocumentFilePath, nameResult), true);

                listChrom.Add(new ChromatogramSet(nameResult, namedResult.Value, Annotations.EMPTY, optimizationFunction));
            }

            var arrayChrom = listChrom.ToArray();
            if (arrayChrom.Length == 0)
            {
                results = null;
            }
            else
            {
                if (results == null)
                {
                    results = new MeasuredResults(arrayChrom);
                }
                else
                {
                    results = results.ChangeChromatograms(arrayChrom);
                }
            }

            if (results != null && Program.DisableJoining)
                results = results.ChangeIsJoiningDisabled(true);

            return doc.ChangeMeasuredResults(results);
        }
Exemplo n.º 14
0
        public void ThermoCancelImportTest()
        {
            var    testFilesDir = new TestFilesDir(TestContext, ZIP_FILE);
            string resultsPath  = testFilesDir.GetTestPath("Site20_STUDY9P_PHASEII_QC_03" +
                                                           ExtensionTestContext.ExtThermoRaw);
            string      dirPath = Path.GetDirectoryName(resultsPath) ?? "";
            string      docPath;
            SrmDocument doc = InitThermoDocument(testFilesDir, out docPath);

            // Give this two chances to succeed. It can succeed tens of thousands of times
            // in a row, but it still occasionally fails on nightly tests. Hopefully two
            // tries will make this extremely unlikely.
            for (int tries = 0; tries < 2; tries++)
            {
                using (var docContainer = new ResultsTestDocumentContainer(doc, docPath))
                {
                    // Remove any existing temp and cache files
                    foreach (var path in Directory.GetFiles(dirPath))
                    {
                        if (IsCacheOrTempFile(path))
                        {
                            FileEx.SafeDelete(path);
                        }
                    }
                    string name = Path.GetFileNameWithoutExtension(resultsPath);
                    var    listChromatograms = new List <ChromatogramSet> {
                        new ChromatogramSet(name, new[] { MsDataFileUri.Parse(resultsPath) })
                    };
                    var docResults = doc.ChangeMeasuredResults(new MeasuredResults(listChromatograms));
                    // Start cache load, but don't wait for completion
                    Assert.IsTrue(docContainer.SetDocument(docResults, doc));

                    // Wait up to 10 second for the cache to start being written
                    bool cacheFound = false;
                    for (int i = 0; i < 1000; i++)
                    {
                        if (Directory.GetFiles(dirPath).IndexOf(IsCacheOrTempFile) != -1)
                        {
                            cacheFound = true;
                            break;
                        }
                        Thread.Sleep(10);
                    }
                    if (!cacheFound)
                    {
                        Assert.Fail(TextUtil.LineSeparate("Failed to create cache file. Found files:", TextUtil.LineSeparate(Directory.GetFiles(dirPath))));
                    }

                    // Cancel by reverting to the original document
                    Assert.IsTrue(docContainer.SetDocument(doc, docResults));
                    // Wait up to 10 seconds for cancel to occur
                    bool cancelOccurred = false;
                    for (int i = 0; i < 1000; i++)
                    {
                        if (docContainer.LastProgress != null && docContainer.LastProgress.IsCanceled)
                        {
                            cancelOccurred = true;
                            break;
                        }
                        Thread.Sleep(10);
                    }
                    // Wait up to 20 seconds for the cache to be removed
                    bool cacheRemoved = false;
                    for (int i = 0; i < 200; i++)
                    {
                        if (Directory.GetFiles(dirPath).IndexOf(IsCacheOrTempFile) == -1)
                        {
                            cacheRemoved = true;
                            break;
                        }
                        Thread.Sleep(100);
                    }
                    if (!cacheRemoved)
                    {
                        if (tries == 0 && File.Exists(Path.ChangeExtension(docPath, ChromatogramCache.EXT)))
                        {
                            // Allow a single failure where we end up with the final cache instead of a cancelation
                            FileEx.SafeDelete(docPath);
                            continue;   // Try again
                        }
                        if (!cancelOccurred)
                        {
                            Assert.Fail("Attempt to cancel results load failed on try {0}. {1}", tries + 1,
                                        docContainer.LastProgress != null && docContainer.LastProgress.ErrorException != null
                                    ? docContainer.LastProgress.ErrorException.Message : string.Empty);
                        }
                        Assert.Fail(TextUtil.LineSeparate("Failed to remove cache file. Found files:", TextUtil.LineSeparate(Directory.GetFiles(dirPath))));
                    }
                    break;  // If we make it here then, successful
                }
            }
            // Cache file has been removed
            testFilesDir.Dispose();
        }
Exemplo n.º 15
0
        public string GenerateDocumentXml(SrmDocument doc, string oldXml)
        {
            if (doc == null)
            {
                return(null);
            }

            // Minimize document to only the peptides we need
            var minimalPeptides = StandardPeptides.ToHashSet();

            var oldPeptides = new Dictionary <Target, PeptideDocNode>();

            if (!string.IsNullOrEmpty(oldXml))
            {
                try
                {
                    using (var reader = new StringReader(oldXml))
                    {
                        var oldDoc = (SrmDocument) new XmlSerializer(typeof(SrmDocument)).Deserialize(reader);
                        oldPeptides = oldDoc.Molecules.Where(pep => minimalPeptides.Contains(pep.Target)).ToDictionary(pep => pep.Target, pep => pep);
                    }
                }
                catch
                {
                    // ignored
                }
            }

            var addPeptides = new List <PeptideDocNode>();

            foreach (var nodePep in doc.Molecules.Where(pep => minimalPeptides.Contains(pep.Target)))
            {
                if (oldPeptides.TryGetValue(nodePep.Target, out var nodePepOld))
                {
                    addPeptides.Add(nodePep.Merge(nodePepOld));
                    oldPeptides.Remove(nodePep.Target);
                }
                else
                {
                    addPeptides.Add(nodePep);
                }
            }
            addPeptides.AddRange(oldPeptides.Values);

            var peptides = new List <PeptideDocNode>();

            foreach (var nodePep in addPeptides)
            {
                var precursors = new List <DocNode>();
                foreach (TransitionGroupDocNode nodeTranGroup in nodePep.Children)
                {
                    var transitions = nodeTranGroup.Transitions.Where(tran => tran.ResultsRank.HasValue).OrderBy(tran => tran.ResultsRank.Value).Cast <DocNode>().ToList();
                    if (transitions.Count > 0)
                    {
                        precursors.Add(nodeTranGroup.ChangeChildren(transitions));
                    }
                }
                if (precursors.Count > 0)
                {
                    peptides.Add((PeptideDocNode)nodePep.ChangeChildren(precursors));
                }
            }
            if (peptides.Count == 0)
            {
                return(null);
            }

            peptides.Sort((nodePep1, nodePep2) => nodePep1.ModifiedTarget.CompareTo(nodePep2.ModifiedTarget));
            doc = (SrmDocument)doc.ChangeChildren(new[] { new PeptideGroupDocNode(new PeptideGroup(), Resources.IrtDb_MakeDocumentXml_iRT_standards, string.Empty, peptides.ToArray()) });

            // Clear some settings to make the document smaller and so that they won't get imported into a document
            // TODO: Convert all modifications to explicit?
            doc = doc.ChangeMeasuredResults(null);
            doc = doc.ChangeSettings(doc.Settings.ChangePeptideLibraries(libs => libs.ChangeLibraries(new List <LibrarySpec>(), new List <Library>())));

            using (var writer = new StringWriter())
                using (var writer2 = new XmlTextWriter(writer))
                {
                    doc.Serialize(writer2, null, SkylineVersion.CURRENT, null);
                    return(writer.ToString());
                }
        }
Exemplo n.º 16
0
        public void WatersMultiFileTest()
        {
            var testFilesDir = new TestFilesDir(TestContext, ZIP_FILE);

            string      docPath;
            SrmDocument docOriginal = InitWatersDocument(testFilesDir, out docPath);
            SrmDocument doc         = docOriginal;

            using (var docContainer = new ResultsTestDocumentContainer(doc, docPath))
            {
                string extRaw = ExtensionTestContext.ExtWatersRaw;

                var listChromatograms = new List <ChromatogramSet>
                {
                    new ChromatogramSet("double", new[]
                    {
                        MsDataFileUri.Parse(testFilesDir.GetTestPath("160109_Mix1_calcurve_070.mzML")),
                        MsDataFileUri.Parse(testFilesDir.GetTestPath("160109_Mix1_calcurve_073.mzML"))
                    }),
                    new ChromatogramSet("trouble", new[]
                    {
                        MsDataFileUri.Parse(testFilesDir.GetTestPath("160109_Mix1_calcurve_075" + extRaw)),
                        MsDataFileUri.Parse(testFilesDir.GetTestPath("160109_Mix1_calcurve_078.mzML"))
                    })
                };
                var docResults = doc.ChangeMeasuredResults(new MeasuredResults(listChromatograms));
                Assert.IsTrue(docContainer.SetDocument(docResults, doc, true));
                docContainer.AssertComplete();
                docResults = docContainer.Document;
                Assert.IsTrue(docResults.Settings.HasResults);
                var measuredResults = docResults.Settings.MeasuredResults;
                var chromatograms   = measuredResults.Chromatograms;
                Assert.AreEqual(2, chromatograms.Count);
                var dictPathToIndex = new Dictionary <MsDataFileUri, int>();
                for (int i = 0; i < 2; i++)
                {
                    Assert.AreEqual(listChromatograms[i].BatchName, chromatograms[i].BatchName);
                    for (int j = 0; j < 2; j++)
                    {
                        Assert.AreEqual(listChromatograms[i].MSDataFileInfos[j].FilePath, chromatograms[i].MSDataFileInfos[j].FilePath);
                        dictPathToIndex.Add(listChromatograms[i].MSDataFileInfos[j].FilePath, j);
                    }
                }

                const float tolerance = (float)TransitionInstrument.DEFAULT_MZ_MATCH_TOLERANCE;

                foreach (var pair in docResults.PeptidePrecursorPairs)
                {
                    var nodePep   = pair.NodePep;
                    var nodeGroup = pair.NodeGroup;
                    Assert.IsTrue(nodeGroup.HasResults, string.Format("Missing results on {0}", nodeGroup));
                    Assert.AreEqual(2, nodeGroup.Results.Count);
                    foreach (var result in nodeGroup.Results)
                    {
                        Assert.AreEqual(2, result.Count);
                    }
                    for (int i = 0; i < 2; i++)
                    {
                        ChromatogramGroupInfo[] chromInfos;
                        Assert.IsTrue(measuredResults.TryLoadChromatogram(i, nodePep, nodeGroup, tolerance, true, out chromInfos),
                                      string.Format("Missing chromatogram {0} - {1}", nodeGroup, i));
                        Assert.AreEqual(2, chromInfos.Length);
                        double[] peakAreas = new double[2];
                        for (int j = 0; j < 2; j++)
                        {
                            var chromInfo = chromInfos[j];
                            // No guarantee that chromInfos will be in the same order as the ChromatogramSet.MsDataFileInfos
                            int peakAreaIndex = dictPathToIndex[chromInfo.FilePath];
                            Assert.IsTrue(chromInfo.BestPeakIndex != -1, string.Format("Missing peak {0} - {1}:{2}", nodeGroup, i, j));
                            foreach (var tranInfo in chromInfo.TransitionPointSets)
                            {
                                var peakInfo = tranInfo.GetPeak(chromInfo.BestPeakIndex);
                                if (peakInfo.IsEmpty || peakInfo.IsForcedIntegration)
                                {
                                    continue;
                                }

                                // Check times
                                var times  = tranInfo.Times;
                                int iStart = CollectionUtil.BinarySearch(times, peakInfo.StartTime);
                                Assert.IsTrue(iStart >= 0, string.Format("Start time not {0}", iStart));
                                int iEnd = CollectionUtil.BinarySearch(times, peakInfo.EndTime);
                                Assert.IsTrue(iEnd >= 0, string.Format("End time not found {0}", iEnd));
                                int iPeak = CollectionUtil.BinarySearch(times, peakInfo.RetentionTime);
                                // Check intensities at times
                                var intensities = tranInfo.Intensities;
                                Assert.IsTrue(intensities[iStart] < intensities[iPeak],
                                              string.Format("Start intensity {0} >= peak intensity {1}", intensities[iStart], intensities[iPeak]));
                                Assert.IsTrue(intensities[iEnd] < intensities[iPeak],
                                              string.Format("End intensity {0} >= peak intensity {1}", intensities[iEnd], intensities[iPeak]));
                                // Sum peak area
                                peakAreas[peakAreaIndex] += peakInfo.Area;
                            }
                        }

                        Assert.IsTrue(peakAreas[0] < peakAreas[1],
                                      string.Format("{0} analyte area >= {1} standard area", peakAreas[0], peakAreas[1]));
                    }
                }
            }
            testFilesDir.Dispose();
        }
Exemplo n.º 17
0
        /// <summary>
        /// This function will add the given replicate, from dataFile, to the given document. If the replicate
        /// does not exist, it will be added. If it does exist, it will be appended to.
        /// </summary>
        public static SrmDocument ImportResults(SrmDocument doc, string docPath, string replicate, MsDataFileUri dataFile,
            OptimizableRegression optimize, IProgressMonitor progressMonitor, out ProgressStatus status)
        {
            using (var docContainer = new ResultsMemoryDocumentContainer(null, docPath) {ProgressMonitor = progressMonitor})
            {
                // Make sure library loading happens, which may not happen, if the doc
                // parameter is used as the baseline document.
                docContainer.SetDocument(doc, null);

                SrmDocument docAdded;
                do
                {
                    doc = docContainer.Document;

                    var listChromatograms = new List<ChromatogramSet>();

                    if (doc.Settings.HasResults)
                        listChromatograms.AddRange(doc.Settings.MeasuredResults.Chromatograms);

                    int indexChrom = listChromatograms.IndexOf(chrom => chrom.Name.Equals(replicate));
                    if (indexChrom != -1)
                    {
                        var chromatogram = listChromatograms[indexChrom];
                        var paths = chromatogram.MSDataFilePaths;
                        var listFilePaths = paths.ToList();
                        listFilePaths.Add(dataFile);
                        listChromatograms[indexChrom] = chromatogram.ChangeMSDataFilePaths(listFilePaths);
                    }
                    else
                    {
                        listChromatograms.Add(new ChromatogramSet(replicate, new[] {dataFile.Normalize()},
                            Annotations.EMPTY, optimize));
                    }

                    var results = doc.Settings.HasResults
                                      ? doc.Settings.MeasuredResults.ChangeChromatograms(listChromatograms)
                                      : new MeasuredResults(listChromatograms, doc.Settings.IsResultsJoiningDisabled);

                    docAdded = doc.ChangeMeasuredResults(results);
                }
                while (!docContainer.SetDocument(docAdded, doc, true));

                status = docContainer.LastProgress;

                return docContainer.Document;
            }
        }
Exemplo n.º 18
0
        public void WiffResultsTest()
        {
            TestFilesDir testFilesDir = new TestFilesDir(TestContext, ZIP_FILE);

            SrmDocument doc = InitWiffDocument(testFilesDir);

            using (var docContainer = new ResultsTestDocumentContainer(doc,
                                                                       testFilesDir.GetTestPath("SimpleWiffTest.sky")))
            {
                FileEx.SafeDelete(ChromatogramCache.FinalPathForName(docContainer.DocumentFilePath, null));

                var listChromatograms = new List <ChromatogramSet>();

                if (ExtensionTestContext.CanImportAbWiff)
                {
                    string   pathWiff = testFilesDir.GetTestPath("051309_digestion.wiff");
                    string[] dataIds  = MsDataFileImpl.ReadIds(pathWiff);

                    for (int i = 0; i < dataIds.Length; i++)
                    {
                        string nameSample = dataIds[i];
                        if (!Equals(nameSample, "test") && listChromatograms.Count == 0)
                        {
                            continue;
                        }
                        string pathSample = SampleHelp.EncodePath(pathWiff, nameSample, i, LockMassParameters.EMPTY, false, false);
                        listChromatograms.Add(new ChromatogramSet(nameSample, new[] { MsDataFileUri.Parse(pathSample) }));
                    }
                }
                else
                {
                    listChromatograms.Add(new ChromatogramSet("test",
                                                              new[] { MsDataFileUri.Parse(testFilesDir.GetTestPath("051309_digestion-test.mzML")) }));
                    listChromatograms.Add(new ChromatogramSet("rfp9,before,h,1",
                                                              new[] { MsDataFileUri.Parse(testFilesDir.GetTestPath("051309_digestion-rfp9,before,h,1.mzML")) }));
                }

                // Should have added test and one after
                Assert.AreEqual(2, listChromatograms.Count);

                var docResults = doc.ChangeMeasuredResults(new MeasuredResults(listChromatograms));
                Assert.IsTrue(docContainer.SetDocument(docResults, doc, true));
                docContainer.AssertComplete();

                docResults = docContainer.Document;

                AssertEx.IsDocumentState(docResults, 6, 9, 9, 18, 54);
                Assert.IsTrue(docResults.Settings.MeasuredResults.IsLoaded);

                foreach (var nodeTran in docResults.PeptideTransitions)
                {
                    Assert.IsTrue(nodeTran.HasResults);
                    Assert.AreEqual(2, nodeTran.Results.Count);
                }

                // Remove the last chromatogram
                listChromatograms.RemoveAt(1);

                var docResultsSingle = docResults.ChangeMeasuredResults(new MeasuredResults(listChromatograms));

                AssertResult.IsDocumentResultsState(docResultsSingle, "test", 9, 2, 9, 8, 27);

                // Add mzXML version of test sample
                listChromatograms.Add(new ChromatogramSet("test-mzXML", new[] { MsDataFileUri.Parse(testFilesDir.GetTestPath("051309_digestion-s3.mzXML")) }));

                var docMzxml = docResults.ChangeMeasuredResults(new MeasuredResults(listChromatograms));
                Assert.IsTrue(docContainer.SetDocument(docMzxml, docResults, true));
                docContainer.AssertComplete();
                docMzxml = docContainer.Document;
                // Verify mzXML and native contained same results
                // Unfortunately mzWiff produces chromatograms with now zeros, which
                // need to be interpolated into place.  This means a .wiff file and
                // its mzWiff mzXML file will never be the same.
                AssertResult.MatchChromatograms(docMzxml, 0, 1, -1, 0);
            }

            // TODO: Switch to a using clause when PWiz is fixed, and this assertion fails
//            AssertEx.ThrowsException<IOException>(() => testFilesDir.Dispose());
        }
Exemplo n.º 19
0
            public SrmDocument AddAnnotationTestValues(SrmDocument document)
            {
                _counter      = 0;
                _elementCount = 0;
                document      = document.ChangeSettings(document.Settings.ChangeDataSettings(
                                                            document.Settings.DataSettings.ChangeAnnotationDefs(ImmutableList.ValueOf(GetTestAnnotations()))));
                var measuredResults = document.MeasuredResults;

                if (measuredResults != null)
                {
                    var chromatograms = measuredResults.Chromatograms.ToArray();
                    for (int i = 0; i < chromatograms.Length; i++)
                    {
                        _elementCount++;
                        chromatograms[i] = chromatograms[i].ChangeAnnotations(
                            AddAnnotations(chromatograms[i].Annotations, AnnotationDef.AnnotationTarget.replicate));
                    }
                    document = document.ChangeMeasuredResults(measuredResults.ChangeChromatograms(chromatograms));
                }
                var proteins = document.MoleculeGroups.ToArray();

                for (int iProtein = 0; iProtein < proteins.Length; iProtein++)
                {
                    var protein = proteins[iProtein];
                    _elementCount++;
                    protein = (PeptideGroupDocNode)protein.ChangeAnnotations(AddAnnotations(protein.Annotations,
                                                                                            AnnotationDef.AnnotationTarget.protein));
                    var peptides = protein.Molecules.ToArray();
                    for (int iPeptide = 0; iPeptide < peptides.Length; iPeptide++)
                    {
                        var peptide = peptides[iPeptide];
                        _elementCount++;
                        peptide = (PeptideDocNode)peptide.ChangeAnnotations(AddAnnotations(peptide.Annotations,
                                                                                           AnnotationDef.AnnotationTarget.peptide));
                        var precursors = peptide.TransitionGroups.ToArray();
                        for (int iPrecursor = 0; iPrecursor < precursors.Length; iPrecursor++)
                        {
                            var precursor = precursors[iPrecursor];
                            precursor = (TransitionGroupDocNode)precursor.ChangeAnnotations(
                                AddAnnotations(precursor.Annotations,
                                               AnnotationDef.AnnotationTarget.precursor));
                            _elementCount++;
                            var transitions = precursor.Transitions.ToArray();
                            for (int iTransition = 0; iTransition < transitions.Length; iTransition++)
                            {
                                var transition = transitions[iTransition];
                                _elementCount++;
                                transition = (TransitionDocNode)transition.ChangeAnnotations(
                                    AddAnnotations(transition.Annotations,
                                                   AnnotationDef.AnnotationTarget.transition));
                                if (transition.Results != null)
                                {
                                    var results = transition.Results.ToArray();
                                    for (int replicateIndex = 0; replicateIndex < results.Length; replicateIndex++)
                                    {
                                        _elementCount          += results[replicateIndex].Count;
                                        results[replicateIndex] = new ChromInfoList <TransitionChromInfo>(
                                            results[replicateIndex]
                                            .Select(chromInfo => chromInfo.ChangeAnnotations(AddAnnotations(
                                                                                                 chromInfo.Annotations,
                                                                                                 AnnotationDef.AnnotationTarget.transition_result))));
                                    }
                                    transition = transition.ChangeResults(new Results <TransitionChromInfo>(results));
                                }
                                transitions[iTransition] = transition;
                            }
                            if (precursor.Results != null)
                            {
                                var results = precursor.Results.ToArray();
                                for (int replicateIndex = 0; replicateIndex < results.Length; replicateIndex++)
                                {
                                    _elementCount          += results[replicateIndex].Count;
                                    results[replicateIndex] = new ChromInfoList <TransitionGroupChromInfo>(
                                        results[replicateIndex].Select(chromInfo =>
                                                                       chromInfo.ChangeAnnotations(AddAnnotations(chromInfo.Annotations,
                                                                                                                  AnnotationDef.AnnotationTarget.precursor_result))));
                                }
                                precursor = precursor.ChangeResults(new Results <TransitionGroupChromInfo>(results));
                            }
                            precursor = (TransitionGroupDocNode)precursor.ChangeChildren(transitions);
                            precursors[iPrecursor] = precursor;
                        }
                        peptide            = (PeptideDocNode)peptide.ChangeChildren(precursors);
                        peptides[iPeptide] = peptide;
                    }
                    protein            = (PeptideGroupDocNode)protein.ChangeChildren(peptides);
                    proteins[iProtein] = protein;
                }
                return((SrmDocument)document.ChangeChildren(proteins));
            }
 private SrmDocument ChangeStandardConcentrationCount(SrmDocument srmDocument, int pointCount)
 {
     var measuredResults = srmDocument.Settings.MeasuredResults;
     var standardConcentrations = measuredResults.Chromatograms
         .Where(chrom => Equals(chrom.SampleType, SampleType.STANDARD))
         .Select(chrom => chrom.AnalyteConcentration)
         .OfType<double>()
         .Distinct()
         .ToArray();
     Array.Sort(standardConcentrations);
     Array.Reverse(standardConcentrations);
     var topConcentrations = new HashSet<double>(standardConcentrations.Take(pointCount));
     var newChromatograms = measuredResults.Chromatograms.ToArray();
     for (int i = 0; i < newChromatograms.Length; i++)
     {
         var chrom = newChromatograms[i];
         if (Equals(SampleType.STANDARD, chrom.SampleType) && chrom.AnalyteConcentration.HasValue)
         {
             if (!topConcentrations.Contains(chrom.AnalyteConcentration.Value))
             {
                 chrom = chrom.ChangeSampleType(SampleType.UNKNOWN);
             }
         }
         newChromatograms[i] = chrom;
     }
     measuredResults = measuredResults.ChangeChromatograms(newChromatograms);
     return srmDocument.ChangeMeasuredResults(measuredResults);
 }
Exemplo n.º 21
0
        /// <summary>
        /// Filters document chromatograms for all but a selected set of files.
        /// </summary>
        private SrmDocument FilterFiles(SrmDocument doc, Func<ChromFileInfo, bool> selectFilesToKeepFunc)
        {
            if (doc.Settings.MeasuredResults == null)
                return doc;

            var measuredResultsNew = doc.Settings.MeasuredResults.FilterFiles(selectFilesToKeepFunc);

            // If nothing changed, don't create a new document instance
            if (measuredResultsNew != null &&
                ArrayUtil.ReferencesEqual(measuredResultsNew.Chromatograms, doc.Settings.MeasuredResults.Chromatograms))
            {
                return doc;
            }

            return doc.ChangeMeasuredResults(measuredResultsNew);
        }
Exemplo n.º 22
0
        public void WatersMultiReplicateTest()
        {
            var testFilesDir = new TestFilesDir(TestContext, ZIP_FILE);

            string      docPath;
            SrmDocument docOriginal    = InitWatersDocument(testFilesDir, out docPath);
            SrmDocument doc            = docOriginal;
            var         listCachePaths = new List <string>();

            using (var docContainer = new ResultsTestDocumentContainer(doc, docPath))
            {
                string extRaw = ExtensionTestContext.ExtWatersRaw;

                string[] replicatePaths =
                {
                    testFilesDir.GetTestPath("160109_Mix1_calcurve_070.mzML"),
                    testFilesDir.GetTestPath("160109_Mix1_calcurve_073.mzML"),
                    testFilesDir.GetTestPath("160109_Mix1_calcurve_075" + extRaw),
                    testFilesDir.GetTestPath("160109_Mix1_calcurve_078.mzML")
                };
                // Count peaks where higher concentration replicates show less area
                int outOfOrder = 0;
                foreach (string path in replicatePaths)
                {
                    var listChromatograms = new List <ChromatogramSet>();
                    if (doc.Settings.HasResults)
                    {
                        listChromatograms.AddRange(doc.Settings.MeasuredResults.Chromatograms);
                    }

                    string name = Path.GetFileNameWithoutExtension(path);
                    if (name != null)
                    {
                        name = name.Substring(name.Length - 12);
                    }
                    listChromatograms.Add(new ChromatogramSet(name, new[] { MsDataFileUri.Parse(path) }));
                    int len = listChromatograms.Count;

                    var docResults = doc.ChangeMeasuredResults(doc.Settings.MeasuredResults != null
                        ? doc.Settings.MeasuredResults.ChangeChromatograms(listChromatograms)
                        : new MeasuredResults(listChromatograms));
                    // Adding unloaded results should add a new null result.
                    foreach (var nodeTran in docResults.PeptideTransitions)
                    {
                        Assert.IsTrue(nodeTran.HasResults);
                        Assert.AreEqual(listChromatograms.Count, nodeTran.Results.Count);
                        Assert.IsTrue(nodeTran.Results[len - 1].IsEmpty);
                    }

                    Assert.IsTrue(docContainer.SetDocument(docResults, doc, true),
                                  string.Format("Failed adding results for {0}.", path));
                    docContainer.AssertComplete();
                    docResults = docContainer.Document;

                    Assert.IsTrue(docResults.Settings.MeasuredResults.IsLoaded);

                    var transOld = doc.PeptideTransitions.ToArray();
                    var transNew = docResults.PeptideTransitions.ToArray();
                    Assert.AreEqual(transOld.Length, transNew.Length);
                    int countPeaks = 0;
                    for (int i = 0; i < transNew.Length; i++)
                    {
                        // Make sure new peak was added to each transition
                        var nodeTranNew = transNew[i];
                        Assert.IsTrue(nodeTranNew.HasResults);
                        Assert.AreEqual(len, nodeTranNew.Results.Count);
                        var chromInfo = nodeTranNew.Results[len - 1][0];
                        Assert.IsNotNull(chromInfo);

                        if (!chromInfo.IsEmpty)
                        {
                            countPeaks++;
                        }

                        // Make sure previously loaded peaks did not change
                        for (int j = 0; j < len - 1; j++)
                        {
                            var chromInfoPrevious = transOld[i].Results[j][0];
                            Assert.AreSame(chromInfoPrevious, nodeTranNew.Results[j][0]);
                            if ((chromInfo.IsEmpty && !chromInfoPrevious.IsEmpty) ||
                                (!chromInfo.IsEmpty && chromInfoPrevious.Area >= chromInfo.Area))
                            {
                                outOfOrder++;
                            }
                        }
                    }
                    // Allow 2 missed peaks
                    Assert.IsTrue(countPeaks >= transNew.Length - (TestSmallMolecules ? 1 : 0) - 2);

                    // Check results calculations for peptides and groups
                    foreach (var nodePep in docResults.Peptides)
                    {
                        Assert.AreEqual(len, nodePep.Results.Count);
                        Assert.IsTrue(nodePep.HasResults);
                        var chromInfo = nodePep.Results[len - 1][0];
                        Assert.AreEqual(1, nodePep.Children.Count);
                        var nodeGroup = (TransitionGroupDocNode)nodePep.Children[0];
                        Assert.IsTrue(nodeGroup.HasResults);
                        Assert.AreEqual(len, nodeGroup.Results.Count);
                        var chromInfoGroup = nodeGroup.Results[len - 1][0];
                        Assert.IsTrue(chromInfoGroup.PeakCountRatio >= 0.5);
                        Assert.IsTrue(chromInfoGroup.RetentionTime.HasValue);
                        Assert.IsTrue(chromInfoGroup.Area.HasValue && chromInfoGroup.Area > 290);

                        Assert.AreEqual(chromInfo.RetentionTime, chromInfoGroup.RetentionTime);
                        Assert.AreEqual(chromInfo.PeakCountRatio, chromInfoGroup.PeakCountRatio);
                    }

                    doc = docResults;
                }

                Assert.AreEqual(13, outOfOrder, 1);

                // Remove the original data
                foreach (string path in replicatePaths)
                {
                    if (File.Exists(path))
                    {
                        FileEx.SafeDelete(path);
                    }
                    else
                    {
                        DirectoryEx.SafeDelete(path);
                    }
                }
                FileEx.SafeDelete(docPath);

                // Save the document
                string xmlSaved     = null;
                var    docPersisted = AssertEx.RoundTrip(doc, ref xmlSaved);
                Assert.IsTrue(!docPersisted.Settings.MeasuredResults.IsLoaded);
                // Make sure the persisted document round-trips.
                // The orginal doesn't because of changing precision in the results info.
                AssertEx.Serializable(docPersisted, AssertEx.DocumentCloned);

                // Make sure the loaded document has reasonable results info
                // before the cache files are loaded
                for (int i = 0; i < doc.Children.Count; i++)
                {
                    PeptideGroupDocNode nodePepGroup1 = (PeptideGroupDocNode)doc.Children[i];
                    if (TestSmallMolecules && nodePepGroup1.Name.Equals(SrmDocument.TestingNonProteomicMoleculeGroupName))
                    {
                        continue;
                    }
                    PeptideGroupDocNode nodePepGroup2 = (PeptideGroupDocNode)docPersisted.Children[i];
                    Assert.AreNotSame(nodePepGroup1, nodePepGroup2);
                    for (int j = 0; j < nodePepGroup1.Children.Count; j++)
                    {
                        PeptideDocNode nodePep1 = (PeptideDocNode)nodePepGroup1.Children[j];
                        PeptideDocNode nodePep2 = (PeptideDocNode)nodePepGroup2.Children[j];
                        Assert.AreNotSame(nodePep1, nodePep2);
                        Assert.AreEqual(nodePep1.Results.Count, nodePep2.Results.Count);
                        for (int k = 0; k < nodePep1.Results.Count; k++)
                        {
                            Assert.AreEqual(nodePep1.Results[k][0].PeakCountRatio, nodePep2.Results[k][0].PeakCountRatio);
                        }
                        for (int k = 0; k < nodePep1.Children.Count; k++)
                        {
                            TransitionGroupDocNode nodeGroup1 = (TransitionGroupDocNode)nodePep1.Children[k];
                            TransitionGroupDocNode nodeGroup2 = (TransitionGroupDocNode)nodePep2.Children[k];
                            Assert.AreNotSame(nodeGroup1, nodeGroup2);
                            Assert.AreEqual(nodeGroup1.Results.Count, nodeGroup2.Results.Count);
                            for (int l = 0; l < nodeGroup1.Results.Count; l++)
                            {
                                Assert.AreEqual(nodeGroup1.Results[l][0].PeakCountRatio,
                                                nodeGroup2.Results[l][0].PeakCountRatio);
                            }
                            for (int l = 0; l < nodeGroup1.Children.Count; l++)
                            {
                                TransitionDocNode nodeTran1 = (TransitionDocNode)nodeGroup1.Children[l];
                                TransitionDocNode nodeTran2 = (TransitionDocNode)nodeGroup2.Children[l];
                                Assert.AreNotSame(nodeTran1, nodeTran2);
                                Assert.AreEqual(nodeTran1.Results.Count, nodeTran2.Results.Count);
                                for (int m = 0; m < nodeTran1.Results.Count; m++)
                                {
                                    if (!nodeTran1.Results[m].IsEmpty && !nodeTran2.Results[m].IsEmpty)
                                    {
                                        Assert.AreEqual(nodeTran1.Results[m][0].IsEmpty, nodeTran2.Results[m][0].IsEmpty);
                                    }
                                    else
                                    {
                                        Assert.AreEqual(nodeTran1.Results[m], nodeTran2.Results[m]); // both null
                                    }
                                }
                            }
                        }
                    }
                }

                // Reload data from .skyd files
                Assert.IsTrue(docContainer.SetDocument(docPersisted, doc, true));
                docContainer.AssertComplete();
                doc = docContainer.Document;

                var         results   = doc.Settings.MeasuredResults;
                const float tolerance = (float)TransitionInstrument.DEFAULT_MZ_MATCH_TOLERANCE;
                foreach (var pair in doc.PeptidePrecursorPairs)
                {
                    foreach (var chromSet in results.Chromatograms)
                    {
                        ChromatogramGroupInfo[] chromGroupInfo;
                        Assert.IsTrue(results.TryLoadChromatogram(chromSet, pair.NodePep, pair.NodeGroup,
                                                                  tolerance, true, out chromGroupInfo));
                    }
                }

                // The single final cache path should be open now
                listCachePaths.AddRange(doc.Settings.MeasuredResults.CachePaths);
                // Should only have one cache file at this point
                Assert.AreEqual(1, listCachePaths.Count);
                foreach (var cachePath in listCachePaths)
                {
                    // Attempting to delete should throw
                    string path = cachePath;
                    AssertEx.ThrowsException <IOException>(() => FileEx.SafeDelete(path));
                }
            }

            foreach (var cachePath in listCachePaths)
            {
                // Cache files should be closed now, and delete successfully.
                FileEx.SafeDelete(cachePath);
            }
            testFilesDir.Dispose();
        }
Exemplo n.º 23
0
        public void WatersMultiFileTest()
        {
            var testFilesDir = new TestFilesDir(TestContext, ZIP_FILE);

            string      docPath;
            SrmDocument docOriginal = InitWatersDocument(testFilesDir, out docPath);
            SrmDocument doc         = docOriginal;

            using (var docContainer = new ResultsTestDocumentContainer(doc, docPath))
            {
                string extRaw = ExtensionTestContext.ExtWatersRaw;

                var listChromatograms = new List <ChromatogramSet>
                {
                    new ChromatogramSet("double", new[]
                    {
                        MsDataFileUri.Parse(testFilesDir.GetTestPath("160109_Mix1_calcurve_070.mzML")),
                        MsDataFileUri.Parse(testFilesDir.GetTestPath("160109_Mix1_calcurve_073.mzML"))
                    }),
                    new ChromatogramSet("trouble", new[]
                    {
                        MsDataFileUri.Parse(testFilesDir.GetTestPath("160109_Mix1_calcurve_075" + extRaw)),
                        MsDataFileUri.Parse(testFilesDir.GetTestPath("160109_Mix1_calcurve_078.mzML"))
                    })
                };
                var docResults = doc.ChangeMeasuredResults(new MeasuredResults(listChromatograms));
                Assert.IsTrue(docContainer.SetDocument(docResults, doc, true));
                docContainer.AssertComplete();
                docResults = docContainer.Document;
                Assert.IsTrue(docResults.Settings.HasResults);
                var measuredResults = docResults.Settings.MeasuredResults;
                var chromatograms   = measuredResults.Chromatograms;
                Assert.AreEqual(2, chromatograms.Count);

                const float tolerance = (float)TransitionInstrument.DEFAULT_MZ_MATCH_TOLERANCE;

                foreach (var pair in docResults.PeptidePrecursorPairs)
                {
                    var nodePep   = pair.NodePep;
                    var nodeGroup = pair.NodeGroup;
                    Assert.IsTrue(nodeGroup.HasResults);
                    Assert.AreEqual(2, nodeGroup.Results.Count);
                    foreach (var result in nodeGroup.Results)
                    {
                        Assert.AreEqual(2, result.Count);
                    }
                    for (int i = 0; i < 2; i++)
                    {
                        ChromatogramGroupInfo[] chromInfos;
                        Assert.IsTrue(measuredResults.TryLoadChromatogram(i, nodePep, nodeGroup, tolerance, true, out chromInfos));
                        Assert.AreEqual(2, chromInfos.Length);
                        double[] peakAreas = new double[2];
                        for (int j = 0; j < 2; j++)
                        {
                            var chromInfo = chromInfos[j];
                            Assert.IsTrue(chromInfo.BestPeakIndex != -1);
                            foreach (var tranInfo in chromInfo.TransitionPointSets)
                            {
                                var peakInfo = tranInfo.GetPeak(chromInfo.BestPeakIndex);
                                if (peakInfo.IsEmpty || peakInfo.IsForcedIntegration)
                                {
                                    continue;
                                }

                                // Check times
                                var times  = tranInfo.Times;
                                int iStart = CollectionUtil.BinarySearch(times, peakInfo.StartTime);
                                Assert.IsTrue(iStart >= 0);
                                int iEnd = CollectionUtil.BinarySearch(times, peakInfo.EndTime);
                                Assert.IsTrue(iEnd >= 0);
                                int iPeak = CollectionUtil.BinarySearch(times, peakInfo.RetentionTime);
                                // Check intensities at times
                                var intensities = tranInfo.Intensities;
                                Assert.IsTrue(intensities[iStart] < intensities[iPeak]);
                                Assert.IsTrue(intensities[iEnd] < intensities[iPeak]);
                                // Sum peak area
                                peakAreas[j] += peakInfo.Area;
                            }
                        }
                        Assert.IsTrue(peakAreas[0] < peakAreas[1]);
                    }
                }
            }
            testFilesDir.Dispose();
        }