SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IntronList.cpp
Go to the documentation of this file.
1 
2 #include <stdio.h>
3 #include <string.h>
4 
6 #include <shogun/lib/config.h>
7 #include <shogun/io/SGIO.h>
9 
10 using namespace shogun;
11 
13 :CSGObject()
14 {
15  m_length = 0;
16  m_all_pos = NULL;
17  m_intron_list = NULL;
18  m_quality_list = NULL;
19 }
21 {
22  for (int i=0; i<m_length; i++)
23  {
26  }
30 }
31 void CIntronList::init_list(int32_t* all_pos, int32_t len)
32 {
33  m_length = len;
34  m_all_pos = SG_MALLOC(int32_t, len);
35  memcpy(m_all_pos, all_pos, len*sizeof(int32_t));
36  m_intron_list = SG_MALLOC(int32_t*, len);
37  m_quality_list = SG_MALLOC(int32_t*, len);
38  if (m_intron_list==NULL||m_quality_list==NULL)
39  SG_ERROR("IntronList: Out of mem 1");
40  //initialize all elements with an array of length one
41  int32_t* one;
42  for (int i=0;i<m_length;i++)
43  {
44  one = SG_MALLOC(int32_t, 1);//use malloc here because mem can be increased efficiently with realloc later
45  m_intron_list[i] = one;
46  m_intron_list[i][0] = 1;
47  one = SG_MALLOC(int32_t, 1);
48  m_quality_list[i] = one;
49  m_quality_list[i][0] = 1;
50  }
51 }
52 void CIntronList::read_introns(int32_t* start_pos, int32_t* end_pos, int32_t* quality, int32_t len)
53 {
54  int k=0;
55  for(int i=0;i<m_length;i++)//iterate over candidate positions
56  {
57  while (k<len)
58  {
59  //SG_PRINT("i:%i, m_all_pos[i]:%i, k:%i, end_pos[k]: %i\n", i, m_all_pos[i], k, end_pos[k]) ;
60  if (k>0)
61  if (end_pos[k]<end_pos[k-1])
62  SG_ERROR("end pos array is not sorted: end_pos[k-1]:%i end_pos[k]:%i\n", end_pos[k-1], end_pos[k]);
63  if (end_pos[k]>=m_all_pos[i])
64  break;
65  else
66  k++;
67 
68  }
69  while (k<len && end_pos[k]==m_all_pos[i])
70  {
71  //SG_PRINT("\tk:%i, end_pos[k]: %i, start_pos[k]:%i\n", k, end_pos[k], start_pos[k]) ;
72  ASSERT(start_pos[k]<end_pos[k]);
73  ASSERT(end_pos[k]<=m_all_pos[m_length-1]);
74  // intron list
75  //------------
76  int32_t from_list_len = m_intron_list[i][0];
77  int32_t* new_list = SG_REALLOC(int32_t, m_intron_list[i], (from_list_len+1));
78  if (new_list == NULL)
79  SG_ERROR("IntronList: Out of mem 4");
80  new_list[from_list_len]= start_pos[k];
81  new_list[0]++;
82  m_intron_list[i] = new_list;
83  // quality list
84  //--------------
85  int32_t q_list_len = m_quality_list[i][0];
86  //SG_PRINT("\t q_list_len:%i, from_list_len:%i \n",q_list_len, from_list_len);
87  ASSERT(q_list_len==from_list_len);
88  new_list = SG_REALLOC(int32_t, m_quality_list[i], (q_list_len+1));
89  if (new_list == NULL)
90  SG_ERROR("IntronList: Out of mem 5");
91  new_list[q_list_len]= quality[k];
92  new_list[0]++;
93  m_quality_list[i] = new_list;
94 
95  k++;
96  }
97  }
98 }
103 void CIntronList::get_intron_support(int32_t* values, int32_t from_pos, int32_t to_pos)
104 {
105  if (from_pos>=m_length)
106  SG_ERROR("from_pos (%i) is not < m_length (%i)\n",to_pos, m_length);
107  if (to_pos>=m_length)
108  SG_ERROR("to_pos (%i) is not < m_length (%i)\n",to_pos, m_length);
109  int32_t* from_list = m_intron_list[to_pos];
110  int32_t* q_list = m_quality_list[to_pos];
111 
112  //SG_PRINT("from_list[0]: %i\n", from_list[0]);
113 
114  int32_t coverage = 0;
115  int32_t quality = 0;
116  for (int i=1;i<from_list[0]; i++)
117  {
118  //SG_PRINT("from_list[%i]: %i, m_all_pos[from_pos]:%i\n", i, from_list[i], m_all_pos[from_pos]);
119  if (from_list[i]==m_all_pos[from_pos])
120  {
121  //SG_PRINT("found intron: %i->%i\n", from_pos, to_pos );
122  coverage = coverage+1;
123  quality = CMath::max(quality, q_list[i]);
124  }
125  }
126  values[0] = coverage;
127  values[1] = quality;
128 }

SHOGUN Machine Learning Toolbox - Documentation