10 #include <sys/types.h>
74 :
CFeatures(orig), num_vectors(orig.num_vectors),
75 single_string(orig.single_string),
76 length_of_single_string(orig.length_of_single_string),
77 max_string_length(orig.max_string_length),
78 num_symbols(orig.num_symbols),
79 original_num_symbols(orig.original_num_symbols),
80 order(orig.order), preprocess_on_get(false),
107 for (int32_t i=0; i<256; i++)
117 features(NULL), single_string(NULL), length_of_single_string(0),
118 max_string_length(0), order(0),
119 preprocess_on_get(false), feature_cache(NULL)
139 remove_all_subsets();
143 SG_FREE(single_string);
147 cleanup_feature_vectors(0, num_vectors-1);
161 SG_FREE(symbol_mask_table);
163 symbol_mask_table=NULL;
177 ASSERT(num<get_num_vectors())
181 int32_t real_num=m_subset_stack->subset_idx_conversion(num);
182 SG_FREE(features[real_num].
string);
183 features[real_num].string=NULL;
184 features[real_num].slen=0;
186 determine_maximum_string_length();
192 if (features && get_num_vectors())
194 ASSERT(start<get_num_vectors())
195 ASSERT(stop<get_num_vectors())
197 for (int32_t i=start; i<=stop; i++)
199 int32_t real_num=m_subset_stack->subset_idx_conversion(i);
200 SG_FREE(features[real_num].
string);
201 features[real_num].string=NULL;
202 features[real_num].slen=0;
204 determine_maximum_string_length();
226 if (num>=get_num_vectors())
228 SG_ERROR(
"Index out of bounds (number of strings %d, you "
229 "requested %d)\n", get_num_vectors(), num);
234 ST* vec=get_feature_vector(num, l, free_vec);
235 ST* dst=SG_MALLOC(ST, l);
236 memcpy(dst, vec, l*
sizeof(ST));
237 free_feature_vector(vec, num, free_vec);
245 if (m_subset_stack->has_subsets())
246 SG_ERROR(
"A subset is set, cannot set feature vector\n")
248 if (num>=num_vectors)
250 SG_ERROR(
"Index out of bounds (number of strings %d, you "
251 "requested %d)\n", num_vectors, num);
255 SG_ERROR(
"String has zero or negative length\n")
257 cleanup_feature_vector(num);
258 features[num].slen=vector.
vlen;
259 features[num].string=SG_MALLOC(ST, vector.
vlen);
260 memcpy(features[num].
string, vector.
vector, vector.
vlen*
sizeof(ST));
262 determine_maximum_string_length();
267 preprocess_on_get=
true;
272 preprocess_on_get=
false;
278 if (num>=get_num_vectors())
279 SG_ERROR(
"Requested feature vector with index %d while total num is", num, get_num_vectors())
281 int32_t real_num=m_subset_stack->subset_idx_conversion(num);
283 if (!preprocess_on_get)
286 len=features[real_num].slen;
287 return features[real_num].string;
291 SG_DEBUG(
"computing feature vector!\n")
292 ST* feat=compute_feature_vector(num, len);
295 if (get_num_preprocessors())
297 ST* tmp_feat_before=feat;
299 for (int32_t i=0; i<get_num_preprocessors(); i++)
304 SG_FREE(tmp_feat_before);
305 tmp_feat_before=feat;
328 num_feat=get_num_vectors();
329 num_vec=get_max_vector_length();
330 ASSERT(have_same_length())
332 SG_DEBUG(
"Allocating memory for transposed string features of size %ld\n",
333 int64_t(num_feat)*num_vec);
337 for (int32_t i=0; i<num_vec; i++)
339 sf[i].
string=SG_MALLOC(ST, num_feat);
343 for (int32_t i=0; i<num_feat; i++)
347 ST* vec=get_feature_vector(i, len, free_vec);
349 for (int32_t j=0; j<num_vec; j++)
350 sf[j].
string[i]=vec[j];
352 free_feature_vector(vec, i, free_vec);
359 if (num>=get_num_vectors())
362 "Trying to access string[%d] but num_str=%d\n", num,
366 int32_t real_num=m_subset_stack->subset_idx_conversion(num);
369 feature_cache->unlock_entry(real_num);
377 if (num>=get_num_vectors())
380 "Trying to access string[%d] but num_str=%d\n", num,
384 int32_t real_num=m_subset_stack->subset_idx_conversion(num);
387 feature_cache->unlock_entry(real_num);
392 ASSERT(vec_num<get_num_vectors())
396 ST* vec=get_feature_vector(vec_num, len, free_vec);
398 ST result=vec[feat_num];
399 free_feature_vector(vec, vec_num, free_vec);
406 ASSERT(vec_num<get_num_vectors())
410 ST* vec=get_feature_vector(vec_num, len, free_vec);
411 free_feature_vector(vec, vec_num, free_vec);
417 return max_string_length;
422 return m_subset_stack->has_subsets() ? m_subset_stack->get_size() : num_vectors;
436 return symbol_mask_table[mask] & symbol;
442 return (offset << (amount*alphabet->get_num_bits()));
448 return (symbol >> (amount*alphabet->get_num_bits()));
454 remove_all_subsets();
456 size_t blocksize=1024*1024;
457 size_t required_blocksize=0;
458 uint8_t* dummy=SG_MALLOC(uint8_t, blocksize);
459 uint8_t* overflow=NULL;
460 int32_t overflow_len=0;
467 FILE* f=fopen(fname,
"ro");
474 SG_INFO(
"counting line numbers in file %s\n", fname)
476 size_t old_block_offs=0;
477 fseek(f, 0, SEEK_END);
478 size_t fsize=ftell(f);
484 SG_DEBUG(
"block_size=%ld file_size=%ld\n", blocksize, fsize)
487 while (sz == blocksize)
489 sz=fread(dummy,
sizeof(uint8_t), blocksize, f);
490 for (
size_t i=0; i<sz; i++)
493 if (dummy[i]==
'\n' || (i==sz-1 && sz<blocksize))
496 required_blocksize=
CMath::max(required_blocksize, block_offs-old_block_offs);
497 old_block_offs=block_offs;
500 SG_PROGRESS(block_offs, 0, fsize, 1,
"COUNTING:\t")
503 SG_INFO(
"found %d strings\n", num_vectors)
505 blocksize=required_blocksize;
506 dummy=SG_MALLOC(uint8_t, blocksize);
507 overflow=SG_MALLOC(uint8_t, blocksize);
513 while (sz == blocksize)
515 sz=fread(dummy,
sizeof(uint8_t), blocksize, f);
518 for (
size_t i=0; i<sz; i++)
520 if (dummy[i]==
'\n' || (i==sz-1 && sz<blocksize))
522 int32_t len=i-old_sz;
524 max_string_length=
CMath::max(max_string_length, len+overflow_len);
526 features[lines].slen=len;
527 features[lines].string=SG_MALLOC(ST, len);
531 for (int32_t j=0; j<overflow_len; j++)
532 features[lines].
string[j]=alpha->
remap_to_bin(overflow[j]);
533 for (int32_t j=0; j<len; j++)
534 features[lines].
string[j+overflow_len]=alpha->
remap_to_bin(dummy[old_sz+j]);
540 for (int32_t j=0; j<overflow_len; j++)
541 features[lines].
string[j]=overflow[j];
542 for (int32_t j=0; j<len; j++)
543 features[lines].
string[j+overflow_len]=dummy[old_sz+j];
554 SG_PROGRESS(lines, 0, num_vectors, 1,
"LOADING:\t")
557 for (
size_t i=old_sz; i<sz; i++)
558 overflow[i-old_sz]=dummy[i];
560 overflow_len=sz-old_sz;
565 SG_INFO(
"file successfully read\n")
566 SG_INFO(
"max_string_length=%d\n", max_string_length)
567 SG_INFO(
"num_strings=%d\n", num_vectors)
588 num_symbols=alphabet->get_num_symbols();
593 remove_all_subsets();
609 if (len>0 && s[0]==
'>')
614 SG_ERROR(
"No fasta hunks (lines starting with '>') found\n")
619 num_symbols=alphabet->get_num_symbols();
632 int32_t spanned_lines=0;
637 SG_ERROR(
"Error reading fasta entry in line %d len=%ld", 4*i+1, len)
639 if (s[0]==
'>' || offs==f.
get_size())
648 len=fasta_len-spanned_lines;
649 strings[i].
string=SG_MALLOC(ST, len);
652 ST* str=strings[i].
string;
654 SG_DEBUG(
"'%.*s', len=%d, spanned_lines=%d\n", (int32_t) id_len,
id, (int32_t) len, (int32_t) spanned_lines)
656 for (int32_t j=0; j<fasta_len; j++)
663 if (ignore_invalid && !alphabet->is_valid((uint8_t) fasta[j]))
666 if (uint64_t(idx)>=len)
667 SG_ERROR(
"idx=%d j=%d fasta_len=%d, spanned_lines=%d str='%.*s'\n", idx, j, fasta_len, spanned_lines, idx, str)
681 return set_features(strings, num, max_len);
685 bool ignore_invalid,
bool bitremap_in_single_string)
687 remove_all_subsets();
699 SG_ERROR(
"Number of lines must be divisible by 4 in fastq files\n")
709 if (bitremap_in_single_string)
712 strings[0].
string=SG_MALLOC(ST, num);
719 original_num_symbols=alphabet->get_num_symbols();
720 str=SG_MALLOC(ST, len);
728 SG_ERROR(
"Error reading 'read' identifier in line %d", 4*i)
732 SG_ERROR(
"Error reading 'read' in line %d len=%ld", 4*i+1, len)
734 if (bitremap_in_single_string)
736 if (len!=(uint64_t) order)
737 SG_ERROR(
"read in line %d not of length %d (is %d)\n", 4*i+1, order, len)
738 for (int32_t j=0; j<order; j++)
739 str[j]=(ST) alphabet->remap_to_bin((uint8_t) s[j]);
741 strings[0].
string[i]=embed_word(str, order);
745 strings[i].
string=SG_MALLOC(ST, len);
751 for (uint64_t j=0; j<len; j++)
753 if (alphabet->is_valid((uint8_t) s[j]))
761 for (uint64_t j=0; j<len; j++)
769 SG_ERROR(
"Error reading 'read' quality identifier in line %d", 4*i+2)
772 SG_ERROR(
"Error reading 'read' quality in line %d", 4*i+3)
775 if (bitremap_in_single_string)
779 max_string_length=max_len;
787 remove_all_subsets();
789 struct dirent **namelist;
796 n=scandir(dirname, &namelist, &
SGIO::filter, alphasort);
799 SG_ERROR(
"error calling scandir - no files found\n")
813 for (int32_t i=0; i<n; i++)
820 if (!stat(fname, &s) && s.st_size>0)
822 filesize=s.st_size/
sizeof(ST);
824 FILE* f=fopen(fname,
"ro");
827 ST* str=SG_MALLOC(ST, filesize);
828 SG_DEBUG(
"%s:%ld\n", fname, (int64_t) filesize)
829 if (fread(str,
sizeof(ST), filesize, f)!=(
size_t) filesize)
832 strings[num].
slen=filesize;
833 max_len=
CMath::max(max_len, strings[num].slen);
840 SG_ERROR(
"empty or non readable file \'%s\'\n", fname)
842 SG_FREE(namelist[i]);
846 if (num>0 && strings)
848 set_features(strings, num, max_len);
862 if (m_subset_stack->has_subsets())
863 SG_ERROR(
"Cannot call set_features() with subset.\n")
870 for (int32_t i=0; i<p_num_vectors; i++)
886 memcpy(features,p_features,
sizeof(
SGString<ST>)*p_num_vectors);
887 num_vectors = p_num_vectors;
888 max_string_length = p_max_string_length;
903 if (m_subset_stack->has_subsets())
904 SG_ERROR(
"Cannot call set_features() with subset.\n")
909 for (int32_t i=0; i<sf_num_str; i++)
913 new_features[i].
string=SG_MALLOC(ST, length);
914 memcpy(new_features[i].
string, sf->
features[real_i].
string, length);
915 new_features[i].
slen=length;
917 return append_features(new_features, sf_num_str,
923 if (m_subset_stack->has_subsets())
924 SG_ERROR(
"Cannot call set_features() with subset.\n")
927 return set_features(p_features, p_num_vectors, p_max_string_length);
932 for (int32_t i=0; i<p_num_vectors; i++)
941 for (int32_t i=0; i<p_num_vectors; i++)
942 alphabet->add_string_to_histogram( p_features[i].
string, p_features[i].
slen);
944 int32_t old_num_vectors=num_vectors;
945 num_vectors=old_num_vectors+p_num_vectors;
948 for (int32_t i=0; i<num_vectors; i++)
950 if (i<old_num_vectors)
952 new_features[i].
string=features[i].string;
953 new_features[i].
slen=features[i].slen;
957 new_features[i].
string=p_features[i-old_num_vectors].
string;
958 new_features[i].
slen=p_features[i-old_num_vectors].
slen;
964 this->features=new_features;
965 max_string_length=
CMath::max(max_string_length, p_max_string_length);
984 if (m_subset_stack->has_subsets())
985 SG_ERROR(
"get features() is not possible on subset")
988 max_str_len=max_string_length;
996 num_str=get_num_vectors();
997 max_str_len=max_string_length;
1000 for (int32_t i=0; i<num_str; i++)
1004 ST* vec=get_feature_vector(i, len, free_vec);
1005 new_feat[i].
string=SG_MALLOC(ST, len);
1006 new_feat[i].
slen=len;
1007 memcpy(new_feat[i].
string, vec, ((
size_t) len) *
sizeof(ST));
1008 free_feature_vector(vec, i, free_vec);
1017 int32_t max_str_len;
1018 *dst=copy_features(num_vec, max_str_len);
1024 remove_all_subsets();
1028 if (!(file=fopen(src,
"r")))
1034 if (fread(&
id[0],
sizeof(
char), 1, file)!=1)
1037 if (fread(&
id[1],
sizeof(
char), 1, file)!=1)
1040 if (fread(&
id[2],
sizeof(
char), 1, file)!=1)
1043 if (fread(&
id[3],
sizeof(
char), 1, file)!=1)
1049 if (fread(&c,
sizeof(uint8_t), 1, file)!=1)
1050 SG_ERROR(
"failed to read compression type")
1055 if (fread(&a,
sizeof(uint8_t), 1, file)!=1)
1056 SG_ERROR(
"failed to read compression alphabet")
1059 if (fread(&num_vectors,
sizeof(int32_t), 1, file)!=1)
1060 SG_ERROR(
"failed to read compression number of vectors")
1063 if (fread(&max_string_length,
sizeof(int32_t), 1, file)!=1)
1064 SG_ERROR(
"failed to read maximum string length")
1065 ASSERT(max_string_length>0)
1070 for (int32_t i=0; i<num_vectors; i++)
1073 int32_t len_compressed;
1074 if (fread(&len_compressed,
sizeof(int32_t), 1, file)!=1)
1075 SG_ERROR(
"failed to read vector length compressed")
1077 int32_t len_uncompressed;
1078 if (fread(&len_uncompressed,
sizeof(int32_t), 1, file)!=1)
1079 SG_ERROR(
"failed to read vector length uncompressed")
1084 features[i].string=SG_MALLOC(ST, len_uncompressed);
1085 features[i].slen=len_uncompressed;
1086 uint8_t* compressed=SG_MALLOC(uint8_t, len_compressed);
1087 if (fread(compressed,
sizeof(uint8_t), len_compressed, file)!=(
size_t) len_compressed)
1088 SG_ERROR(
"failed to read compressed data (expected %d bytes)", len_compressed)
1089 uint64_t uncompressed_size=len_uncompressed;
1090 uncompressed_size*=
sizeof(ST);
1091 compressor->
decompress(compressed, len_compressed,
1092 (uint8_t*) features[i].
string, uncompressed_size);
1093 SG_FREE(compressed);
1094 ASSERT(uncompressed_size==((uint64_t) len_uncompressed)*
sizeof(ST))
1098 int32_t offs=
CMath::ceil(2.0*
sizeof(int32_t)/
sizeof(ST));
1099 features[i].string=SG_MALLOC(ST, len_compressed+offs);
1100 features[i].slen=len_compressed+offs;
1101 int32_t* feat32ptr=((int32_t*) (features[i].
string));
1102 memset(features[i].
string, 0, offs*
sizeof(ST));
1103 feat32ptr[0]=(int32_t) len_compressed;
1104 feat32ptr[1]=(int32_t) len_uncompressed;
1105 uint8_t* compressed=(uint8_t*) (&features[i].
string[offs]);
1106 if (fread(compressed, 1, len_compressed, file)!=(size_t) len_compressed)
1107 SG_ERROR(
"failed to read uncompressed data")
1119 if (m_subset_stack->has_subsets())
1120 SG_ERROR(
"save_compressed() is not possible on subset")
1124 if (!(file=fopen(dest,
"wb")))
1130 const char*
id=
"SGV0";
1131 fwrite(&
id[0],
sizeof(
char), 1, file);
1132 fwrite(&
id[1],
sizeof(
char), 1, file);
1133 fwrite(&
id[2],
sizeof(
char), 1, file);
1134 fwrite(&
id[3],
sizeof(
char), 1, file);
1137 uint8_t c=(uint8_t) compression;
1138 fwrite(&c,
sizeof(uint8_t), 1, file);
1140 uint8_t a=(uint8_t) alphabet->get_alphabet();
1141 fwrite(&a,
sizeof(uint8_t), 1, file);
1143 fwrite(&num_vectors,
sizeof(int32_t), 1, file);
1145 fwrite(&max_string_length,
sizeof(int32_t), 1, file);
1148 for (int32_t i=0; i<num_vectors; i++)
1152 ST* vec=get_feature_vector(i, len, vfree);
1154 uint8_t* compressed=NULL;
1155 uint64_t compressed_size=0;
1157 compressor->
compress((uint8_t*) vec, ((uint64_t) len)*
sizeof(ST),
1158 compressed, compressed_size, level);
1160 int32_t len_compressed=(int32_t) compressed_size;
1162 fwrite(&len_compressed,
sizeof(int32_t), 1, file);
1164 fwrite(&len,
sizeof(int32_t), 1, file);
1166 fwrite(compressed, compressed_size, 1, file);
1167 SG_FREE(compressed);
1169 free_feature_vector(vec, i, vfree);
1179 SG_DEBUG(
"force: %d\n", force_preprocessing)
1181 for (int32_t i=0; i<get_num_preprocessors(); i++)
1183 if ( (!is_preprocessed(i) || force_preprocessing) )
1185 set_preprocessed(i);
1203 if (m_subset_stack->has_subsets())
1208 ASSERT(num_vectors==1 || single_string)
1209 ASSERT(max_string_length>=window_size ||
1210 (single_string && length_of_single_string>=window_size));
1215 num_vectors= (length_of_single_string-window_size)/step_size + 1;
1216 else if (num_vectors==1)
1218 num_vectors= (max_string_length-window_size)/step_size + 1;
1219 length_of_single_string=max_string_length;
1224 for (int32_t i=0; i<num_vectors; i++)
1226 f[i].
string=&features[0].string[offs+skip];
1227 f[i].
slen=window_size-skip;
1230 single_string=features[0].string;
1233 max_string_length=window_size-skip;
1241 if (m_subset_stack->has_subsets())
1246 ASSERT(num_vectors==1 || single_string)
1247 ASSERT(max_string_length>=window_size ||
1248 (single_string && length_of_single_string>=window_size));
1258 len=length_of_single_string;
1261 single_string=features[0].string;
1262 len=max_string_length;
1263 length_of_single_string=max_string_length;
1267 for (int32_t i=0; i<num_vectors; i++)
1271 if (p>=0 && p<=len-window_size)
1273 f[i].
string=&features[0].string[p+skip];
1274 f[i].
slen=window_size-skip;
1279 max_string_length=len;
1280 features[0].slen=len;
1283 SG_ERROR(
"window (size:%d) starting at position[%d]=%d does not fit in sequence(len:%d)\n",
1284 window_size, i, p, len);
1291 max_string_length=window_size-skip;
1298 return obtain_from_char_features(sf, start, p_order, gap, rev);
1305 if (len!=max_string_length)
1308 len=max_string_length;
1310 index_t num_str=get_num_vectors();
1311 for (int32_t i=0; i<num_str; i++)
1313 if (get_vector_length(i)!=len)
1322 if (m_subset_stack->has_subsets())
1325 ASSERT(alphabet->get_num_symbols_in_histogram() > 0)
1328 original_num_symbols=alphabet->get_num_symbols();
1329 int32_t max_val=alphabet->get_num_bits();
1334 num_symbols=original_num_symbols;
1336 SG_INFO(
"max_val (bit): %d order: %d -> results in num_symbols: %.0Lf\n", max_val, p_order, num_symbols)
1339 SG_WARNING(
"symbols did not fit into datatype \"%c\" (%d)\n", (
char) max_val, (
int) max_val)
1342 for (int32_t i=0; i<p_order*max_val; i++)
1343 mask= (mask<<1) | ((ST) 1);
1345 for (int32_t i=0; i<num_vectors; i++)
1347 int32_t len=features[i].slen;
1350 SG_ERROR(
"Sequence must be longer than order (%d vs. %d)\n", len, p_order)
1352 ST* str=features[i].string;
1355 for (int32_t j=0; j<p_order; j++)
1356 str[j]=(ST) alphabet->remap_to_bin(str[j]);
1357 str[0]=embed_word(&str[0], p_order);
1361 for (int32_t j=p_order; j<len; j++)
1363 str[j]=(ST) alphabet->remap_to_bin(str[j]);
1364 str[idx+1]= ((str[idx]<<max_val) | str[j]) & mask;
1368 features[i].slen=len-p_order+1;
1371 compute_symbol_mask_table(max_val);
1376 if (m_subset_stack->has_subsets())
1379 SG_FREE(symbol_mask_table);
1380 symbol_mask_table=SG_MALLOC(ST, 256);
1381 symbol_mask_table_len=256;
1384 for (int32_t i=0; i< (int64_t) max_val; i++)
1387 for (int32_t i=0; i<256; i++)
1389 uint8_t bits=(uint8_t) i;
1390 symbol_mask_table[i]=0;
1392 for (int32_t j=0; j<8; j++)
1395 symbol_mask_table[i]|=mask<<(max_val*j);
1404 uint32_t nbits= (uint32_t) alphabet->get_num_bits();
1407 for (uint32_t i=0; i<nbits; i++)
1408 mask=(mask<<1) | (ST) 1;
1410 for (int32_t i=0; i<len; i++)
1413 seq[len-i-1]=alphabet->remap_to_char((uint8_t) w);
1421 uint32_t nbits= (uint32_t) alphabet->get_num_bits();
1422 for (int32_t i=0; i<len; i++)
1433 max_string_length=0;
1434 index_t num_str=get_num_vectors();
1436 for (int32_t i=0; i<num_str; i++)
1438 max_string_length=
CMath::max(max_string_length,
1439 features[m_subset_stack->subset_idx_conversion(i)].slen);
1446 ST* s=SG_MALLOC(ST, l+1);
1447 memcpy(s, str.
string,
sizeof(ST)*l);
1455 ASSERT(num<get_num_vectors())
1457 int32_t real_num=m_subset_stack->subset_idx_conversion(num);
1460 features[real_num].slen=len ;
1461 features[real_num].string=string ;
1463 max_string_length=
CMath::max(len, max_string_length);
1468 int32_t nsym=get_num_symbols();
1469 int32_t slen=get_max_vector_length();
1470 int64_t sz=int64_t(nsym)*slen*
sizeof(
float64_t);
1475 memset(h_normalizer, 0, slen*
sizeof(
float64_t));
1476 int32_t num_str=get_num_vectors();
1477 for (int32_t i=0; i<num_str; i++)
1481 ST* vec=get_feature_vector(i, len, free_vec);
1482 for (int32_t j=0; j<len; j++)
1484 h[int64_t(j)*nsym+alphabet->remap_to_bin(vec[j])]++;
1487 free_feature_vector(vec, i, free_vec);
1492 for (int32_t i=0; i<slen; i++)
1494 for (int32_t j=0; j<nsym; j++)
1496 if (h_normalizer && h_normalizer[i])
1497 h[int64_t(i)*nsym+j]/=h_normalizer[i];
1501 SG_FREE(h_normalizer);
1510 ASSERT(rows == get_num_symbols())
1515 for (int32_t i=0; i<num_vec; i++)
1517 sf[i].
string=SG_MALLOC(ST, cols);
1522 for (int32_t j=0; j<cols; j++)
1527 for (c=0; c<rows-1; c++)
1529 if (randoms[j]<=lik)
1531 lik+=hist[int64_t(j)*rows+c+1];
1533 sf[i].
string[j]=alphabet->remap_to_char(c);
1537 set_features(sf, num_vec, cols);
1620 index_t real_idx=m_subset_stack->subset_idx_conversion(indices.
vector[i]);
1626 current_string.
slen*
sizeof(ST));
1627 list_copy.
strings[i]=string_copy;
1637 result->
order=order;
1648 determine_maximum_string_length();
1653 ASSERT(features && num<get_num_vectors())
1655 int32_t real_num=m_subset_stack->subset_idx_conversion(num);
1657 len=features[real_num].slen;
1661 ST* target=SG_MALLOC(ST, len);
1662 memcpy(target, features[real_num].
string, len*
sizeof(ST));
1674 length_of_single_string=0;
1675 max_string_length=0;
1677 preprocess_on_get=
false;
1679 symbol_mask_table=NULL;
1680 symbol_mask_table_len=0;
1682 original_num_symbols=0;
1684 m_parameters->add((
CSGObject**) &alphabet,
"alphabet");
1685 m_parameters->add_vector(&features, &num_vectors,
"features",
1686 "This contains the array of features.");
1687 m_parameters->add_vector(&single_string,
1688 &length_of_single_string,
1690 "Created by sliding window.");
1691 m_parameters->add(&max_string_length,
"max_string_length",
1692 "Length of longest string.");
1693 m_parameters->add(&num_symbols,
"num_symbols",
1694 "Number of used symbols.");
1695 m_parameters->add(&original_num_symbols,
"original_num_symbols",
1696 "Original number of used symbols.");
1697 m_parameters->add(&order,
"order",
1698 "Order used in higher order mapping.");
1699 m_parameters->add(&preprocess_on_get,
"preprocess_on_get",
1700 "Preprocess on-the-fly?");
1702 m_parameters->add_vector(&symbol_mask_table, &symbol_mask_table_len,
"mask_table",
"Symbol mask table - using in higher order mapping");
1921 #define LOAD(f_load, sg_type) \
1922 template<> void CStringFeatures<sg_type>::load(CFile* loader) \
1924 SG_INFO("loading...\n") \
1927 SGString<sg_type>* strs; \
1930 loader->f_load(strs, num_str, max_len); \
1931 set_features(strs, num_str, max_len); \
1935 LOAD(get_string_list,
bool)
1936 LOAD(get_string_list,
char)
1937 LOAD(get_string_list, int8_t)
1938 LOAD(get_string_list, uint8_t)
1939 LOAD(get_string_list, int16_t)
1940 LOAD(get_string_list, uint16_t)
1941 LOAD(get_string_list, int32_t)
1942 LOAD(get_string_list, uint32_t)
1943 LOAD(get_string_list, int64_t)
1944 LOAD(get_string_list, uint64_t)
1950 #define SAVE(f_write, sg_type) \
1951 template<> void CStringFeatures<sg_type>::save(CFile* writer) \
1953 if (m_subset_stack->has_subsets()) \
1954 SG_ERROR("save() is not possible on subset") \
1957 writer->f_write(features, num_vectors); \
1961 SAVE(set_string_list,
bool)
1962 SAVE(set_string_list,
char)
1963 SAVE(set_string_list, int8_t)
1964 SAVE(set_string_list, uint8_t)
1965 SAVE(set_string_list, int16_t)
1966 SAVE(set_string_list, uint16_t)
1967 SAVE(set_string_list, int32_t)
1968 SAVE(set_string_list, uint32_t)
1969 SAVE(set_string_list, int64_t)
1970 SAVE(set_string_list, uint64_t)
1976 template <
class ST>
template <
class CT>
1978 int32_t p_order, int32_t gap,
bool rev)
1980 remove_all_subsets();
1986 this->order=p_order;
1997 for (int32_t i=0; i<num_vectors; i++)
2004 features[i].string=SG_MALLOC(ST, len);
2005 features[i].slen=len;
2007 ST* str=features[i].string;
2008 for (int32_t j=0; j<len; j++)
2020 num_symbols=original_num_symbols;
2021 SG_INFO(
"max_val (bit): %d order: %d -> results in num_symbols: %.0Lf\n", max_val, p_order, num_symbols)
2025 SG_ERROR(
"symbol does not fit into datatype \"%c\" (%d)\n", (
char) max_val, (
int) max_val)
2029 SG_DEBUG(
"translate: start=%i order=%i gap=%i(size:%i)\n", start, p_order, gap,
sizeof(ST))
2030 for (int32_t line=0; line<num_vectors; line++)
2034 ST* fv=get_feature_vector(line, len, vfree);
2038 CAlphabet::translate_from_single_order_reversed(fv, len, start+gap, p_order+gap, max_val, gap);
2040 CAlphabet::translate_from_single_order(fv, len, start+gap, p_order+gap, max_val, gap);
2043 features[line].slen-=start+gap ;
2044 if (features[line].slen<0)
2045 features[line].slen=0 ;
2048 compute_symbol_mask_table(max_val);
void set_feature_vector(SGVector< ST > vector, int32_t num)
int32_t obtain_by_sliding_window(int32_t window_size, int32_t step_size, int32_t skip=0)
virtual int32_t get_max_vector_length()
SGVector< ST > get_feature_vector(int32_t num)
CSubsetStack * m_subset_stack
int32_t get_num_symbols_in_histogram()
void enable_on_the_fly_preprocessing()
virtual void load(CFile *loader)
virtual CFeatures * duplicate() const
template class SGStringList
floatmax_t num_symbols
number of used symbols
bool load_from_directory(char *dirname)
bool obtain_from_char(CStringFeatures< char > *sf, int32_t start, int32_t p_order, int32_t gap, bool rev)
virtual CFeatures * copy_subset(SGVector< index_t > indices)
ST shift_offset(ST offset, int32_t amount)
int32_t max_string_length
Template class StringFeatures implements a list of strings.
virtual void subset_changed_post()
static float64_t ceil(float64_t d)
void unembed_word(ST word, uint8_t *seq, int32_t len)
EAlphabet
Alphabet of charfeatures/observations.
int32_t get_max_value_in_histogram()
virtual SGString< ST > * copy_features(int32_t &num_str, int32_t &max_str_len)
bool check_alphabet_size(bool print_error=true)
floatmax_t get_max_num_symbols()
bool load_fasta_file(const char *fname, bool ignore_invalid=false)
SGString< ST > * features
#define SAVE(f_write, sg_type)
virtual const char * get_name() const
return the name of the preprocessor
virtual int32_t get_num_vectors() const
bool append_features(CStringFeatures< ST > *sf)
virtual void create_random(float64_t *hist, int32_t rows, int32_t cols, int32_t num_vec)
#define SG_NOTIMPLEMENTED
The class Alphabet implements an alphabet and alphabet utility functions.
Compression library for compressing and decompressing buffers using one of the standard compression a...
void free_feature_vector(ST *feat_vec, int32_t num, bool dofree)
floatmax_t get_original_num_symbols()
static int filter(CONST_DIRENT_T *d)
uint8_t remap_to_bin(uint8_t c)
virtual bool load_compressed(char *src, bool decompress)
EFeatureClass
shogun feature class
void disable_on_the_fly_preprocessing()
void load_ascii_file(char *fname, bool remap_to_bin=true, EAlphabet ascii_alphabet=DNA, EAlphabet binary_alphabet=RAWDNA)
void add_string_to_histogram(T *p, int64_t len)
virtual bool apply_preprocessor(bool force_preprocessing=false)
char * get_line(uint64_t &len, uint64_t &offs)
ST get_masked_symbols(ST symbol, uint8_t mask)
floatmax_t get_num_symbols()
Class SGObject is the base class of all shogun objects.
int32_t symbol_mask_table_len
order used in higher order mapping
#define LOAD(f_load, sg_type)
CStringFeatures< ST > * get_transposed()
int32_t order
order used in higher order mapping
ST embed_word(ST *seq, int32_t len)
int32_t obtain_by_position_list(int32_t window_size, CDynamicArray< int32_t > *positions, int32_t skip=0)
bool load_fastq_file(const char *fname, bool ignore_invalid=false, bool bitremap_in_single_string=false)
virtual ST * apply_to_string(ST *f, int32_t &len)=0
apply preproc on single feature vector
floatmax_t original_num_symbols
original number of used symbols (before higher order mapping)
int32_t get_num_symbols() const
A File access base class.
virtual EFeatureClass get_feature_class() const
virtual bool save_compressed(char *dest, E_COMPRESSION_TYPE compression, int level)
virtual ST * compute_feature_vector(int32_t num, int32_t &len)
virtual ST get_feature(int32_t vec_num, int32_t feat_num)
index_t subset_idx_conversion(index_t idx) const
CAlphabet * get_alphabet()
Template class StringPreprocessor, base class for preprocessors (cf. CPreprocessor) that apply to CSt...
void compute_symbol_mask_table(int64_t max_val)
virtual EFeatureType get_feature_type() const
void determine_maximum_string_length()
bool check_alphabet(bool print_error=true)
int32_t get_num_bits() const
index_t max_string_length
EFeatureType
shogun feature type
bool have_same_length(int32_t len=-1)
virtual void cleanup_feature_vector(int32_t num)
void compress(uint8_t *uncompressed, uint64_t uncompressed_size, uint8_t *&compressed, uint64_t &compressed_size, int32_t level=1)
static void set_dirname(const char *dirname)
all of classes and functions are contained in the shogun namespace
virtual ~CStringFeatures()
SGStringList< ST > get_features()
The class Features is the base class of all feature objects.
void decompress(uint8_t *compressed, uint64_t compressed_size, uint8_t *uncompressed, uint64_t &uncompressed_size)
ST shift_symbol(ST symbol, int32_t amount)
void embed_features(int32_t p_order)
int32_t get_num_elements() const
virtual void get_histogram(float64_t **hist, int32_t *rows, int32_t *cols, bool normalize=true)
virtual void cleanup_feature_vectors(int32_t start, int32_t stop)
static char * concat_filename(const char *filename)
static floatmax_t powl(floatmax_t x, floatmax_t n)
virtual bool apply_to_string_features(CFeatures *f)=0
const T & get_element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) const
ST * symbol_mask_table
order used in higher order mapping
void set_features(SGStringList< ST > feats)
virtual int32_t get_vector_length(int32_t vec_num)
static void random_vector(T *vec, int32_t len, T min_value, T max_value)
static ST * get_zero_terminated_string_copy(SGString< ST > str)