SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SNPFeatures.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) 2009 Soeren Sonnenburg
8  * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
12 #include <shogun/io/SGIO.h>
14 
15 using namespace shogun;
16 
18 {
19  SG_UNSTABLE("CSNPFeatures::CSNPFeatures()", "\n");
20 
21  strings = NULL;
22 
23  string_length = 0;
24  num_strings = 0;
25  w_dim = 0;
26 
27  normalization_const = 0.0;
28 
29  m_str_min = NULL;
30  m_str_maj = NULL;
31 }
32 
34  m_str_min(NULL), m_str_maj(NULL)
35 {
36  ASSERT(str);
37  ASSERT(str->have_same_length());
38  SG_REF(str);
39 
40  strings=str;
42  ASSERT((string_length & 1) == 0); // length divisible by 2
43  w_dim=3*string_length/2;
45  CAlphabet* alpha=str->get_alphabet();
46  ASSERT(alpha->get_alphabet()==SNP);
47  SG_UNREF(alpha);
48 
51 
52 }
53 
55  : CDotFeatures(orig), strings(orig.strings),
56  normalization_const(orig.normalization_const),
57  m_str_min(NULL), m_str_maj(NULL)
58 {
59  SG_REF(strings);
61  ASSERT((string_length & 1) == 0); // length divisible by 2
64  CAlphabet* alpha=strings->get_alphabet();
65  SG_UNREF(alpha);
66 
68 }
69 
71 {
73 }
74 
76 {
77  return w_dim;
78 }
79 
81 {
82  return w_dim/3;
83 }
84 
86 {
87  return F_UNKNOWN;
88 }
89 
91 {
92  return C_WD;
93 }
94 
96 {
97  return num_strings;
98 }
99 
100 int32_t CSNPFeatures::get_size() const
101 {
102  return sizeof(float64_t);
103 }
104 
106 {
107  return normalization_const;
108 }
109 
111 {
112  m_str_min=(uint8_t*) strdup(str);
113 }
114 
116 {
117  m_str_maj=(uint8_t*) strdup(str);
118 }
119 
121 {
122  return (char*) m_str_min;
123 }
124 
126 {
127  return (char*) m_str_maj;
128 }
129 
130 float64_t CSNPFeatures::dot(int32_t idx_a, CDotFeatures* df, int32_t idx_b)
131 {
132  ASSERT(df);
135  CSNPFeatures* sf=(CSNPFeatures*) df;
136 
137  int32_t alen, blen;
138  bool free_avec, free_bvec;
139 
140  uint8_t* avec = strings->get_feature_vector(idx_a, alen, free_avec);
141  uint8_t* bvec = sf->strings->get_feature_vector(idx_b, blen, free_bvec);
142 
143  ASSERT(alen==blen);
144  if (alen!=string_length)
145  SG_ERROR("alen (%d) !=string_length (%d)\n", alen, string_length);
146  ASSERT(m_str_min);
147  ASSERT(m_str_maj);
148 
149  float64_t total=0;
150 
151  for (int32_t i = 0; i<alen-1; i+=2)
152  {
153  int32_t sumaa=0;
154  int32_t sumbb=0;
155  int32_t sumab=0;
156 
157  uint8_t a1=avec[i];
158  uint8_t a2=avec[i+1];
159  uint8_t b1=bvec[i];
160  uint8_t b2=bvec[i+1];
161 
162  if ((a1!=a2 || a1=='0' || a1=='0') && (b1!=b2 || b1=='0' || b2=='0'))
163  sumab++;
164  else if (a1==a2 && b1==b2)
165  {
166  if (a1!=b1)
167  continue;
168 
169  if (a1==m_str_min[i])
170  sumaa++;
171  else if (a1==m_str_maj[i])
172  sumbb++;
173  else
174  {
175  SG_ERROR("The impossible happened i=%d a1=%c "
176  "a2=%c b1=%c b2=%c min=%c maj=%c\n", i, a1,a2, b1,b2, m_str_min[i], m_str_maj[i]);
177  }
178 
179  }
180  total+=sumaa+sumbb+sumab;
181  }
182 
183  strings->free_feature_vector(avec, idx_a, free_avec);
184  sf->strings->free_feature_vector(bvec, idx_b, free_bvec);
185  return total;
186 }
187 
188 float64_t CSNPFeatures::dense_dot(int32_t vec_idx1, const float64_t* vec2, int32_t vec2_len)
189 {
190  if (vec2_len != w_dim)
191  SG_ERROR("Dimensions don't match, vec2_dim=%d, w_dim=%d\n", vec2_len, w_dim);
192 
193  float64_t sum=0;
194  int32_t len;
195  bool free_vec1;
196  uint8_t* vec = strings->get_feature_vector(vec_idx1, len, free_vec1);
197  int32_t offs=0;
198 
199  for (int32_t i=0; i<len; i+=2)
200  {
201  int32_t dim=0;
202 
203  char a1=vec[i];
204  char a2=vec[i+1];
205 
206  if (a1==a2 && a1!='0' && a2!='0')
207  {
208  if (a1==m_str_min[i])
209  dim=1;
210  else if (a1==m_str_maj[i])
211  dim=2;
212  else
213  {
214  SG_ERROR("The impossible happened i=%d a1=%c a2=%c min=%c maj=%c\n",
215  i, a1,a2, m_str_min[i], m_str_maj[i]);
216  }
217  }
218 
219  sum+=vec2[offs+dim];
220  offs+=3;
221  }
222  strings->free_feature_vector(vec, vec_idx1, free_vec1);
223 
224  return sum/normalization_const;
225 }
226 
227 void CSNPFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val)
228 {
229  if (vec2_len != w_dim)
230  SG_ERROR("Dimensions don't match, vec2_dim=%d, w_dim=%d\n", vec2_len, w_dim);
231 
232  int32_t len;
233  bool free_vec1;
234  uint8_t* vec = strings->get_feature_vector(vec_idx1, len, free_vec1);
235  int32_t offs=0;
236 
237  if (abs_val)
238  alpha=CMath::abs(alpha);
239 
240  for (int32_t i=0; i<len; i+=2)
241  {
242  int32_t dim=0;
243 
244  char a1=vec[i];
245  char a2=vec[i+1];
246 
247  if (a1==a2 && a1!='0' && a2!='0')
248  {
249  if (a1==m_str_min[i])
250  dim=1;
251  else if (a1==m_str_maj[i])
252  dim=2;
253  else
254  {
255  SG_ERROR("The impossible happened i=%d a1=%c a2=%c min=%c maj=%c\n",
256  i, a1,a2, m_str_min[i], m_str_maj[i]);
257  }
258  }
259 
260  vec2[offs+dim]+=alpha;
261  offs+=3;
262  }
263  strings->free_feature_vector(vec, vec_idx1, free_vec1);
264 }
265 
266 void CSNPFeatures::find_minor_major_strings(uint8_t* minor, uint8_t* major)
267 {
268  for (int32_t i=0; i<num_strings; i++)
269  {
270  int32_t len;
271  bool free_vec;
272  uint8_t* vec = ((CStringFeatures<uint8_t>*) strings)->get_feature_vector(i, len, free_vec);
273  ASSERT(string_length==len);
274 
275  for (int32_t j=0; j<len; j++)
276  {
277  // skip sequencing errors
278  if (vec[j]=='0')
279  continue;
280 
281  if (minor[j]==0)
282  minor[j]=vec[j];
283  else if (major[j]==0 && vec[j]!=minor[j])
284  major[j]=vec[j];
285  }
286 
287  ((CStringFeatures<uint8_t>*) strings)->free_feature_vector(vec, i, free_vec);
288  }
289 }
290 
292 {
295  size_t tlen=(string_length+1)*sizeof(uint8_t);
296 
297  m_str_min=SG_CALLOC(uint8_t, tlen);
298  m_str_maj=SG_CALLOC(uint8_t, tlen);
299 
300  find_minor_major_strings(m_str_min, m_str_maj);
301 
302  if (snp)
303  snp->find_minor_major_strings(m_str_min, m_str_maj);
304 
305  for (int32_t j=0; j<string_length; j++)
306  {
307  // if only one symbol occurs use 0
308  if (m_str_min[j]==0)
309  m_str_min[j]='0';
310  if (m_str_maj[j]==0)
311  m_str_maj[j]='0';
312 
313  if (m_str_min[j]>m_str_maj[j])
315  }
316 }
317 
319 {
320  if (n==0)
321  {
324  }
325  else
327 
328  SG_DEBUG("normalization_const:%f\n", normalization_const);
329 }
330 
331 void* CSNPFeatures::get_feature_iterator(int32_t vector_index)
332 {
333  return NULL;
334 }
335 
336 bool CSNPFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator)
337 {
338  return false;
339 }
340 
342 {
343 }
344 
346 {
347  return new CSNPFeatures(*this);
348 }
349 
351 {
352  int32_t nsym=3;
353  float64_t* h= SG_CALLOC(float64_t, size_t(nsym)*string_length/2);
354 
355  float64_t* h_normalizer=SG_MALLOC(float64_t, string_length/2);
356  memset(h_normalizer, 0, string_length/2*sizeof(float64_t));
357  int32_t num_str=get_num_vectors();
358  for (int32_t i=0; i<num_str; i++)
359  {
360  int32_t len;
361  bool free_vec;
362  uint8_t* vec = strings->get_feature_vector(i, len, free_vec);
363 
364  for (int32_t j=0; j<len; j+=2)
365  {
366  int32_t dim=0;
367 
368  char a1=vec[j];
369  char a2=vec[j+1];
370 
371  if (a1==a2 && a1!='0' && a2!='0')
372  {
373  if (a1==m_str_min[j])
374  dim=1;
375  else if (a1==m_str_maj[j])
376  dim=2;
377  else
378  {
379  SG_ERROR("The impossible happened j=%d a1=%c a2=%c min=%c maj=%c\n",
380  j, a1,a2, m_str_min[j], m_str_maj[j]);
381  }
382  }
383 
384  h[int64_t(j/2)*nsym+dim]++;
385  h_normalizer[j/2]++;
386  }
387 
388  strings->free_feature_vector(vec, i, free_vec);
389  }
390 
391  if (normalize)
392  {
393  for (int32_t i=0; i<string_length/2; i++)
394  {
395  for (int32_t j=0; j<nsym; j++)
396  {
397  if (h_normalizer && h_normalizer[i])
398  h[int64_t(i)*nsym+j]/=h_normalizer[i];
399  }
400  }
401  }
402  SG_FREE(h_normalizer);
403 
404  return SGMatrix<float64_t>(h, nsym, string_length/2);
405 }
406 
408 {
409 
411  int32_t len=pos->strings->get_max_vector_length();
412 
413  float64_t* table=SG_MALLOC(float64_t, 3*2*len/2);
414 
415  SGMatrix<float64_t> p_hist=pos->get_histogram(false);
416  SGMatrix<float64_t> n_hist=neg->get_histogram(false);
417 
418 
419  for (int32_t i=0; i<3*len/2; i++)
420  {
421  table[2*i]=p_hist.matrix[i];
422  table[2*i+1]=n_hist.matrix[i];
423  }
424  return SGMatrix<float64_t>(table, 2,3*len/2);
425 }

SHOGUN Machine Learning Toolbox - Documentation