Skip to content

Commit 2729cf6

Browse files
committed
[FEATURE] add possibility to truncate subject ids on index creation
1 parent 7406a37 commit 2729cf6

3 files changed

Lines changed: 74 additions & 3 deletions

File tree

src/lambda_indexer.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,22 @@ inline int
5353
loadSubjSeqsAndIds(TCDStringSet<String<TOrigAlph>> & originalSeqs,
5454
LambdaIndexerOptions const & options)
5555
{
56-
StringSet<CharString, Owner<ConcatDirect<>>> ids;
56+
typedef TCDStringSet<String<char, Alloc<>>> TIDs;
57+
typedef TCDStringSet<String<char, Alloc<Truncate_>>> TIDsTruncated;
58+
TIDs ids;
59+
// difference only in name, same layout in mem, so we can hack the type:
60+
TIDsTruncated* tIds = static_cast<TIDsTruncated*>((void*)&ids);
5761

5862
double start = sysTime();
5963
myPrint(options, 1, "Loading Subject Sequences and Ids...");
6064

6165
SeqFileIn infile(toCString(options.dbFile));
62-
int ret = myReadRecords(ids, originalSeqs, infile);
66+
int ret;
67+
if (options.truncateIDs)
68+
ret = myReadRecords(*tIds, originalSeqs, infile);
69+
else
70+
ret = myReadRecords(ids, originalSeqs, infile);
71+
6372
if (ret)
6473
return ret;
6574

src/misc.hpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,54 @@ localAlignment2(Gaps<TSource0, TGapsSpec0> & row0,
180180
return score;
181181
}
182182

183+
// ----------------------------------------------------------------------------
184+
// Tag Truncate_ (private tag to signify truncating of IDs while reading)
185+
// ----------------------------------------------------------------------------
186+
187+
struct Truncate_;
188+
189+
// ----------------------------------------------------------------------------
190+
// Function readRecord(Fasta); an overload that truncates Ids at first Whitespace
191+
// ----------------------------------------------------------------------------
192+
193+
template <typename TSeqStringSet, typename TSpec, typename TSize>
194+
inline void
195+
readRecords(TCDStringSet<String<char, Alloc<Truncate_>>> & meta,
196+
TSeqStringSet & seq,
197+
FormattedFile<Fastq, Input, TSpec> & file,
198+
TSize maxRecords)
199+
{
200+
typedef typename SeqFileBuffer_<TSeqStringSet, TSpec>::Type TSeqBuffer;
201+
202+
TSeqBuffer seqBuffer;
203+
IsWhitespace func;
204+
205+
// reuse the memory of context(file).buffer for seqBuffer (which has a different type but same sizeof(Alphabet))
206+
swapPtr(seqBuffer.data_begin, context(file).buffer[1].data_begin);
207+
swapPtr(seqBuffer.data_end, context(file).buffer[1].data_end);
208+
seqBuffer.data_capacity = context(file).buffer[1].data_capacity;
209+
210+
for (; !atEnd(file) && maxRecords > 0; --maxRecords)
211+
{
212+
readRecord(context(file).buffer[0], seqBuffer, file);
213+
for (size_t i = 0; i < length(context(file).buffer[0]); ++i)
214+
{
215+
if (func(context(file).buffer[0][i]))
216+
{
217+
resize(context(file).buffer[0], i);
218+
break;
219+
}
220+
}
221+
appendValue(meta, context(file).buffer[0]);
222+
appendValue(seq, seqBuffer);
223+
}
224+
225+
swapPtr(seqBuffer.data_begin, context(file).buffer[1].data_begin);
226+
swapPtr(seqBuffer.data_end, context(file).buffer[1].data_end);
227+
context(file).buffer[1].data_capacity = seqBuffer.data_capacity;
228+
seqBuffer.data_capacity = 0;
229+
}
230+
183231
// ----------------------------------------------------------------------------
184232
// Generic Sequence loading
185233
// ----------------------------------------------------------------------------

src/options.hpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ struct LambdaIndexerOptions : public SharedOptions
277277
std::string segFile = "";
278278
std::string algo = "";
279279

280+
bool truncateIDs;
281+
280282
LambdaIndexerOptions()
281283
: SharedOptions()
282284
{}
@@ -986,13 +988,21 @@ parseCommandLine(LambdaIndexerOptions & options, int argc, char const ** argv)
986988
// setValidValues(parser, "output", "sa fm");
987989

988990
addOption(parser, ArgParseOption("di", "db-index-type",
989-
"suffix array or full-text minute space.",
991+
"Suffix array or full-text minute space.",
990992
ArgParseArgument::STRING,
991993
"type"));
992994
setValidValues(parser, "db-index-type", "sa fm");
993995
setDefaultValue(parser, "db-index-type", "fm");
994996
setAdvanced(parser, "db-index-type");
995997

998+
addOption(parser, ArgParseOption("", "truncate-ids",
999+
"Truncate IDs at first whitespace. This saves a lot of space and is irrelevant for all LAMBDA output formats "
1000+
"other than BLAST Pairwise (.m0).",
1001+
ArgParseArgument::STRING,
1002+
"STR"));
1003+
setValidValues(parser, "truncate-ids", "on off");
1004+
setDefaultValue(parser, "truncate-ids", "off");
1005+
9961006
addSection(parser, "Alphabets and Translation");
9971007
addOption(parser, ArgParseOption("p", "program",
9981008
"Blast Operation Mode.",
@@ -1111,6 +1121,10 @@ parseCommandLine(LambdaIndexerOptions & options, int argc, char const ** argv)
11111121
getOptionValue(tmpdir, parser, "tmp-dir");
11121122
setEnv("TMPDIR", tmpdir);
11131123

1124+
std::string buffer;
1125+
getOptionValue(buffer, parser, "truncate-ids");
1126+
options.truncateIDs = (buffer == "on");
1127+
11141128
return ArgumentParser::PARSE_OK;
11151129
}
11161130

0 commit comments

Comments
 (0)