Skip to content

Use XML parsing for MAST output files - #2082

Merged
peterjc merged 10 commits into
biopython:masterfrom
victorlin:xml-parser-mast
Jun 3, 2019
Merged

peterjc merged 10 commits into
biopython:masterfrom
victorlin:xml-parser-mast

Conversation

@victorlin

Copy link
Copy Markdown
Contributor

This pull request re-implements the current functionality of Bio/motifs/mast.py using the xml module to parse the XML output instead of the plain-text output from MAST. It also includes updated test files from MEME source.

Notable differences:

  • Input to read() should be a valid XML stream instead of formatted text stream.
  • New alphabet_name_map global variable: used for mapping alphabet names to respective Bio.Alphabet class. It seems that Bio.Alphabet was deprecated for letters in Stop using Alphabet objects in Bio.motifs #2066, but I'm unsure which letters to use in replacement. @mdehoon ?
  • Functionality of __read_section_i() and __read_section_ii() condensed into __read_sequences(): there are no "sections" in the XML file. Also removed __read_section_iii() as it had no extracting functionality.
  • New function __make_diagram() used to generate diagram string found in text files.
  • New attribute Record.strand_handling required for creation of diagram string.

Suggested improvements for the future:

  • Read more metadata (left TODOs where additional information could be extracted).
  • New Sequence class with sequence-specific attributes (e.g. diagram, description, evalue, length, combined_pvalue). This would eliminate the redundancy of Record.sequences and Record.diagrams.

ref #1988, #1980

Related: http://web.mit.edu/meme/current/bin/mast_xml_to_txt

  • I hereby agree to dual licence this and any previous contributions under both
    the Biopython License Agreement AND the BSD 3-Clause License.

  • I have read the CONTRIBUTING.rst file, have run flake8 locally, and
    understand that AppVeyor and TravisCI will be used to confirm the Biopython unit
    tests and style checks pass with these changes.

  • I have added my name to the alphabetical contributors listings in the files
    NEWS.rst and CONTRIB.rst as part of this pull request, am listed
    already, or do not wish to be listed. (This acknowledgement is optional.)

@victorlin
victorlin requested a review from mdehoon as a code owner May 27, 2019 02:35
@mdehoon

mdehoon commented May 28, 2019

Copy link
Copy Markdown
Contributor

@victorlin I would suggest to store alphabet_str as is, so "Protein" or "DNA":

record.alphabet = xml_tree.find('alphabet').get('name')

@peterjc

peterjc commented May 28, 2019

Copy link
Copy Markdown
Member

Regarding the alphabets in motifs, see also the changes @mdehoon made in #2066 - that might help understand what is needed. Are you happy to rebase your branch to take in these changes, or do a merge, or will you need help?

@victorlin

Copy link
Copy Markdown
Contributor Author

@peterjc @mdehoon thank you for the pointers. I should be able to resolve the merge conflicts and update this pull request accordingly.

@mdehoon mdehoon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks OK to me, once the appveyor and travis errors have been fixed.

victorlin added 2 commits May 29, 2019 00:27
previously was functionally sufficient since hit_motif is guaranteed to
have the value of hit_motifs[-1] after the last list comprehension. but
obviously it is bad practice.
The previous try/except on `xml_tree.getroot().get('version')` was
useless since an absence of the attribute yields None rather than
raising an error. A try/except on `xml_tree = ET.parse(handle)` is more
meaningful as we can except a ParseError there.

Combining __read_version() and __read_database_and_motifs() into
__read_metadata(). The names were due to functionality from the
plain-text parser which is now irrelevant.
@codecov

codecov Bot commented May 29, 2019

Copy link
Copy Markdown

Codecov Report

Merging #2082 into master will decrease coverage by 2.57%.
The diff coverage is 93.87%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2082      +/-   ##
==========================================
- Coverage   84.92%   82.34%   -2.58%     
==========================================
  Files         318      317       -1     
  Lines       51050    50953      -97     
==========================================
- Hits        43353    41959    -1394     
- Misses       7697     8994    +1297
Impacted Files Coverage Δ
Bio/motifs/mast.py 95.71% <93.87%> (+4.26%) ⬆️
Bio/Phylo/CDAOIO.py 5.38% <0%> (-80.27%) ⬇️
Bio/Phylo/CDAO.py 42.85% <0%> (-57.15%) ⬇️
Bio/PDB/mmtf/__init__.py 33.33% <0%> (-55.56%) ⬇️
Bio/PopGen/GenePop/EasyController.py 22.68% <0%> (-54.64%) ⬇️
Bio/PopGen/GenePop/Controller.py 10.42% <0%> (-54.25%) ⬇️
Bio/_py3k/__init__.py 38.09% <0%> (-38.1%) ⬇️
Bio/Restriction/Restriction.py 45.99% <0%> (-37.55%) ⬇️
Bio/Wise/__init__.py 50% <0%> (-30.36%) ⬇️
Bio/Phylo/Applications/_Phyml.py 71.42% <0%> (-28.58%) ⬇️
... and 40 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9f2d4d8...4e0bc25. Read the comment docs.

@codecov

codecov Bot commented May 29, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #2082 into master will decrease coverage by <.01%.
The diff coverage is 93.61%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2082      +/-   ##
==========================================
- Coverage   84.92%   84.92%   -0.01%     
==========================================
  Files         318      318              
  Lines       51050    51001      -49     
