Skip to content

Show matches vs mismatches in pairwise2 printing - #1352

Merged
peterjc merged 4 commits into
biopython:masterfrom
peterjc:pairwise_print
Aug 4, 2017
Merged

peterjc merged 4 commits into
biopython:masterfrom
peterjc:pairwise_print

Conversation

@peterjc

@peterjc peterjc commented Aug 2, 2017

Copy link
Copy Markdown
Member

This would help with situations like https://www.biostars.org/p/265338/#265624 by making the matches and mismatches of the aligned region easier to see.

Note space is already used to mean an unaligned region, thus I picked : to go with |.

@MarkusPiotrowski and @bow: Could you review this please (eg is there any convention in Bio.SearchIO we could follow here)?

@MarkusPiotrowski

Copy link
Copy Markdown
Contributor

@peterjc I have thought about similar things since I'm working on pairwise2 but I came to the view to let things as they are. While a simple match/mismatch in the match line is fine for DNA sequences, you may want something more elaborate when using protein sequences, e.g. something indicating similarity. And this may depend on the matrix which was used during the alignment. And also for DNA sequences you may want to tag gaps different to mismatches.
So in my view there is little gain in the proposed change but a risk of breaking existing code from the pairwise2 users.

@peterjc

peterjc commented Aug 3, 2017

Copy link
Copy Markdown
Member Author

I agree this could be made much more complicated by considering the scoring, there are conventions in EMBOSS and ClustalW which might make sense to follow here.

It looks like I missed updating the doctests in the pull request; I will leave that extra work for now since you are not keen.

Any thoughts @bow? Should we close this?

@bow

bow commented Aug 3, 2017

Copy link
Copy Markdown
Member

@peterjc, @MarkusPiotrowski

As far as visualizations aid go, I'm almost ok with the changes actually (but I don't feel strongly about accepting/rejecting it either). The only changes I would make is indeed to distinguish between gaps and mismatches. But even then, these are only visualization aids and not meant to be something that end users can work with (e.g. so that they can query the what the second mismatch of the alignment).

If we are making the alignments interpretable, I think a more suitable solution would be having something like CIGAR strings (but different, since CIGARs do not distinguish matches and mismatches).

@peterjc

peterjc commented Aug 3, 2017 •

Copy link
Copy Markdown
Member Author

FYI, CIGAR (in the SAM/BAM format) has M for a match or mismatch but also supports = and X for an explicit match and mismatch. The resulting CIGAR string is of course more verbose and takes more space to store.

@peterjc

peterjc commented Aug 3, 2017

Copy link
Copy Markdown
Member Author

Given the only change is to the "human readable" output, I doubt this would break existing scripts.

Regarding showing gaps more clearly, any suggestion for the symbol to use (dot?), and how to implement that cleanly?

@bow

bow commented Aug 3, 2017

Copy link
Copy Markdown
Member

Ah, indeed there are the X the = cigar strings. Could we not just use - for the gaps as well? I think : is ok for the mismatch symbol.

@peterjc

peterjc commented Aug 3, 2017

Copy link
Copy Markdown
Member Author

Current releases local alignment example from test_pairwise2.py using | for the aligned region:

-AxBx
 ||||
zA-Bz
  Score=1.9

Current suggestion on pull request using | for a match and : for a mismatch or gap:

-AxBx
 |:|:
zA-Bz
  Score=1.9

Bow's suggestion using - for gaps:

-AxBx
 |-|:
zA-Bz
  Score=1.9

That looks better than I expected. Worth trying?

@MarkusPiotrowski

MarkusPiotrowski commented Aug 3, 2017 •

Copy link
Copy Markdown
Contributor

EMBOSS uses a : for similar amino acids and . for mismatches. Gaps are shown as blanks:

EMBOSS_001       251 LFTDWDNNKEAD-AIVSQGGSVIISPLGKILAGPNFESEGLITADLDLGD    299
                     ||:  ||.:||| .||:.|||||||||||||||||||||.:|||||||.|
