1 // ***************************************************************************
2 // BamIndexFactory_p.cpp (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 10 November 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides interface for generating BamIndex implementations
8 // ***************************************************************************
10 #include "api/internal/index/BamIndexFactory_p.h"
11 #include "api/internal/index/BamStandardIndex_p.h"
12 #include "api/internal/index/BamToolsIndex_p.h"
13 using namespace BamTools;
14 using namespace BamTools::Internal;
17 // generates index filename from BAM filename (depending on requested type)
18 // if type is unknown, returns empty string
19 const string BamIndexFactory::CreateIndexFilename(const string& bamFilename,
20 const BamIndex::IndexType& type)
23 case ( BamIndex::STANDARD ) : return ( bamFilename + BamStandardIndex::Extension() );
24 case ( BamIndex::BAMTOOLS ) : return ( bamFilename + BamToolsIndex::Extension() );
30 // creates a new BamIndex object, depending on extension of @indexFilename
31 BamIndex* BamIndexFactory::CreateIndexFromFilename(const string& indexFilename, BamReaderPrivate* reader) {
33 // get file extension from index filename, including dot (".EXT")
34 // if can't get file extension, return null index
35 const string extension = FileExtension(indexFilename);
36 if ( extension.empty() )
39 // create index based on extension
40 if ( extension == BamStandardIndex::Extension() ) return new BamStandardIndex(reader);
41 else if ( extension == BamToolsIndex::Extension() ) return new BamToolsIndex(reader);
46 // creates a new BamIndex, object of requested @type
47 BamIndex* BamIndexFactory::CreateIndexOfType(const BamIndex::IndexType& type,
48 BamReaderPrivate* reader)
51 case ( BamIndex::STANDARD ) : return new BamStandardIndex(reader);
52 case ( BamIndex::BAMTOOLS ) : return new BamToolsIndex(reader);
58 // retrieves file extension (including '.')
59 const string BamIndexFactory::FileExtension(const string& filename) {
61 // if filename cannot contain valid path + extension, return empty string
62 if ( filename.empty() || filename.length() <= 4 )
65 // look for last dot in filename
66 const size_t lastDotPosition = filename.find_last_of('.');
68 // if none found, return empty string
69 if ( lastDotPosition == string::npos )
72 // return substring from last dot position
73 return filename.substr(lastDotPosition);
76 // returns name of existing index file that corresponds to @bamFilename
77 // will defer to @preferredType if possible, if not will attempt to load any supported type
78 // returns empty string if not found
79 const string BamIndexFactory::FindIndexFilename(const string& bamFilename,
80 const BamIndex::IndexType& preferredType)
82 // skip if BAM filename provided is empty
83 if ( bamFilename.empty() )
86 // try to find index of preferred type first
87 // return index filename if found
88 string indexFilename = CreateIndexFilename(bamFilename, preferredType);
89 if ( !indexFilename.empty() )
92 // couldn't find preferred type, try the other supported types
93 // return index filename if found
94 if ( preferredType != BamIndex::STANDARD ) {
95 indexFilename = CreateIndexFilename(bamFilename, BamIndex::STANDARD);
96 if ( !indexFilename.empty() )
99 if ( preferredType != BamIndex::BAMTOOLS ) {
100 indexFilename = CreateIndexFilename(bamFilename, BamIndex::BAMTOOLS);
101 if ( !indexFilename.empty() )
102 return indexFilename;
105 // otherwise couldn't find any index matching this filename