==========================================
- Hits        43353    43311      -42     
+ Misses       7697     7690       -7
Impacted Files Coverage Δ
Bio/motifs/mast.py 95.58% <93.61%> (+4.13%) ⬆️
Bio/codonalign/__init__.py 73.5% <0%> (ø) ⬆️
Bio/TogoWS/__init__.py 16.55% <0%> (ø) ⬆️
Bio/CAPS/__init__.py 100% <0%> (ø) ⬆️
Bio/SeqIO/QualityIO.py 72.01% <0%> (ø) ⬆️
Bio/Cluster/__init__.py 80.66% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9f2d4d8...0d6a154. Read the comment docs.

@peterjc

peterjc commented May 29, 2019

Copy link
Copy Markdown
Member

If I am reading this correctly, the Bio.motifs parser for "mast" format used to take plain text, and with this change will require XML instead.

Would it be better to keep the plain text parser as "mast", and add the new XML parser as "mast-xml" (or similar) in the top level API? That way it won't break things for people using the older format (e.g. pipelines using older version of MAST, or for comparisons of old vs new MAST behaviour).

@mdehoon

mdehoon commented May 29, 2019

Copy link
Copy Markdown
Contributor

@peterjc Which version of Mast does the plain-text parser support?

@peterjc

peterjc commented May 29, 2019

Copy link
Copy Markdown
Member

According to the test files:

MAST version 3.0 (Release date: 2004/08/18 09:07:01)

@victorlin

Copy link
Copy Markdown
Contributor Author

I think we had this discussion in #1988.

In summary, the MAST program (latest 5.0.5) currently outputs 3 different files: .txt, .xml, and .html.
The XML output was introduced in version 4.4.0 (April 23, 2010).

If we want to retain both parsers, maybe having a parameter would be better:

from Bio.motifs import mast

mast.parse(open('mast.txt'))
mast.parse(open('mast.xml'), format='xml')

but different format names for Bio.motifs.parse/read:

from Bio import motifs

motifs.parse(open('mast.txt'), 'mast')
motifs.parse(open('mast.xml'), 'mast-xml')

motifs.read(open('mast.txt'), 'mast')
motifs.read(open('mast.xml'), 'mast-xml')

@peterjc

peterjc commented May 29, 2019

Copy link
Copy Markdown
Member

Yes, you're right - @mdehoon said he was OK with replacing the text parser with the XML parser, #1988 (comment)

I've hardly used the MAST output, so have no personal experience to guide me here.

@mdehoon

mdehoon commented May 29, 2019

Copy link
Copy Markdown
Contributor

I would keep the XML parser only. Parsing plain-text output is not a good idea to begin with, and we should not encourage it.

@peterjc

peterjc commented May 29, 2019

Copy link
Copy Markdown
Member

OK, but this needs a NEWS.rst entry, and perhaps even something in the DEPRECATED.rst file too?

@victorlin

Copy link
Copy Markdown
Contributor Author

Should this wait until there is an updated XML parser for MEME as well, since it is part of the same package?

@peterjc

peterjc commented May 30, 2019

Copy link
Copy Markdown
Member

I think we could merge XML for MAST only, and then add XML for MEME later.

The longer we leave a pull request, the more likely to get merge conflicts (there is one now already). Are you happy with doing agit rebase origin/master or a merge to resolve that @victorlin, or should we?

@victorlin

Copy link
Copy Markdown
Contributor Author

@peterjc I did a merge in d9b8fc8. Is there somewhere I should look for other merge conflicts?

If you're referencing the master branch on my fork, I attempted some rebase/merges yesterday and messed things up a bit. This xml-parser-mast branch can be auto merged with biopython/master though. I was thinking of re-forking after this pull request.

@peterjc

peterjc commented May 30, 2019

Copy link
Copy Markdown
Member

Github says there is no merge conflict anymore :)

@peterjc

peterjc commented May 30, 2019

Copy link
Copy Markdown
Member

I suggest we do a squash-and-merge (since the rebase is non-trivial), suggested commit comment:

Closes issue #1980

Replaced the old text MAST output parser which was written for:

MAST version 3.0 (Release date: 2004/08/18 09:07:01)

Instead the new parser expects MAST XML output.

Test cases updated using http://meme-suite.org/meme-software/5.0.5/meme-5.0.5.tar.gz

Then AFTER than, @victorlin can try deleting and re-forking his repository as the simplest way to sort out his master branch - although there other ways to achieve that.

And, we can update the NEWS.rst file too.

@victorlin

Copy link
Copy Markdown
Contributor Author

Suggested entry for NEWS.rst:

Bio.motifs.mast has been updated to parse XML output files from MAST over the plain-text output file. The goal of this change is to parse a more structured data source with minimal loss of functionality upon future MAST releases. Class structure remains the same plus an additional attribute Record.strand_handling required for diagram parsing.

Suggested entry for DEPRECATED.rst (under section Bio.Motif ?):

Bio.motifs.mast plain-text parsing deprecated in favor of XML parsing as of release 1.74. Also affects Bio.motifs.read and Bio.motifs.parse for the mast format.

How do these sound?

@peterjc

peterjc commented May 30, 2019

Copy link
Copy Markdown
Member

Those sound good. Do you want to add them to this branch (without changing anything else like trying another rebase)?

@victorlin

Copy link
Copy Markdown
Contributor Author

Done.

@peterjc

peterjc commented May 30, 2019

Copy link
Copy Markdown
Member

OK to squash-and-merge @mdehoon? Suggested commit message: #2082 (comment)

@mdehoon

mdehoon commented Jun 3, 2019

Copy link
Copy Markdown
Contributor

@peterjc OK

@peterjc
peterjc merged commit b920c5c into biopython:master Jun 3, 2019
@peterjc

peterjc commented Jun 3, 2019

Copy link
Copy Markdown
Member

Thank you both 👍

@victorlin
victorlin deleted the xml-parser-mast branch June 3, 2019 12:56
@victorlin victorlin mentioned this pull request Nov 24, 2019
3 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants