970cf2daee7ca49a95280bcf1431ae35ba8f223f
[samtools.git] / sam.h
1 #ifndef BAM_SAM_H
2 #define BAM_SAM_H
3
4 #include "bam.h"
5
6 /*!
7   @header
8
9   This file provides higher level of I/O routines and unifies the APIs
10   for SAM and BAM formats. These APIs are more convenient and
11   recommended.
12
13   @copyright Genome Research Ltd.
14  */
15
16 /*! @typedef
17   @abstract SAM/BAM file handler
18   @field  type    type of the handler; bit 1 for BAM and bit 2 for reading
19   @field  bam   BAM file handler; valid if (type&1) == 1
20   @field  tamr  SAM file handler for reading; valid if type == 2
21   @field  tamw  SAM file handler for writing; valid if type == 0
22   @field  header  header struct
23  */
24 typedef struct {
25         int type;
26         union {
27                 tamFile tamr;
28                 bamFile bam;
29                 FILE *tamw;
30         } x;
31         bam_header_t *header;
32 } samfile_t;
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38         /*!
39           @abstract     Open a SAM/BAM file
40
41           @param fn SAM/BAM file name; "-" is recognized as stdin (for
42           reading) or stdout (for writing).
43
44           @param mode open mode /[rw](b?)(u?)(h?)/: 'r' for reading, 'w' for
45           writing, 'b' for BAM I/O, 'u' for uncompressed BAM output and 'h'
46           for outputing header in SAM. If 'b' present, it must immediately
47           follow 'r' or 'w'. Valid modes are "r", "w", "wh", "rb", "wb" and
48           "wbu" exclusively.
49
50           @param aux auxiliary data; if mode[0]=='w', aux points to
51           bam_header_t; if strcmp(mode, "rb")==0 and @SQ header lines in SAM
52           are absent, aux points the file name of the list of the reference;
53           aux is not used otherwise.
54
55           @return       SAM/BAM file handler
56          */
57         samfile_t *samopen(const char *fn, const char *mode, const void *aux);
58
59         /*!
60           @abstract     Close a SAM/BAM handler
61           @param  fp    file handler to be closed
62          */
63         void samclose(samfile_t *fp);
64
65         /*!
66           @abstract     Read one alignment
67           @param  fp    file handler
68           @param  b     alignment
69           @return       bytes read
70          */
71         int samread(samfile_t *fp, bam1_t *b);
72
73         /*!
74           @abstract     Write one alignment
75           @param  fp    file handler
76           @param  b     alignment
77           @return       bytes written
78          */
79         int samwrite(samfile_t *fp, const bam1_t *b);
80
81         /*!
82           @abstract     Get the pileup for a whole alignment file
83           @param  fp    file handler
84           @param  mask  mask transferred to bam_plbuf_set_mask()
85           @param  func  user defined function called in the pileup process
86           #param  data  user provided data for func()
87          */
88         int sampileup(samfile_t *fp, int mask, bam_pileup_f func, void *data);
89
90 #ifdef __cplusplus
91 }
92 #endif
93
94 #endif