EMBOSS_001       251 LFS--DNFREADPTIVTAGGSVIISPLGKILAGPNFESETIITADLDLSD    298

- was also my first idea, but I think it looks ugly. How about using just a blank? I know, in local alignments blanks indicate the non-aligned parts, but I think you cannot misinterpret the result.
What about X for mismatch?

LFTDWDNNKEAD-AIV
||X  ||XX||| X||
LFS--DNFREADPTIV

A local alignment would have leading and trailing blanks which should be clear (since a local alignment will always begin with a match/mismatch).

KFTDWDNNKEAD-AIGS
 |X  ||XX||| X|
LFS--DNFREADPTIVL

@peterjc

peterjc commented Aug 3, 2017

Copy link
Copy Markdown
Member Author

You've convinced me : was a bad idea. Using . for mismatches is easy and would match EMBOSS, while X would be unique but clear.

Given this code uses blanks for unaligned (for local alignments), using it for gaps seems a little bit confusing - but again, it would match other tools like EMBOSS.

@peterjc

peterjc commented Aug 3, 2017

Copy link
Copy Markdown
Member Author

As long as you are aware if it was a local or global alignment, then yes, the leading/trailing spaces on the middle line are not ambiguous.

@MarkusPiotrowski

MarkusPiotrowski commented Aug 3, 2017 •

Copy link
Copy Markdown
Contributor

The problem with format_aligment for local alignments is that it shows the whole sequences and not only the aligned part. However, if we only show the aligned part, which I would like to suggest, then we should add the starting position at the beginning:

10 FTDWDNNKEAD-AI
   |X  ||XX||| X|
 5 FS--DNFREADPTI

You can infer from the alignment result if it was global or local from the end and start positions (global alignments alway start at 0 and end at the length of th alignment):

from Bio import pairwise2 as pw

seq1 = 'AAGCTT'
seq2 = 'GCTT'

alignments = pw.align.globalxx(seq1, seq2)
print(alignments)

alignments = pw.align.localxx(seq1, seq2)
print(alignments)
[('AAGCTT', '--GCTT', 4.0, 0, 6)]
[('AAGCTT', '--GCTT', 4.0, 2, 6)]

TODO: Show only aligned region for local alignments?
@codecov

codecov Bot commented Aug 3, 2017

Copy link
Copy Markdown

Codecov Report

Merging #1352 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1352      +/-   ##
==========================================
+ Coverage   84.48%   84.48%   +<.01%     
==========================================
  Files         319      319              
  Lines       48851    48852       +1     
==========================================
+ Hits        41271    41272       +1     
  Misses       7580     7580
Impacted Files Coverage Δ
Bio/pairwise2.py 97.88% <100%> (ø) ⬆️

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 50eec46...23b72cd. Read the comment docs.

@codecov

codecov Bot commented Aug 3, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #1352 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1352      +/-   ##
==========================================
+ Coverage   84.48%   84.48%   +<.01%     
==========================================
  Files         319      319              
  Lines       48851    48858       +7     
==========================================
+ Hits        41271    41280       +9     
+ Misses       7580     7578       -2
Impacted Files Coverage Δ
Bio/pairwise2.py 97.91% <100%> (+0.03%) ⬆️
Bio/NeuralNetwork/Gene/Schema.py 71.9% <0%> (+0.82%) ⬆️

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 50eec46...8126612. Read the comment docs.

@peterjc

peterjc commented Aug 3, 2017

Copy link
Copy Markdown
Member Author

@MarkusPiotrowski I like your idea to solve the space issue by changing the display of local alignments. Would you like to implement that on top of this branch?

@MarkusPiotrowski

Copy link
Copy Markdown
Contributor

Yes, I will try this locally (after this branch has been merged) and then make a separate PR.

@peterjc

peterjc commented Aug 4, 2017

Copy link
Copy Markdown
Member Author

The newly introduced trailing whitespace has its downsides for linting with flake8.

