Beispiel #1
0
        private bool Load(ILoadMonitor loader)
        {
            ProgressStatus status = new ProgressStatus(string.Format(Resources.ChromatogramLibrary_Load_Loading__0_, Name));

            loader.UpdateProgress(status);
            if (LoadFromCache(loader, status))
            {
                loader.UpdateProgress(status.Complete());
                return(true);
            }
            if (LoadLibraryFromDatabase(loader))
            {
                using (var fileSaver = new FileSaver(CachePath, loader.StreamManager))
                {
                    using (var stream = loader.StreamManager.CreateStream(fileSaver.SafeName, FileMode.Create, true))
                    {
                        var serializer = new Serializer(this, stream);
                        serializer.Write();
                        loader.StreamManager.Finish(stream);
                        fileSaver.Commit();
                        loader.StreamManager.SetCache(FilePath, CachePath);
                    }
                }
                loader.UpdateProgress(status.Complete());
                return(true);
            }
            return(false);
        }
Beispiel #2
0
        private bool Load(ILoadMonitor loader)
        {
            ProgressStatus status = new ProgressStatus(string.Empty);

            loader.UpdateProgress(status);

            bool cached = loader.StreamManager.IsCached(FilePath, CachePath);

            if (Load(loader, status, cached))
            {
                return(true);
            }

            // If loading from the cache failed, rebuild it.
            if (cached)
            {
                // Reset readStream so we don't read corrupt file.
                _readStream = null;
                if (Load(loader, status, false))
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #3
0
        private bool Load(ILoadMonitor loader)
        {
            try
            {
                if (LoadFromCache(loader))
                {
                    return(true);
                }

                if (LoadLibraryFromDatabase(loader))
                {
                    WriteCache(loader);
                    return(true);
                }
            }
            catch (Exception e)
            {
                if (!loader.IsCanceled)
                {
                    var msgException = new ApplicationException(string.Format(Resources.BiblioSpecLiteLibrary_Load_Failed_loading_library__0__, FilePath), e);
                    loader.UpdateProgress(new ProgressStatus().ChangeErrorException(msgException));
                }
            }
            return(false);
        }
        public Digestion Digest(Enzyme enzyme, DigestSettings digestSettings, ILoadMonitor loader)
        {
            IProgressStatus progressStatus = new ProgressStatus(string.Format(Resources.BackgroundProteomeSpec_Digest_Digesting__0__, enzyme.Name));

            loader.UpdateProgress(progressStatus);
            using (var proteomeDb = OpenProteomeDb())
            {
                return(proteomeDb.Digest(new ProteaseImpl(enzyme), digestSettings.MaxMissedCleavages, loader, ref progressStatus));
            }
        }
Beispiel #5
0
        public Digestion Digest(Enzyme enzyme, DigestSettings digestSettings, ILoadMonitor loader)
        {
            ProgressStatus progressStatus = new ProgressStatus(string.Format(Resources.BackgroundProteomeSpec_Digest_Digesting__0__, enzyme.Name));

            using (var proteomeDb = OpenProteomeDb())
            {
                return(proteomeDb.Digest(new ProteaseImpl(enzyme),
                                         (s, i) =>
                {
                    loader.UpdateProgress(progressStatus.ChangePercentComplete(i));
                    return !loader.IsCanceled;
                }));
            }
        }
Beispiel #6
0
        public bool CompleteDocument(SrmDocument document, ILoadMonitor loadMonitor)
        {
            // Refuse completion, if anything is still not final
            var status   = Status;
            var notFinal = status.ProgressList.Where(s => !s.IsFinal).ToArray();

            if (notFinal.Any())
            {
                // Write output in attempt to associate new hangs in nightly tests with the return false below
//                Console.WriteLine(TextUtil.LineSeparate(@"*** Attempt to complete document with non-final status ***",
//                    TextUtil.LineSeparate(notFinal.Select(s => string.Format(@"{0} {1}% - {2}", s.State, s.PercentComplete, s.FilePath)))));
                return(false);
            }

            if (status.ProgressList.Any())
            {
                loadMonitor.UpdateProgress(CompleteStatus());
            }
            ClearDocument(document);
            return(true);
        }
Beispiel #7
0
        // ReSharper restore UnusedMember.Local

        private bool CreateCache(ILoadMonitor loader, IProgressStatus status, int percent)
        {
            var sm = loader.StreamManager;

            BufferedStream stream = new BufferedStream(CreateStream(loader), 32 * 1024);

            int    version = 1;
            string id = string.Empty, revision = string.Empty;
            int    size = ReadSize(stream);
            int    i;

            if (size == 0)
            {
                version = 2;
                size    = ReadSize(stream);

                const int countLibHeader = 256 - 8;
                byte[]    libHeader      = new byte[countLibHeader];
                if (stream.Read(libHeader, 0, libHeader.Length) != libHeader.Length)
                {
                    throw new InvalidDataException(Resources.XHunterLibrary_CreateCache_Data_truncation_in_library_header_File_may_be_corrupted);
                }

                for (i = 0; i < libHeader.Length; i++)
                {
                    if (libHeader[i] == 0)
                    {
                        break;
                    }
                }

                string header = Encoding.UTF8.GetString(libHeader, 0, i);
                Match  match  = REGEX_HEADER.Match(header);
                if (match.Success)
                {
                    version  = int.Parse(match.Groups[1].Value);
                    id       = match.Groups[2].Value;
                    revision = match.Groups[3].Value;
                }
            }
            var setLibKeys     = new HashSet <LibKey>(size);
            var libraryEntries = new List <XHunterSpectrumInfo>(size);

            const int countHeader = ((int)SpectrumHeaders2.count) * sizeof(int);

            byte[] specHeader   = new byte[1024];
            byte[] specSequence = new byte[1024];
            i = 0;

            while (stream.Read(specHeader, 0, countHeader) == countHeader)
            {
                int percentComplete = (i++ *percent / size);
                if (status.PercentComplete != percentComplete)
                {
                    // Check for cancellation after each integer change in percent loaded.
                    if (loader.IsCanceled)
                    {
                        loader.UpdateProgress(status.Cancel());
                        return(false);
                    }

                    // If not cancelled, update progress.
                    loader.UpdateProgress(status = status.ChangePercentComplete(percentComplete));
                }

                int charge = (version == 1
                                  ? GetInt32(specHeader, ((int)SpectrumHeaders1.charge))
                                  : GetInt32(specHeader, ((int)SpectrumHeaders2.charge)));

                float i2 = (version == 1
                                ? GetSingle(specHeader, ((int)SpectrumHeaders1.i2))
                                : GetSingle(specHeader, ((int)SpectrumHeaders2.i2)));
                int seqLength = (version == 1
                                     ? GetInt32(specHeader, ((int)SpectrumHeaders1.seq_len))
                                     : GetInt32(specHeader, ((int)SpectrumHeaders2.seq_len)));

                float expect = (version == 1 ? 0.001f : GetSingle(specHeader, (int)SpectrumHeaders2.expect));


                // Read sequence information
                ReadComplete(stream, specSequence, seqLength);
                specSequence[seqLength] = 0;

                short numPeaks = (short)ReadSize(stream);

                // Save spectrum location
                long location = stream.Position;

                // Read over spectrum
                int countPeaks = (sizeof(byte) + sizeof(float)) * numPeaks;
                stream.Seek(countPeaks, SeekOrigin.Current); // Skip spectrum

                // Read modifications
                int    numMods          = ReadSize(stream);
                string modifiedSequence = Encoding.UTF8.GetString(specSequence, 0, seqLength);
                if (numMods > 0)
                {
                    StringBuilder sb = new StringBuilder();

                    ReadComplete(stream, specHeader, (4 + sizeof(double)) * numMods);
                    int    iLast    = 0;
                    double modTotal = 0;
                    for (int j = 0; j < numMods; j++)
                    {
                        int    iPos = GetInt32(specHeader, j * 3);
                        double mod  = BitConverter.ToDouble(specHeader, (j * 3 + 1) * 4);

                        // X! Hunter allows multiple modifications on the same
                        // residue.  So, they need to be totaled, and assigned to a
                        // single residue to allow them to match Skyline modification
                        // settings.
                        if (iPos > iLast)
                        {
                            if (modTotal != 0)
                            {
                                sb.Append(SequenceMassCalc.GetModDiffDescription(modTotal));
                            }
                            sb.Append(Encoding.UTF8.GetString(specSequence, iLast, iPos - iLast));

                            modTotal = 0;
                        }
                        modTotal += mod;
                        iLast     = iPos;
                    }
                    if (modTotal != 0)
                    {
                        sb.Append(SequenceMassCalc.GetModDiffDescription(modTotal));
                    }
                    sb.Append(Encoding.UTF8.GetString(specSequence, iLast, seqLength - iLast));
                    modifiedSequence = sb.ToString();
                }

                // Skip over homologs (list of protein IDs and start positions from a FASTA
                // file used to generate the library)
                int numHomologs = ReadSize(stream);
                for (int j = 0; j < numHomologs; j++)
                {
                    stream.Seek(ReadSize(stream) + 4, SeekOrigin.Current);
                }

                // These libraries should not have duplicates, but just in case.
                // Apparently, GPM libraries do contain redundancies, as we found
                // when a revision lost this test.
                var key = new LibKey(modifiedSequence, charge);
                if (setLibKeys.Add(key))
                {
                    libraryEntries.Add(new XHunterSpectrumInfo(key, i2, expect, numPeaks, location));
                }
            }

            libraryEntries = FilterInvalidLibraryEntries(ref status, libraryEntries);

            using (FileSaver fs = new FileSaver(CachePath, sm))
                using (Stream outStream = sm.CreateStream(fs.SafeName, FileMode.Create, true))
                {
                    foreach (var info in libraryEntries)
                    {
                        info.Key.Write(outStream);
                        PrimitiveArrays.WriteOneValue(outStream, info.Location);
                        PrimitiveArrays.WriteOneValue(outStream, info.ProcessedIntensity);
                        PrimitiveArrays.WriteOneValue(outStream, info.NumPeaks);
                        PrimitiveArrays.WriteOneValue(outStream, info.Expect);
                    }

                    byte[] revisionBytes = Encoding.UTF8.GetBytes(revision);
                    outStream.Write(revisionBytes, 0, revisionBytes.Length);
                    byte[] idBytes = Encoding.UTF8.GetBytes(id);
                    outStream.Write(idBytes, 0, idBytes.Length);
                    outStream.Write(BitConverter.GetBytes(revisionBytes.Length), 0, sizeof(int));
                    outStream.Write(BitConverter.GetBytes(idBytes.Length), 0, sizeof(int));
                    outStream.Write(BitConverter.GetBytes(FORMAT_VERSION_CACHE), 0, sizeof(int));
                    outStream.Write(BitConverter.GetBytes(libraryEntries.Count), 0, sizeof(int));
                    outStream.Write(BitConverter.GetBytes((long)0), 0, sizeof(long));

                    sm.Finish(outStream);
                    fs.Commit();
                    sm.SetCache(FilePath, CachePath);
                }

            loader.UpdateProgress(status.Complete());

            return(true);
        }
Beispiel #8
0
        public static void AddSpectra(MidasLibSpec libSpec, MsDataFilePath[] resultsFiles, SrmDocument doc, ILoadMonitor monitor, out List <MsDataFilePath> failedFiles)
        {
            // Get spectra from results files
            var       newSpectra          = new List <DbSpectrum>();
            var       progress            = new ProgressStatus(string.Empty).ChangeMessage(Resources.MidasLibrary_AddSpectra_Reading_MIDAS_spectra);
            const int percentResultsFiles = 80;

            failedFiles = new List <MsDataFilePath>();
            for (var i = 0; i < resultsFiles.Length; i++)
            {
                var resultsFile = resultsFiles[i];
                try
                {
                    monitor.UpdateProgress(progress.ChangePercentComplete(i * percentResultsFiles / resultsFiles.Length));

                    var filePath = resultsFile.GetFilePath();
                    if (File.Exists(filePath))
                    {
                        var sampleIndex = resultsFile.GetSampleIndex();
                        using (var msd = new MsDataFileImpl(filePath, sampleIndex == -1 ? 0 : sampleIndex, resultsFile.GetLockMassParameters(), requireVendorCentroidedMS2: true))
                        {
                            if (ChromatogramDataProvider.HasChromatogramData(msd) && SpectraChromDataProvider.HasSpectrumData(msd))
                            {
                                var chromPrecursors = ReadChromPrecursorsFromMsd(msd, monitor).ToList();
                                newSpectra.AddRange(ReadDbSpectraFromMsd(msd, monitor));
                                MatchSpectraToChrom(newSpectra, chromPrecursors, monitor);
                            }
                        }

                        MatchSpectraToPeptides(newSpectra, doc, monitor);
                    }
                    else
                    {
                        failedFiles.Add(resultsFile);
                    }
                }
                catch (Exception x)
                {
                    monitor.UpdateProgress(progress.ChangeErrorException(x));
                    failedFiles.Add(resultsFile);
                }
                if (monitor.IsCanceled)
                {
                    monitor.UpdateProgress(progress.Cancel());
                    return;
                }
            }

            if (!newSpectra.Any())
            {
                monitor.UpdateProgress(progress.Complete());
                return;
            }

            progress = progress.ChangePercentComplete(percentResultsFiles);
            monitor.UpdateProgress(progress);

            // Add spectra to library
            var midasLib = !File.Exists(libSpec.FilePath) ? Create(libSpec) : Load(libSpec, monitor);

            if (midasLib == null)
            {
                monitor.UpdateProgress(progress.ChangeErrorException(new Exception(Resources.MidasLibrary_AddSpectra_Error_loading_MIDAS_library_for_adding_spectra_)));
                return;
            }

            progress = progress.ChangeMessage(Resources.MidasLibrary_AddSpectra_Adding_spectra_to_MIDAS_library);
            monitor.UpdateProgress(progress);

            var results = new Dictionary <string, DbResultsFile>();

            if (midasLib._spectra != null)
            {
                foreach (var kvp in midasLib._spectra)
                {
                    results[kvp.Key.FilePath] = kvp.Key;
                }
            }

            using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory(libSpec.FilePath, typeof(MidasLibrary), false))
                using (var session = new SessionWithLock(sessionFactory.OpenSession(), new ReaderWriterLock(), true))
                    using (var transaction = session.BeginTransaction())
                    {
                        for (var i = 0; i < newSpectra.Count; i++)
                        {
                            if (monitor.IsCanceled)
                            {
                                transaction.Rollback();
                                monitor.UpdateProgress(progress.Cancel());
                                return;
                            }
                            var spectrum = newSpectra[i];

                            monitor.UpdateProgress(progress.ChangePercentComplete(percentResultsFiles + (int)(100.0 * i / newSpectra.Count)));

                            DbResultsFile resultsFile;
                            if (!results.TryGetValue(spectrum.ResultsFile.FilePath, out resultsFile))
                            {
                                resultsFile = new DbResultsFile(spectrum.ResultsFile)
                                {
                                    Id = null
                                };
                                results[spectrum.ResultsFile.FilePath] = resultsFile;
                                session.SaveOrUpdate(resultsFile);
                            }
                            else if (midasLib._spectra != null)
                            {
                                List <DbSpectrum> existingSpectra;
                                if (midasLib._spectra.TryGetValue(resultsFile, out existingSpectra) &&
                                    existingSpectra.Any(x => Equals(x.ResultsFile.FilePath, spectrum.ResultsFile.FilePath) &&
                                                        Equals(x.PrecursorMz, spectrum.PrecursorMz) &&
                                                        Equals(x.RetentionTime, spectrum.RetentionTime)))
                                {
                                    // This spectrum already exists in the library
                                    continue;
                                }
                            }
                            var spectrumNewDisconnected = new DbSpectrum(spectrum)
                            {
                                Id = null, ResultsFile = resultsFile
                            };
                            session.SaveOrUpdate(spectrumNewDisconnected);
                        }
                        transaction.Commit();
                        monitor.UpdateProgress(progress.Complete());
                    }
        }
Beispiel #9
0
        // ReSharper disable LocalizableElement
        private bool LoadLibraryFromDatabase(ILoadMonitor loader)
        {
            var status = new ProgressStatus(
                string.Format(Resources.ChromatogramLibrary_LoadLibraryFromDatabase_Reading_precursors_from__0_,
                              Name));

            try
            {
                loader.UpdateProgress(status);
                var libKeySourceFileDatas = new Dictionary <Tuple <string, int>, Dictionary <string, Tuple <double?, FileData> > >();

                var scores = new EncyclopeDiaScores();
                scores.ReadScores(_pooledSqliteConnection.Connection);
                HashSet <Tuple <string, int> > quantPeptides = new HashSet <Tuple <string, int> >();

                using (var cmd = new SQLiteCommand(_pooledSqliteConnection.Connection))
                {
                    // From the "entries" table, read all of the peptides that were actually found
                    cmd.CommandText =
                        "SELECT PeptideModSeq, PrecursorCharge, SourceFile, Score, RTInSeconds, RTInSecondsStart, RTInSecondsStop FROM entries";
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            if (loader.IsCanceled)
                            {
                                throw new OperationCanceledException();
                            }
                            var libKey = Tuple.Create(reader.GetString(0), Convert.ToInt32(reader.GetValue(1)));
                            // Tuple of filename, score, FileData
                            Dictionary <string, Tuple <double?, FileData> > dataByFilename;

                            if (!libKeySourceFileDatas.TryGetValue(libKey, out dataByFilename))
                            {
                                dataByFilename = new Dictionary <string, Tuple <double?, FileData> >();
                                libKeySourceFileDatas.Add(libKey, dataByFilename);
                            }
                            string fileName = reader.GetString(2);
                            if (dataByFilename.ContainsKey(fileName))
                            {
                                continue;
                            }
                            double score  = reader.GetDouble(3);
                            var    qValue = scores.GetQValue(libKey.Item1, libKey.Item2, fileName) ??
                                            ExplicitPeakBounds.UNKNOWN_SCORE;
                            dataByFilename.Add(fileName, Tuple.Create((double?)score,
                                                                      new FileData(reader.GetDouble(4) / 60,
                                                                                   new ExplicitPeakBounds(reader.GetDouble(5) / 60, reader.GetDouble(6) / 60, qValue))));
                        }
                    }

                    // Also, read the PeptideQuants table in order to get peak boundaries for any peptide&sourcefiles that were
                    // not found in the Entries table.
                    cmd.CommandText =
                        "SELECT PeptideModSeq, PrecursorCharge, SourceFile, RTInSecondsStart, RTInSecondsStop FROM PeptideQuants";
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var libKey = Tuple.Create(reader.GetString(0), Convert.ToInt32(reader.GetValue(1)));
                            quantPeptides.Add(libKey);
                            // Tuple of filename, score, FileData
                            Dictionary <string, Tuple <double?, FileData> > dataByFilename;
                            if (!libKeySourceFileDatas.TryGetValue(libKey, out dataByFilename))
                            {
                                dataByFilename = new Dictionary <string, Tuple <double?, FileData> >();
                                libKeySourceFileDatas.Add(libKey, dataByFilename);
                            }
                            string fileName = reader.GetString(2);
                            if (dataByFilename.ContainsKey(fileName))
                            {
                                continue;
                            }
                            double qValue = scores.GetQValue(libKey.Item1, libKey.Item2, fileName) ??
                                            ExplicitPeakBounds.UNKNOWN_SCORE;
                            dataByFilename.Add(fileName,
                                               Tuple.Create((double?)null,
                                                            new FileData(null, new ExplicitPeakBounds(reader.GetDouble(3) / 60, reader.GetDouble(4) / 60, qValue))));
                        }
                    }
                }
                // ReSharper disable PossibleMultipleEnumeration
                var sourceFiles = libKeySourceFileDatas
                                  .SelectMany(entry => entry.Value.Keys)
                                  .Distinct()
                                  .ToArray();
                Array.Sort(sourceFiles);
                var sourceFileIds = sourceFiles.Select((file, index) => Tuple.Create(file, index))
                                    .ToDictionary(tuple => tuple.Item1, tuple => tuple.Item2);
                var spectrumInfos = libKeySourceFileDatas
                                    .Where(entry => quantPeptides.Contains(entry.Key))
                                    .Select(entry => MakeSpectrumInfo(entry.Key.Item1, entry.Key.Item2, entry.Value, sourceFileIds));
                SetLibraryEntries(spectrumInfos);
                _sourceFiles = ImmutableList.ValueOf(sourceFiles);
                // ReSharper restore PossibleMultipleEnumeration
                loader.UpdateProgress(status.Complete());
                return(true);
            }
            catch (Exception e)
            {
                e = new InvalidDataException(string.Format(Resources.BiblioSpecLiteLibrary_Load_Failed_loading_library__0__, FilePath), e);
                loader.UpdateProgress(status.ChangeErrorException(e));
                return(false);
            }
        }
 public Digestion Digest(Enzyme enzyme, DigestSettings digestSettings, ILoadMonitor loader)
 {
     ProgressStatus progressStatus = new ProgressStatus(string.Format(Resources.BackgroundProteomeSpec_Digest_Digesting__0__, enzyme.Name));
     using (var proteomeDb = OpenProteomeDb())
     {
         return proteomeDb.Digest(new ProteaseImpl(enzyme),
                                     (s, i) =>
                                     {
                                         loader.UpdateProgress(progressStatus.ChangePercentComplete(i));
                                         return !loader.IsCanceled;
                                     });
     }
 }
