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

SHOGUN Machine Learning Toolbox - Documentation