Ejemplo n.º 1
0
            private void ReadHeader()
            {
                _stream.Seek(-sizeof(long), SeekOrigin.End);
                _locationHeader = PrimitiveArrays.ReadOneValue <long>(_stream);
                _stream.Seek(_locationHeader, SeekOrigin.Begin);
                int version = PrimitiveArrays.ReadOneValue <int>(_stream);

                if (version > CURRENT_VERSION || version < MIN_READABLE_VERSION)
                {
                    throw new InvalidDataException(string.Format(Resources.Serializer_ReadHeader_Unsupported_file_version__0_, version));
                }
                _locationEntries         = PrimitiveArrays.ReadOneValue <long>(_stream);
                _library.PanoramaServer  = ReadString(_stream);
                _library.LibraryRevision = PrimitiveArrays.ReadOneValue <int>(_stream);
                _library.SchemaVersion   = ReadString(_stream);
                var sampleFileCount = PrimitiveArrays.ReadOneValue <int>(_stream);
                var sampleFiles     = new List <ChromatogramLibrarySourceInfo>();

                for (int i = 0; i < sampleFileCount; i++)
                {
                    sampleFiles.Add(ChromatogramLibrarySourceInfo.Read(_stream));
                }
                _library._librarySourceFiles = sampleFiles.ToArray();
                _locationEntries             = PrimitiveArrays.ReadOneValue <long>(_stream);
            }
Ejemplo n.º 2
0
            public static ElibSpectrumInfo Read(ValueCache valueCache, Stream stream)
            {
                byte[] peptideModSeqBytes = new byte[PrimitiveArrays.ReadOneValue <int>(stream)];
                stream.Read(peptideModSeqBytes, 0, peptideModSeqBytes.Length);
                var peptideModSeq  = valueCache.CacheValue(Encoding.UTF8.GetString(peptideModSeqBytes));
                int charge         = PrimitiveArrays.ReadOneValue <int>(stream);
                int bestFileId     = PrimitiveArrays.ReadOneValue <int>(stream);
                int peakBoundCount = PrimitiveArrays.ReadOneValue <int>(stream);
                var peakBounds     = new List <KeyValuePair <int, FileData> >();

                while (peakBounds.Count < peakBoundCount)
                {
                    var    fileId       = PrimitiveArrays.ReadOneValue <int>(stream);
                    var    startTime    = PrimitiveArrays.ReadOneValue <double>(stream);
                    var    endTime      = PrimitiveArrays.ReadOneValue <double>(stream);
                    var    score        = PrimitiveArrays.ReadOneValue <double>(stream);
                    byte   bHasApexTime = PrimitiveArrays.ReadOneValue <byte>(stream);
                    double?apexTime;
                    if (bHasApexTime == 0)
                    {
                        apexTime = null;
                    }
                    else
                    {
                        apexTime = PrimitiveArrays.ReadOneValue <double>(stream);
                    }
                    peakBounds.Add(new KeyValuePair <int, FileData>(fileId, new FileData(apexTime, new ExplicitPeakBounds(startTime, endTime, score))));
                }
                return(new ElibSpectrumInfo(peptideModSeq, charge, bestFileId, peakBounds));
            }
Ejemplo n.º 3
0
        public static LibraryKey Read(ValueCache valueCache, Stream stream)
        {
            int length = PrimitiveArrays.ReadOneValue <int>(stream);

            byte[] buffer = new byte[length];
            stream.Read(buffer, 0, length);
            var proto = new LibraryKeyProto();

            proto.MergeFrom(new CodedInputStream(buffer));
            switch (proto.KeyType)
            {
            case LibraryKeyProto.Types.KeyType.Peptide:
                return(new PeptideLibraryKey(proto.ModifiedSequence, proto.Charge).ValueFromCache(valueCache));

            case LibraryKeyProto.Types.KeyType.PrecursorMz:
                return(new PrecursorLibraryKey(proto));

            case LibraryKeyProto.Types.KeyType.SmallMolecule:
                return(new MoleculeLibraryKey(valueCache, proto));

            case LibraryKeyProto.Types.KeyType.Crosslink:
                return(new CrosslinkLibraryKey(proto));
            }
            return(null);
        }
Ejemplo n.º 4
0
            private static string ReadString(Stream stream)
            {
                int byteLength = PrimitiveArrays.ReadOneValue <int>(stream);
                var bytes      = new byte[byteLength];

                stream.Read(bytes, 0, bytes.Length);
                return(Encoding.UTF8.GetString(bytes));
            }
