SHOGUN  6.1.3
SpectrumRBFKernel.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Written (W) 1999-2008 Gunnar Raetsch
9  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
12 #include <vector>
13 
14 #include <shogun/lib/common.h>
15 #include <shogun/io/SGIO.h>
16 #include <shogun/lib/Signal.h>
17 #include <shogun/lib/Trie.h>
18 #include <shogun/base/Parallel.h>
19 
25 
26 #include <vector>
27 #include <string>
28 #include <fstream>
29 
30 #include <assert.h>
31 
32 using namespace shogun;
33 
35  : CStringKernel<char>(0)
36 {
37  init();
39 }
40 
41 CSpectrumRBFKernel::CSpectrumRBFKernel (int32_t size, float64_t *AA_matrix_, int32_t degree_, float64_t width_)
42  : CStringKernel<char>(size), alphabet(NULL), degree(degree_), width(width_), sequences(NULL), string_features(NULL), nof_sequences(0), max_sequence_length(0)
43 {
44  init();
46 
47  target_letter_0=-1 ;
48 
50 
51  sg_memcpy(AA_matrix.matrix, AA_matrix_, 128*128*sizeof(float64_t)) ;
52 
54  SGStringList<char> string_list;
55  string_list.strings = sequences;
56  string_list.num_strings = nof_sequences;
58 
59  //string_features = new CStringFeatures<char>(sequences, nof_sequences, max_sequence_length, PROTEIN);
63 }
64 
66  CStringFeatures<char>* l, CStringFeatures<char>* r, int32_t size, float64_t* AA_matrix_, int32_t degree_, float64_t width_)
67 : CStringKernel<char>(size), alphabet(NULL), degree(degree_), width(width_), sequences(NULL), string_features(NULL), nof_sequences(0), max_sequence_length(0)
68 {
69  target_letter_0=-1 ;
70 
72  sg_memcpy(AA_matrix.matrix, AA_matrix_, 128*128*sizeof(float64_t)) ;
73 
74  init(l, r);
76 }
77 
79 {
80  cleanup();
82  SG_FREE(sequences);
83 }
84 
86 {
87 
88  int32_t aa_to_index[128];//profile
89  aa_to_index[(uint8_t) 'A'] = 0;
90  aa_to_index[(uint8_t) 'R'] = 1;
91  aa_to_index[(uint8_t) 'N'] = 2;
92  aa_to_index[(uint8_t) 'D'] = 3;
93  aa_to_index[(uint8_t) 'C'] = 4;
94  aa_to_index[(uint8_t) 'Q'] = 5;
95  aa_to_index[(uint8_t) 'E'] = 6;
96  aa_to_index[(uint8_t) 'G'] = 7;
97  aa_to_index[(uint8_t) 'H'] = 8;
98  aa_to_index[(uint8_t) 'I'] = 9;
99  aa_to_index[(uint8_t) 'L'] = 10;
100  aa_to_index[(uint8_t) 'K'] = 11;
101  aa_to_index[(uint8_t) 'M'] = 12;
102  aa_to_index[(uint8_t) 'F'] = 13;
103  aa_to_index[(uint8_t) 'P'] = 14;
104  aa_to_index[(uint8_t) 'S'] = 15;
105  aa_to_index[(uint8_t) 'T'] = 16;
106  aa_to_index[(uint8_t) 'W'] = 17;
107  aa_to_index[(uint8_t) 'Y'] = 18;
108  aa_to_index[(uint8_t) 'V'] = 19;
109  SG_DEBUG("initializing background\n")
110  double background[20]; // profile
111  background[0]=0.0799912015849807; //A
112  background[1]=0.0484482507611578;//R
113  background[2]=0.044293531582512;//N
114  background[3]=0.0578891399707563;//D
115  background[4]=0.0171846021407367;//C
116  background[5]=0.0380578923048682;//Q
117  background[6]=0.0638169929675978;//E
118  background[7]=0.0760659374742852;//G
119  background[8]=0.0223465499452473;//H
120  background[9]=0.0550905793661343;//I
121  background[10]=0.0866897071203864;//L
122  background[11]=0.060458245507428;//K
123  background[12]=0.0215379186368154;//M
124  background[13]=0.0396348024787477;//F
125  background[14]=0.0465746314476874;//P
126  background[15]=0.0630028230885602;//S
127  background[16]=0.0580394726014824;//T
128  background[17]=0.0144991866213453;//W
129  background[18]=0.03635438623143;//Y
130  background[19]=0.0700241481678408;//V
131 
132 
133  std::vector<std::string> seqs;
134  //int32_t nof_sequences = 7329;
135 
136  double C = 0.8;
137  const char *filename="/fml/ag-raetsch/home/toussaint/scp/aawd_compbio_workshop/code_nora/data/profile/profiles";
138  std::ifstream fin(filename);
139 
140  SG_DEBUG("Reading profiles from %s\n", filename)
141  std::string line;
142  while (!fin.eof())
143  {
144  std::getline(fin, line);
145 
146  if (line[0] == '>') // new sequence
147  {
148  int idx = line.find_first_of(' ');
149  sequence_labels.push_back(line.substr(1,idx-1));
150  std::getline(fin, line);
151  std::string orig_sequence = line;
152  std::string sequence="";
153 
154  int len_line = line.length();
155 
156  // skip 3 lines
157 
158  std::getline(fin, line);
159  std::getline(fin, line);
160  std::getline(fin, line);
161 
162  profiles.push_back(std::vector<double>());
163 
164  std::vector<double>& curr_profile = profiles.back();
165  for (int i=0; i < len_line; ++i)
166  {
167  std::getline(fin, line);
168  int a = line.find_first_not_of(' '); // index position
169  int b = line.find_first_of(' ', a); // index position
170  a = line.find_first_not_of(' ', b); // aa position
171  b = line.find_first_of(' ', a); // aa position
172  std::string aa=line.substr(a,b-a);
173  if (0) //(aa =="B" || aa == "X" || aa == "Z")
174  {
175  int pos = seqs.size()+1;
176  SG_DEBUG("Skipping aa in sequence %d\n", pos)
177  continue;
178  }
179  else
180  {
181  sequence += aa;
182 
183  a = line.find_first_not_of(' ', b); // beginning of block to ignore
184  b = line.find_first_of(' ', a); // aa position
185 
186  for (int j=0; j < 19; ++j)
187  {
188  a = line.find_first_not_of(' ', b);
189  b = line.find_first_of(' ', a);
190  }
191 
192  int all_zeros = 1;
193  // interesting block
194  for (int j=0; j < 20; ++j)
195  {
196  a = line.find_first_not_of(' ', b);
197  b = line.find_first_of(' ', a);
198  double p = atof(line.substr(a, b-a).c_str());
199  if (p > 0)
200  {
201  all_zeros = 0;
202  }
203  double value = -1* std::log(C*(p/100)+(1-C)*background[j]); // taken from Leslie's example, C actually corresponds to 1/(1+C)
204  curr_profile.push_back(value);
205  //SG_DEBUG("seq %d aa %d value %f p %f bg %f\n", i, j, value,p, background[j])
206  }
207 
208  if (all_zeros)
209  {
210  SG_DEBUG(">>>>>>>>>>>>>>> all zeros")
211  if (aa !="B" && aa != "X" && aa != "Z")
212  {
213  //profile[i][temp_profile_index]=-log(C+(1-C)*background[re_candidate[temp_profile_index]]);
214  int32_t aa_index = aa_to_index[(int)aa.c_str()[0]];
215  double value = -1* std::log(C+(1-C)*background[aa_index]); // taken from Leslie's example, C actually corresponds to 1/(1+C)
216  SG_DEBUG("before %f\n", profiles.back()[(i-1) * 20 + aa_index])
217  curr_profile[(i*20) + aa_index] = value;
218  SG_DEBUG(">>> aa %c \t %d \t %f\n", aa.c_str()[0], aa_index, value)
219 
220  /*
221  for (int z=0; z <20; ++z)
222  {
223  SG_DEBUG(" %d \t %f\t", z, curr_profile[z])
224  }
225  SG_DEBUG("\n")
226  */
227  }
228  }
229  }
230  }
231 
232  if (curr_profile.size() != 20 * sequence.length())
233  {
234  SG_ERROR("Something's wrong with the profile.\n")
235  break;
236  }
237 
238  seqs.push_back(sequence);
239 
240 
241  /*
242  // 6 irrelevant lines
243  for (int i=0; i < 6; ++i)
244  {
245  std::getline(fin, line);
246  }
247  //
248  */
249  }
250  }
251 
252  fin.close();
253 
254  nof_sequences = seqs.size();
255  sequences = SG_MALLOC(SGString<char>, nof_sequences);
256 
257  int max_len = 0;
258  for (int i=0; i < nof_sequences; ++i)
259  {
260  int len = seqs[i].length();
261  sequences[i].string = SG_MALLOC(char, len+1);
262  sequences[i].slen = len;
263  strcpy(sequences[i].string, seqs[i].c_str());
264 
265  if (len > max_len) max_len = len;
266  }
267 
268  max_sequence_length = max_len;
269  //string_features = new CStringFeatures<char>(sequences, nof_sequences, max_sequence_length, PROTEIN);
270 
271 }
272 
273 bool CSpectrumRBFKernel::init(CFeatures* l, CFeatures* r)
274 {
275  // >> profile
276 /*
277  read_profiles_and_sequences();
278  l = string_features;
279  r = string_features;
280  */
281  // << profile
282 
283  int32_t lhs_changed=(lhs!=l);
284  int32_t rhs_changed=(rhs!=r);
285 
287 
288  SG_DEBUG("lhs_changed: %i\n", lhs_changed)
289  SG_DEBUG("rhs_changed: %i\n", rhs_changed)
290 
293 
295  alphabet=sf_l->get_alphabet();
296  CAlphabet* ralphabet=sf_r->get_alphabet();
297 
298  if (!((alphabet->get_alphabet()==DNA) || (alphabet->get_alphabet()==RNA)))
299  properties &= ((uint64_t) (-1)) ^ (KP_LINADD | KP_BATCHEVALUATION);
300 
301  ASSERT(ralphabet->get_alphabet()==alphabet->get_alphabet())
302  SG_UNREF(ralphabet);
303 
304 
305  return init_normalizer();
306 }
307 
309 {
310 
312  alphabet=NULL;
313 
315 }
316 
317 inline bool isaa(char c)
318 {
319  if (c<65 || c>89 || c=='B' || c=='J' || c=='O' || c=='U' || c=='X' || c=='Z')
320  return false ;
321  return true ;
322 }
323 
324 float64_t CSpectrumRBFKernel::AA_helper(const char* path, const int seq_degree, const char* joint_seq, unsigned int index)
325 {
326  //const char* AA = "ARNDCQEGHILKMFPSTWYV";
327  float64_t diff=0.0 ;
328 
329  for (int i=0; i<seq_degree; i++)
330  {
331  if (!isaa(path[i])||!isaa(joint_seq[index+i]))
332  diff+=1.4 ;
333  else
334  {
335  diff += AA_matrix.matrix[ (path[i]-1)*128 + path[i] - 1] ;
336  diff -= 2*AA_matrix.matrix[ (path[i]-1)*128 + joint_seq[index+i] - 1] ;
337  diff += AA_matrix.matrix[ (joint_seq[index+i]-1)*128 + joint_seq[index+i] - 1] ;
338  if (CMath::is_nan(diff))
339  fprintf(stderr, "nan occurred: '%c' '%c'\n", path[i], joint_seq[index+i]) ;
340  }
341  }
342 
343  return exp( - diff/width) ;
344 }
345 
346 float64_t CSpectrumRBFKernel::compute(int32_t idx_a, int32_t idx_b)
347 {
348  int32_t alen, blen;
349  bool afree, bfree;
350 
351  char* avec = ((CStringFeatures<char>*) lhs)->get_feature_vector(idx_a, alen, afree);
352  char* bvec = ((CStringFeatures<char>*) rhs)->get_feature_vector(idx_b, blen, bfree);
353 
354  float64_t result=0;
355  for (int32_t i=0; i<alen; i++)
356  {
357  for (int32_t j=0; j<blen; j++)
358  {
359  if ((i+degree<=alen) && (j+degree<=blen))
360  result += AA_helper(&(avec[i]), degree, bvec, j) ;
361  }
362  }
363 
364  ((CStringFeatures<char>*) lhs)->free_feature_vector(avec, idx_a, afree);
365  ((CStringFeatures<char>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
366  return result;
367 }
368 
370  float64_t* AA_matrix_)
371 {
372 
373  if (AA_matrix_)
374  {
375  SG_DEBUG("Setting AA_matrix\n")
376  sg_memcpy(AA_matrix.matrix, AA_matrix_, 128*128*sizeof(float64_t)) ;
377  return true ;
378  }
379 
380  return false;
381 }
382 
384 {
385  SG_ADD(&degree, "degree", "degree of the kernel", MS_AVAILABLE);
386  SG_ADD(&AA_matrix, "AA_matrix", "128*128 scalar product matrix", MS_NOT_AVAILABLE);
387  SG_ADD(&width, "width", "width of Gaussian", MS_AVAILABLE);
388  SG_ADD(&nof_sequences, "nof_sequences", "length of the sequence",
390  m_parameters->add_vector(&sequences, &nof_sequences, "sequences", "the sequences as a part of profile");
392  "max_sequence_length", "max length of the sequence", MS_NOT_AVAILABLE);
393 }
394 
396 {
397  SG_ADD((CSGObject**)&alphabet, "alphabet", "the alphabet used by kernel",
399 }
400 
401 void CSpectrumRBFKernel::init()
402 {
403  alphabet = NULL;
404  degree = 0;
405  width = 0.0;
406  sequences = NULL;
407  string_features = NULL;
408  nof_sequences = 0;
410 
411  initialized = false;
412 
413  max_mismatch = 0;
414  target_letter_0 = 0;
415 }
RNA - letters A,C,G,U.
Definition: Alphabet.h:32
virtual void cleanup()
Definition: Kernel.cpp:172
DNA - letters A,C,G,T.
Definition: Alphabet.h:26
virtual bool init(CFeatures *l, CFeatures *r)
SGString< T > * strings
Definition: SGStringList.h:88
EAlphabet get_alphabet() const
Definition: Alphabet.h:130
#define SG_ERROR(...)
Definition: SGIO.h:128
The class Alphabet implements an alphabet and alphabet utility functions.
Definition: Alphabet.h:91
Parameter * m_parameters
Definition: SGObject.h:609
float64_t AA_helper(const char *path, const int degree, const char *joint_seq, unsigned int index)
CStringFeatures< char > * string_features
IUPAC_AMINO_ACID.
Definition: Alphabet.h:53
bool set_AA_matrix(float64_t *AA_matrix_)
#define SG_REF(x)
Definition: SGObject.h:52
#define ASSERT(x)
Definition: SGIO.h:176
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:124
SGString< char > * sequences
index_t slen
Definition: SGString.h:79
std::vector< std::vector< float64_t > > profiles
double float64_t
Definition: common.h:60
virtual bool init_normalizer()
Definition: Kernel.cpp:167
bool isaa(char c)
CFeatures * rhs
feature vectors to occur on right hand side
#define SG_UNREF(x)
Definition: SGObject.h:53
void add_vector(bool **param, index_t *length, const char *name, const char *description="")
Definition: Parameter.cpp:335
#define SG_DEBUG(...)
Definition: SGIO.h:106
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
CFeatures * lhs
feature vectors to occur on left hand side
static int is_nan(double f)
checks whether a float is nan
Definition: Math.cpp:210
The class Features is the base class of all feature objects.
Definition: Features.h:69
#define SG_ADD(...)
Definition: SGObject.h:93
float64_t compute(int32_t idx_a, int32_t idx_b)
Template class StringKernel, is the base class of all String Kernels.
Definition: StringKernel.h:26
template class SGStringList
Definition: SGObject.h:46
SGMatrix< float64_t > AA_matrix
std::vector< std::string > sequence_labels

SHOGUN Machine Learning Toolbox - Documentation