Skip to content

Added the Seq.Join() method, issue #1731 - #1746

Closed
kaskales wants to merge 32 commits into
biopython:masterfrom
kaskales:master
Closed

kaskales wants to merge 32 commits into
biopython:masterfrom
kaskales:master

Conversation

@kaskales

@kaskales kaskales commented Aug 1, 2018 •

Copy link
Copy Markdown
Contributor

Added the Seq.Join() method, issue #1731 .

Allows user to concatenate Seq objects with a spacer Seq object.

  • 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 and understand that AppVeyor and
    TravisCI will be used to confirm the Biopython unit tests and flake8 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.)

@kaskales
kaskales requested a review from peterjc as a code owner August 1, 2018 21:32
@codecov

codecov Bot commented Aug 2, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #1746 into master will increase coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1746      +/-   ##
==========================================
+ Coverage   85.02%   85.05%   +0.02%     
==========================================
  Files         310      311       +1     
  Lines       49957    50103     +146     
==========================================
+ Hits        42476    42615     +139     
- Misses       7481     7488       +7
Impacted Files Coverage Δ
Bio/Seq.py 98.2% <100%> (-0.03%) ⬇️
Bio/Statistics/__init__.py 100% <0%> (ø) ⬆️
BioSQL/BioSeqDatabase.py 77.61% <0%> (ø) ⬆️
Bio/motifs/meme.py 80.97% <0%> (ø) ⬆️
Bio/SeqIO/NibIO.py 91.3% <0%> (ø)
Bio/PDB/parse_pdb_header.py 96.27% <0%> (+0.06%) ⬆️
Bio/SeqIO/__init__.py 84.53% <0%> (+0.08%) ⬆️
Bio/GenBank/Scanner.py 82.72% <0%> (+0.23%) ⬆️

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 78a73b9...05e9c3c. Read the comment docs.

Comment thread Bio/Seq.py Resolved
Comment thread Bio/Seq.py Resolved
Comment thread Bio/Seq.py Outdated
from Bio.SeqRecord import SeqRecord # Lazy to avoid circular imports
temp_data = ""
if self._data == "": # if the spacer is empty it will initially default to the type of the first sequence
a = None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What if the arguments are all alphabet-less strings? Add if a is None: a = self.alphabet logic at the end?

Comment thread Doc/examples/tree1.nwk Resolved
Comment thread Tests/test_Seq_objs.py Resolved
@peterjc

peterjc commented Aug 3, 2018

Copy link
Copy Markdown
Member

Do you not want to be in the NEWS and CONTRIB listings? If you do, can you think how to make the pull request template clearer that we'd like you to add yourself?

In general the changes look good - although I've not had a chance to test them yet. I would like to see more tests, including the error conditions.

@kaskales

kaskales commented Aug 4, 2018

Copy link
Copy Markdown
Contributor Author

Thank you for the comments, I made changes based on your suggestions and added a few more test conditions.

At this moment I do not wish to have my name listed, I only checked the box as an acknowledgement that I read the option. Thanks for double checking with me.

@peterjc

peterjc commented Aug 11, 2018

Copy link
Copy Markdown
Member

Would you be happy for add .join for the UnknownSeq? Even if it just calls the Seq method that would be useful.

@kaskales

Copy link
Copy Markdown
Contributor Author

If it calls the Seq .join method, does that mean the UnkownSeq .join should accept both UnkownSeq and Seq as input? Or You should still be only allowed to use join with objects from the same class?

@kaskales

Copy link
Copy Markdown
Contributor Author

I've added join() for MutableSeq objects as well and tested the functionality. Implementation was done based on how the add method functions for Unknown and MutableSeq.

@peterjc

peterjc commented Aug 14, 2018

Copy link
Copy Markdown
Member

Aside from the alphabet special cases with an empty spacer, the behaviour of spacer.join([A, B, C, ..., Z]) should match A + spacer + B + spacer + C + spacer + ... + Z (and it makes sense to check this explicitly in the tests).

Comment thread Bio/Seq.py Resolved
Comment thread Tests/test_Seq_objs.py Resolved
@peterjc

peterjc commented Feb 13, 2019

Copy link
Copy Markdown
Member

@MarkusPiotrowski or @mdehoon would you be able to look at this for a second opinion?

There are some minor changes I might still make like shortening the doctest examples, but I think this would benefit from fresh eyes.

@kaskales

Copy link
Copy Markdown
Contributor Author

In addition to shortening the doctest what other changes did you have in mind?

@peterjc

peterjc commented Feb 20, 2019

Copy link
Copy Markdown
Member

