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