]> git.donarmstrong.com Git - bamtools.git/blob - src/toolkit/bamtools_index.cpp
Minor cleanup
[bamtools.git] / src / toolkit / bamtools_index.cpp
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 // ***************************************************************************
9
10 #include "bamtools_index.h"
11
12 #include <api/BamReader.h>
13 #include <utils/bamtools_options.h>
14 using namespace BamTools;
15
16 #include <iostream>
17 #include <string>
18 using namespace std;
19
20 // ---------------------------------------------
21 // IndexSettings implementation
22
23 struct IndexTool::IndexSettings {
24
25     // flags
26     bool HasInputBamFilename;
27     bool IsUsingBamtoolsIndex;
28
29     // filenames
30     string InputBamFilename;
31     
32     // constructor
33     IndexSettings(void)
34         : HasInputBamFilename(false)
35         , IsUsingBamtoolsIndex(false)
36         , InputBamFilename(Options::StandardIn())
37     { }
38 };  
39
40 // ---------------------------------------------
41 // IndexToolPrivate implementation
42
43 struct IndexTool::IndexToolPrivate {
44
45     // ctor & dtor
46     public:
47         IndexToolPrivate(IndexTool::IndexSettings* settings)
48             : m_settings(settings)
49         { }
50
51         ~IndexToolPrivate(void) { }
52
53     // interface
54     public:
55         bool Run(void);
56
57     // data members
58     private:
59         IndexTool::IndexSettings* m_settings;
60 };
61
62 bool IndexTool::IndexToolPrivate::Run(void) {
63
64     // open our BAM reader
65     BamReader reader;
66     if ( !reader.Open(m_settings->InputBamFilename) ) {
67         cerr << "bamtools index ERROR: could not open BAM file: "
68              << m_settings->InputBamFilename << endl;
69         return false;
70     }
71
72     // create index for BAM file
73     const BamIndex::IndexType type = ( m_settings->IsUsingBamtoolsIndex ? BamIndex::BAMTOOLS
74                                                                         : BamIndex::STANDARD );
75     reader.CreateIndex(type);
76
77     // clean & exit
78     reader.Close();
79     return true;
80 }
81
82 // ---------------------------------------------
83 // IndexTool implementation
84
85 IndexTool::IndexTool(void)
86     : AbstractTool()
87     , m_settings(new IndexSettings)
88     , m_impl(0)
89 {
90     // set program details
91     Options::SetProgramInfo("bamtools index", "creates index for BAM file", "[-in <filename>] [-bti]");
92     
93     // set up options 
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);
97 }
98
99 IndexTool::~IndexTool(void) {
100
101     delete m_settings;
102     m_settings = 0;
103
104     delete m_impl;
105     m_impl = 0;
106 }
107
108 int IndexTool::Help(void) {
109     Options::DisplayHelp();
110     return 0;
111 }
112
113 int IndexTool::Run(int argc, char* argv[]) {
114   
115     // parse command line arguments
116     Options::Parse(argc, argv, 1);
117     
118     // initialize IndexTool with settings
119     m_impl = new IndexToolPrivate(m_settings);
120
121     // run IndexTool, return success/fail
122     if ( m_impl->Run() )
123         return 0;
124     else
125         return 1;
126 }