Beispiel #11
0
        private bool LoadLibraryFromDatabase(ILoadMonitor loader)
        {
            try
            {
                var status = new ProgressStatus(string.Format(Resources.ChromatogramLibrary_LoadLibraryFromDatabase_Reading_precursors_from__0_, Name));
                loader.UpdateProgress(status);
                //                _pooledSessionFactory = new PooledSessionFactory(loader.StreamManager.ConnectionPool,
//                                                                 typeof (ChromLibEntity), FilePath);
                using (var session = _pooledSessionFactory.Connection.OpenSession())
                {
                    var libInfo =
                        session.CreateSQLQuery("SELECT PanoramaServer, LibraryRevision, SchemaVersion FROM LibInfo") // Not L10N
                        .UniqueResult <object[]>();
                    PanoramaServer  = Convert.ToString(libInfo[0]);
                    LibraryRevision = Convert.ToInt32(libInfo[1]);
                    SchemaVersion   = Convert.ToString(libInfo[2]);

                    try
                    {
                        var irtQuery = session.CreateQuery("SELECT PeptideModSeq, Irt, TimeSource FROM IrtLibrary"); // Not L10N
                        _libraryIrts = irtQuery.List <object[]>().Select(
                            irt => new ChromatogramLibraryIrt(new Target((string)irt[0]), (TimeSource)irt[2], Convert.ToDouble(irt[1]))
                            ).ToArray();
                    }
                    catch (GenericADOException)
                    {
                        // IrtLibrary table probably doesn't exist
                    }

                    var rtQuery      = session.CreateQuery("SELECT Precursor.Id, SampleFile.Id, RetentionTime FROM PrecursorRetentionTime"); // Not L10N
                    var rtDictionary = new Dictionary <int, List <KeyValuePair <int, double> > >();                                          // PrecursorId -> [SampleFileId -> RetentionTime]
                    foreach (object[] row in rtQuery.List <object[]>())
                    {
                        var precursorId  = (int)row[0];
                        var sampleFileId = (int)row[1];
                        var rt           = Convert.ToDouble(row[2]);
                        if (!rtDictionary.ContainsKey(precursorId))
                        {
                            rtDictionary.Add(precursorId, new List <KeyValuePair <int, double> >());
                        }
                        rtDictionary[precursorId].Add(new KeyValuePair <int, double>(sampleFileId, rt));
                    }

                    var precursorQuery =
                        session.CreateQuery("SELECT P.Id, P.ModifiedSequence, P.Charge, P.TotalArea FROM " + typeof(Precursor) + // Not L10N
                                            " P");                                                                               // Not L10N
                    var allTransitionAreas = ReadAllTransitionAreas(session);
                    var spectrumInfos      = new List <ChromLibSpectrumInfo>();
                    foreach (object[] row in precursorQuery.List <object[]>())
                    {
                        var id = (int)row[0];
                        if (row[1] == null || row[2] == null)
                        {
                            continue; // Throw an error?
                        }
                        var    modifiedSequence = new Target((string)row[1]);
                        var    charge           = (int)row[2]; // TODO(bspratt) generalize chromatogram libs to small mol
                        double totalArea        = Convert.ToDouble(row[3]);
                        List <KeyValuePair <int, double> > retentionTimes;
                        var indexedRetentionTimes = new IndexedRetentionTimes();
                        if (rtDictionary.TryGetValue(id, out retentionTimes))
                        {
                            indexedRetentionTimes = new IndexedRetentionTimes(retentionTimes);
                        }
                        Target modSeqNormal;
                        try
                        {
                            modSeqNormal = SequenceMassCalc.NormalizeModifiedSequence(modifiedSequence);
                        }
                        catch (ArgumentException)
                        {
                            continue;
                        }

                        var libKey = new LibKey(modSeqNormal.Sequence, charge);
                        IList <SpectrumPeaksInfo.MI> transitionAreas;
                        allTransitionAreas.TryGetValue(id, out transitionAreas);
                        spectrumInfos.Add(new ChromLibSpectrumInfo(libKey, id, totalArea, indexedRetentionTimes, transitionAreas));
                    }
                    SetLibraryEntries(spectrumInfos);

                    var sampleFileQuery =
                        session.CreateQuery("SELECT Id, FilePath, SampleName, AcquiredTime, ModifiedTime, InstrumentIonizationType, " + // Not L10N
                                            "InstrumentAnalyzer, InstrumentDetector FROM SampleFile");                                  // Not L10N
                    var sampleFiles = new List <ChromatogramLibrarySourceInfo>();
                    foreach (object[] row in sampleFileQuery.List <object[]>())
                    {
                        var id = (int)row[0];
                        if (row[1] == null || row[2] == null)
                        {
                            continue; // Throw an error?
                        }
                        var filePath                 = row[1].ToString();
                        var sampleName               = row[2].ToString();
                        var acquiredTime             = row[3] != null ? row[3].ToString() : string.Empty;
                        var modifiedTime             = row[4] != null ? row[4].ToString() : string.Empty;
                        var instrumentIonizationType = row[5] != null ? row[5].ToString() : string.Empty;
                        var instrumentAnalyzer       = row[6] != null ? row[6].ToString() : string.Empty;
                        var instrumentDetector       = row[7] != null ? row[7].ToString() : string.Empty;
                        sampleFiles.Add(new ChromatogramLibrarySourceInfo(id, filePath, sampleName, acquiredTime, modifiedTime, instrumentIonizationType,
                                                                          instrumentAnalyzer, instrumentDetector));
                    }
                    _librarySourceFiles = sampleFiles.ToArray();

                    loader.UpdateProgress(status.Complete());
                    return(true);
                }
            }
            catch (Exception e)
            {
                Trace.TraceWarning(Resources.ChromatogramLibrary_LoadLibraryFromDatabase_Error_loading_chromatogram_library__0_, e);
                return(false);
            }
        }
