Exemplo n.º 1
0
        private void WriteFeatures(ISequence sequence, TextWriter txtWriter)
        {
            ILocationBuilder locBuilder = LocationBuilder;

            if (locBuilder == null)
            {
                throw new InvalidOperationException(Properties.Resource.NullLocationBuild);
            }
            GenBankMetadata metadata = (GenBankMetadata)sequence.Metadata[Helper.GenBankMetadataKey];

            if (metadata != null && metadata.Features != null)
            {
                WriteFeatureSection("FEATURES", "Location/Qualifiers", txtWriter);

                // write the features in the order they were put in the list
                foreach (FeatureItem feature in metadata.Features.All)
                {
                    WriteFeatureSection(FeatureHeaderIndentString + feature.Key, locBuilder.GetLocationString(feature.Location), txtWriter);

                    // The sub-items of a feature are referred to as qualifiers.  These do not have
                    // unique keys, so they are stored as lists in the SubItems dictionary.
                    foreach (KeyValuePair <string, List <string> > qualifierList in feature.Qualifiers)
                    {
                        foreach (string qualifierValue in qualifierList.Value)
                        {
                            string data = "/" + qualifierList.Key;
                            if (qualifierValue != null)
                            {
                                if (qualifierValue != string.Empty)
                                {
                                    data += "=";
                                    string s = "" + (char)34;

                                    if (qualifierValue.StartsWith(s) == false)
                                    {
                                        data += s;
                                    }

                                    data += qualifierValue;
                                    if (qualifierValue.EndsWith(s) == false)
                                    {
                                        data += s;
                                    }
                                }
                            }

                            // use a blank header; the qualifier key is part of the data
                            WriteFeatureSection(string.Empty, data, txtWriter);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void ParseFeatures(MBFTextReader mbfReader, ref Sequence sequence)
        {
            ILocationBuilder locBuilder = LocationBuilder;

            if (locBuilder == null)
            {
                throw new InvalidOperationException(Resource.NullLocationBuild);
            }

            // set data indent for features
            mbfReader.DataIndent = _featureDataIndent;

            // The sub-items of a feature are referred to as qualifiers.  These do not have unique
            // keys, so they are stored as lists in the SubItems dictionary.
            SequenceFeatures    features    = new SequenceFeatures();
            IList <FeatureItem> featureList = features.All;

            while (mbfReader.HasLines)
            {
                if (String.IsNullOrEmpty(mbfReader.Line) || mbfReader.LineHeader == "FEATURES")
                {
                    mbfReader.GoToNextLine();
                    continue;
                }

                if (mbfReader.Line[0] != ' ')
                {
                    // start of non-feature text
                    break;
                }

                if (!mbfReader.LineHasHeader)
                {
                    string message = Properties.Resource.GenbankEmptyFeature;
                    Trace.Report(message);
                    throw new InvalidDataException(message);
                }

                // check for multi-line location string
                string featureKey = mbfReader.LineHeader;
                string location   = mbfReader.LineData;
                mbfReader.GoToNextLine();
                while (mbfReader.HasLines && !mbfReader.LineHasHeader &&
                       mbfReader.LineHasData && !mbfReader.LineData.StartsWith("/", StringComparison.Ordinal))
                {
                    location += mbfReader.LineData;
                    mbfReader.GoToNextLine();
                }

                // create features as MetadataListItems
                FeatureItem feature = new FeatureItem(featureKey, locBuilder.GetLocation(location));

                // process the list of qualifiers, which are each in the form of
                // /key="value"
                string qualifierKey   = string.Empty;
                string qualifierValue = string.Empty;
                while (mbfReader.HasLines)
                {
                    if (!mbfReader.LineHasHeader && mbfReader.LineHasData)
                    {
                        // '/' denotes a continuation of the previous line
                        if (mbfReader.LineData.StartsWith("/", StringComparison.Ordinal))
                        {
                            // new qualifier; save previous if this isn't the first
                            if (!String.IsNullOrEmpty(qualifierKey))
                            {
                                AddQualifierToFeature(feature, qualifierKey, qualifierValue);
                            }

                            // set the key and value of this qualifier
                            int equalsIndex = mbfReader.LineData.IndexOf('=');
                            if (equalsIndex < 0)
                            {
                                // no value, just key (this is allowed, see NC_005213.gbk)
                                qualifierKey   = mbfReader.LineData.Substring(1);
                                qualifierValue = string.Empty;
                            }
                            else if (equalsIndex > 0)
                            {
                                qualifierKey   = mbfReader.LineData.Substring(1, equalsIndex - 1);
                                qualifierValue = mbfReader.LineData.Substring(equalsIndex + 1);
                            }
                            else
                            {
                                string message = String.Format(
                                    CultureInfo.CurrentCulture,
                                    Properties.Resource.GenbankInvalidFeature,
                                    mbfReader.Line);
                                Trace.Report(message);
                                throw new InvalidDataException(message);
                            }
                        }
                        else
                        {
                            // Continuation of previous line; "note" gets a line break, and
                            // everything else except "translation" and "transl_except" gets a
                            // space to separate words.
                            if (qualifierKey == "note")
                            {
                                qualifierValue += Environment.NewLine;
                            }
                            else if (qualifierKey != "translation" && qualifierKey != "transl_except")
                            {
                                qualifierValue += " ";
                            }

                            qualifierValue += mbfReader.LineData;
                        }

                        mbfReader.GoToNextLine();
                    }
                    else if (mbfReader.Line.StartsWith("\t", StringComparison.Ordinal))
                    {
                        // this seems to be data corruption; but BioPerl test set includes
                        // (old, 2003) NT_021877.gbk which has this problem, so we
                        // handle it
                        ApplicationLog.WriteLine("WARN: nonstandard line format at line {0}: '{1}'",
                                                 mbfReader.LineNumber, mbfReader.Line);
                        qualifierValue += " " + mbfReader.Line.Trim();
                        mbfReader.GoToNextLine();
                    }
                    else
                    {
                        break;
                    }
                }

                // add last qualifier
                if (!String.IsNullOrEmpty(qualifierKey))
                {
                    AddQualifierToFeature(feature, qualifierKey, qualifierValue);
                }

                // still add feature, even if it has no qualifiers
                featureList.Add(StandardFeatureMap.GetStandardFeatureItem(feature));
            }

            if (featureList.Count > 0)
            {
                ((GenBankMetadata)sequence.Metadata[Helper.GenBankMetadataKey]).Features = features;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Parses the GenBank features from the GenBank file.
        /// </summary>
        /// <param name="line">parse line</param>
        /// <param name="sequence">The sequence.</param>
        /// <param name="stream">The stream reader.</param>
        /// <returns>The parsed line.</returns>
        private string ParseFeatures(string line, ref Sequence sequence, StreamReader stream)
        {
            ILocationBuilder locBuilder = LocationBuilder;

            if (locBuilder == null)
            {
                throw new InvalidOperationException(Properties.Resource.NullLocationBuild);
            }

            // set data indent for features
            string lineData;

            // The sub-items of a feature are referred to as qualifiers.  These do not have unique
            // keys, so they are stored as lists in the SubItems dictionary.
            SequenceFeatures    features    = new SequenceFeatures();
            IList <FeatureItem> featureList = features.All;

            while (line != null)
            {
                string lineHeader = GetLineHeader(line, FeatureDataIndent);
                if (String.IsNullOrEmpty(line) || lineHeader == "FEATURES")
                {
                    line = GoToNextLine(line, stream);
                    continue;
                }

                if (line[0] != ' ')
                {
                    // start of non-feature text
                    break;
                }

                if (lineHeader == null)
                {
                    string message = Properties.Resource.GenbankEmptyFeature;
                    Trace.Report(message);
                    throw new InvalidDataException(message);
                }

                // check for multi-line location string
                lineData = GetLineData(line, FeatureDataIndent);
                string featureKey = lineHeader;
                string location   = lineData;
                line       = GoToNextLine(line, stream);
                lineData   = GetLineData(line, FeatureDataIndent);
                lineHeader = GetLineHeader(line, FeatureDataIndent);
                while ((line != null) && (lineHeader == string.Empty) &&
                       (lineData != string.Empty) && !lineData.StartsWith("/", StringComparison.Ordinal))
                {
                    location += lineData;
                    GetLineData(line, FeatureDataIndent);
                    line       = GoToNextLine(line, stream);
                    lineData   = GetLineData(line, FeatureDataIndent);
                    lineHeader = GetLineHeader(line, FeatureDataIndent);
                }

                // create features as MetadataListItems
                FeatureItem feature = new FeatureItem(featureKey, locBuilder.GetLocation(location));

                // process the list of qualifiers, which are each in the form of
                // /key="value"
                string qualifierKey         = string.Empty;
                string qualifierValue       = string.Empty;
                bool   quotationMarkStarted = false;

                while (line != null)
                {
                    lineData   = GetLineData(line, FeatureDataIndent);
                    lineHeader = GetLineHeader(line, FeatureDataIndent);
                    if ((lineHeader == string.Empty) && (lineData != null))
                    {
                        // '/' denotes a continuation of the previous line
                        // Note that, if there are multiple lines of qualifierValue,
                        // sometimes a line break will happen such that a "/" which is
                        // part of the qualifierValue will start a continuation line.
                        // This is identified by verifying open and closing double quotes.
                        if (lineData.StartsWith("/", StringComparison.Ordinal) && !quotationMarkStarted)
                        {
                            // new qualifier; save previous if this isn't the first
                            if (!String.IsNullOrEmpty(qualifierKey))
                            {
                                AddQualifierToFeature(feature, qualifierKey, qualifierValue);
                            }

                            // set the key and value of this qualifier
                            int equalsIndex = lineData.IndexOf('=');
                            if (equalsIndex < 0)
                            {
                                // no value, just key (this is allowed, see NC_005213.gbk)
                                qualifierKey   = lineData.Substring(1);
                                qualifierValue = string.Empty;
                            }
                            else if (equalsIndex > 0)
                            {
                                qualifierKey         = lineData.Substring(1, equalsIndex - 1);
                                qualifierValue       = lineData.Substring(equalsIndex + 1);
                                quotationMarkStarted = qualifierValue[0] == '"';
                                if (qualifierValue[qualifierValue.Length - 1] == '"')
                                {
                                    quotationMarkStarted = false;
                                }
                            }
                            else
                            {
                                string message = String.Format(
                                    CultureInfo.CurrentCulture,
                                    Properties.Resource.GenbankInvalidFeature,
                                    line);
                                Trace.Report(message);
                                throw new InvalidDataException(message);
                            }
                        }
                        else
                        {
                            // Continuation of previous line; "note" gets a line break, and
                            // everything else except "translation" and "transl_except" gets a
                            // space to separate words.
                            if (qualifierKey == "note")
                            {
                                qualifierValue += Environment.NewLine;
                            }
                            else if (qualifierKey != "translation" && qualifierKey != "transl_except")
                            {
                                qualifierValue += " ";
                            }

                            qualifierValue += lineData;
                            if (qualifierValue[qualifierValue.Length - 1] == '"')
                            {
                                quotationMarkStarted = false;
                            }
                        }

                        line = GoToNextLine(line, stream);
                    }
                    else if (line.StartsWith("\t", StringComparison.Ordinal))
                    {
                        // this seems to be data corruption; but BioPerl test set includes
                        // (old, 2003) NT_021877.gbk which has this problem, so we
                        // handle it
                        ApplicationLog.WriteLine("WARN: nonstandard line format at line {0}: '{1}'", lineNumber, line);
                        qualifierValue += " " + line.Trim();
                        if (qualifierValue[qualifierValue.Length - 1] == '"')
                        {
                            quotationMarkStarted = false;
                        }

                        line = GoToNextLine(line, stream);
                    }
                    else
                    {
                        break;
                    }
                }

                // add last qualifier
                if (!String.IsNullOrEmpty(qualifierKey))
                {
                    AddQualifierToFeature(feature, qualifierKey, qualifierValue);
                }

                // still add feature, even if it has no qualifiers
                featureList.Add(StandardFeatureMap.GetStandardFeatureItem(feature));
            }

            if (featureList.Count > 0)
            {
                ((GenBankMetadata)sequence.Metadata[Helper.GenBankMetadataKey]).Features = features;
            }

            return(line);
        }