Skip to content

mmCIF issue parsing multine text and line-end comments #1441

Closed

Description

@StefansM

I've just encountered a possible issue using Bio.PDB.MMCIF2Dict to parse a more-complex-than-usual CIF file. This was tested on the most recent development version of biopython.

Multiline text strings don't preserve newlines

The CIF format uses a pair of semicolons in column 1 to delimit multiline blocks of text. The current parser merges each line in the block into one:

from Bio.PDB.MMCIF2Dict import MMCIF2Dict
>>> MMCIF2Dict(io.StringIO("""\
  2 data_test
  3 _test
  4 ;
  5 This should be
  6     parsed as
  7 multiline text.
  8 ;
  9 """))["_test"]
'This should beparsed asmultiline text.'

According to the CIF spec (section 17 and 20), this should be parsed as

\n
This should be\n
    parsed as\n
multiline text

with no trailing \n.

I think the cause of this issue is this block of code:

                for line in handle:
                    line = line.strip()
                    if line == ';':
                        break
                    token += line

I would change that to something like this:

quote_buffer = []
for line in handle:
    line = line.rstrip()
    if line == ';':
        token = "\n".join(quote_buffer)
        break
    quote_buffer.append(line)

This would also fix a similar problem encountered when a semicolon appears on a line of its own with an indent. That is,

>>> MMCIF2Dict(io.StringIO("""\
  2 data_
  3 loop_
  4 _test
  5 ;
  6     ;
  7     Foo
  8 ;
  9 """))["_test"]
['', 'Foo', '']

should give the result ["\n ;\n Foo"].

Parsing of line-end comments

The current mmCIF parser only understands comments at the beginning of lines:

>>> MMCIF2Dict(io.StringIO("""\
  2 data_
  3 loop_
  4 _test
  5 foo # Ignore this
  6 bar
  7 baz
  8 """))["_test"]
['foo', '#', 'Ignore', 'this', 'bar', 'baz']

rather than the expected ['foo', 'bar', 'baz']. The CIF spec (21) is fairly clear here, but the mmCIF description only mentions full-line comments.

Can someone confirm my reading of the spec? If it looks good, I'll make a pull request fixing both issues and adding some tests for these cases.

Activity

  1. peterjc commented on Nov 6, 2017

    @peterjc
    Member

    Probably @jgreener64, @JoaoRodrigues or @lennax is best placed to comment on this...

  2. jgreener64 commented on Nov 6, 2017

    @jgreener64
    Contributor

    Hey @StefansM, nice one for finding this stuff.

    I agree with the first point and the fix you propose.

    Re comments, the CIF spec has a few things to say:

    A comment in a CIF begins with an unquoted character "#" and extends to the end of the current line

    at number 21 as you point out. But also, in the formal grammar section (number 45):

    A "comment" is defined to be initiated with the character # followed by any sequence of characters (which include <SP> or <HT>). The only characters not allowed are those in the production <eol>, which <eol> terminates a comment. A comment is recognized only at the beginning of a line or after blanks, i.e. only after space, tab or <eol> [...]

    This sounds more thorough and says in-line comments are not allowed. However, it would require a change on line 97 of the MMCIF2Dict.py file from

    if line.startswith("#"):

    to

    if line.lstrip().startswith("#"):

    to fit the spec. Let me know what you think.

  3. StefansM commented on Nov 7, 2017

    @StefansM
    ContributorAuthor

    Hi @jgreener64, thanks for the comment. I read "... after blanks, i.e. only after space, tab or <eol>..." as allowing comments after other tokens as long as they are separated by whitespace. That is,

    foo # ignore this
       ↑ comment after whitespace
    

    is legal. The mmCIF dictionary (5.2MiB text file) seems to use line-end comments like #V5 to indicate the version of the spec in which fields were introduced. The current parser fails when parsing that dictionary.

    Either way, it looks like a fairly easy change. If we want to support comments at the end of lines, I think it would just be a case of adding

    elif c == "#" and not quote_open_char:
        return

    to line 86 of MMCIF2Dict.

  4. jgreener64 commented on Nov 7, 2017

    @jgreener64
    Contributor

    Okay I hadn't spotted that ambiguity but I think your reading is correct. We should certainly be able to parse the mmCIF dictionary. Your suggestion looks good.

  5. peterjc commented on Nov 28, 2017

    @peterjc
    Member

    Oh yeah, thanks for closing this issue now that #1445 was merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions