public static void Serialize <T>(string filePath, T objectToWrite, 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.EnterWriteLock(); try { using (Stream stream = File.Open(filePath, FileMode.Create)) { if (gzip) { using (GZipStream gzipSteam = new GZipStream(stream, CompressionMode.Compress)) { Serializer.Serialize(gzipSteam, objectToWrite); } } else { Serializer.Serialize(stream, objectToWrite); } } } finally { rwlock.ExitWriteLock(); } } }
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 byte[] Serialize(object toSerialize) { using (var stream = new MemoryStream()) { Serializer.Serialize(stream, toSerialize); return(stream.ToArray()); } }
private static void WriteFragmentIndexNetSerializer(List <int>[] fragmentIndex, string fragmentIndexFile) { var messageTypes = GetSubclassesAndItself(typeof(List <int>[])); var ser = new NetSerializer.Serializer(messageTypes); using (var file = File.Create(fragmentIndexFile)) ser.Serialize(file, fragmentIndex); }
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 static void WritePeptideIndex(List <CompactPeptide> peptideIndex, string peptideIndexFile) { var messageTypes = GetSubclassesAndItself(typeof(List <CompactPeptide>)); var ser = new NetSerializer.Serializer(messageTypes); using (var file = File.Create(peptideIndexFile)) { ser.Serialize(file, peptideIndex); } }
private void WriteFragmentIndexNetSerializer(List <int>[] fragmentIndex, string fragmentIndexFile, string taskId) { var messageTypes = GetSubclassesAndItself(typeof(Dictionary <float, List <int> >)); var ser = new NetSerializer.Serializer(messageTypes); using (var file = File.Create(fragmentIndexFile)) ser.Serialize(file, fragmentIndex); FinishedWritingFile(fragmentIndexFile, new List <string> { taskId }); }
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 byte[] Serialize <T>(T objectToWrite, bool gzip = false) { NetSerializer.Serializer Serializer = new NetSerializer.Serializer(new Type[] { typeof(T) }); try { using (var stream = new MemoryStream()) { if (gzip) { using (GZipStream gzipSteam = new GZipStream(stream, CompressionMode.Compress)) { Serializer.Serialize(gzipSteam, objectToWrite); } } else { Serializer.Serialize(stream, objectToWrite); } return(stream.ToArray()); } } catch (Exception e) { return(null); } }
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> /// Serializes 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 byte[] Serialize(T obj) { if (typeof(T) == typeof(byte)) { // Type is byte. return(new byte[] { (byte)(object)obj }); } else if (typeof(T) == typeof(byte[])) { // Type is array of byte. return((byte[])(object)obj); } else if (typeof(T) == typeof(string)) { // Type is string. string str = (string)(object)obj; byte[] bytes = new byte[str.Length * sizeof(char)]; System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); return(bytes); } else { // Type is something else. try { using (var memoryStream = new MemoryStream()) { _fastBinaryFormatter.Serialize(memoryStream, obj); return(memoryStream.ToArray()); } } catch { // if fast serialization did not work, try slow .net one. try { using (var memoryStream = new MemoryStream()) { _binaryFormatter.Serialize(memoryStream, obj); return(memoryStream.ToArray()); } } catch { //if any exception in the serialize, it will stop wrapper, so there will ignore any exception. return(null); } } } }
private void WritePeptideIndex(List <CompactPeptide> peptideIndex, string peptideIndexFile, string taskId) { var messageTypes = GetSubclassesAndItself(typeof(List <CompactPeptide>)); var ser = new NetSerializer.Serializer(messageTypes); using (var file = File.Create(peptideIndexFile)) { ser.Serialize(file, peptideIndex); } FinishedWritingFile(peptideIndexFile, new List <string> { taskId }); }
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 Save(string indexFileName) { using (var file = File.Create(indexFileName)) using (var compressor = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(file)) { var serializer = new NetSerializer.Serializer(new Type[] { typeof(Indexes) }); var indexes = new Indexes(); indexes.ChunkIdToFirstNonFreeObjectInChunk = _chunkIdToFirstNonFreeObjectInChunk; indexes.StartOfChunkToChunkId = _startOfChunkToChunkId; indexes.ChunkToReferencingChunks = _chunkToReferencingChunks; indexes.DirectlyRooted = _directlyRooted.ToArray(); indexes.AllRoots = _allRoots; indexes.StaticRootsEnumerated = _staticRootsEnumerated; indexes.ChunkSize = _chunkSize; serializer.Serialize(compressor, indexes); } _context.WriteLine("Wrote index file of size {0}", ((ulong)new FileInfo(indexFileName).Length).ToMemoryUnits()); }
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 SetupNetserializer() { _netSerializer.Serialize(_m5, _t1); }
public static void Serialize(Stream stream, Message msg) { s_serializer.Serialize(stream, msg); }
public void Serialize <T>(Stream stream, T obj) { _netSerializer.Serialize(stream, obj); }