| 1 | [](https://travis-ci.org/walaj/SeqLib) |
| 2 | [](https://coveralls.io/github/walaj/SeqLib?branch=master) |
| 3 | |
| 4 | C++ interface to HTSlib, BWA-MEM and Fermi |
| 5 | |
| 6 | **License:** [Apache2][license] |
| 7 | |
| 8 | API Documentation |
| 9 | ----------------- |
| 10 | [API Documentation][htmldoc] |
| 11 | |
| 12 | Citation |
| 13 | -------- |
| 14 | If you use SeqLib in your applications, please cite: http://bioinformatics.oxfordjournals.org/content/early/2016/12/21/bioinformatics.btw741.full.pdf+html |
| 15 | |
| 16 | Note that the values for the SeqAn benchmarking in Table 2 should be corrected to 7.7 Gb memory and 33.92 seconds in CPU time, when compiling SeqAn with ``-O3 -DNDEBUG``. SeqAn also does full string decompression. |
| 17 | Wall times for SeqAn may be shorter than CPU time because it uses embedded multi-threading during BAM IO. |
| 18 | |
| 19 | Table of contents |
| 20 | ================= |
| 21 | |
| 22 | * [Installation](#installation) |
| 23 | * [Integrating into build system](#integrating-into-build-system) |
| 24 | * [Description](#description) |
| 25 | * [Memory management](#memory-management) |
| 26 | * [Other C++ APIs](#other-c++-apis) |
| 27 | * [Command line usage](#command-line-usage) |
| 28 | * [Examples](#examples) |
| 29 | * [Attributions](#attributions) |
| 30 | |
| 31 | Installation |
| 32 | ------------ |
| 33 | |
| 34 | ####### |
| 35 | ```bash |
| 36 | git clone --recursive https://github.com/walaj/SeqLib.git |
| 37 | cd SeqLib |
| 38 | ## cd htslib && ./configure --enable-libcurl && cd .. # support for remote (FTP/HTTPS/Google etc) BAM access |
| 39 | ./configure ## or: ./configure LDFLAGS='-lcurl -lcrypto' # for remote support |
| 40 | make |
| 41 | make install |
| 42 | make seqtools ## for the command line version |
| 43 | ``` |
| 44 | |
| 45 | You will need to have JsonCPP (v1.9.4 or higher) installed. |
| 46 | |
| 47 | I have successfully compiled with GCC-4.5+ and Clang on Linux and OSX. |
| 48 | |
| 49 | SeqLib is compatible with c++11 and later. |
| 50 | |
| 51 | Integrating into build system |
| 52 | ----------------------------- |
| 53 | |
| 54 | After building, you will need to add the relevant header directories: |
| 55 | ```bash |
| 56 | SEQ=<path_to_seqlib_git_repos> |
| 57 | C_INCLUDE_PATH=$C_INCLUDE_PATH:$SEQ:$SEQ/htslib |
| 58 | ``` |
| 59 | |
| 60 | And need to link the SeqLib static library and Fermi, BWA and HTSlib libraries |
| 61 | ```bash |
| 62 | SEQ=<path_to_seqlib> |
| 63 | LDFLAGS="$LDFLAGS $SEQ/bin/libseqlib.a $SEQ/bin/libbwa.a $SEQ/bin/libfml.a $SEQ/bin/libhts.a" |
| 64 | ``` |
| 65 | |
| 66 | To add support for reading BAMs, etc with HTTPS, FTP, S3, Google cloud, etc, you must compile and link with libcurl. |
| 67 | ```bash |
| 68 | ## set hts to build with libcurl links and hfile_libcurl.c |
| 69 | cd SeqLib/htslib |
| 70 | ./configure --enable-libcurl |
| 71 | ## compile seqlib with libcurl support |
| 72 | cd ../ # back to SeqLib main directory |
| 73 | ./configure LDFLAGS="-lcurl -lcrypto" |
| 74 | make |
| 75 | make install |
| 76 | ``` |
| 77 | Remember then to then link any projects made with SeqLib with the additional ``-lcurl -lcrypto`` flags. |
| 78 | |
| 79 | Description |
| 80 | ----------- |
| 81 | |
| 82 | SeqLib is a C++ library for querying BAM/SAM/CRAM files, performing |
| 83 | BWA-MEM operations in memory, and performing sequence assembly. Core operations |
| 84 | in SeqLib are peformed by: |
| 85 | * [HTSlib][htslib] |
| 86 | * [BWA-MEM][BWA] (Apache2 branch) |
| 87 | * [FermiKit][fermi] |
| 88 | |
| 89 | The primary developer for these three projects is Heng Li. |
| 90 | |
| 91 | SeqLib also has support for storing and manipulating genomic intervals via ``GenomicRegion`` and ``GenomicRegionCollection``. |
| 92 | It uses an [interval tree][int] (provided by Erik Garrison @ekg) to provide for rapid interval queries. |
| 93 | |
| 94 | SeqLib is built to be extendable. See [VariantBam][var] for examples of how to take advantage of C++ |
| 95 | class extensions to build off of the SeqLib base functionality. |
| 96 | |
| 97 | Memory management |
| 98 | ----------------- |
| 99 | SeqLib is built to automatically handle memory management of C code from BWA-MEM and HTSlib by using C++ smart |
| 100 | pointers that handle freeing memory automatically. One of the |
| 101 | main motivations behind SeqLib is that all access to sequencing reads, BWA, etc should |
| 102 | completely avoid ``malloc`` and ``free``. In SeqLib all the mallocs/frees are handled automatically in the constructors and |
| 103 | destructors. |
| 104 | |
| 105 | Other C++ APIs |
| 106 | ------------------------------ |
| 107 | There are overlaps between this project and [BamTools][BT] from Derek Barnett, [Gamgee][gam] |
| 108 | from the Broad Institute, and [SeqAn][seqan] from Freie Universitat Berlin. These projects |
| 109 | provide excellent and high quality APIs. SeqLib provides further performance enhancement and new capabilites for certain classes of |
| 110 | bioinformatics problems. |
| 111 | |
| 112 | Some differences: |
| 113 | * SeqLib has ~2-4x faster read/write speed over BamTools and lower memory footprint. |
| 114 | * SeqLib has support for CRAM file |
| 115 | * SeqLib provides in memory access to BWA-MEM, BLAT, chromosome aware interval tree, read correction, and sequence assembly with Fermi. |
| 116 | * SeqAn provide a substantial amount of additional capabilites not in SeqLib, including graph operations and an expanded suite of multi-sequence alignments. |
| 117 | * SeqAn embeds multi-threading into some functionality like BAM IO to improve wall times. |
| 118 | |
| 119 | For your particular application, our hope is that SeqLib will provide a comprehensive and powerful envrionment to develop |
| 120 | bioinformatics tools, or to be used in conjuction with the capablities in SeqAn and BamTools. Feature requests and comments are welcomed. |
| 121 | |
| 122 | Command Line Usage |
| 123 | ------------------ |
| 124 | ```bash |
| 125 | ## BFC correction (input mode -m is b (BAM/SAM/CRAM), output mode -w is SAM stream |
| 126 | samtools view in.bam -h 1:1,000,000-1,002,000 | seqtools bfc - -G $REF | samtools sort - -m 4g -o corrected.bam |
| 127 | |
| 128 | ## Without a pipe, write to BAM |
| 129 | seqtools bfc in.bam -G $REF -b > corrected.bam |
| 130 | |
| 131 | ## Skip realignment, send to fasta |
| 132 | seqtools bfc in.bam -f > corrected.fasta |
| 133 | |
| 134 | ## Input as gzipped or plain fasta (or fastq), send to aligned BAM |
| 135 | seqtools bfc --infasta in.fasta -G $REG -b > corrected.bam |
| 136 | |
| 137 | |
| 138 | ##### ASSEMBLY (same patterns as above) |
| 139 | samtools view in.bam -h 1:1,000,000-1,002,000 | seqtools fml - -G $REF | samtools sort - -m 4g -o assembled.bam |
| 140 | |
| 141 | ``` |
| 142 | |
| 143 | Examples |
| 144 | -------- |
| 145 | ##### Targeted re-alignment of reads to a given region with BWA-MEM |
| 146 | ``` |
| 147 | #include "SeqLib/RefGenome.h" |
| 148 | #include "SeqLib/BWAWrapper.h" |
| 149 | using namespace SeqLib; |
| 150 | RefGenome ref; |
| 151 | ref.LoadIndex("hg19.fasta"); |
| 152 | |
| 153 | // get sequence at given locus |
| 154 | std::string seq = ref.QueryRegion("1", 1000000,1001000); |
| 155 | |
| 156 | // Make an in-memory BWA-MEM index of region |
| 157 | BWAWrapper bwa; |
| 158 | UnalignedSequenceVector usv = {{"chr_reg1", seq}}; |
| 159 | bwa.ConstructIndex(usv); |
| 160 | |
| 161 | // align an example string with BWA-MEM |
| 162 | std::string querySeq = "CAGCCTCACCCAGGAAAGCAGCTGGGGGTCCACTGGGCTCAGGGAAG"; |
| 163 | BamRecordVector results; |
| 164 | // hardclip=false, secondary score cutoff=0.9, max secondary alignments=10 |
| 165 | bwa.AlignSequence(querySeq, "my_seq", results, false, 0.9, 10); |
| 166 | |
| 167 | // print results to stdout |
| 168 | for (auto& i : results) |
| 169 | std::cout << i << std::endl; |
| 170 | ``` |
| 171 | |
| 172 | ##### Read a BAM line by line, realign reads with BWA-MEM, write to new BAM |
| 173 | ``` |
| 174 | #include "SeqLib/BamReader.h" |
| 175 | #include "SeqLib/BWAWrapper.h" |
| 176 | using namespace SeqLib; |
| 177 | |
| 178 | // open the reader BAM/SAM/CRAM |
| 179 | BamReader bw; |
| 180 | bw.Open("test.bam"); |
| 181 | |
| 182 | // open a new interface to BWA-MEM |
| 183 | BWAWrapper bwa; |
| 184 | bwa.LoadIndex("hg19.fasta"); |
| 185 | |
| 186 | // open the output BAM |
| 187 | BamWriter writer; // or writer(SeqLib::SAM) or writer(SeqLib::CRAM) |
| 188 | writer.SetWriteHeader(bwa.HeaderFromIndex()); |
| 189 | writer.Open("out.bam"); |
| 190 | writer.WriteHeader(); |
| 191 | |
| 192 | BamRecord r; |
| 193 | bool hardclip = false; |
| 194 | float secondary_cutoff = 0.90; // secondary alignments must have score >= 0.9*top_score |
| 195 | int secondary_cap = 10; // max number of secondary alignments to return |
| 196 | while (GetNextRecord(r)) { |
| 197 | BamRecordVector results; // alignment results (can have multiple alignments) |
| 198 | bwa.AlignSequence(r.Sequence(), r.Qname(), results, hardclip, secondary_cutoff, secondary_cap); |
| 199 | |
| 200 | for (auto& i : results) |
| 201 | writer.WriteRecord(i); |
| 202 | } |
| 203 | ``` |
| 204 | |
| 205 | |
| 206 | ##### Perform sequence assembly with Fermi directly from a BAM |
| 207 | ``` |
| 208 | |
| 209 | #include "SeqLib/FermiAssembler.h" |
| 210 | using namespace SeqLib; |
| 211 | |
| 212 | FermiAssembler f; |
| 213 | |
| 214 | // read in data from a BAM |
| 215 | BamReader br; |
| 216 | br.Open("test_data/small.bam"); |
| 217 | |
| 218 | // retreive sequencing reads (up to 20,000) |
| 219 | BamRecord r; |
| 220 | BamRecordVector brv; |
| 221 | size_t count = 0; |
| 222 | while(br.GetNextRead(r) && count++ < 20000) |
| 223 | brv.push_back(r); |
| 224 | |
| 225 | // add the reads and error correct them |
| 226 | f.AddReads(brv); |
| 227 | f.CorrectReads(); |
| 228 | |
| 229 | // peform the assembly |
| 230 | f.PerformAssembly(); |
| 231 | |
| 232 | // retrieve the contigs |
| 233 | std::vector<std::string> contigs = f.GetContigs(); |
| 234 | |
| 235 | // write as a fasta to stdout |
| 236 | for (size_t i = 0; i < contigs.size(); ++i) |
| 237 | std::cout << ">contig" << i << std::endl << contigs[i] << std::endl; |
| 238 | ``` |
| 239 | |
| 240 | ##### Plot a collection of gapped alignments |
| 241 | ``` |
| 242 | using namespace SeqLib; |
| 243 | BamReader r; |
| 244 | r.Open("test_data/small.bam"); |
| 245 | |
| 246 | GenomicRegion gr("X:1,002,942-1,003,294", r.Header()); |
| 247 | r.SetRegion(gr); |
| 248 | |
| 249 | SeqPlot s; |
| 250 | s.SetView(gr); |
| 251 | |
| 252 | BamRecord rec; |
| 253 | BamRecordVector brv; |
| 254 | while(r.GetNextRecord(rec)) |
| 255 | if (!rec.CountNBases() && rec.MappedFlag()) |
| 256 | brv.push_back(rec); |
| 257 | s.SetPadding(20); |
| 258 | |
| 259 | std::cout << s.PlotAlignmentRecords(brv); |
| 260 | ``` |
| 261 | |
| 262 | Trimmed output from above (NA12878): |
| 263 | ``` |
| 264 | CTATCTATCTATCTCTTCTTCTGTCCGTTCATGTGTCTGTCCATCTATCTATCCATCTATCTATCATCTAACTATCTGTCCATCCATCCATCCATCCA |
| 265 | CTATCTATCTATCTCTTCTTCTGTCCGTTCATGTGTCTGTCCATCTATCTATCCATCTAT CATCCATCCATCCATCCATCCACCCATTCATCCATCCACCTATCCATCTATCAATCCATCCATCCATCCA |
| 266 | TATCTATCTATCTCTTCTTCTGTCCGTTCATGTGTCTGTCCATCTATCTATCCATCTATCTATCATCTAACTATCTG----TCCATCCATCCATCCATCCACCCA |
| 267 | TATCTATCTATCTCTTCTTCTGTCCGTTCATGTGTCTGTCCATCTATCTATCCATCTATCTATCATCTAACTATCTGTCCATCCATCCATCCATC |
| 268 | TATCTATCTATCTCTTCTTCTGTCCGTTCATGTGTCTGTCCATCTATCTATCCATCTATCTATCATCTAACTATCTG----TCCATCCATCCATCCATCCACCCA |
| 269 | ATCTATCTATCTCTTCTTCTGTCCGTTCATGTGTCTGTCCATCTATCTATCCATCTATCTATCATCTAACTATCTG----TCCATCCATCCATCCATCCACCCAT |
| 270 | TCTATCTATCTCTTCTTCTGTCCGCTCATGTGTCTGTCCATCTATCTATC GTCCATCCATCCATCCATCCATCCATCCACCCATTCATCCATCCACCTATCCATCTATCAATCCATCCATCCATCCATCCGTCTATCTTATGCATCACAGC |
| 271 | TCTATCTATCTCTTCTTCTGTCCGTTCATGTGTCTGTCCATCTATCTATCCATCTATCTATCATCTAACTATCTG----TCCATCCATCCATCCATCCACCCATT |
| 272 | CTATCTATCTCTTCTTCTGTCCGTTCATGTGTCTGTCCATCTATCTATCCATCTATCTATCATCTAACTATCTG----TCCATCCATCCATCCATCCACCCATTC |
| 273 | CTATCTATCTCTTCTTCTGTCCGTTCATGTGTCTGTCCATCTATCTATCCATCTATCTATCATCTAACTATCTG----TCCATCCATCCATCCATCCACCCATTC |
| 274 | ATCTATCTCTTCTTCTGTCCGTTCATGTGTCTGTCCATCTATCTATCCATCTATCTATCATCTAACTATCTG----TCCATCCATCCATCCATCCACCCATTCAT |
| 275 | ATCTATCTCTTCTTCTGTCCGTTCATGTGTCTGTCCATCTATCTATCCATCTATCTATCATCTAACTATCTG----TCCATCCATCCATCCATCCACCCATTCAT |
| 276 | ``` |
| 277 | |
| 278 | ##### Read simultaneously from a BAM, CRAM and SAM file and send to stdout |
| 279 | ``` |
| 280 | using namespace SeqLib; |
| 281 | #include "SeqLib/BamReader.h" |
| 282 | BamReader r; |
| 283 | |
| 284 | // read from multiple streams coordinate-sorted order |
| 285 | r.Open("test_data/small.bam"); |
| 286 | r.Open("test_data/small.cram"); |
| 287 | r.Open("test_data/small.sam"); |
| 288 | |
| 289 | BamWriter w(SeqLib::SAM); // set uncompressed output |
| 290 | w.Open("-"); // write to stdout |
| 291 | w.SetHeader(r.Header()); // specify the header |
| 292 | w.WriteHeader(); // write out the header |
| 293 | |
| 294 | BamRecord rec; |
| 295 | while(r.GetNextRecord(rec)) |
| 296 | w.WriteRecord(rec); |
| 297 | w.Close(); // Optional. Will close on destruction |
| 298 | ``` |
| 299 | |
| 300 | ##### Perform error correction on reads, using [BFC][bfc] |
| 301 | ``` |
| 302 | #include "SeqLib/BFC.h" |
| 303 | using namespace SeqLib; |
| 304 | |
| 305 | // brv is some set of reads to train the error corrector |
| 306 | for (BamRecordVector::const_iterator r = brv.begin(); r != brv.end(); ++r) |
| 307 | b.AddSequence(r->Sequence().c_str(), r->Qualities().c_str(), r->Qname().c_str()); |
| 308 | b.Train(); |
| 309 | b.clear(); // clear the training sequences. Training parameters saved in BFC object |
| 310 | |
| 311 | // brv2 is some set to correct |
| 312 | for (BamRecordVector::const_iterator r = brv2.begin(); r != brv2.end(); ++r) |
| 313 | b.AddSequence(r->Sequence().c_str(), r->Qualities().c_str(), r->Qname().c_str()); |
| 314 | b.ErrorCorrect(); |
| 315 | |
| 316 | // retrieve the sequences |
| 317 | UnalignedSequenceVector v; |
| 318 | std::string name, seq; |
| 319 | while (b.GetSequences(seq, name)) |
| 320 | v.push_back({name, seq}); |
| 321 | |
| 322 | ``` |
| 323 | |
| 324 | Support |
| 325 | ------- |
| 326 | This project is being actively developed and maintained by Jeremiah Wala ([email protected]). |
| 327 | |
| 328 | Attributions |
| 329 | ------------ |
| 330 | We would like to thank Heng Li (htslib/bwa/fermi), Erik Garrison (interval tree), Christopher Gilbert (aho corasick), |
| 331 | and Mengyao Zhao (sw alignment), for providing open-source and robust bioinformatics solutions. |
| 332 | |
| 333 | Development, support, guidance, testing: |
| 334 | * Steve Huang - Research Scientist, Broad Institute |
| 335 | * Steve Schumacher - Computational Biologist, Dana Farber Cancer Institute |
| 336 | * Cheng-Zhong Zhang - Research Scientist, Broad Institute |
| 337 | * Marcin Imielinski - Assistant Professor, Cornell University |
| 338 | * Rameen Beroukhim - Assistant Professor, Harvard Medical School |
| 339 | |
| 340 | [htslib]: https://github.com/samtools/htslib.git |
| 341 | |
| 342 | [SGA]: https://github.com/jts/sga |
| 343 | |
| 344 | [BLAT]: https://genome.ucsc.edu/cgi-bin/hgBlat?command=start |
| 345 | |
| 346 | [BWA]: https://github.com/lh3/bwa |
| 347 | |
| 348 | [license]: https://github.com/walaj/SeqLib/blob/master/LICENSE |
| 349 | |
| 350 | [BamTools]: https://raw.githubusercontent.com/wiki/pezmaster31/bamtools/Tutorial_Toolkit_BamTools-1.0.pdf |
| 351 | |
| 352 | [API]: http://pezmaster31.github.io/bamtools/annotated.html |
| 353 | |
| 354 | [htmldoc]: http://walaj.github.io/seqlibdocs/doxygen |
| 355 | |
| 356 | [var]: https://github.com/walaj/VariantBam |
| 357 | |
| 358 | [BT]: https://github.com/pezmaster31/bamtools |
| 359 | |
| 360 | [seqan]: https://www.seqan.de |
| 361 | |
| 362 | [gam]: https://github.com/broadinstitute/gamgee |
| 363 | |
| 364 | [int]: https://github.com/ekg/intervaltree.git |
| 365 | |
| 366 | [fermi]: https://github.com/lh3/fermi-lite |
| 367 | |
| 368 | [bfc]: https://github.com/lh3/bfc |