Beispiel #12
0
        protected void Complete(Exception x)
        {
            lock (this)
            {
                ChromatogramCache result = null;
                try
                {
                    if (x == null && !_status.IsFinal)
                    {
                        long locationScanIds = 0, countBytesScanIds = 0;
                        if (_fs.Stream != null)
                        {
                            try
                            {
                                locationScanIds   = _fs.Stream.Position;
                                countBytesScanIds = _fsScans.Stream.Position;

                                ChromatogramCache.WriteStructs(_fs.Stream,
                                                               _fsScans.Stream,
                                                               _fsPeaks.Stream,
                                                               _fsScores.Stream,
                                                               _listCachedFiles,
                                                               _listGroups,
                                                               _listTransitions,
                                                               _listTextIdBytes,
                                                               _listScoreTypes,
                                                               _scoreCount,
                                                               _peakCount);

                                _loader.StreamManager.Finish(_fs.Stream);
                                _fs.Stream = null;
                                // Allow cancelation right up to the final commit
                                if (!_loader.IsCanceled)
                                {
                                    _fs.Commit(_destinationStream);
                                }
                                else
                                {
                                    _loader.UpdateProgress(_status = _status.Cancel());
                                }
                            }
                            catch (Exception xWrite)
                            {
                                throw new IOException(TextUtil.LineSeparate(string.Format(Resources.ChromCacheWriter_Complete_Failure_attempting_to_write_the_file__0_, _fs.RealName), xWrite.Message));
                            }
                        }

                        if (!_status.IsCanceled)
                        {
                            // Create stream identifier, but do not open.  The stream will be opened
                            // the first time the document uses it.
                            var readStream = _loader.StreamManager.CreatePooledStream(CachePath, false);

                            // DebugLog.Info("{0}. {1} - created", readStream.GlobalIndex, CachePath);

                            _fsPeaks.Stream.Seek(0, SeekOrigin.Begin);
                            _fsScores.Stream.Seek(0, SeekOrigin.Begin);
                            var rawData = new ChromatogramCache.RawData
                            {
                                FormatVersion       = ChromatogramCache.FORMAT_VERSION_CACHE,
                                ChromCacheFiles     = _listCachedFiles.ToArray(),
                                ChromatogramEntries = _listGroups.ToArray(),
                                ChromTransitions    = _listTransitions.ToArray(),
                                ChromatogramPeaks   = new BlockedArray <ChromPeak>(
                                    count => ChromPeak.ReadArray(_fsPeaks.FileStream, count), _peakCount,
                                    ChromPeak.SizeOf, ChromPeak.DEFAULT_BLOCK_SIZE),
                                ScoreTypes = _listScoreTypes.ToArray(),
                                Scores     = new BlockedArray <float>(
                                    count => PrimitiveArrays.Read <float>(_fsScores.FileStream, count), _scoreCount,
                                    sizeof(float), ChromatogramCache.DEFAULT_SCORES_BLOCK_SIZE),
                                TextIdBytes       = _listTextIdBytes.ToArray(),
                                LocationScanIds   = locationScanIds,
                                CountBytesScanIds = countBytesScanIds,
                            };
                            result  = new ChromatogramCache(CachePath, rawData, readStream);
                            _status = _status.Complete();
                            _loader.UpdateProgress(_status);
                        }
                    }
                }
                catch (Exception x2)
                {
                    x = x2;
                }
                finally
                {
                    Dispose();
                }

                try
                {
                    _completed(result, x == null ? _status : _status.ChangeErrorException(x));
                }
                catch (Exception x2)
                {
                    _completed(null, _status.ChangeErrorException(x2));
                }
            }
        }
