예제 #1
0
        public void Dispose()
        {
            _sequences.ForEach(seq => ((IDisposable)seq).Dispose());

            if (MmFile != null)
            {
                MmFile.Dispose();
                MmFile = null;
            }
        }
예제 #2
0
        /// <summary>
        /// This loads the sequences from the file
        /// </summary>
        /// <param name="bioValidator"></param>
        /// <returns></returns>
        private int LoadSequences(IBioValidator bioValidator)
        {
            var    headerBuffer = new byte[512];
            string currHeader = string.Empty;
            long   startPos = -1;
            int    totalSequenceCount = 0, startingNonGap = -1;
            bool   loadIntoMemory = _loadAllIntoMemory || (new FileInfo(Filename).Length < SmallFileSize);

            MmFile = MemoryMappedFile.CreateFromFile(Filename, FileMode.Open);
            using (var fs = MmFile.CreateViewStream(0, 0, MemoryMappedFileAccess.Read))
            {
                for (; fs != null;)
                {
                    int db = fs.ReadByte();
                    if (db <= 0)
                    {
                        break;
                    }

                    if ((char)db == '>') // Header for species?
                    {
                        // Just finished a sequence?
                        if (currHeader != string.Empty)
                        {
                            if (MaxSequenceLength < totalSequenceCount)
                            {
                                MaxSequenceLength = totalSequenceCount;
                            }
                            if (startingNonGap == -1)
                            {
                                startingNonGap = totalSequenceCount;
                            }
                            _sequences.Add(
                                new FastASequence(this, loadIntoMemory, currHeader, startPos,
                                                  (int)(fs.Position - startPos), startingNonGap, totalSequenceCount, bioValidator));
                        }

                        // Start new sequence
                        currHeader         = string.Empty;
                        totalSequenceCount = 0;
                        startingNonGap     = -1;

                        for (int i = 0; currHeader == string.Empty;)
                        {
                            db = fs.ReadByte();
                            if (db == -1 || db == 0x0a || db == 0x0d)
                            {
                                currHeader = Encoding.ASCII.GetString(headerBuffer, 0, i);
                            }
                            else
                            {
                                headerBuffer[i++] = (byte)db;
                            }
                        }

                        // This is the start of the nucleotide chain
                        startPos = fs.Position;
                    }
                    else if (db != 0x0a && db != 0x0d)
                    {
                        if (startingNonGap == -1 && bioValidator.IsValid((char)db))
                        {
                            startingNonGap = totalSequenceCount;
                        }
                        totalSequenceCount++;
                    }
                }

                // Handle the final sequence in the file.
                Debug.Assert(currHeader != string.Empty);
                Debug.Assert(totalSequenceCount > 0);
                Debug.Assert(startPos > 0);

                if (MaxSequenceLength < totalSequenceCount)
                {
                    MaxSequenceLength = totalSequenceCount;
                }
                if (startingNonGap == -1)
                {
                    startingNonGap = totalSequenceCount;
                }

                _sequences.Add(
                    new FastASequence(this, loadIntoMemory, currHeader, startPos,
                                      (int)(fs.Position - startPos) - 1, startingNonGap, totalSequenceCount, bioValidator));
            }

            // Force all sequences to have the same "virtual" length
            _sequences.ForEach(ns => ((FastASequence)ns).ForceLength(MaxSequenceLength));

            if (loadIntoMemory)
            {
                MmFile.Dispose();
                MmFile = null;
            }

            return(_sequences.Count);
        }