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