Example #1
0
        private int FindInterval(int position)
        {
            if (_currentIntervalIndex != -1)
            {
                var interval = _phylopIntervals[_currentIntervalIndex];
                if (interval.ContainsPosition(position))
                {
                    return(_currentIntervalIndex);
                }
            }

            var pointInterval = new PhylopInterval(position, 1, 1);// overload construtor for points

            var containingIntervalIndex = _phylopIntervals.BinarySearch(pointInterval);

            // The zero-based index of item in the sorted List<T>, if item is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of Count.
            if (containingIntervalIndex == -1)
            {
                // smaller than the first position of the first interval
                return(-1);// empty string represe
            }

            if (containingIntervalIndex < 0)
            {
                containingIntervalIndex = ~containingIntervalIndex - 1;
            }

            return(containingIntervalIndex);
        }
Example #2
0
        private void StartNewInterval(string line)
        {
            var words     = line.Split();
            var chromName = words[1].Split('=')[1];

            // checking if the writer needs to be initiated/re-initiated
            if (_writer == null)
            {
                OpenWriter(chromName);
            }

            if (chromName != _refSeqName)
            {
                CloseWriter();
                OpenWriter(chromName);
            }

            // dumping existing interval
            if (_currentInterval != null)
            {
                WriteInterval(_currentInterval, _writer);
            }

            var start = Convert.ToInt32(words[2].Split('=')[1]);
            var step  = Convert.ToInt16(words[3].Split('=')[1]);

            _currentInterval = new PhylopInterval(start, 0, step);
        }
Example #3
0
        public int ReadIntervalScores(PhylopInterval interval)
        {
            if (interval == null)
            {
                return(0);
            }

            var filePosition = interval.FilePosition;

            //going to the file location that contains this interval.
            if (filePosition != -1)
            {
                _reader.BaseStream.Position = filePosition;
            }
            else
            {
                return(0); //the interval does not contain any file location for the scores
            }
            var length           = _reader.ReadInt32();
            var compressedScores = _reader.ReadBytes(length);

            int requiredBufferSize = _qlz.GetDecompressedLength(compressedScores, length);

            if (requiredBufferSize > _scoreBytes.Length)
            {
                _scoreBytes = new byte[requiredBufferSize];
            }

            var uncompressedLength = _qlz.Decompress(compressedScores, length, _scoreBytes, _scoreBytes.Length);

            BytesToScores(uncompressedLength, _scoreBytes, _scores);

            _scoreCount = uncompressedLength / 2;
            return(_scoreCount);
        }
        public void WriteIntervalTest()
        {
            var randShorts = GetRandShorts();
            var randomPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            var writer     = new ExtendedBinaryWriter(FileUtilities.GetCreateStream(randomPath));

            var testInterval = new PhylopInterval(100, 0, 1);
            var version      = new DataSourceVersion("phylop", "0", DateTime.Now.Ticks, "unit test");

            using (var phylopWriter = new PhylopWriter("chr1", version, GenomeAssembly.Unknown, randShorts, writer))
            {
                phylopWriter.WriteInterval(testInterval, writer);
            }

            using (var phylopReader = new PhylopReader(FileUtilities.GetReadStream(randomPath)))
            {
                var scoreCount = phylopReader.ReadIntervalScores(testInterval);
                Assert.Equal(randShorts.Length, scoreCount);
                var observedScores = phylopReader.GetAllScores();
                for (var i = 0; i < randShorts.Length; i++)
                {
                    Assert.Equal(randShorts[i], observedScores[i]);
                }
            }

            File.Delete(randomPath);
        }
Example #5
0
        // create a phylopWriter with a certain interval length and empty score buffer
        internal PhylopWriter(string refSeqName, DataSourceVersion version, GenomeAssembly genomeAssembly,
                              int intervalLength, ExtendedBinaryWriter writer) : this(refSeqName, version, genomeAssembly, intervalLength)
        {
            _scoreCount = 0; // tbe score buffer is empty
            _writer     = writer;

            WriteHeader();
            _currentInterval = new PhylopInterval(100, 0, 1);
        }
Example #6
0
        private void Clear()
        {
            _scoreCount           = 0;
            _currentInterval      = null;
            _intervalListOffset   = -1;
            _intervalListPosition = -1;

            ChromosomeIntervals.Clear();
        }
Example #7
0
        internal PhylopWriter(string refSeqName, DataSourceVersion version, GenomeAssembly genomeAssembly,
                              short[] scores, ExtendedBinaryWriter writer) : this(refSeqName, version, genomeAssembly, scores.Length)
        {
            _scores     = scores;
            _scoreCount = scores.Length;
            _writer     = writer;

            WriteHeader();
            _currentInterval = new PhylopInterval(100, 0, 1);
        }
Example #8
0
        private void LoadChromosomeIntervals()
        {
            var currentPos = _reader.BaseStream.Position;

            _reader.BaseStream.Position = _intervalListPosition;

            CheckGuard();

            var intervalCount = _reader.ReadInt32();

            for (var i = 0; i < intervalCount; i++)
            {
                var chromosomeInterval = new PhylopInterval(_reader);
                _phylopIntervals.Add(chromosomeInterval);
            }

            _reader.BaseStream.Position = currentPos;
        }
Example #9
0
        internal void AddScore(short score)
        {
            UpdateMinMaxScore(score);
            _scores[_scoreCount++] = score;

            if (_scoreCount < _intervalLength)
            {
                return;
            }

            // buffer full, its time to compress and write it out to file
            var latestInterval = _currentInterval;

            WriteInterval(_currentInterval, _writer);

            _currentInterval = new PhylopInterval(latestInterval.Begin + latestInterval.Length, 0, 1);
            _scoreCount      = 0;
        }
Example #10
0
        internal void WriteInterval(PhylopInterval interval, ExtendedBinaryWriter writer)
        {
            if (_scoreCount == 0)
            {
                return;
            }
            ScoresToBytes(_scoreBytes, _scores, _scoreCount);

            var compressedSize = _compressor.Compress(_scoreBytes, _scoreCount * 2, _compressedBuffer, _compressedBuffer.Length);

            // finalizing the file position for this chromosome interval
            interval.FilePosition   = _writer.BaseStream.Position;
            _currentInterval.Length = _scoreCount;
            ChromosomeIntervals.Add(interval);

            _scoreCount = 0;

            WriteScores(writer, _compressedBuffer, compressedSize);
        }