Over to you @MarkusPiotrowski - merge as is and then work on changing how local alignments are shown?

@MarkusPiotrowski

Copy link
Copy Markdown
Contributor

Yes, please.

@peterjc
peterjc merged commit 1fda0f6 into biopython:master Aug 4, 2017
@peterjc
peterjc deleted the pairwise_print branch August 4, 2017 13:14
@peterjc

peterjc commented Aug 4, 2017

Copy link
Copy Markdown
Member Author

Thank you both for looking at this.

peterjc added a commit that referenced this pull request Sep 5, 2017
See pull request #1352 and discussion there-in.
Gasta88 pushed a commit to Gasta88/biopython that referenced this pull request Sep 7, 2017
See pull request biopython#1352 and discussion there-in.
MarkusPiotrowski pushed a commit to MarkusPiotrowski/biopython that referenced this pull request Oct 31, 2017
See pull request biopython#1352 and discussion there-in.
@MarkusPiotrowski

Copy link
Copy Markdown
Contributor

@peterjc
I'm working on implementing the print_format function for local alignments, so that only the aligned part of the sequence is shown, together with its starting position like this:

# Global alignment
--TTTTTGGGAATTCCC-------
  .....||||.|||||       
CCCCCCCGGGATTTCCCGGGGGGG
  Score=-15

# Local alignment
5 GGGAATTCCC
  ||||.|||||
7 GGGATTTCCC
  Score=9

My question is: zero-based or one-based positions? Since this is a pretty print I would prefer the one-based output, which is what EMBOSS' Water is doing:

EMBOSS_001         6 GGGAATTCCC     15
                     ||||.|||||
EMBOSS_001         8 GGGATTTCCC     17

@peterjc

peterjc commented Nov 20, 2017

Copy link
Copy Markdown
Member Author

Ping @bow - how do SearchIO or the BLAST alignment objects present coordinates in their alignment pretty prints?

Otherwise given this is pretty printing, I have no particular stance here. As long as it explicit in the docstring ;)

@bow

bow commented Nov 20, 2017 •

Copy link
Copy Markdown
Member

SearchIO displays a 0-based coordinate. Here's an example parsing one of our test cases:

>>> qress = list(SearchIO.parse("Test/Blast/xml_2226_blastn_001.xml", "blast-xml"))
>>> print(qress[1].hits[0].hsps[0])
      Query: gi|356995852:1-490 Mus musculus POU domain, class 5, transcripti...
        Hit: gi|356995852|ref|NM_013633.3| Mus musculus POU domain, class 5, ...
Query range: [0:490] (1)
  Hit range: [0:490] (1)
Quick stats: evalue 0; bitscore 905.98
  Fragments: 1 (490 columns)
     Query - GAGGTGAAACCGTCCCTAGGTGAGCCGTCTTTCCACCAGGCCCCCGGCTCGGGGTGCCC~~~GAACA
             |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||~~~|||||
       Hit - GAGGTGAAACCGTCCCTAGGTGAGCCGTCTTTCCACCAGGCCCCCGGCTCGGGGTGCCC~~~GAACA
>>> qress[1].hits[0].hsps[0].hit_range
(0, 490)
>>> qress[1].hits[0].hsps[0].hit_start
0
>>> qress[1].hits[0].hsps[0].hit_end
490
>>> 

My reason for doing so was simply because it's part of SearchIO's 'contract' with users that coordinates are all converted to 0-based, half open (per the documentation).

For pretty-printing alignment formats, since there are no specific object model per-se (and different alignment tool may do it differently), I would probably go with 1-based. Regardless of the indexing base, I would personally find it helpful if the output includes a mention of the coordinate base (i.e. just output 0-based coordinate or 1-based coordinate). Not enough tools / formats do this IMO (maybe SearchIO should also do this).

@JoaoRodrigues

Copy link
Copy Markdown
Member

As someone who often writes this kind of little helper function for alignments, I'd go with 1-based as well as it's the natural way of counting amino acids/nucleotides in a sequence.

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.

4 participants