Beispiel #13
0
        private bool LoadLibraryFromDatabase(ILoadMonitor loader)
        {
            try
            {
                var status = new ProgressStatus(
                    string.Format(Resources.ChromatogramLibrary_LoadLibraryFromDatabase_Reading_precursors_from__0_,
                                  Name));
                loader.UpdateProgress(status);
                var libKeySourceFileDatas = new Dictionary <Tuple <string, int>, Dictionary <string, Tuple <double?, FileData> > >();

                using (var cmd = new SQLiteCommand(_pooledSqliteConnection.Connection))
                {
                    // From the "entries" table, read all of the peptides that were actually found
                    cmd.CommandText =
                        "SELECT PeptideModSeq, PrecursorCharge, SourceFile, Score, RTInSeconds, RTInSecondsStart, RTInSecondsStop FROM entries"; // Not L10N
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            if (loader.IsCanceled)
                            {
                                throw new OperationCanceledException();
                            }
                            var libKey = Tuple.Create(reader.GetString(0), Convert.ToInt32(reader.GetValue(1)));
                            // Tuple of filename, score, FileData
                            Dictionary <string, Tuple <double?, FileData> > dataByFilename;

                            if (!libKeySourceFileDatas.TryGetValue(libKey, out dataByFilename))
                            {
                                dataByFilename = new Dictionary <string, Tuple <double?, FileData> >();
                                libKeySourceFileDatas.Add(libKey, dataByFilename);
                            }
                            string fileName = reader.GetString(2);
                            if (dataByFilename.ContainsKey(fileName))
                            {
                                continue;
                            }
                            double score = -reader.GetDouble(3);
                            dataByFilename.Add(fileName, Tuple.Create((double?)score,
                                                                      new FileData(reader.GetDouble(4) / 60,
                                                                                   new PeakBounds(reader.GetDouble(5) / 60, reader.GetDouble(6) / 60))));
                        }
                    }
                    // Also, read the PeptideQuants table in order to get peak boundaries for any peptide&sourcefiles that were
                    // not found in the Entries table.
                    cmd.CommandText =
                        "SELECT PeptideModSeq, PrecursorCharge, SourceFile, RTInSecondsStart, RTInSecondsStop from PeptideQuants"; // Not L10N
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var libKey = Tuple.Create(reader.GetString(0), Convert.ToInt32(reader.GetValue(1)));
                            // Tuple of filename, score, FileData
                            Dictionary <string, Tuple <double?, FileData> > dataByFilename;
                            if (!libKeySourceFileDatas.TryGetValue(libKey, out dataByFilename))
                            {
                                dataByFilename = new Dictionary <string, Tuple <double?, FileData> >();
                                libKeySourceFileDatas.Add(libKey, dataByFilename);
                            }
                            string fileName = reader.GetString(2);
                            if (dataByFilename.ContainsKey(fileName))
                            {
                                continue;
                            }
                            dataByFilename.Add(fileName,
                                               Tuple.Create((double?)null,
                                                            new FileData(null, new PeakBounds(reader.GetDouble(3) / 60, reader.GetDouble(4) / 60))));
                        }
                    }
                }
                var sourceFiles = libKeySourceFileDatas
                                  .SelectMany(entry => entry.Value.Keys)
                                  .Distinct()
                                  .ToArray();
                Array.Sort(sourceFiles);
                var sourceFileIds = sourceFiles.Select((file, index) => Tuple.Create(file, index))
                                    .ToDictionary(tuple => tuple.Item1, tuple => tuple.Item2);
                var spectrumInfos =
                    libKeySourceFileDatas.Select(entry => MakeSpectrumInfo(entry.Key.Item1, entry.Key.Item2, entry.Value, sourceFileIds));
                SetLibraryEntries(spectrumInfos);
                _sourceFiles = ImmutableList.ValueOf(sourceFiles);
                loader.UpdateProgress(status.Complete());
                return(true);
            }
            catch (Exception e)
            {
                Trace.TraceWarning(
                    Resources.ChromatogramLibrary_LoadLibraryFromDatabase_Error_loading_chromatogram_library__0_, e);
                return(false);
            }
        }
