Beispiel #1
0
        /// <summary>
        /// Parses a list of biological sequence alignment texts from a reader.
        /// </summary>
        /// <param name="stream">A stream for a biological sequence alignment text, it will be closed at the end.</param>
        /// <returns>The list of parsed ISequenceAlignment objects.</returns>
        public IEnumerable<ISequenceAlignment> Parse(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            // No empty files allowed
            if (!stream.CanRead || stream.Length == 0)
            {
                throw new InvalidDataException(Properties.Resource.IONoTextToParse);
            }

            using (var reader = stream.OpenRead())
            {
                while (!reader.EndOfStream)
                {
                    if (string.IsNullOrWhiteSpace(line))
                    {
                        ReadNextLine(reader);
                        continue;
                    }

                    yield return ParseOne(reader);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Returns an IEnumerable of sequences in the stream being parsed.
        /// </summary>
        /// <param name="stream">Stream to parse.</param>
        /// <returns>Returns ISequence arrays.</returns>
        public IEnumerable<ISequence> Parse(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            byte[] buffer = new byte[PlatformManager.Services.DefaultBufferSize];
            using (var reader = stream.OpenRead())
            {
                while (!reader.EndOfStream)
                {
                    var seq = this.ParseOne(reader, buffer);
                    if (seq != null)
                        yield return seq;
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Parses a list of biological sequence alignment texts from a reader.
        /// </summary>
        /// <param name="stream">A stream for a biological sequence alignment text.</param>
        /// <returns>The list of parsed ISequenceAlignment objects.</returns>
        public IEnumerable<ISequenceAlignment> Parse(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            using (var reader = stream.OpenRead())
            {
                // no empty files allowed
                if (reader.EndOfStream)
                {
                    throw new InvalidDataException(Properties.Resource.IONoTextToParse);
                }

                // Parse Header, Loop through the blocks and parse
                while (line != null)
                {
                    yield return ParseOne(reader);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Parses a single biological sequence alignment text from a reader.
        /// </summary>
        /// <param name="stream">A stream for a biological sequence alignment text.</param>
        /// <returns>The parsed ISequenceAlignment object.</returns>
        public ISequenceAlignment ParseOne(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            using (var reader = stream.OpenRead())
            {
                // no empty files allowed
                if (reader.EndOfStream)
                    throw new InvalidDataException(Properties.Resource.IONoTextToParse);

                return ParseOne(reader);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Parses a Phylogenetic tree text from a reader into a PhylogeneticTree.
        /// </summary>
        /// <param name="stream">A stream for a Phylogenetic tree text.</param>
        /// <returns>A new PhylogeneticTree instance containing parsed data.</returns>
        public Tree Parse(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            using (var reader = stream.OpenRead())
            {
                return InternalParse(reader);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Read XML BLAST data from the reader, and build one or more
        /// BlastRecordGroup objects (each containing one or more
        /// BlastSearchRecord results).
        /// </summary>
        /// <param name="stream">The stream to pull the data from</param>
        /// <returns>A list of BLAST iteration objects</returns>
        public IEnumerable<BlastResult> Parse(Stream stream)
        {
            var records = new List<BlastResult>();
            var sb = new StringBuilder();
            long lineNumber = 1;

            using (var reader = stream.OpenRead())
            {
                string line = ReadNextLine(reader, false);

                while (!string.IsNullOrEmpty(line))
                {
                    if (line.StartsWith("RPS-BLAST", StringComparison.OrdinalIgnoreCase))
                    {
                        line = ReadNextLine(reader, false);
                        lineNumber++;
                        continue;
                    }
                    if (line.StartsWith("<?xml version", StringComparison.OrdinalIgnoreCase) &&
                        lineNumber > 1)
                    {
                        records.Add(ParseXML(sb));
                        sb = new StringBuilder();
                    }
                    sb.AppendLine(line);
                    line = ReadNextLine(reader, false);
                    lineNumber++;
                }

                if (sb.Length > 0)
                    records.Add(ParseXML(sb));

                if (records.Count == 0)
                    throw new FormatException(Properties.Resources.BlastNoRecords);
            }
            return records;
        }
Beispiel #7
0
        /// <summary>
        /// Parses a single GenBank text from a reader into a sequence.
        /// </summary>
        /// <returns>A new Sequence instance containing parsed data.</returns>
        public IEnumerable<ISequence> Parse(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            using (StreamReader reader = stream.OpenRead())
            {
                string line = null;
                int noOfSequence = 0;
                do
                {
                    IAlphabet alphabet = this.Alphabet ?? Alphabets.DNA;
                    Sequence sequence = new Sequence(alphabet, string.Empty);
                    sequence.Metadata[Helper.GenBankMetadataKey] = new GenBankMetadata();

                    // parse the file
                    line = ParseHeaders(ref sequence, noOfSequence, line, reader);
                    line = ParseFeatures(line, ref sequence, reader);
                    ParseSequence(ref line, ref sequence, reader);
                    ISequence finalSequence = CopyMetadata(sequence);
                    noOfSequence++;

                    yield return finalSequence;
                }
                while (line != null);
            }
        }
Beispiel #8
0
        /// <summary>
        /// Parse a single sequence alignment from the stream.
        /// </summary>
        /// <param name="stream">Stream</param>
        /// <returns>Sequence alignment</returns>
        public ISequenceAlignment ParseOne(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            using (var reader = stream.OpenRead())
            {
                return ParseOne(reader);
            }
        }
Beispiel #9
0
        /// <summary>
        ///     Parses a list of GFF sequences.
        /// </summary>
        /// <returns>The list of parsed ISequence objects.</returns>
        public IEnumerable<ISequence> Parse(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            this.sequences = new List<Tuple<ISequence, List<byte>>>();
            this.sequencesInHeader = new List<Tuple<ISequence, List<byte>>>();

            IAlphabet alphabet = this.Alphabet ?? Alphabets.DNA;
            this.commonSeq = new Sequence(alphabet, String.Empty);

            using (var reader = stream.OpenRead())
            {
                // The GFF spec says that all headers need to be at the top of the file.
                string line = this.ParseHeaders(reader);

                // A feature file with no features? May it never be.
                if (reader.EndOfStream)
                {
                    throw new InvalidOperationException(Resource.GFFNoFeatures);
                }

                while (line != null)
                {
                    line = this.ParseFeatures(reader, line);
                }

                this.CopyMetadata();

                return
                    this.sequences.Select(seq => new Sequence(seq.Item1.Alphabet, seq.Item2.ToArray()) {
                            ID = seq.Item1.ID,
                            Metadata = seq.Item1.Metadata
                        })
                        .Cast<ISequence>()
                        .ToList();
            }
        }
Beispiel #10
0
        /// <summary>
        /// Returns an IEnumerable of sequences in the stream being parsed.
        /// </summary>
        /// <param name="stream">Stream to parse.</param>
        /// <returns>Returns a Sequence.</returns>
        public ISequence ParseOne(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            byte[] buffer = new byte[PlatformManager.Services.DefaultBufferSize];
            using (var reader = stream.OpenRead())
            {
                return this.ParseOne(reader, buffer);
            }
        }
Beispiel #11
0
        /// <summary>
        ///     Parse a set of ISequenceRange objects from a stream.
        /// </summary>
        /// <param name="stream">The stream from which the sequence range is to be parsed.</param>
        /// <returns>The list of sequence ranges.</returns>
        public IList<ISequenceRange> ParseRange(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            var result = new List<ISequenceRange>();
            char[] splitters = { '\t' };

            using (StreamReader reader = stream.OpenRead())
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    // TODO: Handle Track definitions
                    if (line.StartsWith("track", StringComparison.Ordinal))
                    {
                        continue;
                    }

                    string[] split = line.Split(splitters, StringSplitOptions.RemoveEmptyEntries);
                    if (split.Length < 3)
                    {
                        continue;
                    }

                    var range = new SequenceRange
                                {
                                    ID = split[0],
                                    Start = long.Parse(split[1], CultureInfo.InvariantCulture),
                                    End = long.Parse(split[2], CultureInfo.InvariantCulture)
                                };

                    // Optional parameters
                    // TODO: When implementing track definitions update this section for 'use{feature}' declarations
                    if (split.Length >= 4)
                    {
                        range.Metadata["Name"] = split[3];
                    }
                    if (split.Length >= 5)
                    {
                        range.Metadata["Score"] = int.Parse(split[4], CultureInfo.InvariantCulture);
                    }
                    if (split.Length >= 6)
                    {
                        range.Metadata["Strand"] = split[5][0];
                    }
                    if (split.Length >= 7)
                    {
                        range.Metadata["ThickStart"] = int.Parse(split[6], CultureInfo.InvariantCulture);
                    }
                    if (split.Length >= 8)
                    {
                        range.Metadata["ThickEnd"] = int.Parse(split[7], CultureInfo.InvariantCulture);
                    }
                    if (split.Length >= 9)
                    {
                        range.Metadata["ItemRGB"] = split[8];
                    }
                    if (split.Length >= 10)
                    {
                        range.Metadata["BlockCount"] = int.Parse(split[9], CultureInfo.InvariantCulture);
                    }
                    if (split.Length >= 11)
                    {
                        range.Metadata["BlockSizes"] = split[10];
                    }
                    if (split.Length >= 12)
                    {
                        range.Metadata["BlockStarts"] = split[11];
                    }

                    result.Add(range);
                }
            }

            return result;
        }
Beispiel #12
0
        /// <summary>
        /// Creates a text reader from the file name and calls Parse(TextReader reader).
        /// </summary>
        /// Flag to indicate whether the resulting sequences should be in readonly mode or not.
        /// If this flag is set to true then the resulting sequences's isReadOnly property 
        /// will be set to true, otherwise it will be set to false.
        /// <returns>A list of sparse sequences that were present in the file.</returns>
        public IEnumerable<ISequence> Parse(Stream stream)
        {
            // Check input arguments
            if (stream == null)
                throw new ArgumentNullException("stream");

            using (var reader = stream.OpenRead())
            {
                return this.Parse(reader);
            }
        }
Beispiel #13
0
        /// <summary>
        ///     Returns an IEnumerable of sequences in the stream being parsed.
        /// </summary>
        /// <param name="stream">Stream to parse.</param>
        /// <returns>Returns ISequence arrays.</returns>
        public IEnumerable<ISequence> Parse(Stream stream)
        {
            using (var sr = stream.OpenRead())
            {
                string fileLine = sr.ReadLine();

                if (this.ContainsHeader)
                {
                    fileLine = sr.ReadLine();
                }

                while (!string.IsNullOrEmpty(fileLine))
                {
                    yield return this.ParseLine(fileLine);
                    fileLine = sr.ReadLine();
                }
            }
        }