]> git.donarmstrong.com Git - bamtools.git/blobdiff - src/api/internal/BamIndexFactory_p.cpp
Cleaned up intra-API includes & moved version numbers to 2.0.0
[bamtools.git] / src / api / internal / BamIndexFactory_p.cpp
index dcc11cebc25bcb1237d79731604857ebaf744a6e..2cf871f9df8ec07650136e4178b7cf3f113b9114 100644 (file)
@@ -1,21 +1,18 @@
 // ***************************************************************************
 // BamIndexFactory_p.cpp (c) 2011 Derek Barnett
 // Marth Lab, Department of Biology, Boston College
-// All rights reserved.
 // ---------------------------------------------------------------------------
-// Last modified: 21 March 2011 (DB)
+// Last modified: 10 October 2011 (DB)
 // ---------------------------------------------------------------------------
 // Provides interface for generating BamIndex implementations
 // ***************************************************************************
 
-#include <api/BamAux.h>
-#include <api/internal/BamIndexFactory_p.h>
-#include <api/internal/BamStandardIndex_p.h>
-#include <api/internal/BamToolsIndex_p.h>
+#include "api/BamAux.h"
+#include "api/internal/BamIndexFactory_p.h"
+#include "api/internal/BamStandardIndex_p.h"
+#include "api/internal/BamToolsIndex_p.h"
 using namespace BamTools;
 using namespace BamTools::Internal;
-
-#include <cstdio>
 using namespace std;
 
 // generates index filename from BAM filename (depending on requested type)
@@ -24,16 +21,15 @@ const string BamIndexFactory::CreateIndexFilename(const string& bamFilename,
                                                   const BamIndex::IndexType& type)
 {
     switch ( type ) {
-        case ( BamIndex::STANDARD ) : return ( bamFilename + BAI_EXTENSION );
-        case ( BamIndex::BAMTOOLS ) : return ( bamFilename + BTI_EXTENSION );
+        case ( BamIndex::STANDARD ) : return ( bamFilename + BamStandardIndex::Extension() );
+        case ( BamIndex::BAMTOOLS ) : return ( bamFilename + BamToolsIndex::Extension() );
         default :
-            fprintf(stderr, "BamIndexFactory ERROR: unknown index type %u\n", type);
             return string();
     }
 }
 
 // creates a new BamIndex object, depending on extension of @indexFilename
-BamIndex* BamIndexFactory::CreateIndexFromFilename(const string& indexFilename) {
+BamIndex* BamIndexFactory::CreateIndexFromFilename(const string& indexFilename, BamReaderPrivate* reader) {
 
     // if file doesn't exist, return null index
     if ( !BamTools::FileExists(indexFilename) )
@@ -46,18 +42,20 @@ BamIndex* BamIndexFactory::CreateIndexFromFilename(const string& indexFilename)
         return 0;
 
     // create index based on extension
-    if      ( extension == BAI_EXTENSION ) return new BamStandardIndex;
-    else if ( extension == BTI_EXTENSION ) return new BamToolsIndex;
-    else                                   return 0;
+    if      ( extension == BamStandardIndex::Extension() ) return new BamStandardIndex(reader);
+    else if ( extension == BamToolsIndex::Extension()    ) return new BamToolsIndex(reader);
+    else
+        return 0;
 }
 
 // creates a new BamIndex, object of requested @type
-BamIndex* BamIndexFactory::CreateIndexOfType(const BamIndex::IndexType& type) {
+BamIndex* BamIndexFactory::CreateIndexOfType(const BamIndex::IndexType& type,
+                                             BamReaderPrivate* reader)
+{
     switch ( type ) {
-        case ( BamIndex::STANDARD ) : return new BamStandardIndex;
-        case ( BamIndex::BAMTOOLS ) : return new BamToolsIndex;
+        case ( BamIndex::STANDARD ) : return new BamStandardIndex(reader);
+        case ( BamIndex::BAMTOOLS ) : return new BamToolsIndex(reader);
         default :
-            fprintf(stderr, "BamIndexFactory ERROR: unknown index type %u\n", type);
             return 0;
     }
 }
@@ -70,7 +68,7 @@ const string BamIndexFactory::FileExtension(const string& filename) {
         return string();
 
     // look for last dot in filename
-    size_t lastDotPosition = filename.find_last_of('.');
+    const size_t lastDotPosition = filename.find_last_of('.');
 
     // if none found, return empty string
     if ( lastDotPosition == string::npos )
@@ -86,6 +84,10 @@ const string BamIndexFactory::FileExtension(const string& filename) {
 const string BamIndexFactory::FindIndexFilename(const string& bamFilename,
                                                 const BamIndex::IndexType& preferredType)
 {
+    // skip if BAM filename provided is empty
+    if ( bamFilename.empty() )
+        return string();
+
     // try to find index of preferred type first
     // return index filename if found
     string indexFilename = CreateIndexFilename(bamFilename, preferredType);