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)); }
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) { } } } }