示例#1
0
        private MsDataSpectrum GetSpectrumFromJObject(JObject jObject, int msLevel)
        {
            // ReSharper disable NonLocalizedString
            string strMzs         = jObject["mzs-base64"].ToString();
            string strIntensities = jObject["intensities-base64"].ToString();

            byte[]   mzBytes        = Convert.FromBase64String(strMzs);
            byte[]   intensityBytes = Convert.FromBase64String(strIntensities);
            double[] mzs            = PrimitiveArrays.FromBytes <double>(
                PrimitiveArrays.ReverseBytesInBlocks(mzBytes, sizeof(double)));
            float[] intensityFloats = PrimitiveArrays.FromBytes <float>(
                PrimitiveArrays.ReverseBytesInBlocks(intensityBytes, sizeof(float)));
            double[] intensities = intensityFloats.Select(f => (double)f).ToArray();
            double?  driftTime   = null;
            JToken   jDriftTime;

            if (jObject.TryGetValue("driftTime", out jDriftTime))
            {
                driftTime = jDriftTime.ToObject <double>();
            }
            MsDataSpectrum spectrum = new MsDataSpectrum
            {
                Index         = jObject["index"].ToObject <int>(),
                RetentionTime = jObject["rt"].ToObject <double>(),
                Mzs           = mzs,
                Intensities   = intensities,
                DriftTimeMsec = driftTime,
            };

            return(spectrum);
            // ReSharper restore NonLocalizedString
        }
示例#2
0
            private static void WriteString(Stream stream, string str)
            {
                var bytes = Encoding.UTF8.GetBytes(str);

                PrimitiveArrays.WriteOneValue(stream, bytes.Length);
                stream.Write(bytes, 0, bytes.Length);
            }
示例#3
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);
                }
            }
        }
示例#4
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));
            }
示例#5
0
        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());
            }
        }
示例#6
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);
            }
示例#7
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);
        }
示例#8
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));
            }
示例#9
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));
            }
示例#10
0
        public void Write(Stream stream)
        {
            var memoryStream = new MemoryStream();

            ToLibraryKeyProto().WriteTo(memoryStream);
            PrimitiveArrays.WriteOneValue(stream, (int)memoryStream.Length);
            memoryStream.Seek(0, SeekOrigin.Begin);
            memoryStream.CopyTo(stream);
        }
 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());
 }
示例#12
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);
        }
示例#13
0
 public void Write(Stream stream)
 {
     PrimitiveArrays.WriteOneValue(stream, Id);
     WriteString(stream, FilePath);
     WriteString(stream, SampleName);
     WriteString(stream, AcquiredTime);
     WriteString(stream, ModifiedTime);
     WriteString(stream, InstrumentIonizationType);
     WriteString(stream, InstrumentAnalyzer);
     WriteString(stream, InstrumentDetector);
 }
示例#14
0
 private void WriteEntries()
 {
     _locationEntries = _stream.Position;
     PrimitiveArrays.WriteOneValue(_stream, _library._libraryEntries.Length);
     foreach (var entry in _library._libraryEntries)
     {
         entry.Write(_stream);
     }
     PrimitiveArrays.WriteOneValue(_stream, _library._libraryIrts.Length);
     foreach (var entry in _library._libraryIrts)
     {
         entry.Write(_stream);
     }
 }
示例#15
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));
            }
示例#16
0
 private void WriteHeader()
 {
     _locationHeader = _stream.Position;
     PrimitiveArrays.WriteOneValue(_stream, CURRENT_VERSION);
     PrimitiveArrays.WriteOneValue(_stream, _locationEntries);
     WriteString(_stream, _library.PanoramaServer);
     PrimitiveArrays.WriteOneValue(_stream, _library.LibraryRevision);
     WriteString(_stream, _library.SchemaVersion);
     PrimitiveArrays.WriteOneValue(_stream, _library._librarySourceFiles.Length);
     foreach (var sampleFile in _library._librarySourceFiles)
     {
         sampleFile.Write(_stream);
     }
     PrimitiveArrays.WriteOneValue(_stream, _locationEntries);
     PrimitiveArrays.WriteOneValue(_stream, _locationHeader);
 }