Beispiel #14
0
        protected void Complete(Exception x)
        {
            lock (this)
            {
                ChromatogramCache result = null;
                try
                {
                    if (x == null && !_status.IsFinal)
                    {
                        long locationScanIds = 0, countBytesScanIds = 0;
                        if (_fs.Stream != null)
                        {
                            locationScanIds   = _fs.Stream.Position;
                            countBytesScanIds = _fsScans.Stream.Position;

                            ChromatogramCache.WriteStructs(_fs.Stream,
                                                           _fsScans.Stream,
                                                           _fsPeaks.Stream,
                                                           _fsScores.Stream,
                                                           _listCachedFiles,
                                                           _listGroups,
                                                           _listTransitions,
                                                           _listTextIdBytes,
                                                           _listScoreTypes,
                                                           _scoreCount,
                                                           _peakCount);

                            _loader.StreamManager.Finish(_fs.Stream);
                            _fs.Stream = null;
                            _fs.Commit(_destinationStream);
                        }

                        // Create stream identifier, but do not open.  The stream will be opened
                        // the first time the document uses it.
                        var readStream = _loader.StreamManager.CreatePooledStream(CachePath, false);

                        _fsPeaks.Stream.Seek(0, SeekOrigin.Begin);
                        _fsScores.Stream.Seek(0, SeekOrigin.Begin);
                        var rawData = new ChromatogramCache.RawData
                        {
                            FormatVersion       = ChromatogramCache.FORMAT_VERSION_CACHE,
                            ChromCacheFiles     = _listCachedFiles.ToArray(),
                            ChromatogramEntries = _listGroups.ToArray(),
                            ChromTransitions    = _listTransitions.ToArray(),
                            ChromatogramPeaks   = new BlockedArray <ChromPeak>(
                                count => ChromPeak.ReadArray(_fsPeaks.FileStream.SafeFileHandle, count), _peakCount,
                                ChromPeak.SizeOf, ChromPeak.DEFAULT_BLOCK_SIZE),
                            ScoreTypes = _listScoreTypes.ToArray(),
                            Scores     = new BlockedArray <float>(
                                count => PrimitiveArrays.Read <float>(_fsScores.FileStream, count), _scoreCount,
                                sizeof(float), ChromatogramCache.DEFAULT_SCORES_BLOCK_SIZE),
                            TextIdBytes       = _listTextIdBytes.ToArray(),
                            LocationScanIds   = locationScanIds,
                            CountBytesScanIds = countBytesScanIds,
                        };
                        result = new ChromatogramCache(CachePath, rawData, readStream);
                        _loader.UpdateProgress(_status.Complete());
                    }
                }
                catch (Exception x2)
                {
                    x = x2;
                }
                finally
                {
                    Dispose();
                }

                try
                {
                    _completed(result, x);
                }
                catch (Exception x2)
                {
                    _completed(null, x2);
                }
            }
        }
