public void Load(string indexFileName) { using (var file = File.OpenRead(indexFileName)) using (var decompressor = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(file)) { var serializer = new NetSerializer.Serializer(new Type[] { typeof(Indexes) }); Indexes indexes = (Indexes)serializer.Deserialize(decompressor); _chunkIdToFirstNonFreeObjectInChunk = indexes.ChunkIdToFirstNonFreeObjectInChunk; _startOfChunkToChunkId = indexes.StartOfChunkToChunkId; _chunkToReferencingChunks = indexes.ChunkToReferencingChunks; _directlyRooted = new HashSet<ulong>(indexes.DirectlyRooted); _staticRootsEnumerated = indexes.StaticRootsEnumerated; _allRoots = indexes.AllRoots; _chunkSize = indexes.ChunkSize; } if (!_staticRootsEnumerated) { _context.WriteWarningLine("This heap index does not have detailed static root information. " + "As a result, you will not see the names of static variables referencing your objects, " + "only their addresses. Recreate the index without the --fast switch to get full " + "information. This may be slower."); } }
private void GenerateIndexes(IndexingEngine indexEngine, List <DbForTask> dbFilenameList, ref List <CompactPeptide> peptideIndex, ref List <int>[] fragmentIndex, string taskId) { string pathToFolderWithIndices = GetExistingFolderWithIndices(indexEngine, dbFilenameList); if (pathToFolderWithIndices == null) { var output_folderForIndices = GenerateOutputFolderForIndices(dbFilenameList); Status("Writing params...", new List <string> { taskId }); var paramsFile = Path.Combine(output_folderForIndices, "indexEngine.params"); WriteIndexEngineParams(indexEngine, paramsFile); FinishedWritingFile(paramsFile, new List <string> { taskId }); Status("Running Index Engine...", new List <string> { taskId }); var indexResults = (IndexingResults)indexEngine.Run(); peptideIndex = indexResults.PeptideIndex; fragmentIndex = indexResults.FragmentIndex; Status("Writing peptide index...", new List <string> { taskId }); var peptideIndexFile = Path.Combine(output_folderForIndices, "peptideIndex.ind"); WritePeptideIndex(peptideIndex, peptideIndexFile); FinishedWritingFile(peptideIndexFile, new List <string> { taskId }); Status("Writing fragment index...", new List <string> { taskId }); var fragmentIndexFile = Path.Combine(output_folderForIndices, "fragmentIndex.ind"); WriteFragmentIndexNetSerializer(fragmentIndex, fragmentIndexFile); FinishedWritingFile(fragmentIndexFile, new List <string> { taskId }); } else { Status("Reading peptide index...", new List <string> { taskId }); var messageTypes = GetSubclassesAndItself(typeof(List <CompactPeptide>)); var ser = new NetSerializer.Serializer(messageTypes); using (var file = File.OpenRead(Path.Combine(pathToFolderWithIndices, "peptideIndex.ind"))) peptideIndex = (List <CompactPeptide>)ser.Deserialize(file); Status("Reading fragment index...", new List <string> { taskId }); messageTypes = GetSubclassesAndItself(typeof(List <int>[])); ser = new NetSerializer.Serializer(messageTypes); using (var file = File.OpenRead(Path.Combine(pathToFolderWithIndices, "fragmentIndex.ind"))) fragmentIndex = (List <int>[])ser.Deserialize(file); } }
public static void TestCompactPeptideSerialization() { // purpose of this test is to serialize/deserialize a CompactPeptide and make sure the deserialized peptide // has the same properties as before it was serialized. This peptide is unmodified string sequence = "PEPTIDE"; PeptideWithSetModifications p = new PeptideWithSetModifications(sequence, new Dictionary <string, Modification>(), 0, null, null, 0, 7, 0, null); CompactPeptide cp = p.CompactPeptide(FragmentationTerminus.Both); CompactPeptide deserializedCp = null; string dir = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "TestCompactPeptideSerialization"); System.IO.Directory.CreateDirectory(dir); string path = System.IO.Path.Combine(dir, "myCompactPeptideIndex.ind"); var messageTypes = typeof(CompactPeptide); var ser = new NetSerializer.Serializer(new List <Type> { messageTypes }); using (var file = System.IO.File.Create(path)) { ser.Serialize(file, cp); } using (var file = System.IO.File.OpenRead(path)) { deserializedCp = (CompactPeptide)ser.Deserialize(file); } Assert.That(cp.Equals(deserializedCp)); }
public static T Deserialize <T>(byte[] arr, bool gzip = false) { NetSerializer.Serializer Serializer = new NetSerializer.Serializer(new Type[] { typeof(T) }); try { using (MemoryStream memoryStream = new MemoryStream(arr)) { if (gzip) { using (GZipStream gzipSteam = new GZipStream(memoryStream, CompressionMode.Decompress)) { return((T)Serializer.Deserialize(gzipSteam)); } } return((T)Serializer.Deserialize(memoryStream)); } } catch { return(default(T)); } }
public T Deserialize <T>(byte[] serialized) { using (var stream = new MemoryStream(serialized)) { Serializer.Deserialize(stream, out var result); return((T)result); } }
static T RunNetSerializer <T>(T obj) // Size = 79 { _memStream.Position = 0; _netSerializer.Serialize(_memStream, obj); _memStream.Position = 0; _netSerializer.Deserialize(_memStream, out object cloneObj); var clone = (T)cloneObj; return(clone); }
public static T Deserialize <T>(string filePath, bool gzip = false) { NetSerializer.Serializer Serializer = new NetSerializer.Serializer(new Type[] { typeof(T) }); fileLock.TryAdd(filePath, new ReaderWriterLockSlim()); if (fileLock.TryGetValue(filePath, out ReaderWriterLockSlim rwlock)) { rwlock.EnterReadLock(); try { using (Stream stream = File.Open(filePath, FileMode.Open)) { if (gzip) { using (GZipStream gzipSteam = new GZipStream(stream, CompressionMode.Decompress)) { return((T)Serializer.Deserialize(gzipSteam)); } } return((T)Serializer.Deserialize(stream)); } } finally { rwlock.ExitReadLock(); } } return(default(T)); }
public static void TestSerializationPeptideFromProtein() { // purpose of this test is to serialize/deserialize a PeptideWithSetModifications and make sure the deserialized peptide // has the same properties as before it was serialized. This peptide is unmodified and generated from digesting a protein Protein protein = new Protein("PEPTIDE", "Accession1", name: "MyProtein"); PeptideWithSetModifications peptide = protein.Digest(new DigestionParams(), new List <Modification>(), new List <Modification>()).First(); PeptideWithSetModifications deserializedPeptide = null; string dir = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "TestSerializationPeptideFromProtein"); System.IO.Directory.CreateDirectory(dir); string path = System.IO.Path.Combine(dir, "myPeptideIndex.ind"); var messageTypes = typeof(PeptideWithSetModifications); var ser = new NetSerializer.Serializer(new List <Type> { messageTypes }); using (var file = System.IO.File.Create(path)) { ser.Serialize(file, peptide); } using (var file = System.IO.File.OpenRead(path)) { deserializedPeptide = (PeptideWithSetModifications)ser.Deserialize(file); } deserializedPeptide.SetNonSerializedPeptideInfo(new Dictionary <string, Modification>(), new Dictionary <string, Protein> { { protein.Accession, protein } }, peptide.DigestionParams); Assert.That(peptide.DigestionParams.Equals(deserializedPeptide.DigestionParams)); Assert.That(peptide.Equals(deserializedPeptide)); Assert.That(deserializedPeptide.Protein.Name == peptide.Protein.Name); Assert.That(deserializedPeptide.MonoisotopicMass == peptide.MonoisotopicMass); Assert.That(deserializedPeptide.SequenceWithChemicalFormulas == peptide.SequenceWithChemicalFormulas); var products = new List <Product>(); deserializedPeptide.Fragment(DissociationType.HCD, FragmentationTerminus.Both, products); List <double> deserializedPeptideFragments = products.Select(v => v.NeutralMass).ToList(); peptide.Fragment(DissociationType.HCD, FragmentationTerminus.Both, products); List <double> peptideFragments = products.Select(v => v.NeutralMass).ToList(); Assert.That(deserializedPeptideFragments.SequenceEqual(peptideFragments)); }
public static void TestSerializationPeptideFromString() { // purpose of this test is to serialize/deserialize a PeptideWithSetModifications and make sure the deserialized peptide // has the same properties as before it was serialized. This peptide is unmodified and generated from reading in a string string sequence = "PEPTIDE"; PeptideWithSetModifications peptide = new PeptideWithSetModifications(sequence, new Dictionary <string, Modification>(), 0, null, null, 1, 7, 0); PeptideWithSetModifications deserializedPeptide = null; string dir = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "TestSerializationPeptideFromString"); System.IO.Directory.CreateDirectory(dir); string path = System.IO.Path.Combine(dir, "myPeptideIndex.ind"); var messageTypes = typeof(PeptideWithSetModifications); var ser = new NetSerializer.Serializer(new List <Type> { messageTypes }); using (var file = System.IO.File.Create(path)) { ser.Serialize(file, peptide); } using (var file = System.IO.File.OpenRead(path)) { deserializedPeptide = (PeptideWithSetModifications)ser.Deserialize(file); } deserializedPeptide.SetNonSerializedPeptideInfo(new Dictionary <string, Modification>(), new Dictionary <string, Protein>(), null); // not asserting any protein properties - since the peptide was created from a sequence string it didn't have a protein to begin with Assert.That(peptide.Equals(deserializedPeptide)); Assert.That(deserializedPeptide.MonoisotopicMass == peptide.MonoisotopicMass); Assert.That(deserializedPeptide.SequenceWithChemicalFormulas == peptide.SequenceWithChemicalFormulas); var products = new List <Product>(); deserializedPeptide.Fragment(DissociationType.HCD, FragmentationTerminus.Both, products); List <double> deserializedPeptideFragments = products.Select(v => v.NeutralMass).ToList(); peptide.Fragment(DissociationType.HCD, FragmentationTerminus.Both, products); List <double> peptideFragments = products.Select(v => v.NeutralMass).ToList(); Assert.That(deserializedPeptideFragments.SequenceEqual(peptideFragments)); }
private void SerializeNetSerializer() { var s = new MemoryStream(); var serializer = new NetSerializer.Serializer(new[] { typeof(T) }); serializer.Serialize(s, Value); var bytes = s.ToArray(); RunTest("Net Serializer", () => { var stream = new MemoryStream(); serializer.Serialize(stream, Value); }, () => { s.Position = 0; serializer.Deserialize(s); }, bytes.Length); }
private void RunBenchmarkNetSerializer <T>(T obj, int count) { try { Stopwatch sw; //----------------------------------- var netSerializer = new NetSerializer.Serializer(new[] { typeof(T) }); var pbuffMem = new MemoryStream(); netSerializer.Serialize(pbuffMem, obj); using (var mem = new MemoryStream()) { sw = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { netSerializer.Serialize(mem, obj); mem.SetLength(0); } } sw.Stop(); Log("NetSerializer.Serialize took: "+ ToString(sw.Elapsed) + " data-size: " + pbuffMem.Length); sw = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { pbuffMem.Seek(0, SeekOrigin.Begin); netSerializer.Deserialize(pbuffMem); } sw.Stop(); Log("NetSerializer.Deserialize took: "+ ToString(sw.Elapsed)); } catch (Exception ex) { Log("NetSerializer failed, " + ex.Message); } }
/// <summary> /// Deserializes the specified object. /// </summary> /// <param name="obj">The object.</param> /// <returns></returns> /// <exception cref="SerializationException">An object in the graph of type parameter <typeparamref name="T" /> is not marked as serializable.</exception> private T Deserialize(byte[] obj) { if (typeof(T) == typeof(byte)) { // Type is byte. return((T)(object)obj[0]); } else if (typeof(T) == typeof(byte[])) { // Type is array of byte. return((T)(object)obj); } else if (typeof(T) == typeof(string)) { // Type is string. char[] chars = new char[obj.Length / sizeof(char)]; System.Buffer.BlockCopy(obj, 0, chars, 0, obj.Length); return((T)(object)new string(chars)); } else { // Type is something else. try { using (var memoryStream = new MemoryStream(obj)) { return((T)_fastBinaryFormatter.Deserialize(memoryStream)); } } catch { // if fast serialization did not work, try slow .net one. using (var memoryStream = new MemoryStream(obj)) { return((T)_binaryFormatter.Deserialize(memoryStream)); } } } }
public void Load(string indexFileName) { using (var file = File.OpenRead(indexFileName)) using (var decompressor = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(file)) { var serializer = new NetSerializer.Serializer(new Type[] { typeof(Indexes) }); Indexes indexes = (Indexes)serializer.Deserialize(decompressor); _chunkIdToFirstNonFreeObjectInChunk = indexes.ChunkIdToFirstNonFreeObjectInChunk; _startOfChunkToChunkId = indexes.StartOfChunkToChunkId; _chunkToReferencingChunks = indexes.ChunkToReferencingChunks; _directlyRooted = new HashSet <ulong>(indexes.DirectlyRooted); _staticRootsEnumerated = indexes.StaticRootsEnumerated; _allRoots = indexes.AllRoots; _chunkSize = indexes.ChunkSize; } if (!_staticRootsEnumerated) { _context.WriteWarningLine("This heap index does not have detailed static root information. " + "As a result, you will not see the names of static variables referencing your objects, " + "only their addresses. Recreate the index without the --fast switch to get full " + "information. This may be slower."); } }
public static void TestSerializationPeptideFromProteinWithMod() { // purpose of this test is to serialize/deserialize a PeptideWithSetModifications and make sure the deserialized peptide // has the same properties as before it was serialized. This peptide is modified with a phosphorylation ModificationMotif.TryGetMotif("T", out ModificationMotif motif); Dictionary <DissociationType, List <double> > myNeutralLosses = new Dictionary <DissociationType, List <double> >() { { DissociationType.HCD, new List <double> { ChemicalFormula.ParseFormula("H3 O4 P1").MonoisotopicMass } }, { DissociationType.ETD, new List <double>() { ChemicalFormula.ParseFormula("H3 N1").MonoisotopicMass } } // this makes no sense in real life, it's just for a unit test }; Modification mod = new Modification(_originalId: "phospho", _modificationType: "testModType", _target: motif, _chemicalFormula: ChemicalFormula.ParseFormula("H1 O3 P1"), _neutralLosses: myNeutralLosses, _locationRestriction: "Anywhere."); Dictionary <int, List <Modification> > mods = new Dictionary <int, List <Modification> > { { 4, new List <Modification> { mod } } }; Protein protein = new Protein("PEPTIDE", "Accession1", name: "MyProtein", oneBasedModifications: mods); PeptideWithSetModifications peptide = protein.Digest(new DigestionParams(), new List <Modification>(), new List <Modification>()).Where(v => v.AllModsOneIsNterminus.Count == 1).First(); PeptideWithSetModifications deserializedPeptide = null; string dir = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "TestSerializationPeptideFromProteinWithMod"); System.IO.Directory.CreateDirectory(dir); string path = System.IO.Path.Combine(dir, "myPeptideIndex.ind"); var messageTypes = typeof(PeptideWithSetModifications); var ser = new NetSerializer.Serializer(new List <Type> { messageTypes }); using (var file = System.IO.File.Create(path)) { ser.Serialize(file, peptide); } using (var file = System.IO.File.OpenRead(path)) { deserializedPeptide = (PeptideWithSetModifications)ser.Deserialize(file); } Dictionary <string, Modification> stringToMod = new Dictionary <string, Modification> { { mods.Values.First().First().IdWithMotif, mods.Values.First().First() } }; deserializedPeptide.SetNonSerializedPeptideInfo(stringToMod, new Dictionary <string, Protein> { { protein.Accession, protein } }, peptide.DigestionParams); Assert.That(peptide.Equals(deserializedPeptide)); Assert.That(deserializedPeptide.Protein.Name == peptide.Protein.Name); Assert.That(deserializedPeptide.MonoisotopicMass == peptide.MonoisotopicMass); Assert.That(deserializedPeptide.SequenceWithChemicalFormulas == peptide.SequenceWithChemicalFormulas); var products = new List <Product>(); deserializedPeptide.Fragment(DissociationType.HCD, FragmentationTerminus.Both, products); List <double> deserializedPeptideFragments = products.Select(v => v.NeutralMass).ToList(); peptide.Fragment(DissociationType.HCD, FragmentationTerminus.Both, products); List <double> peptideFragments = products.Select(v => v.NeutralMass).ToList(); Assert.That(deserializedPeptideFragments.SequenceEqual(peptideFragments)); }
public void NetSerializer() { _m5.Seek(0, SeekOrigin.Begin); _netSerializer.Deserialize(_m5); }
public static Message Deserialize(Stream stream) { object ob = s_serializer.Deserialize(stream); return((Message)ob); }
public T Deserialize <T>(Stream stream) { return((T)_netSerializer.Deserialize(stream)); }