Doctest example,

        >>> spacer = Seq('NNNNN')
        >>> seqlist = [Seq("AAA"), Seq("TTT"), Seq("PPP")]
        >>> concatenated = spacer.join(seqlist)
        >>> concatenated
        Seq('AAANNNNNTTTNNNNNPPP')

Could be:

        >>> Seq('NNNNN').join([Seq("AAA"), Seq("TTT"), Seq("PPP")])
        Seq('AAANNNNNTTTNNNNNPPP')

@peterjc

peterjc commented Feb 20, 2019

Copy link
Copy Markdown
Member

Did we discuss the pros and cons of doing the join internally with string concatenation (as now), versus calling the string join method? I suspect the later will scale better, but have not benchmarked this - and what you have now is a useful first implementation which we can always revise later.

@kaskales

Copy link
Copy Markdown
Contributor Author

I think the discussion of string concatenation vs an internal string join was already spoken about, though I can't find the relevant comments anymore. I did a short test for a join with 300 sequences and I for the moment I see no differences as the most intensive part in both cases seems to be converting everything into a string from a Seq object for the returned Seq() and iterating the list to make sure everything is an allowed object.

@peterjc

peterjc commented Feb 20, 2019

Copy link
Copy Markdown
Member

OK, that makes sense - I thought we'd talked about it too, but also couldn't find the discussion.

Comment thread Bio/Seq.py Outdated
a = self.alphabet
if self._data == "":
return self.__class__(temp_data, a)
return self.__class__(temp_data[len(self._data):], a) # remove the last addition of the spacer

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.

In the comment, you mean "remove the first addition of the spacer"?

Comment thread Bio/Seq.py
if a is None: # if spacer is empty alphabet defaults to type of the first sequence
a = c.alphabet
else:
if not Alphabet._check_type_compatible([a, c.alphabet]):

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.

I found that all these alphabet checks are intrinsicely slow. Also, I would think that often the alphabets are identical. So you can speed this up considerably if you only do the following checks if the alphabets are not identical:

if a != c.alphabet:
    if not Alphabet._ ....
        ...

In a test run with 1000 'AAA' seq objects, this check made the method nearly 9 times faster.

Comment thread Bio/Seq.py Outdated
temp_data += str(self) + str(c)
else:
raise ValueError("Input must be an iterable of Seqs or Strings")

@MarkusPiotrowski MarkusPiotrowski Feb 22, 2019 •

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.

By replacing + string concatenation with join()

(e.g. remove line 1319, replace 1321 with pass and add below 1323:

temp_data = str(self).join([str(z) for z in other])

)
you can gain another 1.6x in speed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good - I did think that would be quicker, but it was masked by the seq/alphabet overhead until your suggestion for the common case of everything having the same alphabet object.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're correct, I previously misunderstood and tested this idea poorly. I've updated it to perform this join.