Beispiel #15
0
        private bool Load(ILoadMonitor loader, IProgressStatus status, bool cached)
        {
            try
            {
                int loadPercent = 100;

                if (!cached)
                {
                    // Building the cache will take 95% of the load time.
                    loadPercent = 5;

                    status = status.ChangeMessage(string.Format(Resources.XHunterLibrary_Load_Building_binary_cache_for__0__library, Path.GetFileName(FilePath)));
                    status = status.ChangePercentComplete(0);

                    loader.UpdateProgress(status);

                    if (!CreateCache(loader, status, 100 - loadPercent))
                    {
                        return(false);
                    }
                }

                status = status.ChangeMessage(string.Format(Resources.XHunterLibrary_Load_Loading__0__library, Path.GetFileName(FilePath)));
                loader.UpdateProgress(status);

                var valueCache = new ValueCache();
                var sm         = loader.StreamManager;
                using (Stream stream = sm.CreateStream(CachePath, FileMode.Open, true))
                {
                    // Read library header from the end of the cache
                    int countHeader = (int)LibHeaders.count * sizeof(int);
                    stream.Seek(-countHeader, SeekOrigin.End);

                    byte[] libHeader = new byte[countHeader];
                    ReadComplete(stream, libHeader, countHeader);

                    int version = GetInt32(libHeader, (int)LibHeaders.format_version);
                    if (version != FORMAT_VERSION_CACHE)
                    {
                        return(false);
                    }

                    int countRevisionBytes = GetInt32(libHeader, (int)LibHeaders.revision_byte_count);
                    int countIdBytes       = GetInt32(libHeader, (int)LibHeaders.id_byte_count);
                    stream.Seek(-countHeader - countRevisionBytes - countIdBytes, SeekOrigin.End);
                    Revision = ReadString(stream, countRevisionBytes);
                    Id       = ReadString(stream, countIdBytes);

                    int numSpectra = GetInt32(libHeader, (int)LibHeaders.num_spectra);

                    var libraryEntries = new XHunterSpectrumInfo[numSpectra];

                    // Seek to beginning of spectrum headers
                    long locationHeaders = BitConverter.ToInt64(libHeader,
                                                                ((int)LibHeaders.location_headers_lo) * sizeof(int));
                    stream.Seek(locationHeaders, SeekOrigin.Begin);

                    for (int i = 0; i < numSpectra; i++)
                    {
                        int percent = (100 - loadPercent) + (i * loadPercent / numSpectra);
                        if (status.PercentComplete != percent)
                        {
                            // Check for cancellation after each integer change in percent loaded.
                            if (loader.IsCanceled)
                            {
                                loader.UpdateProgress(status.Cancel());
                                return(false);
                            }

                            // If not cancelled, update progress.
                            loader.UpdateProgress(status = status.ChangePercentComplete(percent));
                        }

                        // Read spectrum header
                        LibKey key                = LibKey.Read(valueCache, stream);
                        long   location           = PrimitiveArrays.ReadOneValue <long>(stream);
                        float  processedIntensity = PrimitiveArrays.ReadOneValue <float>(stream);
                        int    numPeaks           = PrimitiveArrays.ReadOneValue <int>(stream);
                        float  expect             = PrimitiveArrays.ReadOneValue <float>(stream);
                        libraryEntries[i] = new XHunterSpectrumInfo(key, processedIntensity, expect, (short)numPeaks, location);
                    }

                    // Checksum = checksum.ChecksumValue;
                    SetLibraryEntries(libraryEntries);

                    loader.UpdateProgress(status.Complete());

                    // Create the stream from which the spectra will be read
                    CreateStream(loader);
                }

                return(true);
            }
            catch (InvalidDataException x)
            {
                if (!cached)
                {
                    loader.UpdateProgress(status.ChangeErrorException(x));
                }
                return(false);
            }
            catch (IOException x)
            {
                if (!cached)
                {
                    loader.UpdateProgress(status.ChangeErrorException(x));
                }
                return(false);
            }
            catch (Exception x)
            {
                if (!cached)
                {
                    x = new Exception(string.Format(Resources.XHunterLibrary_Load_Failed_loading_library__0__, FilePath), x);
                    loader.UpdateProgress(status.ChangeErrorException(x));
                }
                return(false);
            }
            finally
            {
                if (ReadStream != null)
                {
                    // Close the read stream to ensure we never leak it.
                    // This only costs on extra open, the first time the
                    // active document tries to read.
                    try { ReadStream.CloseStream(); }
                    catch (IOException) { }
                }
            }
        }
Beispiel #16
0
        private bool Load(ILoadMonitor loader, IProgressStatus status, bool cached)
        {
            try
            {
                int loadPercent = 100;

                if (!cached)
                {
                    // Building the cache will take 95% of the load time.
                    loadPercent = 5;

                    status = status.ChangeMessage(string.Format(Resources.XHunterLibrary_Load_Building_binary_cache_for__0__library, Path.GetFileName(FilePath)));
                    status = status.ChangePercentComplete(0);

                    loader.UpdateProgress(status);

                    if (!CreateCache(loader, status, 100 - loadPercent))
                    {
                        return(false);
                    }
                }

                status = status.ChangeMessage(string.Format(Resources.XHunterLibrary_Load_Loading__0__library, Path.GetFileName(FilePath)));
                loader.UpdateProgress(status);

                var sm = loader.StreamManager;
                using (Stream stream = sm.CreateStream(CachePath, FileMode.Open, true))
                {
                    // Read library header from the end of the cache
                    int countHeader = (int)LibHeaders.count * sizeof(int);
                    stream.Seek(-countHeader, SeekOrigin.End);

                    byte[] libHeader = new byte[countHeader];
                    ReadComplete(stream, libHeader, countHeader);

                    int version = GetInt32(libHeader, (int)LibHeaders.format_version);
                    if (version != FORMAT_VERSION_CACHE)
                    {
                        return(false);
                    }

                    int countRevisionBytes = GetInt32(libHeader, (int)LibHeaders.revision_byte_count);
                    int countIdBytes       = GetInt32(libHeader, (int)LibHeaders.id_byte_count);
                    stream.Seek(-countHeader - countRevisionBytes - countIdBytes, SeekOrigin.End);
                    Revision = ReadString(stream, countRevisionBytes);
                    Id       = ReadString(stream, countIdBytes);

                    int numSpectra = GetInt32(libHeader, (int)LibHeaders.num_spectra);

                    var setSequences   = new Dictionary <LibSeqKey, bool>(numSpectra);
                    var libraryEntries = new XHunterSpectrumInfo[numSpectra];

                    // Seek to beginning of spectrum headers
                    long locationHeaders = BitConverter.ToInt64(libHeader,
                                                                ((int)LibHeaders.location_headers_lo) * sizeof(int));
                    stream.Seek(locationHeaders, SeekOrigin.Begin);

                    byte[] specSequence = new byte[1024];
                    byte[] specHeader   = new byte[1024];
                    countHeader = (int)SpectrumCacheHeader.count * 4;

                    for (int i = 0; i < numSpectra; i++)
                    {
                        int percent = (100 - loadPercent) + (i * loadPercent / numSpectra);
                        if (status.PercentComplete != percent)
                        {
                            // Check for cancellation after each integer change in percent loaded.
                            if (loader.IsCanceled)
                            {
                                loader.UpdateProgress(status.Cancel());
                                return(false);
                            }

                            // If not cancelled, update progress.
                            loader.UpdateProgress(status = status.ChangePercentComplete(percent));
                        }

                        // Read spectrum header
                        ReadComplete(stream, specHeader, countHeader);

                        int seqKeyHash   = GetInt32(specHeader, ((int)SpectrumCacheHeader.seq_key_hash));
                        int seqKeyLength = GetInt32(specHeader, ((int)SpectrumCacheHeader.seq_key_length));
                        int charge       = GetInt32(specHeader, ((int)SpectrumCacheHeader.charge));
                        if (charge == 0 || charge > TransitionGroup.MAX_PRECURSOR_CHARGE)
                        {
                            throw new InvalidDataException(Resources.XHunterLibrary_Load_Invalid_precursor_charge_found_File_may_be_corrupted);
                        }
                        float i2        = BitConverter.ToSingle(specHeader, ((int)SpectrumCacheHeader.i2) * 4);
                        long  location  = BitConverter.ToInt64(specHeader, ((int)SpectrumCacheHeader.location_lo) * 4);
                        int   numPeaks  = GetInt32(specHeader, ((int)SpectrumCacheHeader.num_peaks));
                        float expect    = BitConverter.ToSingle(specHeader, ((int)SpectrumCacheHeader.expect) * 4);
                        int   seqLength = GetInt32(specHeader, (int)SpectrumCacheHeader.seq_len);

                        // Read sequence information
                        ReadComplete(stream, specSequence, seqLength);

                        LibKey key = new LibKey(specSequence, 0, seqLength, charge);
                        libraryEntries[i] = new XHunterSpectrumInfo(key, i2, expect, (short)numPeaks, location);

                        if (seqKeyLength > 0)
                        {
                            LibSeqKey seqKey = new LibSeqKey(key, seqKeyHash, seqKeyLength);
                            setSequences.Add(seqKey, true);
                        }
                    }

                    // Checksum = checksum.ChecksumValue;
                    _libraryEntries = libraryEntries;
                    _setSequences   = setSequences;

                    loader.UpdateProgress(status.Complete());

                    // Create the stream from which the spectra will be read
                    CreateStream(loader);
                }

                return(true);
            }
            catch (InvalidDataException x)
            {
                if (!cached)
                {
                    loader.UpdateProgress(status.ChangeErrorException(x));
                }
                return(false);
            }
            catch (IOException x)
            {
                if (!cached)
                {
                    loader.UpdateProgress(status.ChangeErrorException(x));
                }
                return(false);
            }
            catch (Exception x)
            {
                if (!cached)
                {
                    x = new Exception(string.Format(Resources.XHunterLibrary_Load_Failed_loading_library__0__, FilePath), x);
                    loader.UpdateProgress(status.ChangeErrorException(x));
                }
                return(false);
            }
            finally
            {
                if (ReadStream != null)
                {
                    // Close the read stream to ensure we never leak it.
                    // This only costs on extra open, the first time the
                    // active document tries to read.
                    try { ReadStream.CloseStream(); }
                    catch (IOException) { }
                }
            }
        }
