-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
StringFeatures.cpp
2105 lines (1758 loc) · 52.4 KB
/
StringFeatures.cpp
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
#include <shogun/base/Parameter.h>
#include <shogun/base/progress.h>
#include <shogun/features/StringFeatures.h>
#include <shogun/io/MemoryMappedFile.h>
#include <shogun/io/SGIO.h>
#include <shogun/lib/SGStringList.h>
#include <shogun/mathematics/Math.h>
#include <shogun/preprocessor/Preprocessor.h>
#include <shogun/preprocessor/StringPreprocessor.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include <tchar.h>
#include <strsafe.h>
#include <vector>
#else
#include <unistd.h>
#endif
namespace shogun
{
template<class ST> CStringFeatures<ST>::CStringFeatures() : CFeatures(0)
{
init();
alphabet=new CAlphabet();
}
template<class ST> CStringFeatures<ST>::CStringFeatures(EAlphabet alpha) : CFeatures(0)
{
init();
alphabet=new CAlphabet(alpha);
SG_REF(alphabet);
num_symbols=alphabet->get_num_symbols();
original_num_symbols=num_symbols;
}
template<class ST> CStringFeatures<ST>::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);
}
template<class ST> CStringFeatures<ST>::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);
}
template<class ST> CStringFeatures<ST>::CStringFeatures(CAlphabet* alpha)
: CFeatures(0)
{
init();
ASSERT(alpha)
SG_REF(alpha);
alphabet=alpha;
num_symbols=alphabet->get_num_symbols();
original_num_symbols=num_symbols;
}
template<class ST> CStringFeatures<ST>::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=SG_MALLOC(SGString<ST>, orig.num_vectors);
for (int32_t i=0; i<num_vectors; i++)
{
features[i].string=SG_MALLOC(ST, orig.features[i].slen);
features[i].slen=orig.features[i].slen;
sg_memcpy(features[i].string, orig.features[i].string, sizeof(ST)*orig.features[i].slen);
}
}
if (orig.symbol_mask_table)
{
symbol_mask_table=SG_MALLOC(ST, 256);
symbol_mask_table_len=256;
for (int32_t i=0; i<256; i++)
symbol_mask_table[i]=orig.symbol_mask_table[i];
}
m_subset_stack=orig.m_subset_stack;
SG_REF(m_subset_stack);
}
template<class ST> CStringFeatures<ST>::CStringFeatures(CFile* loader, EAlphabet alpha)
: CFeatures(), num_vectors(0),
features(NULL), single_string(NULL), length_of_single_string(0),
max_string_length(0), order(0),
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);
}
template<class ST> CStringFeatures<ST>::~CStringFeatures()
{
cleanup();
SG_UNREF(alphabet);
}
template<class ST> void CStringFeatures<ST>::cleanup()
{
remove_all_subsets();
if (single_string)
{
SG_FREE(single_string);
single_string=NULL;
}
else
cleanup_feature_vectors(0, num_vectors-1);
/*
if (single_string)
{
SG_FREE(single_string);
single_string=NULL;
}
else
cleanup_feature_vectors(0, num_vectors-1);
*/
num_vectors=0;
SG_FREE(features);
SG_FREE(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);
}
template<class ST> void CStringFeatures<ST>::cleanup_feature_vector(int32_t num)
{
ASSERT(num<get_num_vectors())
if (features)
{
int32_t real_num=m_subset_stack->subset_idx_conversion(num);
SG_FREE(features[real_num].string);
features[real_num].string=NULL;
features[real_num].slen=0;
determine_maximum_string_length();
}
}
template<class ST> void CStringFeatures<ST>::cleanup_feature_vectors(int32_t start, int32_t stop)
{
if (features && get_num_vectors())
{
ASSERT(start<get_num_vectors())
ASSERT(stop<get_num_vectors())
for (int32_t i=start; i<=stop; i++)
{
int32_t real_num=m_subset_stack->subset_idx_conversion(i);
SG_FREE(features[real_num].string);
features[real_num].string=NULL;
features[real_num].slen=0;
}
determine_maximum_string_length();
}
}
template<class ST> EFeatureClass CStringFeatures<ST>::get_feature_class() const { return C_STRING; }
template<class ST> EFeatureType CStringFeatures<ST>::get_feature_type() const { return F_UNKNOWN; }
template<class ST> CAlphabet* CStringFeatures<ST>::get_alphabet()
{
SG_REF(alphabet);
return alphabet;
}
template<class ST> CFeatures* CStringFeatures<ST>::duplicate() const
{
return new CStringFeatures<ST>(*this);
}
template<class ST> SGVector<ST> CStringFeatures<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=SG_MALLOC(ST, l);
sg_memcpy(dst, vec, l*sizeof(ST));
free_feature_vector(vec, num, free_vec);
return SGVector<ST>(dst, l, true);
}
template<class ST> void CStringFeatures<ST>::set_feature_vector(SGVector<ST> vector, int32_t num)
{
ASSERT(features)
if (m_subset_stack->has_subsets())
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].slen=vector.vlen;
features[num].string=SG_MALLOC(ST, vector.vlen);
sg_memcpy(features[num].string, vector.vector, vector.vlen*sizeof(ST));
determine_maximum_string_length();
}
template<class ST> void CStringFeatures<ST>::enable_on_the_fly_preprocessing()
{
preprocess_on_get=true;
}
template<class ST> void CStringFeatures<ST>::disable_on_the_fly_preprocessing()
{
preprocess_on_get=false;
}
template<class ST> ST* CStringFeatures<ST>::get_feature_vector(int32_t num, int32_t& len, bool& dofree)
{
ASSERT(features)
if (num>=get_num_vectors())
SG_ERROR("Requested feature vector with index %d while total num is", num, get_num_vectors())
int32_t real_num=m_subset_stack->subset_idx_conversion(num);
if (!preprocess_on_get)
{
dofree=false;
len=features[real_num].slen;
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);
SG_FREE(tmp_feat_before);
tmp_feat_before=feat;
}
}
// TODO: implement caching
return feat;
}
}
template<class ST> CStringFeatures<ST>* 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);
}
template<class ST> SGString<ST>* CStringFeatures<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=SG_MALLOC(SGString<ST>, num_vec);
for (int32_t i=0; i<num_vec; i++)
{
sf[i].string=SG_MALLOC(ST, num_feat);
sf[i].slen=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;
}
template<class ST> void CStringFeatures<ST>::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=m_subset_stack->subset_idx_conversion(num);
if (feature_cache)
feature_cache->unlock_entry(real_num);
if (dofree)
SG_FREE(feat_vec);
}
template<class ST> void CStringFeatures<ST>::free_feature_vector(SGVector<ST> feat_vec, int32_t num)
{
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=m_subset_stack->subset_idx_conversion(num);
if (feature_cache)
feature_cache->unlock_entry(real_num);
}
template<class ST> ST CStringFeatures<ST>::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;
}
template<class ST> int32_t CStringFeatures<ST>::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;
}
template<class ST> int32_t CStringFeatures<ST>::get_max_vector_length()
{
return max_string_length;
}
template<class ST> int32_t CStringFeatures<ST>::get_num_vectors() const
{
return m_subset_stack->has_subsets() ? m_subset_stack->get_size() : num_vectors;
}
template<class ST> floatmax_t CStringFeatures<ST>::get_num_symbols() { return num_symbols; }
template<class ST> floatmax_t CStringFeatures<ST>::get_max_num_symbols() { return CMath::powl(2,sizeof(ST)*8); }
template<class ST> floatmax_t CStringFeatures<ST>::get_original_num_symbols() { return original_num_symbols; }
template<class ST> int32_t CStringFeatures<ST>::get_order() { return order; }
template<class ST> ST CStringFeatures<ST>::get_masked_symbols(ST symbol, uint8_t mask)
{
ASSERT(symbol_mask_table)
return symbol_mask_table[mask] & symbol;
}
template<class ST> ST CStringFeatures<ST>::shift_offset(ST offset, int32_t amount)
{
ASSERT(alphabet)
return (offset << (amount*alphabet->get_num_bits()));
}
template<class ST> ST CStringFeatures<ST>::shift_symbol(ST symbol, int32_t amount)
{
ASSERT(alphabet)
return (symbol >> (amount*alphabet->get_num_bits()));
}
template<class ST> void CStringFeatures<ST>::load_ascii_file(char* fname, bool remap_to_bin,
EAlphabet ascii_alphabet, EAlphabet binary_alphabet)
{
remove_all_subsets();
size_t blocksize=1024*1024;
size_t required_blocksize=0;
uint8_t* dummy=SG_MALLOC(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)
auto pb = SG_PROGRESS(range(fsize));
size_t sz=blocksize;
while (sz == blocksize)
{
sz=fread(dummy, sizeof(uint8_t), blocksize, f);
for (size_t i=0; i<sz; i++)
{
block_offs++;
if (dummy[i]=='\n' || (i==sz-1 && sz<blocksize))
{
num_vectors++;
required_blocksize=CMath::max(required_blocksize, block_offs-old_block_offs);
old_block_offs=block_offs;
}
}
pb.print_progress();
}
pb.complete();
SG_INFO("found %d strings\n", num_vectors)
SG_FREE(dummy);
blocksize=required_blocksize;
dummy=SG_MALLOC(uint8_t, blocksize);
overflow=SG_MALLOC(uint8_t, blocksize);
features=SG_MALLOC(SGString<ST>, num_vectors);
auto pb2 =
PRange<int>(range(num_vectors), *this->io, "LOADING: ", UTF8, []() {
return true;
});
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].slen=len;
features[lines].string=SG_MALLOC(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].slen);
}
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].slen);
}
// clear overflow
overflow_len=0;
//CMath::display_vector(features[lines].string, len);
old_sz=i+1;
lines++;
pb2.print_progress();
}
}
pb2.complete();
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);
}
SG_FREE(dummy);
SG_FREE(overflow);
SG_UNREF(alphabet);
if (remap_to_bin)
{
alphabet=alpha_bin;
SG_UNREF(alpha);
}
else
{
alphabet=alpha;
SG_UNREF(alpha_bin);
}
SG_REF(alphabet);
num_symbols=alphabet->get_num_symbols();
}
template<class ST> bool CStringFeatures<ST>::load_fasta_file(const char* fname, bool ignore_invalid)
{
remove_all_subsets();
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=SG_MALLOC(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=SG_MALLOC(ST, len);
strings[i].slen=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 (uint64_t(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].slen);
break;
}
spanned_lines++;
fasta_len+=len+1; // including '\n'
s=f.get_line(len, offs);
}
}
return set_features(strings, num, max_len);
}
template<class ST> bool CStringFeatures<ST>::load_fastq_file(const char* fname,
bool ignore_invalid, bool bitremap_in_single_string)
{
remove_all_subsets();
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=NULL;
if (bitremap_in_single_string)
{
strings=SG_MALLOC(SGString<ST>, 1);
strings[0].string=SG_MALLOC(ST, num);
strings[0].slen=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();
str=SG_MALLOC(ST, len);
}
else
strings=SG_MALLOC(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!=(uint64_t) 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=SG_MALLOC(ST, len);
strings[i].slen=len;
str=strings[i].string;
if (ignore_invalid)
{
for (uint64_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 (uint64_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)
}
if (bitremap_in_single_string)
num=1;
num_vectors=num;
max_string_length=max_len;
features=strings;
return true;
}
template<class ST> bool CStringFeatures<ST>::load_from_directory(char* dirname)
{
remove_all_subsets();
struct dirent **namelist;
int32_t n;
SGIO::set_dirname(dirname);
SG_DEBUG("dirname '%s'\n", dirname)
#ifdef _WIN32
TCHAR search_dir[MAX_PATH];
WIN32_FIND_DATA ffd;
LARGE_INTEGER filesize;
HANDLE h_find = INVALID_HANDLE_VALUE;
StringCchCopy(search_dir, MAX_PATH, dirname);
StringCchCat(search_dir, MAX_PATH, TEXT("\\*"));
h_find = FindFirstFile(search_dir, &ffd);
if (INVALID_HANDLE_VALUE == h_find)
{
SG_ERROR("Error finding finds in %s\n", dirname)
return false;
}
std::vector<struct dirent*> files;
do
{
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
{
struct dirent* d = SG_MALLOC(struct dirent, 1);
StringCchCopy(d->d_name, MAX_PATH, ffd.cFileName);
files.push_back(d);
n++;
}
}
while (FindNextFile(h_find, &ffd) != 0);
namelist = &files[0];
FindClose(h_find);
#else
n=scandir(dirname, &namelist, &SGIO::filter, alphasort);
#endif
if (n <= 0)
{
SG_ERROR("error calling scandir - no files found\n")
return false;
}
else
{
SGString<ST>* strings=NULL;
int32_t num=0;
int32_t max_len=-1;
//usually n==num_vec, but it might not in race conditions
//(file perms modified, file erased)
strings=SG_MALLOC(SGString<ST>, n);
for (int32_t i=0; i<n; i++)
{
char* fname=SGIO::concat_filename(namelist[i]->d_name);
struct stat s;
off_t filesize=0;
if (!stat(fname, &s) && s.st_size>0)
{
filesize=s.st_size/sizeof(ST);
FILE* f=fopen(fname, "ro");
if (f)
{
ST* str=SG_MALLOC(ST, filesize);
SG_DEBUG("%s:%ld\n", fname, (int64_t) filesize)
if (fread(str, sizeof(ST), filesize, f)!=(size_t) filesize)
SG_ERROR("failed to read file\n")
strings[num].string=str;
strings[num].slen=filesize;
max_len=CMath::max(max_len, strings[num].slen);
num++;
fclose(f);
}
}
else
SG_ERROR("empty or non readable file \'%s\'\n", fname)
SG_FREE(namelist[i]);
}
SG_FREE(namelist);
if (num>0 && strings)
{
set_features(strings, num, max_len);
return true;
}
}
return false;
}
template<class ST> void CStringFeatures<ST>::set_features(SGStringList<ST> feats)
{
set_features(feats.strings, feats.num_strings, feats.max_string_length);
}
template<class ST> bool CStringFeatures<ST>::set_features(SGString<ST>* p_features, int32_t p_num_vectors, int32_t p_max_string_length)
{
if (m_subset_stack->has_subsets())
SG_ERROR("Cannot call set_features() with subset.\n")
if (p_features)
{
CAlphabet* alpha=new CAlphabet(alphabet->get_alphabet());
//compute histogram for char/byte
for (int32_t i=0; i<p_num_vectors; i++)
alpha->add_string_to_histogram( p_features[i].string, p_features[i].slen);
SG_INFO("max_value_in_histogram:%d\n", alpha->get_max_value_in_histogram())
SG_INFO("num_symbols_in_histogram:%d\n", alpha->get_num_symbols_in_histogram())
if (alpha->check_alphabet_size() && alpha->check_alphabet())
{
cleanup();
SG_UNREF(alphabet);
alphabet=alpha;
SG_REF(alphabet);
// TODO remove copying
features = SG_MALLOC(SGString<ST>,p_num_vectors);
sg_memcpy(features,p_features,sizeof(SGString<ST>)*p_num_vectors);
num_vectors = p_num_vectors;
max_string_length = p_max_string_length;
return true;
}
else
SG_UNREF(alpha);
}
return false;
}
template<class ST> bool CStringFeatures<ST>::append_features(CStringFeatures<ST>* sf)
{
ASSERT(sf)
if (m_subset_stack->has_subsets())
SG_ERROR("Cannot call set_features() with subset.\n")
SGString<ST>* new_features=SG_MALLOC(SGString<ST>, sf->get_num_vectors());
index_t sf_num_str=sf->get_num_vectors();
for (int32_t i=0; i<sf_num_str; i++)
{
int32_t real_i = sf->m_subset_stack->subset_idx_conversion(i);
int32_t length=sf->features[real_i].slen;
new_features[i].string=SG_MALLOC(ST, length);
sg_memcpy(new_features[i].string, sf->features[real_i].string, length);
new_features[i].slen=length;
}
return append_features(new_features, sf_num_str,
sf->max_string_length);
}
template<class ST> bool CStringFeatures<ST>::append_features(SGString<ST>* p_features, int32_t p_num_vectors, int32_t p_max_string_length)
{
if (m_subset_stack->has_subsets())
SG_ERROR("Cannot call set_features() with subset.\n")
if (!features)
return set_features(p_features, p_num_vectors, p_max_string_length);
CAlphabet* alpha=new CAlphabet(alphabet->get_alphabet());
//compute histogram for char/byte
for (int32_t i=0; i<p_num_vectors; i++)
alpha->add_string_to_histogram( p_features[i].string, p_features[i].slen);
SG_INFO("max_value_in_histogram:%d\n", alpha->get_max_value_in_histogram())
SG_INFO("num_symbols_in_histogram:%d\n", alpha->get_num_symbols_in_histogram())
if (alpha->check_alphabet_size() && alpha->check_alphabet())
{
SG_UNREF(alpha);
for (int32_t i=0; i<p_num_vectors; i++)
alphabet->add_string_to_histogram( p_features[i].string, p_features[i].slen);
int32_t old_num_vectors=num_vectors;
num_vectors=old_num_vectors+p_num_vectors;
SGString<ST>* new_features=SG_MALLOC(SGString<ST>, num_vectors);
for (int32_t i=0; i<num_vectors; i++)
{
if (i<old_num_vectors)
{
new_features[i].string=features[i].string;