10 #include <sys/types.h> 112 for (int32_t i=0; i<256; i++)
202 for (int32_t i=start; i<=stop; i++)
233 SG_ERROR(
"Index out of bounds (number of strings %d, you " 240 ST* dst=SG_MALLOC(ST, l);
241 sg_memcpy(dst, vec, l*
sizeof(ST));
251 SG_ERROR(
"A subset is set, cannot set feature vector\n")
255 SG_ERROR(
"Index out of bounds (number of strings %d, you " 260 SG_ERROR(
"String has zero or negative length\n")
296 SG_DEBUG(
"computing feature vector!\n")
302 ST* tmp_feat_before=feat;
309 SG_FREE(tmp_feat_before);
310 tmp_feat_before=feat;
337 SG_DEBUG(
"Allocating memory for transposed string features of size %ld\n",
338 int64_t(num_feat)*num_vec);
342 for (int32_t i=0; i<num_vec; i++)
344 sf[i].
string=SG_MALLOC(ST, num_feat);
348 for (int32_t i=0; i<num_feat; i++)
354 for (int32_t j=0; j<num_vec; j++)
355 sf[j].
string[i]=vec[j];
367 "Trying to access string[%d] but num_str=%d\n", num,
385 "Trying to access string[%d] but num_str=%d\n", num,
403 ST result=vec[feat_num];
461 size_t blocksize=1024*1024;
462 size_t required_blocksize=0;
463 uint8_t* dummy=SG_MALLOC(uint8_t, blocksize);
464 uint8_t* overflow=NULL;
465 int32_t overflow_len=0;
472 FILE* f=fopen(fname,
"ro");
479 SG_INFO(
"counting line numbers in file %s\n", fname)
481 size_t old_block_offs=0;
482 fseek(f, 0, SEEK_END);
483 size_t fsize=ftell(f);
489 SG_DEBUG(
"block_size=%ld file_size=%ld\n", blocksize, fsize)
492 while (sz == blocksize)
494 sz=fread(dummy,
sizeof(uint8_t), blocksize, f);
495 for (
size_t i=0; i<sz; i++)
498 if (dummy[i]==
'\n' || (i==sz-1 && sz<blocksize))
501 required_blocksize=
CMath::max(required_blocksize, block_offs-old_block_offs);
502 old_block_offs=block_offs;
505 SG_PROGRESS(block_offs, 0, fsize, 1,
"COUNTING:\t")
510 blocksize=required_blocksize;
511 dummy=SG_MALLOC(uint8_t, blocksize);
512 overflow=SG_MALLOC(uint8_t, blocksize);
518 while (sz == blocksize)
520 sz=fread(dummy,
sizeof(uint8_t), blocksize, f);
523 for (
size_t i=0; i<sz; i++)
525 if (dummy[i]==
'\n' || (i==sz-1 && sz<blocksize))
527 int32_t len=i-old_sz;
536 for (int32_t j=0; j<overflow_len; j++)
538 for (int32_t j=0; j<len; j++)
545 for (int32_t j=0; j<overflow_len; j++)
546 features[lines].
string[j]=overflow[j];
547 for (int32_t j=0; j<len; j++)
548 features[lines].
string[j+overflow_len]=dummy[old_sz+j];
562 for (
size_t i=old_sz; i<sz; i++)
563 overflow[i-old_sz]=dummy[i];
565 overflow_len=sz-old_sz;
570 SG_INFO(
"file successfully read\n")
614 if (len>0 && s[0]==
'>')
619 SG_ERROR(
"No fasta hunks (lines starting with '>') found\n")
637 int32_t spanned_lines=0;
642 SG_ERROR(
"Error reading fasta entry in line %d len=%ld", 4*i+1, len)
644 if (s[0]==
'>' || offs==f.
get_size())
653 len=fasta_len-spanned_lines;
654 strings[i].
string=SG_MALLOC(ST, len);
657 ST* str=strings[i].
string;
659 SG_DEBUG(
"'%.*s', len=%d, spanned_lines=%d\n", (int32_t) id_len,
id, (int32_t) len, (int32_t) spanned_lines)
661 for (int32_t j=0; j<fasta_len; j++)
671 if (uint64_t(idx)>=len)
672 SG_ERROR(
"idx=%d j=%d fasta_len=%d, spanned_lines=%d str='%.*s'\n", idx, j, fasta_len, spanned_lines, idx, str)
690 bool ignore_invalid,
bool bitremap_in_single_string)
704 SG_ERROR(
"Number of lines must be divisible by 4 in fastq files\n")
714 if (bitremap_in_single_string)
717 strings[0].
string=SG_MALLOC(ST, num);
725 str=SG_MALLOC(ST, len);
733 SG_ERROR(
"Error reading 'read' identifier in line %d", 4*i)
737 SG_ERROR(
"Error reading 'read' in line %d len=%ld", 4*i+1, len)
739 if (bitremap_in_single_string)
741 if (len!=(uint64_t)
order)
742 SG_ERROR(
"read in line %d not of length %d (is %d)\n", 4*i+1, order, len)
743 for (int32_t j=0; j<
order; j++)
750 strings[i].
string=SG_MALLOC(ST, len);
756 for (uint64_t j=0; j<len; j++)
766 for (uint64_t j=0; j<len; j++)
774 SG_ERROR(
"Error reading 'read' quality identifier in line %d", 4*i+2)
777 SG_ERROR(
"Error reading 'read' quality in line %d", 4*i+3)
780 if (bitremap_in_single_string)
794 struct dirent **namelist;
802 TCHAR search_dir[MAX_PATH];
804 LARGE_INTEGER filesize;
805 HANDLE h_find = INVALID_HANDLE_VALUE;
807 StringCchCopy(search_dir, MAX_PATH, dirname);
808 StringCchCat(search_dir, MAX_PATH, TEXT(
"\\*"));
810 h_find = FindFirstFile(search_dir, &ffd);
811 if (INVALID_HANDLE_VALUE == h_find)
813 SG_ERROR(
"Error finding finds in %s\n", dirname)
817 std::vector<struct dirent*> files;
820 if (ffd.dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
822 struct dirent* d = SG_MALLOC(
struct dirent, 1);
823 StringCchCopy(d->d_name, MAX_PATH, ffd.cFileName);
828 while (FindNextFile(h_find, &ffd) != 0);
829 namelist = &files[0];
832 n=scandir(dirname, &namelist, &
SGIO::filter, alphasort);
836 SG_ERROR(
"error calling scandir - no files found\n")
850 for (int32_t i=0; i<n; i++)
857 if (!stat(fname, &s) && s.st_size>0)
859 filesize=s.st_size/
sizeof(ST);
861 FILE* f=fopen(fname,
"ro");
864 ST* str=SG_MALLOC(ST, filesize);
865 SG_DEBUG(
"%s:%ld\n", fname, (int64_t) filesize)
866 if (fread(str,
sizeof(ST), filesize, f)!=(
size_t) filesize)
869 strings[num].
slen=filesize;
870 max_len=
CMath::max(max_len, strings[num].slen);
877 SG_ERROR(
"empty or non readable file \'%s\'\n", fname)
879 SG_FREE(namelist[i]);
883 if (num>0 && strings)
901 SG_ERROR(
"Cannot call set_features() with subset.\n")
908 for (int32_t i=0; i<p_num_vectors; i++)
942 SG_ERROR(
"Cannot call set_features() with subset.\n")
947 for (int32_t i=0; i<sf_num_str; i++)
951 new_features[i].
string=SG_MALLOC(ST, length);
952 sg_memcpy(new_features[i].
string, sf->
features[real_i].
string, length);
953 new_features[i].
slen=length;
962 SG_ERROR(
"Cannot call set_features() with subset.\n")
965 return set_features(p_features, p_num_vectors, p_max_string_length);
970 for (int32_t i=0; i<p_num_vectors; i++)
979 for (int32_t i=0; i<p_num_vectors; i++)
988 if (i<old_num_vectors)
995 new_features[i].
string=p_features[i-old_num_vectors].
string;
996 new_features[i].
slen=p_features[i-old_num_vectors].
slen;
1000 SG_FREE(p_features);
1023 SG_ERROR(
"get features() is not possible on subset")
1038 for (int32_t i=0; i<num_str; i++)
1043 new_feat[i].
string=SG_MALLOC(ST, len);
1044 new_feat[i].
slen=len;
1045 sg_memcpy(new_feat[i].
string, vec, ((
size_t) len) *
sizeof(ST));
1055 int32_t max_str_len;
1066 if (!(file=fopen(src,
"r")))
1072 if (fread(&
id[0],
sizeof(
char), 1, file)!=1)
1075 if (fread(&
id[1],
sizeof(
char), 1, file)!=1)
1078 if (fread(&
id[2],
sizeof(
char), 1, file)!=1)
1081 if (fread(&
id[3],
sizeof(
char), 1, file)!=1)
1087 if (fread(&c,
sizeof(uint8_t), 1, file)!=1)
1088 SG_ERROR(
"failed to read compression type")
1093 if (fread(&a,
sizeof(uint8_t), 1, file)!=1)
1094 SG_ERROR(
"failed to read compression alphabet")
1097 if (fread(&
num_vectors,
sizeof(int32_t), 1, file)!=1)
1098 SG_ERROR(
"failed to read compression number of vectors")
1102 SG_ERROR(
"failed to read maximum string length")
1111 int32_t len_compressed;
1112 if (fread(&len_compressed,
sizeof(int32_t), 1, file)!=1)
1113 SG_ERROR(
"failed to read vector length compressed")
1115 int32_t len_uncompressed;
1116 if (fread(&len_uncompressed,
sizeof(int32_t), 1, file)!=1)
1117 SG_ERROR(
"failed to read vector length uncompressed")
1124 uint8_t* compressed=SG_MALLOC(uint8_t, len_compressed);
1125 if (fread(compressed,
sizeof(uint8_t), len_compressed, file)!=(
size_t) len_compressed)
1126 SG_ERROR(
"failed to read compressed data (expected %d bytes)", len_compressed)
1127 uint64_t uncompressed_size=len_uncompressed;
1128 uncompressed_size*=
sizeof(ST);
1129 compressor->
decompress(compressed, len_compressed,
1130 (uint8_t*)
features[i].
string, uncompressed_size);
1131 SG_FREE(compressed);
1132 ASSERT(uncompressed_size==((uint64_t) len_uncompressed)*
sizeof(ST))
1136 int32_t offs=
CMath::ceil(2.0*
sizeof(int32_t)/
sizeof(ST));
1139 int32_t* feat32ptr=((int32_t*) (
features[i].
string));
1140 memset(
features[i].
string, 0, offs*
sizeof(ST));
1141 feat32ptr[0]=(int32_t) len_compressed;
1142 feat32ptr[1]=(int32_t) len_uncompressed;
1143 uint8_t* compressed=(uint8_t*) (&
features[i].
string[offs]);
1144 if (fread(compressed, 1, len_compressed, file)!=(size_t) len_compressed)
1145 SG_ERROR(
"failed to read uncompressed data")
1158 SG_ERROR(
"save_compressed() is not possible on subset")
1162 if (!(file=fopen(dest,
"wb")))
1168 const char*
id=
"SGV0";
1169 fwrite(&
id[0],
sizeof(
char), 1, file);
1170 fwrite(&
id[1],
sizeof(
char), 1, file);
1171 fwrite(&
id[2],
sizeof(
char), 1, file);
1172 fwrite(&
id[3],
sizeof(
char), 1, file);
1175 uint8_t c=(uint8_t) compression;
1176 fwrite(&c,
sizeof(uint8_t), 1, file);
1179 fwrite(&a,
sizeof(uint8_t), 1, file);
1192 uint8_t* compressed=NULL;
1193 uint64_t compressed_size=0;
1195 compressor->
compress((uint8_t*) vec, ((uint64_t) len)*
sizeof(ST),
1196 compressed, compressed_size, level);
1198 int32_t len_compressed=(int32_t) compressed_size;
1200 fwrite(&len_compressed,
sizeof(int32_t), 1, file);
1202 fwrite(&len,
sizeof(int32_t), 1, file);
1204 fwrite(compressed, compressed_size, 1, file);
1205 SG_FREE(compressed);
1217 SG_DEBUG(
"force: %d\n", force_preprocessing)
1265 f[i].
slen=window_size-skip;
1309 if (p>=0 && p<=len-window_size)
1312 f[i].
slen=window_size-skip;
1321 SG_ERROR(
"window (size:%d) starting at position[%d]=%d does not fit in sequence(len:%d)\n",
1322 window_size, i, p, len);
1349 for (int32_t i=0; i<num_str; i++)
1374 SG_INFO(
"max_val (bit): %d order: %d -> results in num_symbols: %.0Lf\n", max_val, p_order,
num_symbols)
1377 SG_WARNING(
"symbols did not fit into datatype \"%c\" (%d)\n", (
char) max_val, (
int) max_val)
1380 for (int32_t i=0; i<p_order*max_val; i++)
1381 mask= (mask<<1) | ((ST) 1);
1388 SG_ERROR(
"Sequence must be longer than order (%d vs. %d)\n", len, p_order)
1393 for (int32_t j=0; j<p_order; j++)
1399 for (int32_t j=p_order; j<len; j++)
1402 str[idx+1]= ((str[idx]<<max_val) | str[j]) & mask;
1422 for (int32_t i=0; i< (int64_t) max_val; i++)
1425 for (int32_t i=0; i<256; i++)
1427 uint8_t bits=(uint8_t) i;
1430 for (int32_t j=0; j<8; j++)
1445 for (uint32_t i=0; i<nbits; i++)
1446 mask=(mask<<1) | (ST) 1;
1448 for (int32_t i=0; i<len; i++)
1460 for (int32_t i=0; i<len; i++)
1474 for (int32_t i=0; i<num_str; i++)
1484 ST* s=SG_MALLOC(ST, l+1);
1485 sg_memcpy(s, str.
string,
sizeof(ST)*l);
1508 int64_t sz=int64_t(nsym)*slen*
sizeof(
float64_t);
1513 memset(h_normalizer, 0, slen*
sizeof(
float64_t));
1515 for (int32_t i=0; i<num_str; i++)
1520 for (int32_t j=0; j<len; j++)
1530 for (int32_t i=0; i<slen; i++)
1532 for (int32_t j=0; j<nsym; j++)
1534 if (h_normalizer && h_normalizer[i])
1535 h[int64_t(i)*nsym+j]/=h_normalizer[i];
1539 SG_FREE(h_normalizer);
1553 for (int32_t i=0; i<num_vec; i++)
1555 sf[i].
string=SG_MALLOC(ST, cols);
1560 for (int32_t j=0; j<cols; j++)
1565 for (c=0; c<rows-1; c++)
1567 if (randoms[j]<=lik)
1569 lik+=hist[int64_t(j)*rows+c+1];
1664 current_string.
slen*
sizeof(ST));
1665 list_copy.
strings[i]=string_copy;
1699 ST* target=SG_MALLOC(ST, len);
1700 sg_memcpy(target,
features[real_num].
string, len*
sizeof(ST));
1724 "This contains the array of features.");
1728 "Created by sliding window.");
1730 "Length of longest string.");
1732 "Number of used symbols.");
1734 "Original number of used symbols.");
1736 "Order used in higher order mapping.");
1738 "Preprocess on-the-fly?");
1959 #define LOAD(f_load, sg_type) \ 1960 template<> void CStringFeatures<sg_type>::load(CFile* loader) \ 1962 SG_INFO("loading...\n") \ 1965 SGString<sg_type>* strs; \ 1968 loader->f_load(strs, num_str, max_len); \ 1969 set_features(strs, num_str, max_len); \ 1973 LOAD(get_string_list,
bool)
1974 LOAD(get_string_list,
char)
1975 LOAD(get_string_list, int8_t)
1976 LOAD(get_string_list, uint8_t)
1977 LOAD(get_string_list, int16_t)
1978 LOAD(get_string_list, uint16_t)
1979 LOAD(get_string_list, int32_t)
1980 LOAD(get_string_list, uint32_t)
1981 LOAD(get_string_list, int64_t)
1982 LOAD(get_string_list, uint64_t)
1988 #define SAVE(f_write, sg_type) \ 1989 template<> void CStringFeatures<sg_type>::save(CFile* writer) \ 1991 if (m_subset_stack->has_subsets()) \ 1992 SG_ERROR("save() is not possible on subset") \ 1995 writer->f_write(features, num_vectors); \ 1999 SAVE(set_string_list,
bool)
2000 SAVE(set_string_list,
char)
2001 SAVE(set_string_list, int8_t)
2002 SAVE(set_string_list, uint8_t)
2003 SAVE(set_string_list, int16_t)
2004 SAVE(set_string_list, uint16_t)
2005 SAVE(set_string_list, int32_t)
2006 SAVE(set_string_list, uint32_t)
2007 SAVE(set_string_list, int64_t)
2008 SAVE(set_string_list, uint64_t)
2014 template <
class ST>
template <
class CT>
2016 int32_t p_order, int32_t gap,
bool rev)
2024 this->
order=p_order;
2046 for (int32_t j=0; j<len; j++)
2059 SG_INFO(
"max_val (bit): %d order: %d -> results in num_symbols: %.0Lf\n", max_val, p_order,
num_symbols)
2063 SG_ERROR(
"symbol does not fit into datatype \"%c\" (%d)\n", (
char) max_val, (
int) max_val)
2067 SG_DEBUG(
"translate: start=%i order=%i gap=%i(size:%i)\n", start, p_order, gap,
sizeof(ST))
void disable_on_the_fly_preprocessing()
CSubsetStack * m_subset_stack
int32_t get_num_symbols_in_histogram()
void determine_maximum_string_length()
bool preprocess_on_get
preprocess on-the-fly?
static void random_vector(T *vec, int32_t len, T min_value, T max_value)
bool load_fasta_file(const char *fname, bool ignore_invalid=false)
void set_feature_vector(SGVector< ST > vector, int32_t num)
void set_features(SGStringList< ST > feats)
virtual CFeatures * duplicate() const
static float64_t ceil(float64_t d)
virtual EFeatureType get_feature_type() const
virtual SGString< ST > * copy_features(int32_t &num_str, int32_t &max_str_len)
EAlphabet
Alphabet of charfeatures/observations.
int32_t get_max_value_in_histogram()
bool check_alphabet_size(bool print_error=true)
SGString< ST > * features
void set_preprocessed(int32_t num)
ST shift_offset(ST offset, int32_t amount)
#define SAVE(f_write, sg_type)
virtual const char * get_name() const
return the name of the preprocessor
EAlphabet get_alphabet() const
static void translate_from_single_order(ST *obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val)
char * get_line(uint64_t &len, uint64_t &offs)
int32_t get_num_elements() const
#define SG_NOTIMPLEMENTED
virtual void load(CFile *loader)
The class Alphabet implements an alphabet and alphabet utility functions.
ST get_masked_symbols(ST symbol, uint8_t mask)
Compression library for compressing and decompressing buffers using one of the standard compression a...
CPreprocessor * get_preprocessor(int32_t num) const
void compute_symbol_mask_table(int64_t max_val)
int32_t length_of_single_string
length of prior single string
floatmax_t get_max_num_symbols()
const T & get_element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) const
floatmax_t num_symbols
number of used symbols
virtual EFeatureClass get_feature_class() const
bool obtain_from_char_features(CStringFeatures< CT > *sf, int32_t start, int32_t p_order, int32_t gap, bool rev)
virtual int32_t get_num_vectors() const
static int filter(CONST_DIRENT_T *d)
virtual void cleanup_feature_vectors(int32_t start, int32_t stop)
void unlock_entry(int64_t number)
uint8_t remap_to_bin(uint8_t c)
EFeatureClass
shogun feature class
int32_t symbol_mask_table_len
order used in higher order mapping
virtual bool save_compressed(char *dest, E_COMPRESSION_TYPE compression, int level)
int32_t get_num_preprocessors() const
virtual int32_t get_vector_length(int32_t vec_num)
void enable_on_the_fly_preprocessing()
int32_t order
order used in higher order mapping
void add_string_to_histogram(T *p, int64_t len)
static void translate_from_single_order_reversed(ST *obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val)
void add(bool *param, const char *name, const char *description="")
bool obtain_from_char(CStringFeatures< char > *sf, int32_t start, int32_t p_order, int32_t gap, bool rev)
Template class StringFeatures implements a list of strings.
Class SGObject is the base class of all shogun objects.
ST shift_symbol(ST symbol, int32_t amount)
#define LOAD(f_load, sg_type)
virtual void get_histogram(float64_t **hist, int32_t *rows, int32_t *cols, bool normalize=true)
bool is_preprocessed(int32_t num) const
int32_t obtain_by_sliding_window(int32_t window_size, int32_t step_size, int32_t skip=0)
floatmax_t get_original_num_symbols()
virtual ST * apply_to_string(ST *f, int32_t &len)=0
apply preproc on single feature vector
int32_t get_num_symbols() const
A File access base class.
void free_feature_vector(ST *feat_vec, int32_t num, bool dofree)
void load_ascii_file(char *fname, bool remap_to_bin=true, EAlphabet ascii_alphabet=DNA, EAlphabet binary_alphabet=RAWDNA)
virtual ~CStringFeatures()
index_t max_string_length
index_t subset_idx_conversion(index_t idx) const
SGVector< ST > get_feature_vector(int32_t num)
SGStringList< ST > get_features()
floatmax_t get_num_symbols()
Template class StringPreprocessor, base class for preprocessors (cf. CPreprocessor) that apply to CSt...
int32_t obtain_by_position_list(int32_t window_size, CDynamicArray< int32_t > *positions, int32_t skip=0)
virtual void create_random(float64_t *hist, int32_t rows, int32_t cols, int32_t num_vec)
bool check_alphabet(bool print_error=true)
virtual CFeatures * copy_subset(SGVector< index_t > indices)
int32_t get_num_bits() const
CCache< ST > * feature_cache
EFeatureType
shogun feature type
void compress(uint8_t *uncompressed, uint64_t uncompressed_size, uint8_t *&compressed, uint64_t &compressed_size, int32_t level=1)
void unembed_word(ST word, uint8_t *seq, int32_t len)
bool append_features(CStringFeatures< ST > *sf)
static void set_dirname(const char *dirname)
void add_vector(bool **param, index_t *length, const char *name, const char *description="")
all of classes and functions are contained in the shogun namespace
ST * symbol_mask_table
order used in higher order mapping
bool load_fastq_file(const char *fname, bool ignore_invalid=false, bool bitremap_in_single_string=false)
virtual void subset_changed_post()
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)
int32_t max_string_length
virtual int32_t get_max_vector_length()
virtual bool has_subsets() const
CAlphabet * get_alphabet()
static char * concat_filename(const char *filename)
floatmax_t original_num_symbols
original number of used symbols (before higher order mapping)
virtual void cleanup_feature_vector(int32_t num)
uint8_t remap_to_char(uint8_t c)
virtual bool load_compressed(char *src, bool decompress)
static ST * get_zero_terminated_string_copy(SGString< ST > str)
static floatmax_t powl(floatmax_t x, floatmax_t n)
virtual bool apply_to_string_features(CFeatures *f)=0
virtual void remove_all_subsets()
virtual ST get_feature(int32_t vec_num, int32_t feat_num)
void embed_features(int32_t p_order)
bool have_same_length(int32_t len=-1)
ST embed_word(ST *seq, int32_t len)
virtual ST * compute_feature_vector(int32_t num, int32_t &len)
bool load_from_directory(char *dirname)
CStringFeatures< ST > * get_transposed()
virtual bool apply_preprocessor(bool force_preprocessing=false)
template class SGStringList