Repository navigation
Header API for SAM/BAM/CRAM - #812
Conversation
d4ebf44 to
c98b5f8
Compare
| } | ||
|
|
||
| // @HD goes at the top (unless there's one already) | ||
| if (h_type->type == hd_type && hrecs->first_line->type != hd_type) { |
There was a problem hiding this comment.
We should probably prevent the addition of a second @HD line.
| if (-1 == bam_hrecs_vadd(hrecs, "PG", args, | ||
| "ID", id, | ||
| "PN", name, | ||
| "PN", !specified_pn ? name : "", |
There was a problem hiding this comment.
If specified_pp is not null, should it be used as an argument, i.e. "PP", specified_pp?
| void bam_hdr_destroy(bam_hdr_t *h); | ||
| int bam_name2id(bam_hdr_t *h, const char *ref); | ||
| bam_hdr_t* bam_hdr_dup(const bam_hdr_t *h0); | ||
| /// Alias of bam_hdr_name2ref(), for backwards compatibility. |
There was a problem hiding this comment.
We should probably ditch (deprecate) the use of bam_hdr_t.sdict altogether and make bam_name2id an actual alias of bam_hdr_name2ref. Currently, they have different implementations.
| if (hrecs->refs_changed >= 0 && rebuild_target_arrays(bh) != 0) | ||
| return -1; | ||
|
|
||
| bam_hdr_link_pg(bh); |
There was a problem hiding this comment.
Calling bam_hdr_link_pg would result in resetting bh->text, which is not intuitive when calling bam_hdr_parse, or some of its callers that do not modify the header, such as bam_hdr_name2ref.
For example, the following code would result in a null text field, which is probably not the expected behaviour.
samFile *in = sam_open("test_header.sam", "r");
bam_hdr_t *h = sam_hdr_read(in);
int id = bam_hdr_name2ref(h, "ref2");
printf("id=%d, header=%p\n", id, h->text);
Perhaps, a better place for the bam_hdr_link_pg call would be inside bam_hdr_rebuild.
|
Added All these |
|
It is just a matter of convention. I was thinking that people see the lines more as 1st |
| static int build_header_line(const bam_hrec_type_t *ty, kstring_t *ks) { | ||
| bam_hrec_tag_t *tag; | ||
| int r = 0; | ||
| char c[2]= { ty->type >> 8, ty->type & 0xff };; |
|
|
||
| int bam_hdr_find_line_pos(bam_hdr_t *bh, const char *type, | ||
| int pos, kstring_t *ks) { | ||
| bam_hrecs_t *hrecs; |
There was a problem hiding this comment.
Different indentation.
| int bam_hdr_find_line_pos(bam_hdr_t *bh, const char *type, | ||
| int pos, kstring_t *ks) { | ||
| bam_hrecs_t *hrecs; | ||
| if (!bh || !type) |
There was a problem hiding this comment.
We should check for null type in bam_hdr_find_line_id:1026.
The strings being copied are allocated from the string pool, which is immutable and has the same life-time as the header. It is therefore safe to point at the existing strings in the hash tables. Co-Authored-By: Rob Davies <[email protected]>
… when there is a need for reading it. sam_hdr_error(), string_dup() and string_ndup() now take const pointers to the data they are printing / copying so they can be used with const inputs.
Makes the diffs for the copy to header.c in the next commit easier to read.
Removes bam_hrecs_t text kstring as it duplicates the existing bam_hdr_t text field. bam_hdr_t text is freed and set to NULL when the new API functions are used to make an edit so that outdated text cannot be used in error. Remove bam_hrecs_t ref_count as bam_hdr_t now has one. Still a lot of work to be done, but a solid first step. Co-Authored-By: Rob Davies <[email protected]>
Co-Authored-By: Rob Davies <[email protected]>
Added extra comments and details. Made cigar_tab statically allocated. Made KS_FREE set the string pointer to NULL. Add a couple of warnings.
bootstrap_bam_hrecs() becomes bam_hrecs_populate() sam_hdr_find_update2() becomes sam_hdr_update_line() sam_hdr_keep_line_key2() becomes sam_hdr_keep_line() sam_hdr_PG_ID2() becomes sam_hdr_pg_id() sam_hdr_add_PG2() becomes sam_hdr_add_pg() sam_hdr_rebuild2() and sam_hdr_update_line() are moved. Conflicts: header.c
Adds a global line order linked list for all header records. This allows the input header to be reconstructed exactly when passed through the new header API. (The one exception to this is that an out-of-order @hd line will be lifted to the top of the header). Headers added programatically will currently be placed immediately after the last existing line of the same type, of at the end of the header if no line of the given type is present. Removes special handling for comments. Simplifies bam_hrecs_rebuild_text() as it now just has to walk through the global linked list.
Add documentation for some functions that were missed before Add note about legacy bam_hdr_t::text and bam_hdr_t::l_text fields Add some information on ownership of returned strings Adjust doxygen formatting to be similar to other functions in htslib/sam.h. In particular put a one-line abstract in a '///' comment before the main documentation block. Backslash-escape @-signs that are part of header record types. Remove stray bam_name2id() declaration.
Remove bam_hdr_link_pg() from public API as it should not be necessary to call it directly. Make bam_hdr_add_pg() work when ID, PP and PN tags are passed to it in the varargs list. Make bam_hrecs_vadd() ignore tags passed in its varargs list which have an empty string as value. This allows them to be ignored when the same tag exists in the args va_list. Add various bam_hdr_add_pg() tests.
Instead of returning char *, write the output into a kstring. Allows the caller to easily reuse allocated memory when calling them repeatedly. Includes some documentation fixes.
Make sam_parse1() check for it.
…er line identified by type and position. Rename methods to highlight if the line identification is done by key:value pair or by position. Make `bam_name2id` static inline.
Also fix double semicolon in bam_hrecs_remove_line() and incorrect indentation in bam_hdr_find_line_pos().
|
While we're here, we should make bam_hdr_len and related functions return size_t (or ssize_t if they need negatives for errors). See samtools/samtools#1066 for reasons why. This also means changing the l_text field, or at least having a separate copy that the new API is using so the bam_hdr_len function can return large headers. |
Allows (at least on 64-bit platforms with enough RAM) headers longer than the usual 2^31 byte limit. Such headers cannot be written to BAM or CRAM files due to format limits, but should be usable with SAM. Attempting to write them to BAM or CRAM will print an error message and fail. Places where the header length gets used are upgraded to use size_t as well. Makes sam_hdr_sanitise() only check how long the header is if it really wants to resize it. Prevent h->l_text from being set to an incorrect value if old_sam_hdr_change_HD() fails. Also fixes possible infinite loop in sam_hdr_write() when searching for @sq lines.
Improves performance on very big headers.
Also minor Makefile fix.
Add NEWS item. Bump TWO_TO_THREE_TRANSITION_COUNT due to ABI changes.
|
It is great that HTSlib will finally have a header API 🎉 It is a little bit of a shame that the new functions are of the form Adding a new swathe of header API seems like an opportunity to make the canonical header struct be Was this If they might not have time/interest/priorities to do it themselves, would the maintainers be amenable to pull requests making the new header API functions be named |
|
@jmarshall Yes it was, it changed in 7f876cf. I don't particularly want to change it back at this point. I think the |
|
I can see why having successfully merged this after so many years of the saga, you would be less than keen to reopen it again. 😄 But if the maintainers can bear to look at it for the sake of HTSlib's There were no troublesome clashes with pre-existing API bits and pieces; I suspect that problem may have been limited to this PR when it was in its intermediate state with various parts still up in the air. |
Management methods:
sam_hdr_init- create a new headersam_hdr_destroy- free the resources used by the headersam_hdr_read- read the header from a BGZF filesam_hdr_write- write the header to a BGZF filesam_hdr_dup- duplicate the header structuresam_hdr_length- return the length of the header textsam_hdr_str- return the header text as a stringsam_hdr_nref- return the number of references (@SQlines)sam_hdr_rebuild- rebuild the header text representationsam_hdr_incr_ref- increment the reference counter on the header structuresam_hdr_name2tid- convert reference name to reference indexsam_hdr_tid2name- retrieve reference name using the index (tid)sam_hdr_tid2len- retrieve reference length using the index (tid)Line level methods:
sam_hdr_add_lines- add a group of lines from a char buffersam_hdr_add_line- add a single line with individual key:value pairssam_hdr_find_line_id- find a line specified by a key:value identifiersam_hdr_find_line_pos- find a line specified by positionsam_hdr_remove_line_id- remove a line specified by a key:value identifiersam_hdr_remove_line_pos- remove a line specified by positionsam_hdr_update_line- update a line (works for tags)sam_hdr_keep_line- remove all lines of the same type except the current onesam_hdr_remove_lines- remove all lines, except the one specified by the hash set argsam_hdr_count_lines- count the header linessam_hdr_add_pg- add a new@PGlinesam_hdr_find_hd- return the@HDlinesam_hdr_update_hd-@HDline update specific macroTag level methods:
sam_hdr_find_tag_id- find a tag in a line specified by a key:value identifiersam_hdr_find_tag_pos- find a tag in a line specified by positionsam_hdr_remove_tag_id- remove a tag from a line specified by a key:value identifier