]> git.donarmstrong.com Git - bamtools.git/blob - src/toolkit/bamtools_index.cpp
b1e4bc3e00eaf6fd359b35fc081d6147b4ecb0ae
[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: 2 September 2010
7 // ---------------------------------------------------------------------------
8 // Creates a BAM index (".bai") file for the provided BAM file.
9 // ***************************************************************************
10
11 #include <iostream>
12 #include <string>
13
14 #include "bamtools_index.h"
15 #include "bamtools_options.h"
16 #include "BamReader.h"
17
18 using namespace std;
19 using namespace BamTools;
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 // IndexTool implementation
43
44 IndexTool::IndexTool(void)
45     : AbstractTool()
46     , m_settings(new IndexSettings)
47 {
48     // set program details
49     Options::SetProgramInfo("bamtools index", "creates index for BAM file", "[-in <filename>] [-bti]");
50     
51     // set up options 
52     OptionGroup* IO_Opts = Options::CreateOptionGroup("Input & Output");
53     Options::AddValueOption("-in", "BAM filename", "the input BAM file", "", m_settings->HasInputBamFilename, m_settings->InputBamFilename, IO_Opts, Options::StandardIn());
54     Options::AddOption("-bti", "create (non-standard) BamTools index file", m_settings->IsUsingBamtoolsIndex, IO_Opts);
55 }
56
57 IndexTool::~IndexTool(void) {
58     delete m_settings;
59     m_settings = 0;
60 }
61
62 int IndexTool::Help(void) {
63     Options::DisplayHelp();
64     return 0;
65 }
66
67 int IndexTool::Run(int argc, char* argv[]) {
68   
69     // parse command line arguments
70     Options::Parse(argc, argv, 1);
71     
72     // open our BAM reader
73     BamReader reader;
74     reader.Open(m_settings->InputBamFilename);
75     
76     // create index for BAM file
77     bool useDefaultIndex = !m_settings->IsUsingBamtoolsIndex;
78     reader.CreateIndex(useDefaultIndex);
79     
80     // clean & exit
81     reader.Close();
82     return 0;
83 }