Skip to content

Commit ea3a895

Browse files
committed
[API] change index option format; validate in search mode
1 parent 384165c commit ea3a895

4 files changed

Lines changed: 92 additions & 23 deletions

File tree

src/lambda.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,17 @@ realMain(LambdaOptions & options,
330330
"\n======================================================"
331331
"\nVersion ", SEQAN_APP_VERSION, "\n\n");
332332

333+
int ret = validateIndexOptions<TRedAlph, p>(options);
334+
if (ret)
335+
return ret;
336+
333337
if (options.verbosity >= 2)
334338
printOptions<TLocalHolder>(options);
335339

336340
TGlobalHolder globalHolder;
337341
// context(globalHolder.outfile).scoringScheme._internalScheme = matr;
338342

339-
int ret = prepareScoring(globalHolder, options);
343+
ret = prepareScoring(globalHolder, options);
340344
if (ret)
341345
return ret;
342346

src/lambda.hpp

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,81 @@ struct Comp :
9494
// Functions
9595
// ============================================================================
9696

97+
// --------------------------------------------------------------------------
98+
// Function readIndexOption()
99+
// --------------------------------------------------------------------------
100+
101+
inline void
102+
readIndexOption(std::string & optionString,
103+
std::string const & optionIdentifier,
104+
LambdaOptions const & options)
105+
{
106+
std::ifstream f{(options.indexDir + "/option:" + optionIdentifier).c_str(),
107+
std::ios_base::in | std::ios_base::binary};
108+
if (f.is_open())
109+
{
110+
auto fit = directionIterator(f, Input());
111+
readLine(optionString, fit);
112+
f.close();
113+
}
114+
else
115+
{
116+
throw std::runtime_error("ERROR: Expected option specifier:\n" + options.indexDir + "/option:" +
117+
optionIdentifier + "\nYour index seems incompatible, try to recreate it "
118+
"and report a bug if the issue persists.");
119+
}
120+
}
121+
122+
// --------------------------------------------------------------------------
123+
// Function validateIndexOptions()
124+
// --------------------------------------------------------------------------
125+
126+
template <typename TRedAlph,
127+
BlastProgram p>
128+
inline int
129+
validateIndexOptions(LambdaOptions const & options)
130+
{
131+
std::string buffer;
132+
readIndexOption(buffer, "alph_translated", options);
133+
if (buffer != _alphName(TransAlph<p>()))
134+
{
135+
std::cerr << "ERROR: Your index is of translated alphabet type: " << buffer << "\n But lambda expected: "
136+
<< _alphName(TransAlph<p>()) << "\n Did you specify the right -p parameter?\n\n";
137+
return -1;
138+
139+
}
140+
buffer.clear();
141+
readIndexOption(buffer, "alph_reduced", options);
142+
if (buffer != _alphName(TRedAlph()))
143+
{
144+
std::cerr << "ERROR: Your index is of reduced alphabet type: " << buffer << "\n But lambda expected: "
145+
<< _alphName(TRedAlph()) << "\n Did you specify the right -ar parameter?\n\n";
146+
return -1;
147+
}
148+
buffer.clear();
149+
readIndexOption(buffer, "db_index_type", options);
150+
unsigned long b = 0;
151+
if ((!lexicalCast(b, buffer)) || (b != static_cast<unsigned long>(options.dbIndexType)))
152+
{
153+
std::cerr << "ERROR: Your index type is: " << _indexName(static_cast<DbIndexType>(std::stoul(buffer)))
154+
<< "\n But lambda expected: " << _indexName(options.dbIndexType)
155+
<< "\n Did you specify the right -di parameter?\n\n";
156+
return -1;
157+
}
158+
if (qIsTranslated(p) && sIsTranslated(p))
159+
{
160+
buffer.clear();
161+
readIndexOption(buffer, "genetic_code", options);
162+
unsigned long b = 0;
163+
if ((!lexicalCast(b, buffer)) || (b != static_cast<unsigned long>(options.geneticCode)))
164+
{
165+
std::cerr << "WARNING: The codon translation table used during indexing and during search are different. "
166+
"This is not a problem per se, but is likely not what you want.\n\n";
167+
}
168+
}
169+
return 0;
170+
}
171+
97172
// --------------------------------------------------------------------------
98173
// Function prepareScoring()
99174
// --------------------------------------------------------------------------
@@ -281,18 +356,6 @@ loadDbIndexFromDisk(TGlobalHolder & globalHolder,
281356
std::string path = toCString(options.indexDir);
282357
path += "/index";
283358

284-
// Check if the index is of the old format (pre 0.9.0) by looking for different files
285-
// if ((TGlobalHolder::blastProgram != BlastProgram::BLASTN) && // BLASTN indexes are compatible
286-
// ((TGlobalHolder::alphReduction && fileExists(toCString(path + ".txt.concat"))) ||
287-
// (!TGlobalHolder::alphReduction && TGlobalHolder::indexIsFM && !fileExists(toCString(path + ".lf.drv.wtc.24")))))
288-
// {
289-
// std::cerr << ((options.verbosity == 0) ? strIdent : std::string())
290-
// << " failed.\n"
291-
// << "It appears you tried to open an old index (created before 0.9.0) which "
292-
// << "is not supported. Please remove the old files and create a new index with lambda_indexer!\n";
293-
// return 200;
294-
// }
295-
296359
int ret = open(globalHolder.dbIndex, path.c_str(), OPEN_RDONLY);
297360
if (ret != true)
298361
{

src/lambda_indexer.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,18 @@ realMain(LambdaIndexerOptions const & options,
213213
}
214214

215215
// dump options
216-
for (auto && s : std::initializer_list<std::string>
217-
{
218-
{ options.indexDir + "/option:db_index_type:" + std::string(_indexName(options.dbIndexType)) },
219-
{ options.indexDir + "/option:alph_original:" + std::string(_alphName(OrigSubjAlph<p>())) },
220-
{ options.indexDir + "/option:alph_translated:"+ std::string(_alphName(TransAlph<p>())) },
221-
{ options.indexDir + "/option:alph_reduced:" + std::string(_alphName(TRedAlph())) },
222-
{ options.indexDir + "/option:genetic_code:" + std::to_string(options.geneticCode) }
223-
})
216+
for (auto && s : std::initializer_list<std::pair<std::string, std::string>>
217+
{
218+
{ options.indexDir + "/option:db_index_type", std::to_string(static_cast<uint32_t>(options.dbIndexType))},
219+
{ options.indexDir + "/option:alph_original", std::string(_alphName(OrigSubjAlph<p>())) },
220+
{ options.indexDir + "/option:alph_translated", std::string(_alphName(TransAlph<p>())) },
221+
{ options.indexDir + "/option:alph_reduced", std::string(_alphName(TRedAlph())) },
222+
{ options.indexDir + "/option:genetic_code", std::to_string(options.geneticCode) }
223+
})
224224
{
225-
open(s.c_str(), O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666);
225+
std::ofstream f{std::get<0>(s).c_str(), std::ios_base::out | std::ios_base::binary};
226+
f << std::get<1>(s);
227+
f.close();
226228
}
227229

228230
return 0;

src/options.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,7 @@ _alphName(Dna5 const & /**/)
14401440
// Function _indexName()
14411441
// --------------------------------------------------------------------------
14421442

1443-
constexpr const char *
1443+
inline std::string
14441444
_indexName(DbIndexType const t)
14451445
{
14461446
switch (t)

0 commit comments

Comments
 (0)