Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to write templates directly to file #60

Closed
msto opened this issue Oct 17, 2023 · 2 comments
Closed

Add function to write templates directly to file #60

msto opened this issue Oct 17, 2023 · 2 comments
Assignees

Comments

@msto
Copy link
Contributor

msto commented Oct 17, 2023

I've written something along the lines of the following for a couple client projects, and think it would be sensible to have this functionality available in fgpyo.

Ideally the syntax would parallel pysam's, e.g. writer.write(template), but I don't think this is possible without monkey patching AlignmentFile. Is there a preference between implementing this as a standalone function, a la below, or as a method on Template?

def write_template(
    template: Template,
    writer: AlignmentFile,
    primary_only: bool = False,
) -> None:
    """Write the records associated with a Template to file.

    Args:
        template: The Template to write.
        writer: An open, writable AlignmentFile.
        primary_only: If True, only write primary alignments.
    """

    if primary_only:
        rec_iter = template.primary_recs()
    else:
        rec_iter = template.all_recs()

    for rec in rec_iter:
        writer.write(rec)
@msto msto self-assigned this Oct 17, 2023
@tfenne
Copy link
Member

tfenne commented Oct 17, 2023

I agree this would be nice. I think I would prefer it as a function on Template, perhaps called def write_to(self, writer: AlignmentFile, ...). I think if it's an independent function it will be harder to find/discover.

@msto
Copy link
Contributor Author

msto commented Oct 17, 2023

Addressed in #62

@msto msto closed this as completed Oct 17, 2023
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

No branches or pull requests

2 participants