-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
StringFeatures.h
2504 lines (2155 loc) · 63.1 KB
/
StringFeatures.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 1999-2009 Soeren Sonnenburg
* Written (W) 1999-2008 Gunnar Raetsch
* Subset support written (W) 2011 Heiko Strathmann
* Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
*/
#ifndef _CSTRINGFEATURES__H__
#define _CSTRINGFEATURES__H__
#include <shogun/lib/common.h>
#include <shogun/io/SGIO.h>
#include <shogun/lib/Cache.h>
#include <shogun/lib/DynamicArray.h>
#include <shogun/io/File.h>
#include <shogun/io/MemoryMappedFile.h>
#include <shogun/mathematics/Math.h>
#include <shogun/lib/Compressor.h>
#include <shogun/base/Parameter.h>
#include <shogun/preprocessor/Preprocessor.h>
#include <shogun/preprocessor/StringPreprocessor.h>
#include <shogun/features/Features.h>
#include <shogun/features/Alphabet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
namespace shogun
{
class CCompressor;
enum E_COMPRESSION_TYPE;
class CAlphabet;
enum EAlphabet;
template <class T> class CDynamicArray;
class CFile;
template <class T> class CMemoryMappedFile;
class CMath;
template <class ST> class CStringPreprocessor;
template <class T> class SGString;
struct SSKDoubleFeature
{
int feature1;
int feature2;
int group;
};
struct SSKTripleFeature
{
int feature1;
int feature2;
int feature3;
int group;
};
/** @brief Template class StringFeatures implements a list of strings.
*
* As this class is a template the underlying storage type is quite arbitrary and
* not limited to character strings, but could also be sequences of floating
* point numbers etc. Strings differ from matrices (cf. CSimpleFeatures) in a
* way that the dimensionality of the feature vectors (i.e. the strings) is not
* fixed; it may vary between strings.
*
* Most string kernels require StringFeatures but a number of them actually
* requires strings to have same length.
*
* When preprocessors are attached to string features they may shorten the
* string, but are not allowed to return strings longer than max_string_length,
* as some algorithms depend on this.
*
* Also note that string features cannot currently be computed on-the-fly.
*
* (Partly) subset access is supported for this feature type.
* Simple use the (inherited) set_subset(), remove_subset() functions.
* If done, all calls that work with features are translated to the subset.
* See comments to find out whether it is supported for that method
*/
template <class ST> class CStringFeatures : public CFeatures
{
public:
/** default constructor
*
*/
CStringFeatures() : CFeatures(0)
{
init();
alphabet=new CAlphabet();
}
/** constructor
*
* @param alpha alphabet (type) to use for string features
*/
CStringFeatures(EAlphabet alpha) : CFeatures(0)
{
init();
alphabet=new CAlphabet(alpha);
SG_REF(alphabet);
num_symbols=alphabet->get_num_symbols();
original_num_symbols=num_symbols;
}
/** constructor
*
* @param alpha alphabet (type) to use for string features
*/
CStringFeatures(SGStringList<ST> string_list, EAlphabet alpha)
: CFeatures(0)
{
init();
alphabet=new CAlphabet(alpha);
SG_REF(alphabet);
num_symbols=alphabet->get_num_symbols();
original_num_symbols=num_symbols;
set_features(string_list.strings, string_list.num_strings, string_list.max_string_length);
}
/** constructor
*
* @param alpha an actual alphabet
*/
CStringFeatures(SGStringList<ST> string_list, CAlphabet* alpha)
: CFeatures(0)
{
init();
alphabet=new CAlphabet(alpha);
SG_REF(alphabet);
num_symbols=alphabet->get_num_symbols();
original_num_symbols=num_symbols;
set_features(string_list.strings, string_list.num_strings, string_list.max_string_length);
}
/** constructor
*
* @param alpha alphabet to use for string features
*/
CStringFeatures(CAlphabet* alpha)
: CFeatures(0)
{
init();
ASSERT(alpha);
SG_REF(alpha);
alphabet=alpha;
num_symbols=alphabet->get_num_symbols();
original_num_symbols=num_symbols;
}
/** copy constructor */
CStringFeatures(const CStringFeatures & orig)
: CFeatures(orig), num_vectors(orig.num_vectors),
single_string(orig.single_string),
length_of_single_string(orig.length_of_single_string),
max_string_length(orig.max_string_length),
num_symbols(orig.num_symbols),
original_num_symbols(orig.original_num_symbols),
order(orig.order), preprocess_on_get(false),
feature_cache(NULL)
{
init();
ASSERT(orig.single_string == NULL); //not implemented
alphabet=orig.alphabet;
SG_REF(alphabet);
if (orig.features)
{
features=new SGString<ST>[orig.num_vectors];
for (int32_t i=0; i<num_vectors; i++)
{
features[i].string=new ST[orig.features[i].length];
features[i].length=orig.features[i].length;
memcpy(features[i].string, orig.features[i].string, sizeof(ST)*orig.features[i].length);
}
}
if (orig.symbol_mask_table)
{
symbol_mask_table=new ST[256];
for (int32_t i=0; i<256; i++)
symbol_mask_table[i]=orig.symbol_mask_table[i];
}
m_subset=orig.m_subset->duplicate();
}
/** constructor
*
* @param loader File object via which to load data
* @param alpha alphabet (type) to use for string features
*/
CStringFeatures(CFile* loader, EAlphabet alpha=DNA)
: CFeatures(loader), num_vectors(0),
features(NULL), single_string(NULL), length_of_single_string(0),
max_string_length(0), order(0),
symbol_mask_table(NULL), preprocess_on_get(false), feature_cache(NULL)
{
init();
alphabet=new CAlphabet(alpha);
SG_REF(alphabet);
num_symbols=alphabet->get_num_symbols();
original_num_symbols=num_symbols;
load(loader);
}
virtual ~CStringFeatures()
{
cleanup();
SG_UNREF(alphabet);
}
/** cleanup string features.
*
* removes any subset before
*
* */
virtual void cleanup()
{
remove_subset();
if (single_string)
{
delete[] single_string;
single_string=NULL;
}
else
{
for (int32_t i=0; i<num_vectors; i++)
cleanup_feature_vector(i);
}
num_vectors=0;
delete[] features;
delete[] symbol_mask_table;
features=NULL;
symbol_mask_table=NULL;
/* start with a fresh alphabet, but instead of emptying the histogram
* create a new object (to leave the alphabet object alone if it is used
* by others)
*/
CAlphabet* alpha=new CAlphabet(alphabet->get_alphabet());
SG_UNREF(alphabet);
alphabet=alpha;
SG_REF(alphabet);
}
/** cleanup a single feature vector
*
* possible with subset
*
* @param num number of the vector
* */
virtual void cleanup_feature_vector(int32_t num)
{
ASSERT(num<get_num_vectors());
if (features)
{
int32_t real_num=subset_idx_conversion(num);
delete[] features[real_num].string;
features[real_num].string=NULL;
features[real_num].length=0;
determine_maximum_string_length();
}
}
/** get feature class
*
* @return feature class STRING
*/
inline virtual EFeatureClass get_feature_class() { return C_STRING; }
/** get feature type
*
* @return templated feature type
*/
inline virtual EFeatureType get_feature_type() { return F_UNKNOWN; }
/** get alphabet used in string features
*
* @return alphabet
*/
inline CAlphabet* get_alphabet()
{
SG_REF(alphabet);
return alphabet;
}
/** duplicate feature object
*
* @return feature object
*/
virtual CFeatures* duplicate() const
{
return new CStringFeatures<ST>(*this);
}
/** get string for selected example num
*
* possible with subset
*
* @param num index of the string
*/
SGVector<ST> get_feature_vector(int32_t num)
{
ASSERT(features);
if (num>=get_num_vectors())
{
SG_ERROR("Index out of bounds (number of strings %d, you "
"requested %d)\n", get_num_vectors(), num);
}
int32_t l;
bool free_vec;
ST* vec=get_feature_vector(num, l, free_vec);
ST* dst=(ST*) SG_MALLOC(l*sizeof(ST));
memcpy(dst, vec, l*sizeof(ST));
free_feature_vector(vec, num, free_vec);
return SGVector<ST>(dst,l);
}
/** set string for selected example num
*
* not possible with subset
*
* @param src destination where vector will be copied from
* @param len number of features in vector
* @param num index of the string
*/
void set_feature_vector(SGVector<ST> vector, int32_t num)
{
ASSERT(features);
if (m_subset)
SG_ERROR("A subset is set, cannot set feature vector\n");
if (num>=num_vectors)
{
SG_ERROR("Index out of bounds (number of strings %d, you "
"requested %d)\n", num_vectors, num);
}
if (vector.vlen<=0)
SG_ERROR("String has zero or negative length\n");
cleanup_feature_vector(num);
features[num].length=vector.vlen;
features[num].string=new ST[vector.vlen];
memcpy(features[num].string, vector.vector, vector.vlen*sizeof(ST));
determine_maximum_string_length();
}
/** call this to preprocess string features upon get_feature_vector
*/
void enable_on_the_fly_preprocessing()
{
preprocess_on_get=true;
}
/** call this to disable on the fly feature preprocessing on
* get_feature_vector. Useful when you manually apply preprocessors.
*/
void disable_on_the_fly_preprocessing()
{
preprocess_on_get=false;
}
/** get feature vector for sample num
*
* possible with subset
*
* @param num index of feature vector
* @param len length is returned by reference
* @param dofree whether returned vector must be freed by
* caller via free_feature_vector
* @return feature vector for sample num
*/
ST* get_feature_vector(int32_t num, int32_t& len, bool& dofree)
{
ASSERT(features);
ASSERT(num<get_num_vectors());
int32_t real_num=subset_idx_conversion(num);
if (!preprocess_on_get)
{
dofree=false;
len=features[real_num].length;
return features[real_num].string;
}
else
{
SG_DEBUG( "computing feature vector!\n") ;
ST* feat=compute_feature_vector(num, len);
dofree=true;
if (get_num_preprocessors())
{
ST* tmp_feat_before=feat;
for (int32_t i=0; i<get_num_preprocessors(); i++)
{
CStringPreprocessor<ST>* p=(CStringPreprocessor<ST>*) get_preprocessor(i);
feat=p->apply_to_string(tmp_feat_before, len);
SG_UNREF(p);
delete[] tmp_feat_before;
tmp_feat_before=feat;
}
}
// TODO: implement caching
return feat;
}
}
/** get a transposed copy of the features
*
* possible with subset
*
* @return transposed copy
*/
CStringFeatures<ST>* get_transposed()
{
int32_t num_feat;
int32_t num_vec;
SGString<ST>* s=get_transposed(num_feat, num_vec);
SGStringList<ST> string_list;
string_list.strings = s;
string_list.num_strings = num_vec;
string_list.max_string_length = num_feat;
return new CStringFeatures<ST>(string_list, alphabet);
}
/** compute and return the transpose of string features matrix
* which will be prepocessed.
* num_feat, num_vectors are returned by reference
* caller has to clean up
*
* note that strings all have to have same length
*
* possible with subset
*
* @param num_feat number of features in matrix
* @param num_vec number of vectors in matrix
* @return transposed string features
*/
SGString<ST>* get_transposed(int32_t &num_feat, int32_t &num_vec)
{
num_feat=get_num_vectors();
num_vec=get_max_vector_length();
ASSERT(have_same_length());
SG_DEBUG("Allocating memory for transposed string features of size %ld\n",
int64_t(num_feat)*num_vec);
SGString<ST>* sf=new SGString<ST>[num_vec];
for (int32_t i=0; i<num_vec; i++)
{
sf[i].string=new ST[num_feat];
sf[i].length=num_feat;
}
for (int32_t i=0; i<num_feat; i++)
{
int32_t len=0;
bool free_vec=false;
ST* vec=get_feature_vector(i, len, free_vec);
for (int32_t j=0; j<num_vec; j++)
sf[j].string[i]=vec[j];
free_feature_vector(vec, i, free_vec);
}
return sf;
}
/** free feature vector
*
* possible with subset
*
* @param feat_vec feature vector to free
* @param num index in feature cache, possibly from subset
* @param dofree if vector should be really deleted
*/
void free_feature_vector(ST* feat_vec, int32_t num, bool dofree)
{
if (num>=get_num_vectors())
{
SG_ERROR(
"Trying to access string[%d] but num_str=%d\n", num,
get_num_vectors());
}
int32_t real_num=subset_idx_conversion(num);
if (feature_cache)
feature_cache->unlock_entry(real_num);
if (dofree)
delete[] feat_vec ;
}
/** get feature
*
* possible with subset
*
* @param vec_num which vector
* @param feat_num which feature, possibly from subset
* @return feature
*/
virtual ST inline get_feature(int32_t vec_num, int32_t feat_num)
{
ASSERT(vec_num<get_num_vectors());
int32_t len;
bool free_vec;
ST* vec=get_feature_vector(vec_num, len, free_vec);
ASSERT(feat_num<len);
ST result=vec[feat_num];
free_feature_vector(vec, vec_num, free_vec);
return result;
}
/** get vector length
*
* possible with subset
*
* @param vec_num which vector, possibly from subset
* @return length of vector
*/
virtual inline int32_t get_vector_length(int32_t vec_num)
{
ASSERT(vec_num<get_num_vectors());
int32_t len;
bool free_vec;
ST* vec=get_feature_vector(vec_num, len, free_vec);
free_feature_vector(vec, vec_num, free_vec);
return len;
}
/** get maximum vector length
*
* this one is updated when a subset is set
*
* @return maximum vector/string length
*/
virtual inline int32_t get_max_vector_length()
{
return max_string_length;
}
/** @return number of vectors, possibly of subset */
virtual inline int32_t get_num_vectors() const
{
return m_subset ? m_subset->get_size() : num_vectors;
}
/** get number of symbols
*
* Note: floatmax_t sounds weird, but LONG is not long enough
*
* @return number of symbols
*/
inline floatmax_t get_num_symbols() { return num_symbols; }
/** get maximum number of symbols
*
* Note: floatmax_t sounds weird, but int64_t is not long enough (and
* there is no int128_t type)
*
* @return maximum number of symbols
*/
inline floatmax_t get_max_num_symbols() { return CMath::powl(2,sizeof(ST)*8); }
// these functions are necessary to find out about a former conversion process
/** number of symbols before higher order mapping
*
* @return original number of symbols
*/
inline floatmax_t get_original_num_symbols() { return original_num_symbols; }
/** order used for higher order mapping
*
* @return order
*/
inline int32_t get_order() { return order; }
/** a higher order mapped symbol will be shaped such that the symbols
* specified by bits in the mask will be returned.
*
* @param symbol symbol to mask
* @param mask mask to apply
* @return masked symbol
*/
inline ST get_masked_symbols(ST symbol, uint8_t mask)
{
ASSERT(symbol_mask_table);
return symbol_mask_table[mask] & symbol;
}
/** shift offset to the left by amount
*
* @param offset offset to shift
* @param amount amount to shift the offset
* @return shifted offset
*/
inline ST shift_offset(ST offset, int32_t amount)
{
ASSERT(alphabet);
return (offset << (amount*alphabet->get_num_bits()));
}
/** shift symbol to the right by amount (taking care of custom symbol sizes)
*
* @param symbol symbol to shift
* @param amount amount to shift the symbol
* @return shifted symbol
*/
inline ST shift_symbol(ST symbol, int32_t amount)
{
ASSERT(alphabet);
return (symbol >> (amount*alphabet->get_num_bits()));
}
/** load features from file
*
* @param loader File object via which to load data
*/
virtual inline void load(CFile* loader);
/** load ascii line-based string features from file.
*
* any subset is removed before
*
* @param fname filename to load from
* @param remap_to_bin if translation to other binary alphabet
* should be performed
* @param ascii_alphabet src alphabet
* @param binary_alphabet alphabet to translate to
*/
void load_ascii_file(char* fname, bool remap_to_bin=true,
EAlphabet ascii_alphabet=DNA, EAlphabet binary_alphabet=RAWDNA)
{
remove_subset();
size_t blocksize=1024*1024;
size_t required_blocksize=0;
uint8_t* dummy=new uint8_t[blocksize];
uint8_t* overflow=NULL;
int32_t overflow_len=0;
cleanup();
CAlphabet* alpha=new CAlphabet(ascii_alphabet);
CAlphabet* alpha_bin=new CAlphabet(binary_alphabet);
FILE* f=fopen(fname, "ro");
if (f)
{
num_vectors=0;
max_string_length=0;
SG_INFO("counting line numbers in file %s\n", fname);
size_t block_offs=0;
size_t old_block_offs=0;
fseek(f, 0, SEEK_END);
size_t fsize=ftell(f);
rewind(f);
if (blocksize>fsize)
blocksize=fsize;
SG_DEBUG("block_size=%ld file_size=%ld\n", blocksize, fsize);
size_t sz=blocksize;
while (sz == blocksize)
{
sz=fread(dummy, sizeof(uint8_t), blocksize, f);
bool contains_cr=false;
for (size_t i=0; i<sz; i++)
{
block_offs++;
if (dummy[i]=='\n' || (i==sz-1 && sz<blocksize))
{
num_vectors++;
contains_cr=true;
required_blocksize=CMath::max(required_blocksize, block_offs-old_block_offs);
old_block_offs=block_offs;
}
}
SG_PROGRESS(block_offs, 0, fsize, 1, "COUNTING:\t");
}
SG_INFO("found %d strings\n", num_vectors);
delete[] dummy;
blocksize=required_blocksize;
dummy=new uint8_t[blocksize];
overflow=new uint8_t[blocksize];
features=new SGString<ST>[num_vectors];
rewind(f);
sz=blocksize;
int32_t lines=0;
while (sz == blocksize)
{
sz=fread(dummy, sizeof(uint8_t), blocksize, f);
size_t old_sz=0;
for (size_t i=0; i<sz; i++)
{
if (dummy[i]=='\n' || (i==sz-1 && sz<blocksize))
{
int32_t len=i-old_sz;
//SG_PRINT("i:%d len:%d old_sz:%d\n", i, len, old_sz);
max_string_length=CMath::max(max_string_length, len+overflow_len);
features[lines].length=len;
features[lines].string=new ST[len];
if (remap_to_bin)
{
for (int32_t j=0; j<overflow_len; j++)
features[lines].string[j]=alpha->remap_to_bin(overflow[j]);
for (int32_t j=0; j<len; j++)
features[lines].string[j+overflow_len]=alpha->remap_to_bin(dummy[old_sz+j]);
alpha->add_string_to_histogram(&dummy[old_sz], len);
alpha_bin->add_string_to_histogram(features[lines].string, features[lines].length);
}
else
{
for (int32_t j=0; j<overflow_len; j++)
features[lines].string[j]=overflow[j];
for (int32_t j=0; j<len; j++)
features[lines].string[j+overflow_len]=dummy[old_sz+j];
alpha->add_string_to_histogram(&dummy[old_sz], len);
alpha->add_string_to_histogram(features[lines].string, features[lines].length);
}
// clear overflow
overflow_len=0;
//CMath::display_vector(features[lines].string, len);
old_sz=i+1;
lines++;
SG_PROGRESS(lines, 0, num_vectors, 1, "LOADING:\t");
}
}
for (size_t i=old_sz; i<sz; i++)
overflow[i-old_sz]=dummy[i];
overflow_len=sz-old_sz;
}
if (alpha->check_alphabet_size() && alpha->check_alphabet())
{
SG_INFO("file successfully read\n");
SG_INFO("max_string_length=%d\n", max_string_length);
SG_INFO("num_strings=%d\n", num_vectors);
}
fclose(f);
}
delete[] dummy;
SG_UNREF(alphabet);
if (remap_to_bin)
alphabet=alpha_bin;
else
alphabet=alpha;
SG_REF(alphabet);
num_symbols=alphabet->get_num_symbols();
}
/** load fasta file as string features
*
* any subset is removed before
*
* @param fname filename to load from
* @param ignore_invalid if set to true, characters other than A,C,G,T are converted to A
* @return if loading was successful
*/
bool load_fasta_file(const char* fname, bool ignore_invalid=false)
{
remove_subset();
int32_t i=0;
uint64_t len=0;
uint64_t offs=0;
int32_t num=0;
int32_t max_len=0;
CMemoryMappedFile<char> f(fname);
while (true)
{
char* s=f.get_line(len, offs);
if (!s)
break;
if (len>0 && s[0]=='>')
num++;
}
if (num==0)
SG_ERROR("No fasta hunks (lines starting with '>') found\n");
cleanup();
SG_UNREF(alphabet);
alphabet=new CAlphabet(DNA);
num_symbols=alphabet->get_num_symbols();
SGString<ST>* strings=new SGString<ST>[num];
offs=0;
for (i=0;i<num; i++)
{
uint64_t id_len=0;
char* id=f.get_line(id_len, offs);
char* fasta=f.get_line(len, offs);
char* s=fasta;
int32_t fasta_len=0;
int32_t spanned_lines=0;
while (true)
{
if (!s || len==0)
SG_ERROR("Error reading fasta entry in line %d len=%ld", 4*i+1, len);
if (s[0]=='>' || offs==f.get_size())
{
offs-=len+1; // seek to beginning
if (offs==f.get_size())
{
SG_DEBUG("at EOF\n");
fasta_len+=len;
}
len=fasta_len-spanned_lines;
strings[i].string=new ST[len];
strings[i].length=len;
ST* str=strings[i].string;
int32_t idx=0;
SG_DEBUG("'%.*s', len=%d, spanned_lines=%d\n", (int32_t) id_len, id, (int32_t) len, (int32_t) spanned_lines);
for (int32_t j=0; j<fasta_len; j++)
{
if (fasta[j]=='\n')
continue;
ST c=(ST) fasta[j];
if (ignore_invalid && !alphabet->is_valid((uint8_t) fasta[j]))
c=(ST) 'A';
if (idx>=len)
SG_ERROR("idx=%d j=%d fasta_len=%d, spanned_lines=%d str='%.*s'\n", idx, j, fasta_len, spanned_lines, idx, str);
str[idx++]=c;
}
max_len=CMath::max(max_len, strings[i].length);
break;
}
spanned_lines++;
fasta_len+=len+1; // including '\n'
s=f.get_line(len, offs);
}
}
return set_features(strings, num, max_len);
}
/** load fastq file as string features
*
* removes subset beforehand
*
* @param fname filename to load from
* @param ignore_invalid if set to true, characters other than A,C,G,T are converted to A
* @param bitremap_in_single_string if set to true, do binary embedding of symbols
* @return if loading was successful
*/
bool load_fastq_file(const char* fname,
bool ignore_invalid=false, bool bitremap_in_single_string=false)
{
remove_subset();
CMemoryMappedFile<char> f(fname);
int32_t i=0;
uint64_t len=0;
uint64_t offs=0;
int32_t num=f.get_num_lines();
int32_t max_len=0;
if (num%4)
SG_ERROR("Number of lines must be divisible by 4 in fastq files\n");
num/=4;
cleanup();
SG_UNREF(alphabet);
alphabet=new CAlphabet(DNA);
SGString<ST>* strings;
ST* str;
if (bitremap_in_single_string)
{
strings=new SGString<ST>[1];
strings[0].string=new ST[num];
strings[0].length=num;
f.get_line(len, offs);
f.get_line(len, offs);
order=len;
max_len=num;
offs=0;
original_num_symbols=alphabet->get_num_symbols();
int32_t max_val=alphabet->get_num_bits();
str=new ST[len];
}
else
strings=new SGString<ST>[num];
for (i=0;i<num; i++)
{
if (!f.get_line(len, offs))
SG_ERROR("Error reading 'read' identifier in line %d", 4*i);
char* s=f.get_line(len, offs);
if (!s || len==0)
SG_ERROR("Error reading 'read' in line %d len=%ld", 4*i+1, len);
if (bitremap_in_single_string)
{
if (len!=order)
SG_ERROR("read in line %d not of length %d (is %d)\n", 4*i+1, order, len);
for (int32_t j=0; j<order; j++)
str[j]=(ST) alphabet->remap_to_bin((uint8_t) s[j]);
strings[0].string[i]=embed_word(str, order);
}
else
{
strings[i].string=new ST[len];
strings[i].length=len;
str=strings[i].string;
if (ignore_invalid)
{
for (int32_t j=0; j<len; j++)
{
if (alphabet->is_valid((uint8_t) s[j]))
str[j]= (ST) s[j];
else
str[j]= (ST) 'A';
}
}
else
{
for (int32_t j=0; j<len; j++)
str[j]= (ST) s[j];
}
max_len=CMath::max(max_len, (int32_t) len);
}
if (!f.get_line(len, offs))
SG_ERROR("Error reading 'read' quality identifier in line %d", 4*i+2);
if (!f.get_line(len, offs))
SG_ERROR("Error reading 'read' quality in line %d", 4*i+3);