示例#17
0
        public static InterpolatedTimeIntensities ReadFromStream(Stream stream, ChromGroupHeaderInfo chromGroupHeaderInfo, ChromTransition[] chromTransitions)
        {
            Dictionary <ChromSource, int[]> scanIds = new Dictionary <ChromSource, int[]>();
            int numTrans = chromTransitions.Length;

            Assume.IsTrue(numTrans == chromGroupHeaderInfo.NumTransitions);
            int numPoints             = chromGroupHeaderInfo.NumPoints;
            var sharedTimes           = PrimitiveArrays.Read <float>(stream, numPoints);
            var transitionIntensities = new IList <float> [numTrans];

            for (int i = 0; i < numTrans; i++)
            {
                transitionIntensities[i] = PrimitiveArrays.Read <float>(stream, numPoints);
            }
            IList <float>[] transitionMassErrors = new IList <float> [numTrans];
            if (chromGroupHeaderInfo.HasMassErrors)
            {
                for (int i = 0; i < numTrans; i++)
                {
                    transitionMassErrors[i] = ReadMassErrors(stream, numPoints);
                }
            }
            if (chromGroupHeaderInfo.HasFragmentScanIds)
            {
                scanIds.Add(ChromSource.fragment, PrimitiveArrays.Read <int>(stream, numPoints));
            }
            if (chromGroupHeaderInfo.HasSimScanIds)
            {
                scanIds.Add(ChromSource.sim, PrimitiveArrays.Read <int>(stream, numPoints));
            }
            if (chromGroupHeaderInfo.HasMs1ScanIds)
            {
                scanIds.Add(ChromSource.ms1, PrimitiveArrays.Read <int>(stream, numPoints));
            }
            List <TimeIntensities> listOfTimeIntensities = new List <TimeIntensities>();

            for (int i = 0; i < numTrans; i++)
            {
                var   chromSource = chromTransitions[i].Source;
                int[] transitionScanIds;
                scanIds.TryGetValue(chromSource, out transitionScanIds);

                var timeIntensities = new TimeIntensities(sharedTimes, transitionIntensities[i], transitionMassErrors[i], transitionScanIds);
                listOfTimeIntensities.Add(timeIntensities);
            }
            return(new InterpolatedTimeIntensities(listOfTimeIntensities, chromTransitions.Select(chromTransition => chromTransition.Source)));
        }
        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));
        }
示例#19
0
 private IEnumerable <SpectrumPeaksInfo.MI> ReadSpectrumFromPeptideQuants(SQLiteConnection connection, ElibSpectrumInfo info)
 {
     using (var cmd = new SQLiteCommand(connection))
     {
         cmd.CommandText = @"SELECT QuantIonMassLength, QuantIonMassArray, QuantIonIntensityLength, QuantIonIntensityArray FROM peptidequants WHERE PrecursorCharge = ? AND PeptideModSeq = ?";
         cmd.Parameters.Add(new SQLiteParameter(DbType.Int32)
         {
             Value = info.Key.Charge
         });
         cmd.Parameters.Add(new SQLiteParameter(DbType.String)
         {
             Value = info.PeptideModSeq
         });
         SQLiteDataReader reader;
         try
         {
             reader = cmd.ExecuteReader();
         }
         catch (DbException)
         {
             // Older .elib files do not have these columns, so just return null
             return(null);
         }
         using (reader)
         {
             if (!reader.Read())
             {
                 // None of the transitions are considered Quantifiable.
                 return(new SpectrumPeaksInfo.MI[0]);
             }
             double[] mzs = PrimitiveArrays.FromBytes <double>(
                 PrimitiveArrays.ReverseBytesInBlocks(
                     UncompressEncyclopeDiaData((byte[])reader.GetValue(1), reader.GetInt32(0)),
                     sizeof(double)));
             float[] intensities =
                 PrimitiveArrays.FromBytes <float>(PrimitiveArrays.ReverseBytesInBlocks(
                                                       UncompressEncyclopeDiaData((byte[])reader.GetValue(3), reader.GetInt32(2)), sizeof(float)));
             return(mzs.Select(
                        (mz, index) => new SpectrumPeaksInfo.MI {
                 Mz = mz, Intensity = intensities[index]
             }));
         }
     }
 }
            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);
                }
            }
