modified: SpatialOmicsCoord.py
[GalaxyCodeBases.git] / BGI / SOAPsnp / SamCtrl.cpp
blob3762239dbc5bca2c022561d0f332f679e42afe00
1 /*
2 *****************************************************************************
3 *Copyright 2010
4 * BGI-SHENZHEN
5 *All Rights Reserved
6 *ATHUOR : Bill Tang
7 *CREATE DATE : 2010-7-29
8 *FUNCTION : definition of class SamCtrl
9 *FILE NAME : SamCtrl.cpp
10 *UPDATE DATE : 2010-8-1
11 *UPDATE BY : Bill Tang
12 *******************************************************************************
16 #include "SamCtrl.h"
19 SamCtrl::SamCtrl()
21 m_out_path = "-";
22 m_out_mode = "w";
23 m_fn_list = 0;
24 m_in = 0;
25 m_out = 0;
26 m_b = bam_init1(); // initialize m_b
31 SamCtrl::~SamCtrl(void)
33 bam_destroy1(m_b);
34 close();
37 /**
38 * DATE: 2010-7-29
39 * FUNCTION: open sam/bam file in pattern mode.
40 * PARAMETER: path: the file's path. mode: the pattern.
41 * RETURN: successful: true. failed: flase.
43 bool SamCtrl::open(const char *path, const char *mode)
45 if (mode[0] == 'r') {
46 // judge if opened a file before
47 if (m_in != 0) {
48 return false;
51 m_in_mode = mode;
52 m_in_path = path;
54 // open sam/bam file
55 if ((m_in = samopen(m_in_path.c_str(), m_in_mode.c_str(), m_fn_list)) == 0)
57 return false;
60 if (m_in->header == 0)
62 return false;
65 if ((m_out = samopen(m_out_path.c_str(), m_out_mode.c_str(), m_in->header)) == 0)
67 return false;
69 return true;
71 return false;
74 /**
75 * DATE: 2010-7-29
76 * FUNCTION: read a line from sam/bam file.
77 * PARAMETER: line: the read data will stored in this varible.
78 * RETURN: the line size when read successful. -1 when read failed
80 int SamCtrl::readline(std::string &line)
82 if (m_in == 0)
84 return -1;
87 int ret = 0;
88 // begin to read
89 while ((ret = samread(m_in, m_b)) >= 0)
91 // when read failed continue
92 if (__g_skip_aln(m_in->header, m_b))
94 continue;
97 m_s = bam_format1_core(m_out->header, m_b, m_out->type>>2&3); // read the buffer
98 line = m_s; // store into the line
99 free(m_s);
100 return line.size();
102 return -1;
106 * DATE: 2010-7-29
107 * FUNCTION: close the sam/bam file
108 * PARAMETER: void
109 * RETURN: void
111 void SamCtrl::close()
113 if (m_fn_list != 0)
114 delete m_fn_list;
115 if (m_in != 0)
116 samclose(m_in);
117 if (m_out != 0)
118 samclose(m_out);
119 m_in = 0;
120 m_out = 0;
121 m_fn_list = 0;
125 * DATE: 2010-7-29
126 * FUNCTION: judge if the file is opened successful
127 * PARAMETER: void
128 * RETURN: true if successful else false if failed
130 bool SamCtrl::isOpened()
132 return !(m_in == 0);