Ejemplo n.º 5
0
            public static ChromatogramLibraryIrt Read(Stream stream)
            {
                var seq        = Target.FromSerializableString(ReadString(stream));
                var timeSource = (TimeSource)PrimitiveArrays.ReadOneValue <int>(stream);
                var irt        = PrimitiveArrays.ReadOneValue <double>(stream);

                return(new ChromatogramLibraryIrt(seq, timeSource, irt));
            }
Ejemplo n.º 6
0
            public static ChromatogramLibrarySourceInfo Read(Stream stream)
            {
                int    id                       = PrimitiveArrays.ReadOneValue <int>(stream);
                string filePath                 = ReadString(stream);
                string sampleName               = ReadString(stream);
                string acquiredTime             = ReadString(stream);
                string modifiedTime             = ReadString(stream);
                string instrumentIonizationType = ReadString(stream);
                string instrumentAnalyzer       = ReadString(stream);
                string instrumentDetector       = ReadString(stream);

                return(new ChromatogramLibrarySourceInfo(id, filePath, sampleName, acquiredTime, modifiedTime, instrumentIonizationType,
                                                         instrumentAnalyzer, instrumentDetector));
            }
Ejemplo n.º 7
0
        public static ChromLibSpectrumInfo Read(Stream stream)
        {
            LibKey key      = LibKey.Read(stream);
            int    id       = PrimitiveArrays.ReadOneValue <int>(stream);
            double peakArea = PrimitiveArrays.ReadOneValue <double>(stream);
            var    retentionTimesByFileId = IndexedRetentionTimes.Read(stream);
            int    mzCount = PrimitiveArrays.ReadOneValue <int>(stream);
            var    mzs     = PrimitiveArrays.Read <double>(stream, mzCount);
            var    areas   = PrimitiveArrays.Read <float>(stream, mzCount);
            var    mzAreas = ImmutableList.ValueOf(Enumerable.Range(0, mzCount)
                                                   .Select(index => new SpectrumPeaksInfo.MI
            {
                Mz        = mzs[index],
                Intensity = areas[index]
            }));

            return(new ChromLibSpectrumInfo(key, id, peakArea, retentionTimesByFileId, mzAreas));
        }
Ejemplo n.º 8
0
            private void ReadEntries()
            {
                _stream.Seek(_locationEntries, SeekOrigin.Begin);
                int entryCount = PrimitiveArrays.ReadOneValue <int>(_stream);
                var entries    = new ChromLibSpectrumInfo[entryCount];

                for (int i = 0; i < entryCount; i++)
                {
                    entries[i] = ChromLibSpectrumInfo.Read(_stream);
                }
                _library.SetLibraryEntries(entries);
                int irtCount = PrimitiveArrays.ReadOneValue <int>(_stream);

                _library._libraryIrts = new ChromatogramLibraryIrt[irtCount];
                for (int i = 0; i < irtCount; i++)
                {
                    _library._libraryIrts[i] = ChromatogramLibraryIrt.Read(_stream);
                }
            }
Ejemplo n.º 9
0
 private bool LoadFromCache(ILoadMonitor loader)
 {
     if (!loader.StreamManager.IsCached(FilePath, CachePath))
     {
         return(false);
     }
     try
     {
         ValueCache valueCache = new ValueCache();
         using (var stream = loader.StreamManager.CreateStream(CachePath, FileMode.Open, true))
         {
             int version = PrimitiveArrays.ReadOneValue <int>(stream);
             if (version != FORMAT_VERSION_CACHE)
             {
                 return(false);
             }
             int           fileCount   = PrimitiveArrays.ReadOneValue <int>(stream);
             List <String> sourceFiles = new List <string>(fileCount);
             while (sourceFiles.Count < fileCount)
             {
                 int    byteCount = PrimitiveArrays.ReadOneValue <int>(stream);
                 byte[] bytes     = new byte[byteCount];
                 stream.Read(bytes, 0, bytes.Length);
                 sourceFiles.Add(Encoding.UTF8.GetString(bytes));
             }
             int spectrumInfoCount = PrimitiveArrays.ReadOneValue <int>(stream);
             _sourceFiles = ImmutableList.ValueOf(sourceFiles);
             List <ElibSpectrumInfo> spectrumInfos = new List <ElibSpectrumInfo>();
             while (spectrumInfos.Count < spectrumInfoCount)
             {
                 spectrumInfos.Add(ElibSpectrumInfo.Read(valueCache, stream));
             }
             SetLibraryEntries(spectrumInfos);
             return(true);
         }
     }
     catch (Exception exception)
     {
         Trace.TraceWarning(@"Exception loading cache: {0}", exception);
         return(false);
     }
 }
Ejemplo n.º 10
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) { }
                }
            }
        }