public override void WriteToStream(Stream stream)
        {
            PrimitiveArrays.Write(stream, TransitionTimeIntensities.First().Times.ToArray());
            foreach (var timeIntensities in TransitionTimeIntensities)
            {
                PrimitiveArrays.Write(stream, timeIntensities.Intensities.ToArray());
            }

            foreach (var timeIntensities in TransitionTimeIntensities)
            {
                if (timeIntensities.MassErrors == null)
                {
                    continue;
                }
                WriteMassErrors(stream, timeIntensities.MassErrors);
            }
            var scanIdsByChromSource = ScanIdsByChromSource();

            foreach (var chromSource in PERSISTED_CHROM_SOURCES)
            {
                ImmutableList <int> scanIds;
                if (!scanIdsByChromSource.TryGetValue(chromSource, out scanIds))
                {
                    continue;
                }
                PrimitiveArrays.Write(stream, scanIds.ToArray());
            }
        }
Beispiel #2
0
        // ReSharper restore LocalizableElement

        private void WriteCache(ILoadMonitor loader)
        {
            using (FileSaver fs = new FileSaver(CachePath, loader.StreamManager))
            {
                using (var stream = loader.StreamManager.CreateStream(fs.SafeName, FileMode.Create, true))
                {
                    PrimitiveArrays.WriteOneValue(stream, FORMAT_VERSION_CACHE);
                    PrimitiveArrays.WriteOneValue(stream, _sourceFiles.Count);
                    foreach (var file in _sourceFiles)
                    {
                        byte[] fileNameBytes = Encoding.UTF8.GetBytes(file);
                        PrimitiveArrays.WriteOneValue(stream, fileNameBytes.Length);
                        PrimitiveArrays.Write(stream, fileNameBytes);
                    }
                    PrimitiveArrays.WriteOneValue(stream, _libraryEntries.Length);
                    foreach (var elibSpectrumInfo in _libraryEntries)
                    {
                        elibSpectrumInfo.Write(stream);
                    }
                    loader.StreamManager.Finish(stream);
                    fs.Commit();
                    loader.StreamManager.SetCache(FilePath, CachePath);
                }
            }
        }
Beispiel #3
0
        private void WriteData(Block block, Stream fileStream)
        {
            // Create back link to previous spilled block.
            var lastFilePosition = _filePosition;

            _filePosition = (int)fileStream.Position;
            PrimitiveArrays.WriteOneValue(fileStream, lastFilePosition);

            PrimitiveArrays.Write(fileStream, block._data);
        }
 public void Write(Stream stream)
 {
     Key.Write(stream);
     PrimitiveArrays.WriteOneValue(stream, Id);
     PrimitiveArrays.WriteOneValue(stream, PeakArea);
     RetentionTimesByFileId.Write(stream);
     PrimitiveArrays.WriteOneValue(stream, TransitionAreas.Count);
     PrimitiveArrays.Write(stream, TransitionAreas.Select(mi => mi.Mz).ToArray());
     PrimitiveArrays.Write(stream, TransitionAreas.Select(mi => mi.Intensity).ToArray());
 }
Beispiel #5
0
 public void Write(Stream stream)
 {
     PrimitiveArrays.WriteOneValue(stream, PeptideModSeq.Length);
     PrimitiveArrays.Write(stream, Encoding.UTF8.GetBytes(PeptideModSeq));
     PrimitiveArrays.WriteOneValue(stream, Key.Charge);
     PrimitiveArrays.WriteOneValue(stream, BestFileId);
     PrimitiveArrays.WriteOneValue(stream, FileDatas.Count);
     foreach (var peakBoundEntry in FileDatas)
     {
         PrimitiveArrays.WriteOneValue(stream, peakBoundEntry.Key);
         PrimitiveArrays.WriteOneValue(stream, peakBoundEntry.Value.PeakBounds.StartTime);
         PrimitiveArrays.WriteOneValue(stream, peakBoundEntry.Value.PeakBounds.EndTime);
         if (peakBoundEntry.Value.ApexTime.HasValue)
         {
             PrimitiveArrays.WriteOneValue <byte>(stream, 1);
             PrimitiveArrays.WriteOneValue(stream, peakBoundEntry.Value.ApexTime.Value);
         }
         else
         {
             PrimitiveArrays.WriteOneValue <byte>(stream, 0);
         }
     }
 }
 private static void WriteMassErrors(Stream stream, IList <float> values)
 {
     PrimitiveArrays.Write(stream, values.Select(value => ChromPeak.To10x(value)).ToArray());
 }
