Esempio n. 1
0
        // opens BAM file
        public void Open(string filename)
        {
            if (!File.Exists(filename))
            {
                throw new ApplicationException(string.Format("ERROR: The supplied BAM filename ({0}) does not exist.",
                                                             filename));
            }

            BamPath = filename;

            // sanity check: make sure this is a GZIP file
            ushort gzipMagicNumber;

            using (BinaryReader checkReader = new BinaryReader(new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)))
            {
                gzipMagicNumber = checkReader.ReadUInt16();
            }

            if (gzipMagicNumber != BamConstants.GzipMagicNumber)
            {
                throw new ApplicationException(string.Format(
                                                   "The input file ({0}) does not seem to be a BAM file. A GZIP magic number could not be found.", filename));
            }

            // open our file streams
            try
            {
                BgzfFileStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
                _reader        = new BinaryReader(BgzfFileStream);
            }
            catch (IOException e)
            {
                throw new ApplicationException(string.Format(
                                                   "ERROR: Unable to open the BAM file ({0}) for reading: {1}", filename, e.Message));
            }

            IsOpen = true;

            // open the index file if it exists
            string indexPath = string.Format("{0}.bai", filename);

            if (_index.ReadIndex(indexPath))
            {
                _hasIndex = true;
            }

            LoadHeaderData();
            LoadReferenceData();

            // store file offset of first alignment
            _alignmentsOffset = ((BlockAddress << 16) | ((long)BlockOffset & 0xFFFF));
        }
Esempio n. 2
0
        // opens BAM file
        public void Open(Stream inStream)
        {
            // sanity check: make sure this is a GZIP file
            ushort gzipMagicNumber;

            BgzfFileStream = inStream;
            try
            {
                _reader = new BinaryReader(BgzfFileStream);
            }
            catch (IOException e)
            {
                throw new IOException(string.Format(
                                          "ERROR: Unable to open the BAM file ({0}) for reading: {1}", BamPath, e.Message));
            }

            try
            {
                gzipMagicNumber = _reader.ReadUInt16();
                if (gzipMagicNumber != BamConstants.GzipMagicNumber)
                {
                    throw new InvalidDataException($"A GZIP magic number could not be found. Check BAM file {BamPath}");
                }
            }
            catch (Exception e)
            {
                throw new IOException($"The input file ({BamPath}) does not seem to be a BAM file.", e);
            }

            inStream.Seek(0, SeekOrigin.Begin);
            IsOpen = true;

            // open the index file if it exists
            string indexPath = string.Format("{0}.bai", BamPath);

            if (!string.IsNullOrEmpty(BamPath) && _index.ReadIndex(indexPath))
            {
                _hasIndex = true;
            }

            LoadHeaderData();
            LoadReferenceData();

            // store file offset of first alignment
            _alignmentsOffset = ((BlockAddress << 16) | ((long)BlockOffset & 0xFFFF));
        }