Comment thread Bio/Seq.py
are not Seq or String objects
"""
seq_joined = self.toseq().join(other) # returns Seq object instead of MutableSeq
return seq_joined

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.

It's not clear to me, why you return a Seq object here, instead of a MutableSeq. Somewhere in the discussion you said that you tried to be consistent with the add method (you mean __add__?), but for MutableSeq __add__ returns a new MutableSeq object.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

An earlier iteration did return a MutableSeq, but required a lot of code duplication - returning a Seq avoided that neatly.

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.

But can't you finally make a new MutableSeq with the joined sequence and alphabet and return this? E.g. using tomutable(). Like in the __add__ method?

On the other hand, I wonder if I would expect the result of a join operation, where the MutableSeq acts "only" as spacer, to be a MutableSeq. So this is probably fine, but should be mentioned in the docstring.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You could argue either way on the the return type, personally I think a Seq is more likely to be useful as when I have needed a MutableSeq it has only been briefly. The doctest does show a Seq, but you are right, saying so in the text of the docstring would be clearer.

@peterjc

peterjc commented Mar 15, 2019

Copy link
Copy Markdown
Member

The merge conflict is due to another test being added for the MutableSeq object.

If you've never dealt with a conflict before, just leave it as is - I can do the merge manually.

peterjc pushed a commit to peterjc/biopython that referenced this pull request Mar 15, 2019
@kaskales

Copy link
Copy Markdown
Contributor Author

I don't think I understand, where is the merge conflict? GitHub is showing me that I've passed all 4 checks and that there are no conflicts with the base branch. Maybe it's better if I leave this up to you.

@peterjc

peterjc commented Mar 15, 2019

Copy link
Copy Markdown
Member

I see "This branch cannot be rebased due to conflicts" and the merge buttons are greyed out. You may not see this message because you won't see the merge buttons either.

I pulled your changes into a temporary branch on my fork, and if that passes TravisCI, will merge it:

peterjc@e856cd3

@peterjc

peterjc commented Mar 15, 2019

Copy link
Copy Markdown
Member

The conflict itself is from fa9a625 - you both added tests to the class at around line 740 - happily very easy to resolve the conflict as we want both that test and your new tests.

@kaskales

Copy link
Copy Markdown
Contributor Author

Ok, I see. Thank you!

@peterjc

peterjc commented Mar 15, 2019

Copy link
Copy Markdown
Member

Do you want to be added to the NEWS.rst or CONTRIB.rst files? Just as "kaskales" is fine if you want to remain pseudonymous.

@peterjc

peterjc commented Mar 15, 2019

Copy link
Copy Markdown
Member

Applied as e856cd3 - thank you!

@kaskales

Copy link
Copy Markdown
Contributor Author

Thank you!

I would like to make more contributions so I'll remember to add my name for the next time to the NEWS.rst file.

peterjc added a commit to peterjc/biopython that referenced this pull request Feb 5, 2024
Original authors David Arenillas and Anthony Mathelier have
agreed - see biopython#2473, as have the other contributors as tracked
by git version control:

 - Aziz Khan (@asntech)
   biopython#3651
 - Carlos Pena (@carlosp420)
   biopython#898 (comment)
 - Catherine Lesuisse (@kaskales)
   biopython#1746
 - Chris Rands (@chris-rands)
   biopython#1301
 - Christian Brueffer (@cbrueffer)
   biopython#898 (comment)
 - Jeremy LaBarge (@biojerm)
   biopython#1847 (comment)
 - Markus Piotrowski (@MarkusPiotrowski)
   biopython#898 (comment)
 - Michiel de Hoon (@mdehoon)
   biopython#898 (comment)
 - @morrme
   biopython#1172 (comment)
 - Peter Cock (@peterjc)
   biopython#898 (comment)
 - Sergio Valqui (@svalqui)
   biopython#1749
 - Sujan Dulal (@Dsujan)
   biopython#2430
 - Suresh Panneerselvam (@Amrithasuresh)
   biopython#1575
 - Travis Wrightsman (@twrightsman)
   biopython#898 (comment)
peterjc added a commit to peterjc/biopython that referenced this pull request Feb 5, 2024
Original authors David Arenillas and Anthony Mathelier have
agreed - see biopython#2473, as have the other contributors as tracked
by git version control:

 - Aziz Khan (@asntech)
   biopython#3651
 - Carlos Pena (@carlosp420)
   biopython#898 (comment)
 - Catherine Lesuisse (@kaskales)
   biopython#1746
 - Chris Rands (@chris-rands)
   biopython#1301
 - Christian Brueffer (@cbrueffer)
   biopython#898 (comment)
 - Jeremy LaBarge (@biojerm)
   biopython#1847 (comment)
 - Markus Piotrowski (@MarkusPiotrowski)
   biopython#898 (comment)
 - Michiel de Hoon (@mdehoon)
   biopython#898 (comment)
 - @morrme
   biopython#1172 (comment)
 - Peter Cock (@peterjc)
   biopython#898 (comment)
 - Sergio Valqui (@svalqui)
   biopython#1749
 - Sujan Dulal (@Dsujan)
   biopython#2430
 - Suresh Panneerselvam (@Amrithasuresh)
   biopython#1575
 - Travis Wrightsman (@twrightsman)
   biopython#898 (comment)
peterjc added a commit that referenced this pull request Feb 5, 2024
Original authors David Arenillas and Anthony Mathelier have
agreed - see #2473, as have the other contributors as tracked
by git version control:

 - Aziz Khan (@asntech)
   #3651
 - Carlos Pena (@carlosp420)
   #898 (comment)
 - Catherine Lesuisse (@kaskales)
   #1746
 - Chris Rands (@chris-rands)
   #1301
 - Christian Brueffer (@cbrueffer)
   #898 (comment)
 - Jeremy LaBarge (@biojerm)
   #1847 (comment)
 - Markus Piotrowski (@MarkusPiotrowski)
   #898 (comment)
 - Michiel de Hoon (@mdehoon)
   #898 (comment)
 - @morrme
   #1172 (comment)
 - Peter Cock (@peterjc)
   #898 (comment)
 - Sergio Valqui (@svalqui)
   #1749
 - Sujan Dulal (@Dsujan)
   #2430
 - Suresh Panneerselvam (@Amrithasuresh)
   #1575
 - Travis Wrightsman (@twrightsman)
   #898 (comment)
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