Beispiel #7
0
        private bool JoinNextPart()
        {
            // Check for cancellation on every part.
            if (_loader.IsCanceled)
            {
                _loader.UpdateProgress(_status = _status.Cancel());
                Complete(null);
                return(false);
            }

            if (_currentPartIndex >= CacheFilePaths.Count)
            {
                Complete(null);
                return(false);
            }

            // If not cancelled, update progress.
            string cacheFilePath = CacheFilePaths[_currentPartIndex];
            string message       = string.Format(Resources.ChromCacheJoiner_JoinNextPart_Joining_file__0__, cacheFilePath);
            int    percent       = _currentPartIndex * 100 / CacheFilePaths.Count;

            _status = _status.ChangeMessage(message).ChangePercentComplete(percent);
            _loader.UpdateProgress(_status);

            try
            {
                using (var inStream = _loader.StreamManager.CreateStream(cacheFilePath, FileMode.Open, false))
                {
                    if (_fs.Stream == null)
                    {
                        _fs.Stream = _loader.StreamManager.CreateStream(_fs.SafeName, FileMode.Create, true);
                    }

                    ChromatogramCache.RawData rawData;
                    long bytesData = ChromatogramCache.LoadStructs(inStream, out rawData);

                    // If joining, then format version should have already been checked.
                    Assume.IsTrue(ChromatogramCache.IsVersionCurrent(rawData.FormatVersion) ||
                                  // WatersCacheTest uses older format partial caches
                                  rawData.FormatVersion == ChromatogramCache.FORMAT_VERSION_CACHE_2);

                    int  offsetFiles       = _listCachedFiles.Count;
                    int  offsetTransitions = _listTransitions.Count;
                    int  offsetPeaks       = _peakCount;
                    int  offsetScores      = _scoreCount;
                    long offsetPoints      = _fs.Stream.Position;

                    // Scan ids
                    long offsetScanIds = _fsScans.Stream.Position;
                    _listCachedFiles.AddRange(rawData.ChromCacheFiles.Select(f => f.RelocateScanIds(f.LocationScanIds + offsetScanIds)));
                    if (rawData.CountBytesScanIds > 0)
                    {
                        inStream.Seek(rawData.LocationScanIds, SeekOrigin.Begin);
                        inStream.TransferBytes(_fsScans.Stream, rawData.CountBytesScanIds);
                    }

                    _peakCount += rawData.ChromatogramPeaks.Length;
                    rawData.ChromatogramPeaks.WriteArray(block => ChromPeak.WriteArray(_fsPeaks.FileStream.SafeFileHandle, block));
                    _listTransitions.AddRange(rawData.ChromTransitions);
                    // Initialize the score types the first time through
                    if (_scoreTypesCount == -1)
                    {
                        _listScoreTypes.AddRange(rawData.ScoreTypes);
                        _scoreTypesCount = _listScoreTypes.Count;
                    }
                    else if (!ArrayUtil.EqualsDeep(_listScoreTypes, rawData.ScoreTypes))
                    {
                        // If the existing score types in the caches are not the same, the caches cannot be joined
                        if (_listScoreTypes.Intersect(rawData.ScoreTypes).Count() != _listScoreTypes.Count)
                        {
                            throw new InvalidDataException("Data cache files with different score types cannot be joined.");    // Not L10N
                        }
                    }
                    _scoreCount += rawData.Scores.Length;
                    rawData.Scores.WriteArray(block => PrimitiveArrays.Write(_fsScores.Stream, block));

                    for (int i = 0; i < rawData.ChromatogramEntries.Length; i++)
                    {
                        rawData.RecalcEntry(i,
                                            offsetFiles,
                                            offsetTransitions,
                                            offsetPeaks,
                                            offsetScores,
                                            offsetPoints,
                                            _dictTextIdToByteIndex,
                                            _listTextIdBytes);
                    }
                    _listGroups.AddRange(rawData.ChromatogramEntries);

                    inStream.Seek(0, SeekOrigin.Begin);

                    long copyBytes = bytesData;
                    while (copyBytes > 0)
                    {
                        int read = inStream.Read(_buffer, 0, (int)Math.Min(_buffer.Length, copyBytes));
                        _fs.Stream.Write(_buffer, 0, read);
                        copyBytes -= read;
                    }

                    _currentPartIndex++;
                    return(true);
                }
            }
            catch (InvalidDataException x)
            {
                Complete(x);
            }
            catch (IOException x)
            {
                Complete(x);
            }
            catch (Exception x)
            {
                Complete(new Exception(String.Format(Resources.ChromCacheJoiner_JoinNextPart_Failed_to_create_cache__0__, CachePath), x));
            }
            return(false);
        }
            public void WriteChromGroup(ChromatogramGroupInfo originalChromGroup, MinimizedChromGroup minimizedChromGroup)
            {
                if (minimizedChromGroup.RetainedTransitionIndexes.Count == 0)
                {
                    return;
                }

                var originalHeader       = originalChromGroup.Header;
                int fileIndex            = originalHeader.FileIndex;
                int startTransitionIndex = _transitions.Count;
                int startPeakIndex       = _peakCount;
                int startScoreIndex      = _scoreCount;

                for (int iPeak = 0; iPeak < originalHeader.NumPeaks; iPeak++)
                {
                    int iScores = originalHeader.StartScoreIndex + iPeak * _scoreTypes.Count;
                    var scores  = _originalCache.GetCachedScores(iScores).ToArray();
                    PrimitiveArrays.Write(_outputStreamScores, scores);
                    _scoreCount += scores.Length;
                }
                int numPoints           = minimizedChromGroup.OptimizedLastScan - minimizedChromGroup.OptimizedFirstScan + 1;
                var retainedPeakIndexes = new HashSet <int>();

                if (minimizedChromGroup.OptimizedStartTime.HasValue && minimizedChromGroup.OptimizedEndTime.HasValue)
                {
                    for (int iPeak = 0; iPeak < originalHeader.NumPeaks; iPeak++)
                    {
                        bool outsideRange = false;
                        for (var transitionIndex = 0; transitionIndex < originalHeader.NumTransitions; transitionIndex++)
                        {
                            if (!minimizedChromGroup.RetainedTransitionIndexes.Contains(transitionIndex))
                            {
                                continue;
                            }

                            var peak = _originalCache.GetPeak(originalHeader.StartPeakIndex +
                                                              transitionIndex * originalHeader.NumPeaks + iPeak);
                            if (peak.StartTime < minimizedChromGroup.OptimizedStartTime.Value ||
                                peak.EndTime > minimizedChromGroup.OptimizedEndTime.Value)
                            {
                                outsideRange = true;
                                break;
                            }
                        }
                        if (!outsideRange)
                        {
                            retainedPeakIndexes.Add(iPeak);
                        }
                    }
                }
                else
                {
                    retainedPeakIndexes.UnionWith(Enumerable.Range(0, originalHeader.NumPeaks));
                }
                int numPeaks = retainedPeakIndexes.Count;
                int maxPeakIndex;

                if (retainedPeakIndexes.Contains(originalHeader.MaxPeakIndex))
                {
                    maxPeakIndex = retainedPeakIndexes.Count(index => index < originalHeader.MaxPeakIndex);
                }
                else
                {
                    maxPeakIndex = -1;
                }
                long location = _outputStream.Position;

                float[]        times         = originalChromGroup.Times.Skip(minimizedChromGroup.OptimizedFirstScan).Take(numPoints).ToArray();
                List <float[]> intensities   = new List <float[]>();
                List <short[]> massError10Xs = originalChromGroup.MassError10XArray != null
                                                  ? new List <short[]>()
                                                  : null;

                int[][] scanIndexes = null;
                if (originalChromGroup.ScanIndexes != null)
                {
                    scanIndexes = new int[originalChromGroup.ScanIndexes.Length][];
                    for (int i = 0; i < scanIndexes.Length; i++)
                    {
                        if (originalChromGroup.ScanIndexes[i] != null)
                        {
                            scanIndexes[i] =
                                originalChromGroup.ScanIndexes[i].Skip(minimizedChromGroup.OptimizedFirstScan)
                                .Take(numPoints)
                                .ToArray();
                        }
                    }
                }

                foreach (var originalIndex in minimizedChromGroup.RetainedTransitionIndexes)
                {
                    _transitions.Add(_originalCache.GetTransition(originalIndex + originalHeader.StartTransitionIndex));
                    for (int originalPeakIndex = 0; originalPeakIndex < originalHeader.NumPeaks; originalPeakIndex++)
                    {
                        if (!retainedPeakIndexes.Contains(originalPeakIndex))
                        {
                            continue;
                        }

                        var originalPeak = _originalCache.GetPeak(originalHeader.StartPeakIndex +
                                                                  originalIndex * originalHeader.NumPeaks +
                                                                  originalPeakIndex);
                        _peakCount++;
                        ChromPeak.WriteArray(_outputStreamPeaks.SafeFileHandle, new[] { originalPeak });
                    }
                    intensities.Add(originalChromGroup.IntensityArray[originalIndex]
                                    .Skip(minimizedChromGroup.OptimizedFirstScan)
                                    .Take(numPoints).ToArray());
                    if (massError10Xs != null)
                    {
                        massError10Xs.Add(originalChromGroup.MassError10XArray[originalIndex]
                                          .Skip(minimizedChromGroup.OptimizedFirstScan)
                                          .Take(numPoints).ToArray());
                    }
                }
                var massError10XArray = massError10Xs != null?massError10Xs.ToArray() : null;

                byte[] points = ChromatogramCache.TimeIntensitiesToBytes(times, intensities.ToArray(), massError10XArray, scanIndexes);
                // Compress the data (can be huge for AB data with lots of zeros)
                byte[] pointsCompressed = points.Compress(3);
                int    lenCompressed    = pointsCompressed.Length;

                _outputStream.Write(pointsCompressed, 0, lenCompressed);
                var header = new ChromGroupHeaderInfo5(originalHeader.Precursor,
                                                       originalHeader.TextIdIndex,
                                                       originalHeader.TextIdLen,
                                                       fileIndex,
                                                       _transitions.Count - startTransitionIndex,
                                                       startTransitionIndex,
                                                       numPeaks,
                                                       startPeakIndex,
                                                       startScoreIndex,
                                                       maxPeakIndex,
                                                       numPoints,
                                                       pointsCompressed.Length,
                                                       location,
                                                       originalHeader.Flags,
                                                       originalHeader.StatusId,
                                                       originalHeader.StatusRank);

                _chromGroupHeaderInfos.Add(header);
            }