示例#21
0
 private IEnumerable <SpectrumPeaksInfo.MI> ReadSpectrumFromEntriesTable(SQLiteConnection connection, ElibSpectrumInfo info,
                                                                         int sourceFileId)
 {
     using (var cmd = new SQLiteCommand(connection))
     {
         cmd.CommandText =
             @"SELECT MassEncodedLength, MassArray, IntensityEncodedLength, IntensityArray FROM entries WHERE PrecursorCharge = ? AND PeptideModSeq = ? AND SourceFile = ?";
         cmd.Parameters.Add(new SQLiteParameter(DbType.Int32)
         {
             Value = info.Key.Charge
         });
         cmd.Parameters.Add(new SQLiteParameter(DbType.String)
         {
             Value = info.PeptideModSeq
         });
         cmd.Parameters.Add(new SQLiteParameter(DbType.String)
         {
             Value = _sourceFiles[sourceFileId]
         });
         using (var reader = cmd.ExecuteReader())
         {
             if (reader.Read())
             {
                 double[] mzs = PrimitiveArrays.FromBytes <double>(
                     PrimitiveArrays.ReverseBytesInBlocks(
                         UncompressEncyclopeDiaData((byte[])reader.GetValue(1), reader.GetInt32(0)),
                         sizeof(double)));
                 float[] intensities =
                     PrimitiveArrays.FromBytes <float>(PrimitiveArrays.ReverseBytesInBlocks(
                                                           UncompressEncyclopeDiaData((byte[])reader.GetValue(3), reader.GetInt32(2)),
                                                           sizeof(float)));
                 return(mzs.Select((mz, index) => new SpectrumPeaksInfo.MI
                 {
                     Mz = mz,
                     Intensity = intensities[index],
                 })         // CONSIDER(bspratt): annotation?
                        .ToArray());
             }
             return(null);
         }
     }
 }
示例#22
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);
     }
 }
示例#23
0
        public static byte[] ToBytes(IEnumerable <string> scanIds)
        {
            var listStartBytes = new List <int>();
            var listLengths    = new List <int>();
            var listIdBytes    = new List <byte>();

            foreach (var scanId in scanIds)
            {
                int len    = Encoding.UTF8.GetByteCount(scanId);
                var buffer = new byte[len];
                Encoding.UTF8.GetBytes(scanId, 0, scanId.Length, buffer, 0);
                listStartBytes.Add(listIdBytes.Count);
                listLengths.Add(len);
                listIdBytes.AddRange(buffer);
            }
            if (listStartBytes.Count == 0)
            {
                return(new byte[0]);
            }

            var listEntryBytes = new List <byte>();

            listEntryBytes.AddRange(PrimitiveArrays.ToBytes(listStartBytes.ToArray()));
            listEntryBytes.AddRange(PrimitiveArrays.ToBytes(listLengths.ToArray()));
            var entryBytes            = listEntryBytes.ToArray();
            var entryBytesCompressed  = entryBytes.Compress();
            var scanIdBytes           = listIdBytes.ToArray();
            var scanIdBytesCompressed = scanIdBytes.Compress();

            var listResultBytes = new List <byte>();

            listResultBytes.AddRange(BitConverter.GetBytes(entryBytes.Length));
            listResultBytes.AddRange(BitConverter.GetBytes(entryBytesCompressed.Length));
            listResultBytes.AddRange(BitConverter.GetBytes(scanIdBytes.Length));
            listResultBytes.AddRange(BitConverter.GetBytes(scanIdBytesCompressed.Length));
            listResultBytes.AddRange(entryBytesCompressed);
            listResultBytes.AddRange(scanIdBytesCompressed);
            return(listResultBytes.ToArray());
        }