Beispiel #17
0
        // ReSharper restore UnusedMember.Local

        private bool Load(ILoadMonitor loader)
        {
            ProgressStatus status =
                new ProgressStatus(string.Format(Resources.BiblioSpecLibrary_Load_Loading__0__library,
                                                 Path.GetFileName(FilePath)));

            loader.UpdateProgress(status);

            long lenRead = 0;

            // AdlerChecksum checksum = new AdlerChecksum();

            try
            {
                // Use a buffered stream for initial read
                BufferedStream stream = new BufferedStream(CreateStream(loader), 32 * 1024);

                int    countHeader = (int)LibHeaders.count * 4;
                byte[] libHeader   = new byte[countHeader];
                if (stream.Read(libHeader, 0, countHeader) != countHeader)
                {
                    throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Data_truncation_in_library_header_File_may_be_corrupted);
                }
                lenRead += countHeader;
                // Check the first byte of the primary version number to determine
                // whether the format is little- or big-endian.  Little-endian will
                // have the version number in this byte, while big-endian will have zero.
                if (libHeader[(int)LibHeaders.version1 * 4] == 0)
                {
                    _bigEndian = true;
                }

                int numSpectra   = GetInt32(libHeader, (int)LibHeaders.num_spectra);
                var dictLibrary  = new Dictionary <LibKey, BiblioSpectrumInfo>(numSpectra);
                var setSequences = new HashSet <LibSeqKey>();

                string revStr = string.Format("{0}.{1}", // Not L10N
                                              GetInt32(libHeader, (int)LibHeaders.version1),
                                              GetInt32(libHeader, (int)LibHeaders.version2));
                Revision = float.Parse(revStr, CultureInfo.InvariantCulture);

                // checksum.MakeForBuff(libHeader, AdlerChecksum.ADLER_START);

                countHeader = (int)SpectrumHeaders.count * 4;
                byte[] specHeader   = new byte[1024];
                byte[] specSequence = new byte[1024];
                for (int i = 0; i < numSpectra; i++)
                {
                    int percent = i * 100 / numSpectra;
                    if (status.PercentComplete != percent)
                    {
                        // Check for cancellation after each integer change in percent loaded.
                        if (loader.IsCanceled)
                        {
                            loader.UpdateProgress(status.Cancel());
                            return(false);
                        }

                        // If not cancelled, update progress.
                        loader.UpdateProgress(status = status.ChangePercentComplete(percent));
                    }

                    // Read spectrum header
                    int bytesRead = stream.Read(specHeader, 0, countHeader);
                    if (bytesRead != countHeader)
                    {
                        throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Data_truncation_in_spectrum_header_File_may_be_corrupted);
                    }

                    // If this is the first header, and the sequence length is zero,
                    // then this is a Linux format library.  Switch to linux format,
                    // and start over.
                    if (i == 0 && GetInt32(specHeader, (int)SpectrumHeaders.seq_len) == 0)
                    {
                        _linuxFormat = true;
                        stream.Seek(lenRead, SeekOrigin.Begin);

                        // Re-ead spectrum header
                        countHeader = (int)SpectrumHeadersLinux.count * 4;
                        bytesRead   = stream.Read(specHeader, 0, countHeader);
                        if (bytesRead != countHeader)
                        {
                            throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Data_truncation_in_spectrum_header_File_may_be_corrupted);
                        }
                    }

                    lenRead += bytesRead;

                    // checksum.MakeForBuff(specHeader, checksum.ChecksumValue);

                    int charge = GetInt32(specHeader, (int)SpectrumHeaders.charge);
                    if (charge > TransitionGroup.MAX_PRECURSOR_CHARGE)
                    {
                        throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Invalid_precursor_charge_found_File_may_be_corrupted);
                    }

                    int numPeaks  = GetInt32(specHeader, (int)SpectrumHeaders.num_peaks);
                    int seqLength = GetInt32(specHeader, (_linuxFormat ?
                                                          (int)SpectrumHeadersLinux.seq_len : (int)SpectrumHeaders.seq_len));
                    int copies = GetInt32(specHeader, (_linuxFormat ?
                                                       (int)SpectrumHeadersLinux.copies : (int)SpectrumHeaders.copies));

                    // Read sequence information
                    int countSeq = (seqLength + 1) * 2;
                    if (stream.Read(specSequence, 0, countSeq) != countSeq)
                    {
                        throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Data_truncation_in_spectrum_sequence_File_may_be_corrupted);
                    }

                    lenRead += countSeq;

                    // checksum.MakeForBuff(specSequence, checksum.ChecksumValue);

                    // Store in dictionary
                    if (IsUnmodified(specSequence, seqLength + 1, seqLength))
                    {
                        // These libraries should not have duplicates, but just in case.
                        // CONSIDER: Emit error about redundancy?
                        // These legacy libraries assume [+57.0] modified Cysteine
                        LibKey key = new LibKey(GetCModified(specSequence, ref seqLength), 0, seqLength, charge);
                        if (!dictLibrary.ContainsKey(key))
                        {
                            dictLibrary.Add(key, new BiblioSpectrumInfo((short)copies, (short)numPeaks, lenRead));
                        }
                        setSequences.Add(new LibSeqKey(key));
                    }

                    // Read over peaks
                    int countPeaks = 2 * sizeof(Single) * numPeaks;
                    stream.Seek(countPeaks, SeekOrigin.Current);    // Skip spectrum
                    lenRead += countPeaks;

                    // checksum.MakeForBuff(specPeaks, checksum.ChecksumValue);
                }

                // Checksum = checksum.ChecksumValue;
                _dictLibrary  = dictLibrary;
                _setSequences = setSequences;
                loader.UpdateProgress(status.Complete());
                return(true);
            }
            catch (InvalidDataException x)
            {
                loader.UpdateProgress(status.ChangeErrorException(x));
                return(false);
            }
            catch (IOException x)
            {
                loader.UpdateProgress(status.ChangeErrorException(x));
                return(false);
            }
            catch (Exception x)
            {
                x = new Exception(string.Format(Resources.BiblioSpecLibrary_Load_Failed_loading_library__0__, FilePath), x);
                loader.UpdateProgress(status.ChangeErrorException(x));
                return(false);
            }
            finally
            {
                if (ReadStream != null)
                {
                    // Close the read stream to ensure we never leak it.
                    // This only costs on extra open, the first time the
                    // active document tries to read.
                    try { ReadStream.CloseStream(); }
                    catch (IOException) {}
                }
            }
        }