Beispiel #9
0
            public void WriteChromGroup(ChromatogramGroupInfo originalChromGroup, MinimizedChromGroup minimizedChromGroup)
            {
                if (minimizedChromGroup.RetainedTransitionIndexes.Count == 0)
                {
                    return;
                }

                var originalHeader       = originalChromGroup.Header;
                int fileIndex            = originalHeader.FileIndex;
                int startTransitionIndex = _transitions.Count;
                int startPeakIndex       = _peakCount;
                int startScoreIndex      = _scoreCount;

                _scoreCount += minimizedChromGroup.PeakScores.Length;
                PrimitiveArrays.Write(_outputStreamScores, minimizedChromGroup.PeakScores);

                _peakCount += minimizedChromGroup.TotalPeakCount;
                _transitions.AddRange(minimizedChromGroup.Transitions);

                var          flags                  = originalHeader.Flags;
                MemoryStream pointsStream           = new MemoryStream();
                var          transitionChromSources = minimizedChromGroup.Transitions.Select(tran => tran.Source).ToArray();
                var          timeIntensitiesGroup   = minimizedChromGroup.MinimizedTimeIntensitiesGroup;

                if (timeIntensitiesGroup is RawTimeIntensities && _cacheFormat.FormatVersion < CacheFormatVersion.Twelve)
                {
                    timeIntensitiesGroup = ((RawTimeIntensities)minimizedChromGroup.MinimizedTimeIntensitiesGroup)
                                           .Interpolate(transitionChromSources);
                }
                timeIntensitiesGroup.WriteToStream(pointsStream);
                if (timeIntensitiesGroup is RawTimeIntensities)
                {
                    flags |= ChromGroupHeaderInfo.FlagValues.raw_chromatograms;
                }
                else
                {
                    flags &= ~ChromGroupHeaderInfo.FlagValues.raw_chromatograms;
                    var interpolatedTimeIntensities = timeIntensitiesGroup as InterpolatedTimeIntensities;
                    if (interpolatedTimeIntensities != null)
                    {
                        flags &=
                            ~(ChromGroupHeaderInfo.FlagValues.has_frag_scan_ids |
                              ChromGroupHeaderInfo.FlagValues.has_sim_scan_ids |
                              ChromGroupHeaderInfo.FlagValues.has_ms1_scan_ids);
                        var scanIdsByChromSource = interpolatedTimeIntensities.ScanIdsByChromSource();
                        if (scanIdsByChromSource.ContainsKey(ChromSource.fragment))
                        {
                            flags |= ChromGroupHeaderInfo.FlagValues.has_frag_scan_ids;
                        }
                        if (scanIdsByChromSource.ContainsKey(ChromSource.ms1))
                        {
                            flags |= ChromGroupHeaderInfo.FlagValues.has_ms1_scan_ids;
                        }
                        if (scanIdsByChromSource.ContainsKey(ChromSource.sim))
                        {
                            flags |= ChromGroupHeaderInfo.FlagValues.has_sim_scan_ids;
                        }
                    }
                }

                int numPeaks     = minimizedChromGroup.NumPeaks;
                int maxPeakIndex = minimizedChromGroup.MaxPeakIndex;

                _cacheFormat.ChromPeakSerializer().WriteItems(_outputStreamPeaks, minimizedChromGroup.MinimizedPeaks);

                long location        = _outputStream.Position;
                int  lenUncompressed = (int)pointsStream.Length;

                byte[] pointsCompressed = pointsStream.ToArray().Compress(3);
                int    lenCompressed    = pointsCompressed.Length;

                _outputStream.Write(pointsCompressed, 0, lenCompressed);
                int textIdIndex;
                int textIdLen;
                var newTextId = GetNewTextId(originalHeader);

                if (newTextId == null)
                {
                    textIdIndex = -1;
                    textIdLen   = 0;
                }
                else
                {
                    textIdIndex = CalcTextIdOffset(newTextId);
                    textIdLen   = newTextId.Count;
                }

                var header = new ChromGroupHeaderInfo(originalHeader.Precursor,
                                                      textIdIndex,
                                                      textIdLen,
                                                      fileIndex,
                                                      _transitions.Count - startTransitionIndex,
                                                      startTransitionIndex,
                                                      numPeaks,
                                                      startPeakIndex,
                                                      startScoreIndex,
                                                      maxPeakIndex,
                                                      timeIntensitiesGroup.NumInterpolatedPoints,
                                                      lenCompressed,
                                                      lenUncompressed,
                                                      location,
                                                      flags,
                                                      originalHeader.StatusId,
                                                      originalHeader.StatusRank,
                                                      originalHeader.StartTime, originalHeader.EndTime,
                                                      originalHeader.CollisionalCrossSection,
                                                      originalHeader.IonMobilityUnits);

                _chromGroupHeaderInfos.Add(header);
            }