public void Write(string filename, SequenceAlignment alignment) { if (filename == null) { return; //Error condition, output filename not specified. } CreateTempWorkingDirectory(); if (_tempDirectory != null) { WriteManifest(alignment); WriteAlignment(alignment); if (File.Exists(filename)) { try { File.Delete(filename); } catch { DeleteTempWorkingDirectory(); //Error condition, failed to remove an existing copy of the target file return; } } Package outputfile = ZipPackage.Open(filename, FileMode.Create); PackagePart manifestFilePart = outputfile.CreatePart(new Uri("/" + CRWSequenceAlignmentFormatTags.ManifestFileName, UriKind.Relative), System.Net.Mime.MediaTypeNames.Text.Xml, CompressionOption.Fast); PackagePart alignmentFilePart = outputfile.CreatePart(new Uri("/" + CRWSequenceAlignmentFormatTags.AlignmentFileName, UriKind.Relative), System.Net.Mime.MediaTypeNames.Text.Plain, CompressionOption.Maximum); Stream manifestFileStream = manifestFilePart.GetStream(); Stream alignmentFileStream = alignmentFilePart.GetStream(); using (var manifestFile = new BufferedStream(File.Open((_tempDirectory + CRWSequenceAlignmentFormatTags.ManifestFileName), FileMode.Open))) { byte[] buf = new byte[1024]; //We read with 1MB buffer int count = 0; while ((count = manifestFile.Read(buf, 0, buf.Length)) > 0) { manifestFileStream.Write(buf, 0, count); //We write into the zip archive } } using (var alignmentFile = new BufferedStream(File.Open((_tempDirectory + CRWSequenceAlignmentFormatTags.AlignmentFileName), FileMode.Open))) { byte[] buf = new byte[1024]; //We read with 1MB buffer int count = 0; while ((count = alignmentFile.Read(buf, 0, buf.Length)) > 0) { alignmentFileStream.Write(buf, 0, count); //We write into the zip archive } } outputfile.Close(); DeleteTempWorkingDirectory(); return; } else { DeleteTempWorkingDirectory(); return; } //Error condition, failed to create a working directory. }
private XElement WriteAlignmentMetadata(SequenceAlignment alignment) { XElement output = new XElement(CRWSequenceAlignmentFormatTags.AlignmentLabel); output.Add(new XElement(CRWSequenceAlignmentFormatTags.AlignmentFileNameLabel, CRWSequenceAlignmentFormatTags.AlignmentFileName)); output.Add(new XElement(CRWSequenceAlignmentFormatTags.AlignmentLogicalNameLabel, alignment.LogicalName)); output.Add(new XElement(CRWSequenceAlignmentFormatTags.SequenceTypeLabel, new XElement(CRWSequenceAlignmentFormatTags.MoleculeTypeLabel, alignment.MoleculeType), new XElement(CRWSequenceAlignmentFormatTags.GeneTypeLabel, alignment.GeneType), new XElement(CRWSequenceAlignmentFormatTags.GeneNameLabel, alignment.GeneName))); return(output); }
private void WriteAlignment(SequenceAlignment alignment) { FastaFormatter formatter = new FastaFormatter(); StreamWriter alnoutput = new StreamWriter(_tempDirectory + CRWSequenceAlignmentFormatTags.AlignmentFileName); alnoutput.AutoFlush = true; foreach (ISequence sequence in alignment.Sequences) { alnoutput.Write(formatter.FormatString(sequence)); } alnoutput.Close(); }
private bool LoadAlignment() { Uri alignmentURI = new Uri("/" + _alignmentFileName, UriKind.Relative); var alnfileentry = from entry in _crwFile.GetParts() where entry.Uri.Equals(alignmentURI) select entry; PackagePart alnEntry = alnfileentry.First(); if (alnEntry != null) { //Extract the alignment file from the zip archive. using (var reader = new BufferedStream(alnEntry.GetStream(FileMode.Open))) { using (var writer = new BufferedStream(File.Create(_tempDirectory + _alignmentFileName))) { byte[] buf = new byte[1024]; int readCount = 0; while ((readCount = reader.Read(buf, 0, buf.Length)) > 0) { writer.Write(buf, 0, readCount); } } } ISequenceParser parser = SequenceParsers.FindParserByFile(_tempDirectory + _alignmentFileName); _alignment = new SequenceAlignment(parser.Parse(_tempDirectory + _alignmentFileName)); _alignment.MoleculeType = _moleculeType; _alignment.GeneType = _geneType; _alignment.GeneName = _geneName; _alignment.LogicalName = _logicalAlignmentName; foreach (var seq in _alignment.Sequences) { //Workaround for MBF Framework, have to cast Sequence objects as writeable, opened feature request. Sequence s = seq as Sequence; if (s != null) { s.IsReadOnly = false; } } return(true); } else { return(false); } //Error condition, missing alignment file. }
private void WriteManifest(SequenceAlignment alignment) { XElement manifest = new XElement(CRWSequenceAlignmentFormatTags.ManifestLabel); XElement alnMetadata = WriteAlignmentMetadata(alignment); manifest.Add(WriteAlignmentMetadata(alignment)); foreach (var sequence in alignment.Sequences) { XElement seqmetdata = WriteSequenceMetadata(sequence); if (seqmetdata != null) { manifest.Add(seqmetdata); } } manifest.Save(_tempDirectory + CRWSequenceAlignmentFormatTags.ManifestFileName); }