]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/index/BamIndexFactory_p.cpp
Updated file headers (filename, license, description, etc)
[bamtools.git] / src / api / internal / index / BamIndexFactory_p.cpp
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 // ***************************************************************************
9
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;
15 using namespace std;
16
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)
21 {
22     switch ( type ) {
23         case ( BamIndex::STANDARD ) : return ( bamFilename + BamStandardIndex::Extension() );
24         case ( BamIndex::BAMTOOLS ) : return ( bamFilename + BamToolsIndex::Extension() );
25         default :
26             return string();
27     }
28 }
29
30 // creates a new BamIndex object, depending on extension of @indexFilename
31 BamIndex* BamIndexFactory::CreateIndexFromFilename(const string& indexFilename, BamReaderPrivate* reader) {
32
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() )
37         return 0;
38
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);
42     else
43         return 0;
44 }
45
46 // creates a new BamIndex, object of requested @type
47 BamIndex* BamIndexFactory::CreateIndexOfType(const BamIndex::IndexType& type,
48                                              BamReaderPrivate* reader)
49 {
50     switch ( type ) {
51         case ( BamIndex::STANDARD ) : return new BamStandardIndex(reader);
52         case ( BamIndex::BAMTOOLS ) : return new BamToolsIndex(reader);
53         default :
54             return 0;
55     }
56 }
57
58 // retrieves file extension (including '.')
59 const string BamIndexFactory::FileExtension(const string& filename) {
60
61     // if filename cannot contain valid path + extension, return empty string
62     if ( filename.empty() || filename.length() <= 4 )
63         return string();
64
65     // look for last dot in filename
66     const size_t lastDotPosition = filename.find_last_of('.');
67
68     // if none found, return empty string
69     if ( lastDotPosition == string::npos )
70         return string();
71
72     // return substring from last dot position
73     return filename.substr(lastDotPosition);
74 }
75
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)
81 {
82     // skip if BAM filename provided is empty
83     if ( bamFilename.empty() )
84         return string();
85
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() )
90         return indexFilename;
91
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() )
97             return indexFilename;
98     }
99     if ( preferredType != BamIndex::BAMTOOLS ) {
100         indexFilename = CreateIndexFilename(bamFilename, BamIndex::BAMTOOLS);
101         if ( !indexFilename.empty() )
102             return indexFilename;
103     }
104
105     // otherwise couldn't find any index matching this filename
106     return string();
107 }