Skip to content

Commit

Permalink
Merge branch 'demo' of github.com:vasudeva8/htslib into demo
Browse files Browse the repository at this point in the history
  • Loading branch information
vasudeva8 committed Jun 2, 2023
1 parent a74a90d commit d474a19
Show file tree
Hide file tree
Showing 14 changed files with 1,183 additions and 174 deletions.
1 change: 1 addition & 0 deletions htslib/sam.h
Original file line number Diff line number Diff line change
Expand Up @@ -2287,6 +2287,7 @@ int bam_mods_at_next_pos(const bam1_t *b, hts_base_mod_state *state,
* @param state The base modification state pointer.
* @param mods A supplied array for returning base modifications
* @param n_mods The size of the mods array
* @param pos Pointer holding position of modification in sequence
* @return The number of modifications found on success,
* 0 if no more modifications are present,
* -1 on failure.
Expand Down
11 changes: 9 additions & 2 deletions sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -6215,6 +6215,15 @@ static int seqi_rc[] = { 0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15 };
*
*/
int bam_parse_basemod(const bam1_t *b, hts_base_mod_state *state) {

//reset position, else upcoming calls may fail on seq pos - length check
state->seq_pos = 0;
//TODO: When a file w/o MM tag is processed and an alignment with lower
//length than previous one is parsed, parsing will fail due to the
//seq_pos n l_qlength check in fwd processing on bam_mods_at_next_pos
//and this method is supposed to reset the state as per comments. so
//this resetting be done in all cases?

// Read MM and ML tags
uint8_t *mm = bam_aux_get(b, "MM");
if (!mm) mm = bam_aux_get(b, "Mm");
Expand Down Expand Up @@ -6244,8 +6253,6 @@ int bam_parse_basemod(const bam1_t *b, hts_base_mod_state *state) {
uint8_t *ml_end = ml ? ml+6 + le_to_u32(ml+2) : NULL;
if (ml) ml += 6;

state->seq_pos = 0;

// Aggregate freqs of ACGTN if reversed, to get final-delta (later)
int freq[16];
if (b->core.flag & BAM_FREVERSE)
Expand Down
8 changes: 3 additions & 5 deletions samples/add_header.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ returns 1 on failure 0 on success
int main(int argc, char *argv[])
{
const char *inname = NULL, sq[] = "@SQ\tSN:TR1\tLN:100\n@SQ\tSN:TR2\tLN:50";
//char *id = NULL;
//const char *types[] = {"HD", "SQ", "RG", "PG", "CO"}, *idfield[] = { NULL, "SN", "ID", "ID", ""};
int c = 0, ret = EXIT_FAILURE;
samFile *infile = NULL, *outfile = NULL;
sam_hdr_t *in_samhdr = NULL;
Expand All @@ -67,7 +65,7 @@ int main(int argc, char *argv[])
printf("Could not open %s\n", inname);
goto end;
}
if (!(outfile = sam_open("-", "w"))) { //use stdout as the output file for ease of display of update
if (!(outfile = sam_open("-", "w"))) { //use stdout as the output file for ease of display of update
printf("Could not open stdout\n");
goto end;
}
Expand All @@ -85,7 +83,7 @@ int main(int argc, char *argv[])
}

//add SQ line with SN as TR1 and TR2
if (sam_hdr_add_lines(in_samhdr, &sq, 0)) { //length as 0 for NULL terminated data
if (sam_hdr_add_lines(in_samhdr, &sq[0], 0)) { //length as 0 for NULL terminated data
printf("Failed to add SQ lines\n");
goto end;
}
Expand All @@ -102,7 +100,7 @@ int main(int argc, char *argv[])
goto end;
}

if (sam_hdr_add_line(in_samhdr, "CO", "Test data", NULL)) { //NULL is to indicate end of args
if (sam_hdr_add_line(in_samhdr, "CO", "Test data", NULL)) { //NULL is to indicate end of args
printf("Failed to add PG line\n");
goto end;
}
Expand Down
2 changes: 1 addition & 1 deletion samples/dump_aux.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ returns 1 on failure 0 on success
int main(int argc, char *argv[])
{
const char *inname = NULL;
int c = 0, ret = EXIT_FAILURE;
int ret = EXIT_FAILURE;
sam_hdr_t *in_samhdr = NULL;
samFile *infile = NULL;
int ret_r = 0;
Expand Down
220 changes: 220 additions & 0 deletions samples/mod_aux.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
/* mod_aux.c -- showcases the htslib api usage
Copyright (C) 2023 Genome Research Ltd.
Author: Vasudeva Sarma <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE
*/

/* The pupose of this code is to demonstrate the library apis and need proper error handling and optimization */

#include <getopt.h>
#include <unistd.h>
#include <htslib/sam.h>

/// print_usage - print the demo_usage
/** @param fp pointer to the file / terminal to which demo_usage to be dumped
returns nothing
*/
static void print_usage(FILE *fp)
{
fprintf(fp, "Usage: mod_aux infile QNAME tag type val\n\
Add/update the given aux tag to all alignments\n\
type A-char C-int F-float Z-string\n");
}

/// main_demo - start of the demo
/** @param argc - count of arguments
* @param argv - pointer to array of arguments
returns 1 on failure 0 on success
*/
int main(int argc, char *argv[])
{
const char *inname = NULL, *tag = NULL, *qname = NULL, *val = NULL;
char type = '\0';
int c = 0, ret = EXIT_FAILURE, ret_r = 0, length = 0;
sam_hdr_t *in_samhdr = NULL;
samFile *infile = NULL, *outfile = NULL;
bam1_t *bamdata = NULL;
uint8_t *data = NULL;

//mod_aux infile QNAME tag type val
if (argc != 6) {
print_usage(stderr);
goto end;
}
inname = argv[1];
qname = argv[2];
tag = argv[3];
type = argv[4][0];
val = argv[5];

if (type >= 'a' && type <='z') {
type -= 32; //make it upper case
}
if (!(bamdata = bam_init1())) {
printf("Failed to allocate data memory!\n");
goto end;
}

//open input file
if (!(infile = sam_open(inname, "r"))) {
printf("Could not open %s\n", inname);
goto end;
}
//open output file
if (!(outfile = sam_open("-", "w"))) {
printf("Could not open std output\n");
goto end;
}

if (!(in_samhdr = sam_hdr_read(infile))) {
printf("Failed to read header from file!\n");
goto end;
}

if (sam_hdr_write(outfile, in_samhdr) == -1) {
printf("Failed to write header\n");
goto end;
}

while ((ret_r = sam_read1(infile, in_samhdr, bamdata)) >= 0) {
if (strcasecmp(bam_get_qname(bamdata), qname)) {
if (sam_write1(outfile, in_samhdr, bamdata) < 0) {
printf("Failed to write output\n");
goto end;
}
continue; //not matching
}

errno = 0;
//matched to qname, update aux
if (!(data = bam_aux_get(bamdata, tag))) {
int i = 0; float f = 0;
//tag not present append
switch (type) {
case 'F':
case 'D':
length = sizeof(float);
f = atof(val);
val = (const char*) &f;
break;
case 'C':
case 'S':
case 'I':
length = sizeof(int);
i = atoi(val);
val = (const char*) &i;
break;
case 'Z':
length = strlen(val) + 1; //1 for NUL termination
break;
case 'A':
length = 1;
break;
}
if (bam_aux_append(bamdata, tag, type, length, (const uint8_t*)val)) {
printf("Failed to append aux data, errno: %d\n", errno);
goto end;
}
}
else {
char auxtype = bam_aux_type(data);
//update the tag with newer value
switch (type) {
case 'F':
case 'D':
if (auxtype != 'f' && auxtype != 'd') {
printf("Invalid aux type passed\n");
goto end;
}
if (bam_aux_update_float(bamdata, tag, atof(val))) {
printf("Failed to update float data, errno: %d\n", errno);
goto end;
}
break;
case 'C':
case 'S':
case 'I':
if (auxtype != 'c' && auxtype != 'C' && auxtype != 's' && auxtype != 'S' && auxtype != 'i' && auxtype != 'I') {
printf("Invalid aux type passed\n");
goto end;
}
if (bam_aux_update_int(bamdata, tag, atoll(val))) {
printf("Failed to update int data, errno: %d\n", errno);
goto end;
}
break;
case 'Z':
if (auxtype != 'Z') {
printf("Invalid aux type passed\n");
goto end;
}
length = strlen(val) + 1; //1 for NUL termination
if (bam_aux_update_str(bamdata, tag, length, val)) {
//with length as -1, length will be detected based on null terminated val data
printf("Failed to update string data, errno: %d\n", errno);
goto end;
}
break;
case 'A':
if (auxtype != 'A') {
printf("Invalid aux type passed\n");
goto end;
}
//update the char data directly on buffer
*(data+1) = val[0];
break;
//TODO there is no H update support!
default:
printf("Invalid data type\n");
goto end;
break;
}
}
if (sam_write1(outfile, in_samhdr, bamdata) < 0) {
printf("Failed to write output\n");
goto end;
}
}
if (ret_r < -1) {
//read error
printf("Failed to read data\n");
goto end;
}

ret = EXIT_SUCCESS;
end:
//cleanup
if (in_samhdr) {
sam_hdr_destroy(in_samhdr);
}
if (infile) {
sam_close(infile);
}
if (outfile) {
sam_close(outfile);
}
if (bamdata) {
bam_destroy1(bamdata);
}
return ret;
}
Loading

0 comments on commit d474a19

Please sign in to comment.