Example #1
0
 /// <summary>
 ///     Opens the file
 /// </summary>
 public override void Open(string filePath)
 {
     _writer = new BgzipWriter(filePath, GzipCompressionLevel.BestSpeed1);
     FileName = filePath;
     IsOpen = true;
     WriteHeaderLines();
 }
Example #2
0
		/// <summary>
		///     Given a list of input bgzipped files, output a single bgzipped file which is a concatentation
		///     of the input files. The zero size blocks that are used as EOF markers will be removed except for a final 
		///     zero size block at the end of the output file.
		///     Note: this was adapted from bgzf_cat.c written by Chris Saunders
		/// </summary>
		public static void BgzfCat(List<string> inputFilenames, string outputFilename, GzipCompressionLevel compressionLevel = GzipCompressionLevel.DefaultCompression5)
		{
			// some useful constants:
			int gZipId1 = 31;
			int gZipId2 = 139;
			int emptyBlockSize = 28;

			// our buffers
			byte[] blockBuffer = new byte[MaxBlockSize];
			byte[] emptyBlockBuffer = new byte[emptyBlockSize];

			// our output file
			using (BgzipWriter writer = new BgzipWriter(outputFilename, compressionLevel))
			{
				foreach (string inputFilename in inputFilenames)
				{
					using (BgzipReader inputFile = new BgzipReader(inputFilename))
					{
						// if we uncompressed some data in the process of opening this file we need to compress and write it back to the output
						if (inputFile.BlockOffset < inputFile.BlockLength)
						{
							//sanity check, we just opened this file so the offset should be 0
							if (inputFile.BlockOffset != 0)
								throw new ApplicationException("Opened bgzipped file and not at the start of a new block!");
							writer.Write(inputFile.LineBuffer, (uint)(inputFile.BlockLength - inputFile.BlockOffset));
							writer.FlushBlock();
						}
						bool firstRead = true;
						int bytesRead;
						while ((bytesRead = inputFile.BgzfFileStream.Read(blockBuffer, 0, MaxBlockSize)) > 0)
						{
							if (bytesRead < emptyBlockSize)
							{
								// if this is the first read it must be at least the size of an empty block
								if (firstRead)
									throw new ApplicationException("BgzfCat Error: truncated file?: " + inputFilename);

								// this is the remainder of the final empty block for this input file

								// let's write out the beginning of the data from the emptyBlockBuffer which we know is not part of this final empty block (i.e. it is the end of the block from the previous write)
								writer.BgzfFileStream.Write(emptyBlockBuffer, 0, bytesRead);

								// save the final empty block so we can perform a sanity check
								Buffer.BlockCopy(emptyBlockBuffer, bytesRead, emptyBlockBuffer, 0, emptyBlockSize - bytesRead);
								Buffer.BlockCopy(blockBuffer, 0, emptyBlockBuffer, emptyBlockSize - bytesRead, bytesRead);
							}
							else
							{
								if (!firstRead)
								{
									// let's write the data that we saved from the previous read (i.e. it wasn't the empty block EOF)
									writer.BgzfFileStream.Write(emptyBlockBuffer, 0, emptyBlockSize);
								}
								// save some data at the end in case it is from the empty block EOF. we don't want to write out the empty block EOF.
								// the empty block EOF is automatically written when the BgzipWriter is disposed
								Buffer.BlockCopy(blockBuffer, bytesRead - emptyBlockSize, emptyBlockBuffer, 0, emptyBlockSize);
								//write out the data that we aren't saving
								writer.BgzfFileStream.Write(blockBuffer, 0, bytesRead - emptyBlockSize);
							}
							firstRead = false;
						}

						// if the one and only block was the empty block we need to copy it to emptyBlockBuffer to perform sanity checking
						if (firstRead)
							Buffer.BlockCopy(inputFile.CompressedBlock, 0, emptyBlockBuffer, 0, emptyBlockSize);

						// sanity check for the final gzip block 
						int blockSize = emptyBlockBuffer[emptyBlockSize - 4];
						if (emptyBlockBuffer[0] != gZipId1 || emptyBlockBuffer[1] != gZipId2 || blockSize != 0)
						{
							throw new ApplicationException("BgzfCat Error: unexpected final block structure in file " + inputFilename);
						}
					}
				}
			}
		}
Example #3
0
		/// <summary>
		///     Bgzip a text file. Also converts any \r\n newlines to \n
		/// </summary>
		public static void CompressFile(StreamReader inputStream, Stream outputStream, GzipCompressionLevel compressionLevel = GzipCompressionLevel.DefaultCompression5)
		{
			// our output file
			using (BgzipWriter writer = new BgzipWriter(outputStream, compressionLevel))
			{
				string fileLine;
				while ((fileLine = inputStream.ReadLine()) != null)
				{
					writer.WriteLine(fileLine);
				}
			}
		}