Beispiel #18
0
        // ReSharper restore UnusedMember.Local
        private bool Load(ILoadMonitor loader)
        {
            ProgressStatus status =
                new ProgressStatus(string.Format(Resources.BiblioSpecLibrary_Load_Loading__0__library,
                                                 Path.GetFileName(FilePath)));
            loader.UpdateProgress(status);

            long lenRead = 0;
            // AdlerChecksum checksum = new AdlerChecksum();

            try
            {
                // Use a buffered stream for initial read
                BufferedStream stream = new BufferedStream(CreateStream(loader), 32 * 1024);

                int countHeader = (int) LibHeaders.count*4;
                byte[] libHeader = new byte[countHeader];
                if (stream.Read(libHeader, 0, countHeader) != countHeader)
                    throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Data_truncation_in_library_header_File_may_be_corrupted);
                lenRead += countHeader;
                // Check the first byte of the primary version number to determine
                // whether the format is little- or big-endian.  Little-endian will
                // have the version number in this byte, while big-endian will have zero.
                if (libHeader[(int) LibHeaders.version1 * 4] == 0)
                    _bigEndian = true;

                int numSpectra = GetInt32(libHeader, (int) LibHeaders.num_spectra);
                var dictLibrary = new Dictionary<LibKey, BiblioSpectrumInfo>(numSpectra);
                var setSequences = new HashSet<LibSeqKey>();

                string revStr = string.Format("{0}.{1}", // Not L10N
                                              GetInt32(libHeader, (int) LibHeaders.version1),
                                              GetInt32(libHeader, (int) LibHeaders.version2));
                Revision = float.Parse(revStr, CultureInfo.InvariantCulture);

                // checksum.MakeForBuff(libHeader, AdlerChecksum.ADLER_START);

                countHeader = (int) SpectrumHeaders.count*4;
                byte[] specHeader = new byte[1024];
                byte[] specSequence = new byte[1024];
                for (int i = 0; i < numSpectra; i++)
                {
                    int percent = i * 100 / numSpectra;
                    if (status.PercentComplete != percent)
                    {
                        // Check for cancellation after each integer change in percent loaded.
                        if (loader.IsCanceled)
                        {
                            loader.UpdateProgress(status.Cancel());
                            return false;
                        }

                        // If not cancelled, update progress.
                        loader.UpdateProgress(status = status.ChangePercentComplete(percent));
                    }

                    // Read spectrum header
                    int bytesRead = stream.Read(specHeader, 0, countHeader);
                    if (bytesRead != countHeader)
                        throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Data_truncation_in_spectrum_header_File_may_be_corrupted);

                    // If this is the first header, and the sequence length is zero,
                    // then this is a Linux format library.  Switch to linux format,
                    // and start over.
                    if (i == 0 && GetInt32(specHeader, (int)SpectrumHeaders.seq_len) == 0)
                    {
                        _linuxFormat = true;
                        stream.Seek(lenRead, SeekOrigin.Begin);

                        // Re-ead spectrum header
                        countHeader = (int)SpectrumHeadersLinux.count * 4;
                        bytesRead = stream.Read(specHeader, 0, countHeader);
                        if (bytesRead != countHeader)
                            throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Data_truncation_in_spectrum_header_File_may_be_corrupted);
                    }

                    lenRead += bytesRead;

                    // checksum.MakeForBuff(specHeader, checksum.ChecksumValue);

                    int charge = GetInt32(specHeader, (int)SpectrumHeaders.charge);
                    if (charge > TransitionGroup.MAX_PRECURSOR_CHARGE)
                        throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Invalid_precursor_charge_found_File_may_be_corrupted);

                    int numPeaks = GetInt32(specHeader, (int)SpectrumHeaders.num_peaks);
                    int seqLength = GetInt32(specHeader, (_linuxFormat ?
                        (int)SpectrumHeadersLinux.seq_len : (int)SpectrumHeaders.seq_len));
                    int copies = GetInt32(specHeader, (_linuxFormat ?
                        (int)SpectrumHeadersLinux.copies : (int)SpectrumHeaders.copies));

                    // Read sequence information
                    int countSeq = (seqLength + 1)*2;
                    if (stream.Read(specSequence, 0, countSeq) != countSeq)
                        throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Data_truncation_in_spectrum_sequence_File_may_be_corrupted);

                    lenRead += countSeq;

                    // checksum.MakeForBuff(specSequence, checksum.ChecksumValue);

                    // Store in dictionary
                    if (IsUnmodified(specSequence, seqLength + 1, seqLength))
                    {
                        // These libraries should not have duplicates, but just in case.
                        // CONSIDER: Emit error about redundancy?
                        // These legacy libraries assume [+57.0] modified Cysteine
                        LibKey key = new LibKey(GetCModified(specSequence, ref seqLength), 0, seqLength, charge);
                        if (!dictLibrary.ContainsKey(key))
                            dictLibrary.Add(key, new BiblioSpectrumInfo((short)copies, (short)numPeaks, lenRead));
                        setSequences.Add(new LibSeqKey(key));
                    }

                    // Read over peaks
                    int countPeaks = 2*sizeof(Single)*numPeaks;
                    stream.Seek(countPeaks, SeekOrigin.Current);    // Skip spectrum
                    lenRead += countPeaks;

                    // checksum.MakeForBuff(specPeaks, checksum.ChecksumValue);
                }

                // Checksum = checksum.ChecksumValue;
                _dictLibrary = dictLibrary;
                _setSequences = setSequences;
                loader.UpdateProgress(status.Complete());
                return true;
            }
            catch (InvalidDataException x)
            {
                loader.UpdateProgress(status.ChangeErrorException(x));
                return false;
            }
            catch (IOException x)
            {
                loader.UpdateProgress(status.ChangeErrorException(x));
                return false;
            }
            catch (Exception x)
            {
                x = new Exception(string.Format(Resources.BiblioSpecLibrary_Load_Failed_loading_library__0__, FilePath), x);
                loader.UpdateProgress(status.ChangeErrorException(x));
                return false;
            }
            finally
            {
                if (ReadStream != null)
                {
                    // Close the read stream to ensure we never leak it.
                    // This only costs on extra open, the first time the
                    // active document tries to read.
                    try { ReadStream.CloseStream(); }
                    catch(IOException) {}
                }
            }
        }