| 1 | #define USE_BOOST |
| 2 | |
| 3 | //#define JUMPING_TEST 1 |
| 4 | #define READ_TEST 1 |
| 5 | |
| 6 | #include "SeqLib/SeqLibUtils.h" |
| 7 | |
| 8 | #ifdef USE_BOOST |
| 9 | #include <boost/timer/timer.hpp> |
| 10 | #endif |
| 11 | |
| 12 | #include <cmath> |
| 13 | |
| 14 | //#define RUN_SEQAN 1 |
| 15 | //#define RUN_BAMTOOLS 1 |
| 16 | #define RUN_SEQLIB 1 |
| 17 | //#define RUN_HTSLIB 1 |
| 18 | |
| 19 | #ifdef RUN_SEQAN |
| 20 | #include <seqan/bam_io.h> |
| 21 | #include <seqan/sequence.h> |
| 22 | using namespace seqan; |
| 23 | #endif |
| 24 | |
| 25 | #ifdef RUN_SEQLIB |
| 26 | #include "SeqLib/BamReader.h" |
| 27 | #include "SeqLib/BamWriter.h" |
| 28 | #endif |
| 29 | |
| 30 | #ifdef RUN_HTSLIB |
| 31 | #include <iostream> |
| 32 | extern "C" { |
| 33 | #include "htslib/htslib/hts.h" |
| 34 | #include "htslib/htslib/sam.h" |
| 35 | #include "htslib/htslib/bgzf.h" |
| 36 | #include "htslib/htslib/kstring.h" |
| 37 | #include "htslib/htslib/faidx.h" |
| 38 | } |
| 39 | #endif |
| 40 | |
| 41 | //#define BAMTOOLS_GET_CORE 1 |
| 42 | |
| 43 | #ifdef RUN_BAMTOOLS |
| 44 | #include "api/BamReader.h" |
| 45 | #endif |
| 46 | |
| 47 | int main() |
| 48 | { |
| 49 | |
| 50 | const size_t limit = 5000000; |
| 51 | const size_t print_limit = 1000000; |
| 52 | #ifdef JUMPING_TEST |
| 53 | const size_t jump_limit = 1000; |
| 54 | #endif |
| 55 | size_t count = 0; |
| 56 | |
| 57 | //std::string bam = "/xchip/gistic/Jeremiah/GIT/SeqLib/seq_test/test_data/small.bam"; |
| 58 | std::string bam = "/broad/broadsv/NA12878/20120117_ceu_trio_b37_decoy/CEUTrio.HiSeq.WGS.b37_decoy.NA12878.clean.dedup.recal.20120117.bam"; |
| 59 | std::string bami = "/broad/broadsv/NA12878/20120117_ceu_trio_b37_decoy/CEUTrio.HiSeq.WGS.b37_decoy.NA12878.clean.dedup.recal.20120117.bam.bai"; |
| 60 | std::string obam = "/xchip/gistic/Jeremiah/GIT/SeqLib/seq_test/tmp_out.bam"; |
| 61 | |
| 62 | #ifdef USE_BOOST |
| 63 | boost::timer::auto_cpu_timer t; |
| 64 | #endif |
| 65 | |
| 66 | #ifdef RUN_BAMTOOLS |
| 67 | std::cerr << " **** RUNNING BAMTOOLS **** " << std::endl; |
| 68 | BamTools::BamReader btr; |
| 69 | btr.Open(bam); |
| 70 | btr.OpenIndex(bami); |
| 71 | |
| 72 | BamTools::BamAlignment ba; |
| 73 | std::vector<BamTools::BamAlignment> bav; |
| 74 | |
| 75 | #ifdef READ_TEST |
| 76 | #ifndef BAMTOOLS_GET_CORE |
| 77 | std::cerr << " **** FULL REC **** " << std::endl; |
| 78 | while(btr.GetNextAlignment(ba) && count++ < limit) { |
| 79 | #else |
| 80 | std::cerr << " **** CORE REC **** " << std::endl; |
| 81 | while(btr.GetNextAlignmentCore(ba) && count++ < limit) { |
| 82 | #endif |
| 83 | if (count % print_limit == 0) |
| 84 | std::cerr << "...at read " << SeqLib::AddCommas(count) << std::endl; |
| 85 | bav.push_back(ba); |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | #ifdef JUMPING_TEST |
| 90 | // perform jumping |
| 91 | for (int i = 0; i < jump_limit; ++i) { |
| 92 | int chr = rand() % 22; |
| 93 | int pos = rand() % 1000000 + 1000000; |
| 94 | if (btr.SetRegion(BamTools::BamRegion(chr,chr, pos, pos + 10000))) { |
| 95 | btr.GetNextAlignment(ba); |
| 96 | bav.push_back(ba); |
| 97 | } else { |
| 98 | std::cerr << " jump to " << chr << "-" << pos << " not successful " << std::endl; |
| 99 | } |
| 100 | } |
| 101 | #endif |
| 102 | |
| 103 | #endif |
| 104 | |
| 105 | #ifdef RUN_SEQLIB |
| 106 | std::cerr << " **** RUNNING SEQLIB **** " << std::endl; |
| 107 | SeqLib::BamReader r; |
| 108 | r.Open(bam); |
| 109 | //SeqLib::BamWriter w(SeqLib::BAM); |
| 110 | //w.SetHeader(r.Header()); |
| 111 | //w.Open(obam); |
| 112 | |
| 113 | |
| 114 | SeqLib::BamRecord rec; |
| 115 | SeqLib::BamRecordVector bav; |
| 116 | std::string dummy; |
| 117 | std::stringstream ss; |
| 118 | #ifdef READ_TEST |
| 119 | std::vector<std::string> sq; |
| 120 | while(r.GetNextRecord(rec) && count++ < limit) { |
| 121 | if (count % print_limit == 0) |
| 122 | std::cerr << "...at read " << SeqLib::AddCommas(count) << std::endl; |
| 123 | bav.push_back(rec); |
| 124 | //dummy = rec.Sequence(); |
| 125 | //sq.push_back(rec.Sequence()); |
| 126 | //sq.push_back(rec.Qname()); |
| 127 | //sq.push_back(rec.CigarString()); |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | #ifdef JUMPING_TEST |
| 132 | // perform jumping test |
| 133 | for (int i = 0; i < jump_limit; ++i) { |
| 134 | int chr = rand() % 22; |
| 135 | int pos = rand() % 1000000 + 1000000; |
| 136 | r.SetRegion(SeqLib::GenomicRegion(chr,pos, pos + 10000)); |
| 137 | r.GetNextRecord(rec); |
| 138 | bav.push_back(rec); |
| 139 | } |
| 140 | #endif |
| 141 | |
| 142 | #endif |
| 143 | |
| 144 | #ifdef RUN_SEQAN |
| 145 | |
| 146 | std::cerr << " **** RUNNING SEQAN **** " << std::endl; |
| 147 | |
| 148 | seqan::BamFileIn bamFileIn; |
| 149 | seqan::BamHeader header; |
| 150 | |
| 151 | std::vector<seqan::BamAlignmentRecord> bav; |
| 152 | seqan::BamAlignmentRecord record; |
| 153 | |
| 154 | if (!open(bamFileIn, bam.c_str(), seqan::OPEN_RDONLY)) |
| 155 | { |
| 156 | std::cerr << "ERROR: could not open input file " << bam << ".\n"; |
| 157 | return 1; |
| 158 | } |
| 159 | |
| 160 | // Open output SAM file. |
| 161 | //seqan::BamFileOut samFileOut(context(bamFileIn), obam.c_str()); |
| 162 | |
| 163 | // Copy header. |
| 164 | try |
| 165 | { |
| 166 | readHeader(header, bamFileIn); |
| 167 | //writeHeader(samFileOut, header); |
| 168 | } |
| 169 | catch (seqan::IOError const & e) |
| 170 | { |
| 171 | std::cerr << "ERROR: could not copy header. " << e.what() << "\n"; |
| 172 | } |
| 173 | |
| 174 | #ifdef JUMPING_TEST |
| 175 | // read the index |
| 176 | BamIndex<Bai> baiIndex; |
| 177 | if (!open(baiIndex, bami.c_str())) |
| 178 | { |
| 179 | std::cerr << "ERROR: Could not read BAI index file " << bami << "\n"; |
| 180 | return 1; |
| 181 | } |
| 182 | |
| 183 | bool hasAlignments = false; |
| 184 | long l = 0; |
| 185 | for (int i = 0; i < jump_limit; ++i) { |
| 186 | int chr = rand() % 22; |
| 187 | int pos = rand() % 1000000 + 1000000; |
| 188 | if (!jumpToRegion(bamFileIn, hasAlignments, chr, pos, pos+10000, baiIndex)) { |
| 189 | std::cerr << "ERROR: Could not jump to " << pos << ":" << (pos+10000) << "\n"; |
| 190 | return 1; |
| 191 | } |
| 192 | if (hasAlignments) { |
| 193 | readRecord(record, bamFileIn); |
| 194 | l += getAlignmentLengthInRef(record); |
| 195 | //bav.push_back(record); |
| 196 | } else { |
| 197 | std::cerr << "no alignments here " << std::endl; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | } |
| 202 | |
| 203 | #endif |
| 204 | |
| 205 | #ifdef READ_TEST |
| 206 | // Copy all records. |
| 207 | while (!atEnd(bamFileIn) && count++ < limit) |
| 208 | { |
| 209 | try |
| 210 | { |
| 211 | if (count % print_limit == 0) |
| 212 | std::cerr << "...at read " << SeqLib::AddCommas(count) << std::endl; |
| 213 | readRecord(record, bamFileIn); |
| 214 | bav.push_back(record); |
| 215 | //writeRecord(samFileOut, record); |
| 216 | } |
| 217 | catch (seqan::IOError const & e) |
| 218 | { |
| 219 | std::cerr << "ERROR: could not copy record. " << e.what() << "\n"; |
| 220 | } |
| 221 | } |
| 222 | #endif |
| 223 | |
| 224 | #endif |
| 225 | |
| 226 | #ifdef RUN_HTSLIB |
| 227 | |
| 228 | std::cerr << " **** RUNNING HTSLIB **** " << std::endl; |
| 229 | bam1_t* b = bam_init1(); |
| 230 | htsFile* in = hts_open(bam.c_str(), "r"); |
| 231 | bam_hdr_t* header; |
| 232 | if (in == NULL) |
| 233 | return -1; |
| 234 | if (b == NULL) |
| 235 | return -1; |
| 236 | header = sam_hdr_read(in); |
| 237 | int i = 0; |
| 238 | std::vector<bam1_t*> bav; |
| 239 | while (sam_read1(in, header, b) >= 0 && count++ < limit) { |
| 240 | if (count % print_limit == 0) |
| 241 | std::cerr << "...at read " << SeqLib::AddCommas(count) << std::endl; |
| 242 | bav.push_back(b); |
| 243 | /* |
| 244 | int i; |
| 245 | const bam1_core_t* c = &b->core; |
| 246 | uint8_t *s = bam1_seq(b), *t = bam1_qual(b); |
| 247 | fwrite(bam1_qname(b), c->l_qname - 1, sizeof(char), stdout); |
| 248 | fputc('\t', stdout); |
| 249 | for (i = 0; i < c->l_qseq; ++i) |
| 250 | fputc(bam_nt16_rev_table[bam1_seqi(s, i)], stdout); |
| 251 | fputc('\t', stdout); |
| 252 | if (t[0] == 0xff) { |
| 253 | fputs("*", stdout); |
| 254 | } |
| 255 | else { |
| 256 | for (i = 0; i < c->l_qseq; ++i) |
| 257 | fputc(t[i] + 33, stdout); |
| 258 | } |
| 259 | fputc('\n', stdout); |
| 260 | */ |
| 261 | } |
| 262 | printf("%d", i); |
| 263 | bam_hdr_destroy(header); |
| 264 | hts_close(in); |
| 265 | #endif |
| 266 | |
| 267 | std::cerr << " Copied " << bav.size() << " records " << std::endl; |
| 268 | |
| 269 | return 0; |
| 270 | } |