Skip to content

Proposal: Use sam_hdr_ prefix for new header API functions - #887

Merged
daviesrob merged 10 commits into
samtools:developfrom
jmarshall:samify-header-api
Jul 5, 2019
Merged

daviesrob merged 10 commits into
samtools:developfrom
jmarshall:samify-header-api

Conversation

@jmarshall

@jmarshall jmarshall commented Jun 26, 2019 •

Copy link
Copy Markdown
Member

With the exception of some early functions, HTSlib's convention for generic SAM/BAM/CRAM functions is a sam_ prefix. (cf e.g. bam_hdr_read() which works for BAM files only vs. sam_hdr_read() which is for any of SAM/BAM/CRAM.)

For your consideration, this PR renames the newly added public header API functions to use a sam_hdr_ prefix rather than the bam_hdr_ prefix of PR #812. If the maintainers' consensus is that they would like to go down this route, I would be happy to add commits to rename the new internal functions similarly and/or to add renaming shims for e.g. sam_hdr_init() and sam_hdr_t, i.e., things that have long had a somewhat unfortunate bam_hdr_ prefix.

I have also tried out the renaming on samtools/samtools#998; that compiles cleanly, and both htslib and samtools test suites pass.

(In #812 (comment) Rob alluded to clashes with pre-existing bits of API. I think that problem must have been mostly limited to an intermediate version of the API. Once cram.h's sam_hdr_length/_str declarations were tidied up, this PR had no such problems.)

@jkbonfield

jkbonfield commented Jun 26, 2019 •

Copy link
Copy Markdown
Contributor

Anything that avoids the nonsense of schizophrenic name changes like this would be appreciated:

bam_hdr_t *h;
...
h = sam_hdr_read(fp);
...
bam_hdr_destroy(h);

For frell's sake - allocate with sam and destroy with bam. Ugh.

I think the problem is the structure is bam_hdr_t which isn't really correct. Our functions should reflect the structure, and the structure is a SAM header (a parsed form of the textual format which is shared between SAM, BAM and CRAM) and not labelling it as a BAM specific structure name.

Unfortunately we also have bam1_t for the bam structure, but that - more or less - actually is BAM format in memory.

The original API (from io_lib and later htslib/cram) had sam prefixes and it's still my preferred naming. Our commit didn't mention what clashes we were attempting to resolve, but it's possible it was linking old samtools vs modern htslib. (We know the new one will link.) Did you test that combination? Maybe Rob can remember the reasoning. I alas cannot.

@jmarshall

Copy link
Copy Markdown
Member Author

it's possible it was linking old samtools vs modern htslib. (We know the new one will link.) Did you test that combination?

Yep, that compiled without warnings and passed its tests. (I half-expected samtools to define its own sam_hdr_foo function that would conflict, but there was no such problem. Also no bcftools problems.)

@daviesrob

Copy link
Copy Markdown
Member

I think I would like to see the structure renamed sam_hdr_t (with typedef for backwards compatibility) and the remaining bam_hdr_ functions get sam_hdr_ equivalents. If we want to go down this route we should try to end up with something fully consistent.

I think the name clashing may well have been resolved when the parsed headers struct got renamed to bam_hrecs_t and lots of internal functions got renamed to start bam_hrecs_ instead of sam_hdr_. A good spot on the struct sam_hdr, though, which is a bit I obviously missed when doing this and definitely wrong.

@jmarshall

Copy link
Copy Markdown
Member Author

Thanks Rob. That sounds like a likely theory about the intermediate name clashing.

Agreed re fully consistent. I'm happy to add to this PR to rename the struct to sam_hdr_t and similarly the remaining bam_hdr_ functions, with compatibility typedefs and static inline wrappers. I'd be able to get that done over the weekend.

@daviesrob

Copy link
Copy Markdown
Member

Yes, that would be fine, thanks.

@daviesrob

Copy link
Copy Markdown
Member

Any progress on this yet?

@jmarshall

Copy link
Copy Markdown
Member Author

Yep, sorry about the delay: day job got busy, and there's a couple of internal functions to disentangle. Should be ready tomorrow.

In the meantime, for mulling on, here are a couple of minor new-API problems I've noticed so far:

  • bam_hdr_length() returns size_t but has a -1 on error return code — should this be ssize_t?

  • bam_hdr_remove_lines() has a void * parameter, but doesn't describe what sort of a khash you're supposed to give it. If anybody's going to be able to use this function, it needs a better parameter type (probably not possible) or some example code in the function's docs.

@daviesrob

Copy link
Copy Markdown
Member

On bam_hdr_length() - yes it should be ssize_t although that does introduce some trickyness the the header l_text being size_t. It's therefore possible to overflow it (at least on 32-bit platforms) although I suspect if the header gets that big other problems are likely to show up pretty quickly.

On bam_hdr_remove_lines() - yes, I was thinking of making a typedef, but then forgot about it. Possibly it might be easier to make it take a list of strings and have it build the hash table itself.

@jmarshall

jmarshall commented Jul 5, 2019 •

Copy link
Copy Markdown
Member Author

This should be ready to go now.

You might or might not want to take the final commit (1faa906) which renames bam_hrecs_* to sam_hrecs_* and similarly for bam_hrec_*. These identifiers are all internal, so it doesn't matter too much for the API; probably mostly it just comes down to how used you folks are to the status quo for these identifiers.

I left the struct tag as struct bam_hdr_t for compatibility with wrappers such as pysam and Bio::DB::HTS. Actually by inspecting the code (untested): it doesn't matter for Bio::DB::HTS, as that just uses the bam_hdr_t typedef; for pysam, their .pxd file says ctypedef struct bam_hdr_t but then they mostly bundle particular HTSlib versions and tweak their cython code accordingly anyway. Hmmm… not sure whether it matters for these or other wrappers…
It turns out that the struct tag was only added recently (in 4c2c536) and after the last release, so it's fine to rename it.

@jmarshall

Copy link
Copy Markdown
Member Author

Deserves a NEWS entry, I guess…

@jmarshall

Copy link
Copy Markdown
Member Author

NEWS entry added and struct tag renamed.

@jmarshall
jmarshall force-pushed the samify-header-api branch 2 times, most recently from 16d448c to 62b72d2 Compare July 5, 2019 09:58
@jmarshall

Copy link
Copy Markdown
Member Author

Sigh… that NEWS update caused a merge conflict so CI couldn't run. Added a commit merging from develop so we can observe the green tick 😄

That [OMIT ME] commit should be omitted when you merge this. Or let me know if you're unconcerned about seeing CI on the final state of this PR and I'll remove the [OMIT ME] commit from the PR.

@daviesrob

Copy link
Copy Markdown
Member

Looks good. Would it be possible to rebase the whole thing and force push the result onto your existing branch?

@jmarshall

Copy link
Copy Markdown
Member Author

No problem, will do. So that's a yes to bam_hrecs_→sam_hrecs_ as well?

@daviesrob

Copy link
Copy Markdown
Member

Yes, consistency for the win. Plus it's internal so shouldn't break anything.

jmarshall added 10 commits July 5, 2019 12:56
This follows the convention of sam_-prefixed functions operating
on SAM/BAM/CRAM while bam_-prefixed functions operate only on BAM.
(Apart from legacy items such as bam_hdr_init().)

htslib/cram.h contained sam_hdr_length() and sam_hdr_str() functions
even before the header API landed; remove these declarations as
compatible functions are now provided by htslib/sam.h. #include that
from htslib/cram.h to get those declarations and as cram.h now uses
bam_hdr_t directly. (Amend htslib_vars.mk accordingly.)

Remove now unnecessary sam_hdr_length()->bam_hdr_length() and *_str()
shims from headers.c.
There is already a (legacy) SAM_hdr type, so having sam_hdr as well
would be confusing.
These instances of [sb]am_hdr_remove_line_key() were omitted from
919d33c while the remainder appear
to have originated as typos.
Add bam_hdr_t compatibility typedef. Rename the struct tag
to `struct sam_hdr_t`; in the previous 1.9 release, bam_hdr_t was
a typedef for an unnamed struct, so renaming causes no compatibility
issue with wrappers such as pysam and Bio::DB::HTS.

In some *.c files rename the `bh` parameter accordingly, though not
in header.c etc where it is prevalent. In htslib/sam.h, use parameter
name `sam_hdr_t *h` rather than `bh` as per existing API functions.

Leave some of test/sam.c's functions using bam_hdr_t as a verification
that the compatibility typedef is operating correctly.
Add static inline bam_hdr_* compatibility functions, and leave some
of test/sam.c's functions using them as a verification that they
are operating correctly.
Write sam_hdr_parse() directly and make the legacy sam_hdr_parse_()
call it, rather than the other way around. Make both legacy functions
sam_hdr_parse_() and sam_hdr_free() static inline wrappers present
only in the header file; their names will not appear in the library.

Remove comments mentioning sam_hdr_dec_ref(), which no longer exists.
This function has been renamed to sam_hdr_add_line().

Remove the preceding comment block. This comment block has existed
unchanged since fe11d21 which created
htslib/cram.h. It describes sam_hdr_add_lines(), but no declaration for
that legacy cram/sam_header.c function was ever added to the external
header!

Make htslib/cram.h's enum cram_block_method match the definition
in cram/cram_structs.h: the first constant is BM_ERROR.
Rename bam_hdr_parse() to sam_hdr_fill_hrecs() to avoid confusion
with the public sam_hdr_parse() function. Add a `sam_hdr_` prefix
to update_target_arrays() to avoid polluting the library client's
global namespace.
For consistency with the sam_hdr_* parts of the header.c/.h.
(Performed mechanically with sed(1).)
Mention that the new header API pertains only to SAM/BAM/CRAM,
not to VCF/BCF as well.
@jmarshall
jmarshall force-pushed the samify-header-api branch from 62b72d2 to 4694497 Compare July 5, 2019 12:19
@jmarshall

Copy link
Copy Markdown
Member Author

Rebased on top of current develop.

@daviesrob
daviesrob merged commit 4694497 into samtools:develop Jul 5, 2019
@daviesrob

Copy link
Copy Markdown
Member

Now merged, thanks very much.

@jmarshall
jmarshall deleted the samify-header-api branch July 5, 2019 16:02
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