1 /*
2 Copyright (c) 2010-2013 Genome Research Ltd.
3 Author: James Bonfield <jkb@sanger.ac.uk>
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 
8    1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 
11    2. Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14 
15    3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
16 Institute nor the names of its contributors may be used to endorse or promote
17 products derived from this software without specific prior written permission.
18 
19 THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH LTD OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 
31 #include <config.h>
32 
33 #include <assert.h>
34 #include <string.h>
35 #include <stdlib.h>
36 
37 #include "cram/cram.h"
38 #include "htslib/sam.h"
39 
40 /*---------------------------------------------------------------------------
41  * Samtools compatibility portion
42  */
bam_construct_seq(bam_seq_t ** bp,size_t extra_len,const char * qname,size_t qname_len,int flag,int rname,int pos,int end,int mapq,uint32_t ncigar,const uint32_t * cigar,int mrnm,int mpos,int isize,int len,const char * seq,const char * qual)43 int bam_construct_seq(bam_seq_t **bp, size_t extra_len,
44 		      const char *qname, size_t qname_len,
45 		      int flag,
46 		      int rname,      // Ref ID
47 		      int pos,
48 		      int end,        // aligned start/end coords
49 		      int mapq,
50 		      uint32_t ncigar, const uint32_t *cigar,
51 		      int mrnm,       // Mate Ref ID
52 		      int mpos,
53 		      int isize,
54 		      int len,
55 		      const char *seq,
56 		      const char *qual) {
57     static const char L[256] = {
58 	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
59 	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
60 	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
61 	15,15,15,15,15,15,15,15,15,15,15,15,15, 0,15,15,
62 	15, 1,14, 2,13,15,15, 4,11,15,15,12,15, 3,15,15,
63 	15,15, 5, 6, 8,15, 7, 9,15,10,15,15,15,15,15,15,
64 	15, 1,14, 2,13,15,15, 4,11,15,15,12,15, 3,15,15,
65 	15,15, 5, 6, 8,15, 7, 9,15,10,15,15,15,15,15,15,
66 	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
67 	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
68 	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
69 	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
70 	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
71 	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
72 	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
73 	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15
74     };
75     bam1_t *b = (bam1_t *)*bp;
76     uint8_t *cp;
77     int i, qname_nuls, bam_len;
78 
79     //b->l_aux = extra_len; // we fill this out later
80 
81     qname_nuls = 4 - qname_len%4;
82     if (qname_len + qname_nuls > 255) // Check for core.l_qname overflow
83         return -1;
84     bam_len = qname_len + qname_nuls + ncigar*4 + (len+1)/2 + len + extra_len;
85     if (b->m_data < bam_len) {
86 	b->m_data = bam_len;
87 	kroundup32(b->m_data);
88 	b->data = (uint8_t*)realloc(b->data, b->m_data);
89 	if (!b->data)
90 	    return -1;
91     }
92     b->l_data = bam_len;
93 
94     b->core.tid     = rname;
95     b->core.pos     = pos-1;
96     b->core.bin     = bam_reg2bin(pos-1, end);
97     b->core.qual    = mapq;
98     b->core.l_qname = qname_len+qname_nuls;
99     b->core.l_extranul = qname_nuls-1;
100     b->core.flag    = flag;
101     b->core.n_cigar = ncigar;
102     b->core.l_qseq  = len;
103     b->core.mtid    = mrnm;
104     b->core.mpos    = mpos-1;
105     b->core.isize   = isize;
106 
107     cp = b->data;
108 
109     strncpy((char *)cp, qname, qname_len);
110     for (i = 0; i < qname_nuls; i++)
111 	cp[qname_len+i] = '\0';
112     cp += qname_len+qname_nuls;
113     if (ncigar > 0) memcpy(cp, cigar, ncigar*4);
114     cp += ncigar*4;
115 
116     for (i = 0; i+1 < len; i+=2) {
117 	*cp++ = (L[(uc)seq[i]]<<4) + L[(uc)seq[i+1]];
118     }
119     if (i < len)
120 	*cp++ = L[(uc)seq[i]]<<4;
121 
122     if (qual)
123 	memcpy(cp, qual, len);
124     else
125 	memset(cp, '\xff', len);
126 
127     return bam_len;
128 }
129 
cram_header_to_bam(SAM_hdr * h)130 bam_hdr_t *cram_header_to_bam(SAM_hdr *h) {
131     int i;
132     bam_hdr_t *header = bam_hdr_init();
133 
134     header->l_text = ks_len(&h->text);
135     header->text = malloc(header->l_text+1);
136     memcpy(header->text, ks_str(&h->text), header->l_text);
137     header->text[header->l_text] = 0;
138 
139     header->n_targets = h->nref;
140     header->target_name = (char **)calloc(header->n_targets,
141 					  sizeof(char *));
142     header->target_len = (uint32_t *)calloc(header->n_targets, 4);
143 
144     for (i = 0; i < h->nref; i++) {
145 	header->target_name[i] = strdup(h->ref[i].name);
146 	header->target_len[i] = h->ref[i].len;
147     }
148 
149     return header;
150 }
151 
bam_header_to_cram(bam_hdr_t * h)152 SAM_hdr *bam_header_to_cram(bam_hdr_t *h) {
153     return sam_hdr_parse_(h->text, h->l_text);
154 }
155