]> git.donarmstrong.com Git - bamtools.git/blobdiff - src/toolkit/bamtools_index.cpp
Bug fix: error in index file creation when sorted BAM has empty
[bamtools.git] / src / toolkit / bamtools_index.cpp
index 41dd5c71f095004ec2312672d055292d808f0179..ef0e76519e70e7de36926a774a450e549463e85b 100644 (file)
@@ -1,23 +1,22 @@
 // ***************************************************************************
 // bamtools_index.cpp (c) 2010 Derek Barnett, Erik Garrison
 // Marth Lab, Department of Biology, Boston College
-// All rights reserved.
 // ---------------------------------------------------------------------------
-// Last modified: 7 July 2010
+// Last modified: 7 April 2011
 // ---------------------------------------------------------------------------
-// Creates a BAM index (".bai") file for the provided BAM file.
+// Creates a BAM index file
 // ***************************************************************************
 
-#include <iostream>
-#include <string>
-
 #include "bamtools_index.h"
-#include "bamtools_options.h"
-#include "BamReader.h"
 
-using namespace std;
+#include <api/BamReader.h>
+#include <utils/bamtools_options.h>
 using namespace BamTools;
 
+#include <iostream>
+#include <string>
+using namespace std;
+
 // ---------------------------------------------
 // IndexSettings implementation
 
@@ -38,12 +37,55 @@ struct IndexTool::IndexSettings {
     { }
 };  
 
+// ---------------------------------------------
+// IndexToolPrivate implementation
+
+struct IndexTool::IndexToolPrivate {
+
+    // ctor & dtor
+    public:
+        IndexToolPrivate(IndexTool::IndexSettings* settings)
+            : m_settings(settings)
+        { }
+
+        ~IndexToolPrivate(void) { }
+
+    // interface
+    public:
+        bool Run(void);
+
+    // data members
+    private:
+        IndexTool::IndexSettings* m_settings;
+};
+
+bool IndexTool::IndexToolPrivate::Run(void) {
+
+    // open our BAM reader
+    BamReader reader;
+    if ( !reader.Open(m_settings->InputBamFilename) ) {
+        cerr << "bamtools index ERROR: could not open BAM file: "
+             << m_settings->InputBamFilename << endl;
+        return false;
+    }
+
+    // create index for BAM file
+    const BamIndex::IndexType type = ( m_settings->IsUsingBamtoolsIndex ? BamIndex::BAMTOOLS
+                                                                        : BamIndex::STANDARD );
+    reader.CreateIndex(type);
+
+    // clean & exit
+    reader.Close();
+    return true;
+}
+
 // ---------------------------------------------
 // IndexTool implementation
 
 IndexTool::IndexTool(void)
     : AbstractTool()
     , m_settings(new IndexSettings)
+    , m_impl(0)
 {
     // set program details
     Options::SetProgramInfo("bamtools index", "creates index for BAM file", "[-in <filename>] [-bti]");
@@ -51,12 +93,16 @@ IndexTool::IndexTool(void)
     // set up options 
     OptionGroup* IO_Opts = Options::CreateOptionGroup("Input & Output");
     Options::AddValueOption("-in", "BAM filename", "the input BAM file", "", m_settings->HasInputBamFilename, m_settings->InputBamFilename, IO_Opts, Options::StandardIn());
-    Options::AddOption("-bti", "use (non-standard) BamTools indexing scheme", m_settings->IsUsingBamtoolsIndex, IO_Opts);
+    Options::AddOption("-bti", "create (non-standard) BamTools index file (*.bti). Default behavior is to create standard BAM index (*.bai)", m_settings->IsUsingBamtoolsIndex, IO_Opts);
 }
 
 IndexTool::~IndexTool(void) {
+
     delete m_settings;
     m_settings = 0;
+
+    delete m_impl;
+    m_impl = 0;
 }
 
 int IndexTool::Help(void) {
@@ -69,15 +115,12 @@ int IndexTool::Run(int argc, char* argv[]) {
     // parse command line arguments
     Options::Parse(argc, argv, 1);
     
-    // open our BAM reader
-    BamReader reader;
-    reader.Open(m_settings->InputBamFilename);
-    
-    // create index for BAM file
-    bool useDefaultIndex = !m_settings->IsUsingBamtoolsIndex;
-    reader.CreateIndex(useDefaultIndex);
-    
-    // clean & exit
-    reader.Close();
-    return 0;
+    // initialize IndexTool with settings
+    m_impl = new IndexToolPrivate(m_settings);
+
+    // run IndexTool, return success/fail
+    if ( m_impl->Run() )
+        return 0;
+    else
+        return 1;
 }