示例#24
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);
         }
     }
 }
示例#25
0
        private MsDataSpectrum GetSpectrumFromJObject(JObject jObject, int msLevel)
        {
            // ReSharper disable NonLocalizedString
            string strMzs         = jObject["mzs-base64"].ToString();
            string strIntensities = jObject["intensities-base64"].ToString();

            byte[]   mzBytes        = Convert.FromBase64String(strMzs);
            byte[]   intensityBytes = Convert.FromBase64String(strIntensities);
            double[] mzs            = PrimitiveArrays.FromBytes <double>(
                PrimitiveArrays.ReverseBytesInBlocks(mzBytes, sizeof(double)));
            float[] intensityFloats = PrimitiveArrays.FromBytes <float>(
                PrimitiveArrays.ReverseBytesInBlocks(intensityBytes, sizeof(float)));
            double[]         intensities = intensityFloats.Select(f => (double)f).ToArray();
            IonMobilityValue ionMobility = IonMobilityValue.EMPTY;
            JToken           jDriftTime;

            if (jObject.TryGetValue("driftTime", out jDriftTime))
            {
                var driftTime = jDriftTime.ToObject <double>();
                if (driftTime != 0)
                {
                    ionMobility = IonMobilityValue.GetIonMobilityValue(driftTime, MsDataFileImpl.eIonMobilityUnits.drift_time_msec);
                }
            }
            MsDataSpectrum spectrum = new MsDataSpectrum
            {
                Index         = jObject["index"].ToObject <int>(),
                RetentionTime = jObject["rt"].ToObject <double>(),
                Mzs           = mzs,
                Intensities   = intensities,
                IonMobility   = ionMobility,
            };

            return(spectrum);
            // ReSharper restore NonLocalizedString
        }
示例#26
0
        public static MsDataFileScanIds FromBytes(byte[] byteArray)
        {
            if (byteArray.Length == 0)
            {
                return(null);
            }

            int i = 0;
            int entryBytesCount            = GetInt(i++, byteArray);
            int entryBytesCompressedCount  = GetInt(i++, byteArray);
            int scanIdBytesCount           = GetInt(i++, byteArray);
            int scanIdBytesCompressedCount = GetInt(i++, byteArray);

            var entryBytesCompressed = new byte[entryBytesCompressedCount];

            Array.Copy(byteArray, i * sizeof(int), entryBytesCompressed, 0, entryBytesCompressedCount);
            var entryBytes      = entryBytesCompressed.Uncompress(entryBytesCount);
            var startBytesArray = new byte[entryBytes.Length / 2];

            Array.Copy(entryBytes, 0, startBytesArray, 0, startBytesArray.Length);
            var startBytes       = PrimitiveArrays.FromBytes <int>(startBytesArray);
            var lengthBytesArray = new byte[entryBytes.Length / 2];

            Array.Copy(entryBytes, startBytesArray.Length, lengthBytesArray, 0, lengthBytesArray.Length);
            var lengths = PrimitiveArrays.FromBytes <int>(lengthBytesArray);

            Assume.IsTrue(startBytesArray.Length + lengthBytesArray.Length == entryBytes.Length);

            var scanIdBytesCompressed = new byte[scanIdBytesCompressedCount];

            Array.Copy(byteArray, i * sizeof(int) + entryBytesCompressedCount, scanIdBytesCompressed,
                       0, scanIdBytesCompressedCount);
            var scanIdBytes = scanIdBytesCompressed.Uncompress(scanIdBytesCount);

            return(new MsDataFileScanIds(startBytes, lengths, scanIdBytes));
        }
示例#27
0
 private static float[] ReadMassErrors(Stream stream, int count)
 {
     return(PrimitiveArrays.Read <short>(stream, count).Select(value => value / 10f).ToArray());
 }
示例#28
0
 private static void WriteMassErrors(Stream stream, IList <float> values)
 {
     PrimitiveArrays.Write(stream, values.Select(value => ChromPeak.To10x(value)).ToArray());
 }
示例#29
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) { }
                }
            }
        }
示例#30
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);
        }