% data splitting
load tutorial_data.mat XT LT

% split into training and test data
rand('seed', 8947584375) ;
p=randperm(size(XT,2)) ;
XTE=XT(:,p(1:20000)) ;
LTE=LT(:,p(1:20000)) ;
XT=XT(:,p(20001:30000)) ;
LT=LT(:,p(20001:30000)) ;


% initialize
cache_size = 200 ;
C          = 1000 ;     % regularization paramter
sg( 'send_command', 'clean_features TRAIN' );
sg( 'send_command', 'clean_preproc' );
sg( 'send_command', 'clean_kernel');

% training features and kernel
XT_=(XT=='G' | XT=='C') ;
XT_GC=[mean(XT_(1:99,:));mean(XT_(102:end,:))];

sg('set_features', 'TRAIN', XT_GC) ;
sg('set_labels', 'TRAIN', LT) ;
%sg( 'send_command', sprintf('set_kernel GAUSSIAN REAL %i 100', cache_size) );
sg( 'send_command', sprintf('set_kernel POLY REAL %i 1 0 0', cache_size) );

% create and train SVM
sg( 'send_command', 'new_svm LIGHT') ;
sg( 'send_command', 'init_kernel TRAIN' );
sg( 'send_command', sprintf( 'c %1.2e', C ) );
sg( 'send_command', 'svm_train' );

% set test features
XTE_=(XTE=='G' | XTE=='C') ;
XTE_GC=[mean(XTE_(1:99,:));mean(XTE_(102:end,:))];

sg('set_features', 'TEST', XTE_GC) ;
sg('send_command', 'init_kernel TEST' );

O_GC = sg( 'svm_classify' );
ROC_GC=calcrocscore(O_GC, LTE)

% spectrum
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
order=1 ;

sg( 'send_command', 'clean_features TRAIN' );
sg( 'send_command', 'clean_preproc' );
sg( 'send_command', 'clean_kernel');

sg( 'send_command', 'use_linadd 0');
sg( 'send_command', 'use_batch_computation 0');
sg( 'send_command', 'svm_epsilon 1e-6');
sg( 'send_command','loglevel ALL') ; % for debugging

sg( 'set_labels', 'TRAIN', LT) ;
sg( 'send_command', sprintf('set_kernel COMBINED %i', cache_size) );

sg('add_features', 'TRAIN', XT(1:99,:)) ;
sg('send_command', sprintf('convert TRAIN SIMPLE CHAR SIMPLE WORD DNA %i %i',order,order-1)) ;
sg('send_command', 'add_preproc SORTWORD') ;
sg('send_command', 'attach_preproc TRAIN') ;
sg( 'send_command', 'add_kernel 1 COMM WORD 10 0 NO' );

sg('add_features', 'TRAIN', XT(102:end,:)) ;
sg('send_command', sprintf('convert TRAIN SIMPLE CHAR SIMPLE WORD DNA %i %i',order,order-1)) ;
sg('send_command', 'add_preproc SORTWORD') ;
sg('send_command', 'attach_preproc TRAIN') ;
sg( 'send_command', 'add_kernel 1 COMM WORD 10 0 NO' );

sg( 'send_command', 'new_svm LIGHT') ;
sg( 'send_command', 'init_kernel TRAIN' );
sg( 'send_command', sprintf( 'c %1.2e', C ) );
sg( 'send_command', 'svm_train' );
sg('send_command', 'init_kernel_optimization') ;

% set test features
sg('send_command', 'clean_features TEST' );
sg('add_features', 'TEST', XTE(1:99,:)) ;
sg('send_command', sprintf('convert TEST SIMPLE CHAR SIMPLE WORD DNA %i %i',order,order-1)) ;

sg('add_features', 'TEST', XTE(102:end,:)) ;
sg('send_command', sprintf('convert TEST SIMPLE CHAR SIMPLE WORD DNA %i %i',order,order-1)) ;

sg('send_command', 'attach_preproc TEST') ;
sg('send_command', 'init_kernel TEST' );

O_Spec = sg( 'svm_classify' );
ROC_Spec=calcrocscore(O_Spec, LTE)


% spectrum
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
order=3 ;

sg( 'send_command', 'clean_features TRAIN' );
sg( 'send_command', 'clean_preproc' );
sg( 'send_command', 'clean_kernel');

sg( 'send_command', 'use_linadd 0');
sg( 'send_command', 'use_batch_computation 0');
sg( 'send_command', 'svm_epsilon 1e-6');
sg( 'send_command','loglevel ALL') ; % for debugging

sg( 'set_labels', 'TRAIN', LT) ;
sg( 'send_command', sprintf('set_kernel COMBINED %i', cache_size) );

sg('add_features', 'TRAIN', XT(1:99,:)) ;
sg('send_command', sprintf('convert TRAIN SIMPLE CHAR SIMPLE WORD DNA %i %i',order,order-1)) ;
sg('send_command', 'add_preproc SORTWORD') ;
sg('send_command', 'attach_preproc TRAIN') ;
sg( 'send_command', 'add_kernel 1 COMM WORD 10 0 NO' );

sg('add_features', 'TRAIN', XT(102:end,:)) ;
sg('send_command', sprintf('convert TRAIN SIMPLE CHAR SIMPLE WORD DNA %i %i',order,order-1)) ;
sg('send_command', 'add_preproc SORTWORD') ;
sg('send_command', 'attach_preproc TRAIN') ;
sg( 'send_command', 'add_kernel 1 COMM WORD 10 0 NO' );

sg( 'send_command', 'new_svm LIGHT') ;
sg( 'send_command', 'init_kernel TRAIN' );
sg( 'send_command', sprintf( 'c %1.2e', C ) );
sg( 'send_command', 'svm_train' );
sg('send_command', 'init_kernel_optimization') ;

% set test features
sg('send_command', 'clean_features TEST' );
sg('add_features', 'TEST', XTE(1:99,:)) ;
sg('send_command', sprintf('convert TEST SIMPLE CHAR SIMPLE WORD DNA %i %i',order,order-1)) ;

sg('add_features', 'TEST', XTE(102:end,:)) ;
sg('send_command', sprintf('convert TEST SIMPLE CHAR SIMPLE WORD DNA %i %i',order,order-1)) ;

sg('send_command', 'attach_preproc TEST') ;
sg('send_command', 'init_kernel TEST' );

O_Spec3 = sg( 'svm_classify' );
ROC_Spec3=calcrocscore(O_Spec3, LTE)



% plot something
clf
[a,b]=calcroc(O_GC, LTE) ;
plot(a,b,'k') ;
hold on
[a,b]=calcroc(O_Spec, LTE) ;
plot(a,b,'g') ;
[a,b]=calcroc(O_Spec3, LTE) ;
plot(a,b,'g') ;
xlabel('false positive rate') ;
ylabel('true positive rate') ;
title('ROC for GC features and order Spectrum kernel') ;

%print -depsc2 gc.eps


