Skip to content

Allow easier access to fasta line wrapping  #666

Closed

Description

@ColinAnthony

For many applications, line-wrapped fasta files pose some complications.
It would be great to have the Bio.SeqIO.FastaIO.FastaWriter: "wrap=None" option exposed to SeqIO, to allow SeqIO.write(data, handle, wrap=None, format)
or
set "wrap=none" as the default.

Activity

  1. peterjc commented on Nov 9, 2015

    @peterjc
    Member

    Hi Colin. SeqIO is deliberately simple and avoids this. What would you expect the wrap argument to do with FASTQ (line wrapping is possible but strongly discouraged), GenBank (can sometimes be useful to disable line wrapping in breach of the spec), etc? I fear this is the thin end of the wedge...

    [Out of interest, why do you need FASTA without line wrapping anyway? The default has rarely if ever caused me problems.]

  2. digfish commented on Nov 17, 2015

    @digfish

    The problem is, it's impossible to use a FASTA file with fixed wrapping like that, because the function parse() in the same module Bio.SeqIO is not even able to read that file !
    Try it for yourself !
    If I want wrapping I just turn on the wrapping on the text editor !
    Let's not complicate matters: a FASTA file have two lines for every sequence: the first one with the identifiers and the second one with the proper sequence !

  3. ColinAnthony commented on Nov 18, 2015

    @ColinAnthony
    Author

    The problem I was having was that I was getting line wrapping as a default
    with SeqIO.write . I don't want line wrapping.
    I want a two line per record fasta file.
    From your reply, it seems that this shouldn't be the case, so perhaps I am
    missing something.
    I recall opening files in gedit and seeing the lines were wrapped (also if
    I 'head' the file in bash , the lines are wrapped).
    I will double check my code and the output. Perhaps something else is going
    on here?
    On 18 Nov 2015 01:14, "digfish" [email protected] wrote:

    The problem is, it's impossible to use a FASTA file with fixed wrapping
    like that, because the function parse() in the same module Bio.SeqIO is not
    even able to read that file !
    Try it for yourself !
    If I want wrapping I just turn on the wrapping on the text editor !
    Let's not complicate matters: a FASTA file have two lines for every
    sequence: the first one with the identifiers and the second one with the
    proper sequence !

    —
    Reply to this email directly or view it on GitHub
    #666 (comment)
    .

  4. peterjc commented on Nov 18, 2015

    @peterjc
    Member

    It seems @digfish has found a separate problem with the FASTA parser - it can usually read the FASTA files written by Biopython: Please file a separate bug for that.

    In any case, the convention is very clear: FASTA sequence lines can be wrapped, and in most cases are wrapped. Remember the sequence can be millions (even billions) or letters long in the case of chromosomes - and singles lines of that length do not work well at the command line terminal nor in most editors.

    However, as @ColinAnthony notes, there are times (when dealing with shorter sequences) that no-line-wrapping FASTA files with exactly two lines per record have advantages.

  5. digfish commented on Nov 18, 2015

    @digfish

    I'm working with reads with approximately 255 bp. I understand the motivation for having wrapping for reads with more than > 1000 bp.
    I'm new in the Bioinformatics world, and I was thinking that the sequences (I'm talking on the specific case of FASTA) have the sequence length according to each model of sequencer. Yes, I'm doing downstream analysis, I have the need to select to filter out sequences and each time create a new FASTA file with only some sequences that I need and after I discovered this FASTA file formatting issued I started to avoid using the function SeqIO.write() for that and relying on a function made by me for writing the FASTA files.
    I'm not against the wrapping: I just think that it does not have to be the default behaviour.

  6. peterjc commented on Nov 18, 2015

    @peterjc
    Member

    @digfish - I'd like to know more about this FASTA output problem you've had with Bio.SeqIO.write(), could you file a separate issue for that please.

  7. Zsailer commented on Nov 8, 2017

    @Zsailer
    Contributor

    I'd like to avoid wrapping fasta output using the SeqIO.write method as well. Why not float a **kwargs in the SeqIO.write method that is then passed to the writer classes as specific options for that format?

  8. peterjc commented on Nov 8, 2017

    @peterjc
    Member

    @Zsailer this was a deliberate design choice to keep the main SeqIO API as simple and usable as possible. Would an output variant name of fasta-nowrap or similar be acceptable?

  9. Zsailer commented on Nov 8, 2017

    @Zsailer
    Contributor

    @peterjc I completely agree with the design decision to make the API simple and usable. And don't get me wrong, I think this API great!

    I guess my point was that adding **kwargs to SeqIO.write and passing it to whatever _FormatToWriter the user specifies doesn't really add any extra complexity for the user or the API. Most users ignore it.

    I went ahead and created a branch to show what I mean. You can see it here.

    You can see I just added **kwargs to lines 449 and 492 in Bio.SeqIO.__init__.

    449 def write(sequences, handle, format, **kwargs):
        .
        .
        .
    488    with as_handle(handle, mode) as fp:
    489        # Map the file format to a writer class
    490        if format in _FormatToWriter:
    491            writer_class = _FormatToWriter[format]
    492            count = writer_class(fp, **kwargs).write_file(sequences)

    With this API, the user can add options to the each format.

    >>> SeqIO.write(seq_records, file_handle, 'fasta', wrap=0)

    I won't be offended if this doesn't go in 😉 . I think fasta-nowrap is a fine solution as well.

  10. peterjc commented on Nov 9, 2017

    @peterjc
    Member

    I agree the code change to enable **kwargs is trivial, it is the implication of doing this which worries me. It suddenly massively expands the public API - documentation alone would be a mass of work. So I'm again going to be stubborn on this.

    Adding fasta-nowrap (or similar) as an output variant format would solve what seems to be a relatively common use case, within the current API and naming approach. Do you fancy working on that instead? Again, the code changes should be quite small.

    Later we could also add this format-variant as an input format too (it would raise an exception if asked to parse multi-line FASTA files).

  11. Zsailer commented on Nov 9, 2017

    @Zsailer
    Contributor

    Fair enough 👍

    I've submitted PR #1450 adding this variant. Let me know what you think.

    Thanks for your thoughts on this, @peterjc!

  12. peterjc commented on Dec 11, 2017

    @peterjc
    Member

    I started thinking about merging #1450 from @Zsailer and then how to implement the matching (strict) no-wrapping parser, and realised there is a choice here - do we really want a strict two-line-per-record FASTA variant here?

    The distinction is about the corner case of zero-length records, which could be one line only (the > line) or two lines (the > line and a blank sequence line). As written, the code would produce the two-line form on output.

    I think a strict two-line-per-record interpretation is much more useful as it allows more assumptions for high speed parsing and indexing.

    If you agree, as part of adding a strict two-line-per-record FASTA parser, should I rename the proposed fasta-nowrap format variant to fasta-2line?

    (Something to keep in mind: Separately we might introduce fastq-4line or fastq-nowrap as an optimised version of the FASTQ parser)

  13. Zsailer commented on Dec 11, 2017

    @Zsailer
    Contributor

    @peterjc, renaming fasta-nowrap to fasta-2line makes sense to me.

    I agree, a strict interpretation is better. As a user, I'd expect fasta-2line to produce two-line output even in a zero-record scenario. The output from a fasta-nowrap syntax in that scenario feels ambiguous.

  14. peterjc commented on Dec 11, 2017

    @peterjc
    Member

    @Zsailer Good, we are in agreement. I added a few commits along these lines to #1450.

  15. jvkersch commented on Oct 4, 2018

    @jvkersch

    I benefitted a lot from @Zsailer's patch to expose fasta-2line but now I have a similar question: would it make sense to expose another option qual-2line to output qual files in strict 2-line format? I have a pipeline with tools, some of which expect 2-line qual files. I monkey-patched my BioPython to add a QualPhredTwoLineWriter along the lines of what was done in #1450, so for my use case I can get by, but if there's independent interest I (or someone else) can contribute this back.

  16. peterjc commented on Oct 4, 2018

    @peterjc
    Member

    Hi Joris - I'm surprised to here anyone is still using qual files - I used to with Roche 454 data but that was some years back - I'd be curious to here more about the use case, and if it justifies adding this extra code. Would you mind opening an new issue about this proposal? Thanks!

    Also I am belatedly closing this issue, the fasta-2line support added in Biopython 1.71 addressed the original goal from Colin.

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