1 // ***************************************************************************
2 // bamtools_index.cpp (c) 2010 Derek Barnett, Erik Garrison
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 7 April 2011
6 // ---------------------------------------------------------------------------
7 // Creates a BAM index file
8 // ***************************************************************************
10 #include "bamtools_index.h"
12 #include <api/BamReader.h>
13 #include <utils/bamtools_options.h>
14 using namespace BamTools;
20 // ---------------------------------------------
21 // IndexSettings implementation
23 struct IndexTool::IndexSettings {
26 bool HasInputBamFilename;
27 bool IsUsingBamtoolsIndex;
30 string InputBamFilename;
34 : HasInputBamFilename(false)
35 , IsUsingBamtoolsIndex(false)
36 , InputBamFilename(Options::StandardIn())
40 // ---------------------------------------------
41 // IndexToolPrivate implementation
43 struct IndexTool::IndexToolPrivate {
47 IndexToolPrivate(IndexTool::IndexSettings* settings)
48 : m_settings(settings)
51 ~IndexToolPrivate(void) { }
59 IndexTool::IndexSettings* m_settings;
62 bool IndexTool::IndexToolPrivate::Run(void) {
64 // open our BAM reader
66 if ( !reader.Open(m_settings->InputBamFilename) ) {
67 cerr << "bamtools index ERROR: could not open BAM file: "
68 << m_settings->InputBamFilename << endl;
72 // create index for BAM file
73 const BamIndex::IndexType type = ( m_settings->IsUsingBamtoolsIndex ? BamIndex::BAMTOOLS
74 : BamIndex::STANDARD );
75 reader.CreateIndex(type);
82 // ---------------------------------------------
83 // IndexTool implementation
85 IndexTool::IndexTool(void)
87 , m_settings(new IndexSettings)
90 // set program details
91 Options::SetProgramInfo("bamtools index", "creates index for BAM file", "[-in <filename>] [-bti]");
94 OptionGroup* IO_Opts = Options::CreateOptionGroup("Input & Output");
95 Options::AddValueOption("-in", "BAM filename", "the input BAM file", "", m_settings->HasInputBamFilename, m_settings->InputBamFilename, IO_Opts, Options::StandardIn());
96 Options::AddOption("-bti", "create (non-standard) BamTools index file (*.bti). Default behavior is to create standard BAM index (*.bai)", m_settings->IsUsingBamtoolsIndex, IO_Opts);
99 IndexTool::~IndexTool(void) {
108 int IndexTool::Help(void) {
109 Options::DisplayHelp();
113 int IndexTool::Run(int argc, char* argv[]) {
115 // parse command line arguments
116 Options::Parse(argc, argv, 1);
118 // initialize IndexTool with settings
119 m_impl = new IndexToolPrivate(m_settings);
121 // run IndexTool, return success/fail