1 // ***************************************************************************
2 // bamtools_index.cpp (c) 2010 Derek Barnett, Erik Garrison
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 7 April 2011
7 // ---------------------------------------------------------------------------
8 // Creates a BAM index file
9 // ***************************************************************************
11 #include "bamtools_index.h"
13 #include <api/BamReader.h>
14 #include <utils/bamtools_options.h>
15 using namespace BamTools;
21 // ---------------------------------------------
22 // IndexSettings implementation
24 struct IndexTool::IndexSettings {
27 bool HasInputBamFilename;
28 bool IsUsingBamtoolsIndex;
31 string InputBamFilename;
35 : HasInputBamFilename(false)
36 , IsUsingBamtoolsIndex(false)
37 , InputBamFilename(Options::StandardIn())
41 // ---------------------------------------------
42 // IndexToolPrivate implementation
44 struct IndexTool::IndexToolPrivate {
48 IndexToolPrivate(IndexTool::IndexSettings* settings)
49 : m_settings(settings)
52 ~IndexToolPrivate(void) { }
60 IndexTool::IndexSettings* m_settings;
63 bool IndexTool::IndexToolPrivate::Run(void) {
65 // open our BAM reader
67 if ( !reader.Open(m_settings->InputBamFilename) ) {
68 cerr << "bamtools index ERROR: could not open BAM file: "
69 << m_settings->InputBamFilename << endl;
73 // create index for BAM file
74 const BamIndex::IndexType type = ( m_settings->IsUsingBamtoolsIndex ? BamIndex::BAMTOOLS
75 : BamIndex::STANDARD );
76 reader.CreateIndex(type);
83 // ---------------------------------------------
84 // IndexTool implementation
86 IndexTool::IndexTool(void)
88 , m_settings(new IndexSettings)
91 // set program details
92 Options::SetProgramInfo("bamtools index", "creates index for BAM file", "[-in <filename>] [-bti]");
95 OptionGroup* IO_Opts = Options::CreateOptionGroup("Input & Output");
96 Options::AddValueOption("-in", "BAM filename", "the input BAM file", "", m_settings->HasInputBamFilename, m_settings->InputBamFilename, IO_Opts, Options::StandardIn());
97 Options::AddOption("-bti", "create (non-standard) BamTools index file (*.bti). Default behavior is to create standard BAM index (*.bai)", m_settings->IsUsingBamtoolsIndex, IO_Opts);
100 IndexTool::~IndexTool(void) {
109 int IndexTool::Help(void) {
110 Options::DisplayHelp();
114 int IndexTool::Run(int argc, char* argv[]) {
116 // parse command line arguments
117 Options::Parse(argc, argv, 1);
119 // initialize IndexTool with settings
120 m_impl = new IndexToolPrivate(m_settings);
122 // run IndexTool, return success/fail