-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.cpp
More file actions
188 lines (143 loc) · 5.65 KB
/
lambda.cpp
File metadata and controls
188 lines (143 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
SeedHam is a program to learn DNA binding motifs from SELEX datasets.
Copyright (C) 2016, 2017 Jarkko Toivonen
SeedHam is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
SeedHam is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "lambda.hpp"
#include "common.hpp"
#include "data.hpp"
#include "parameters.hpp"
#include <boost/tuple/tuple.hpp>
//#include <vector>
#include <set>
#include <cmath>
int myget1(boost::tuple<int, int> t)
{
return boost::get<0>(t);
}
int myget2(boost::tuple<int, int> t)
{
return boost::get<1>(t);
}
int
find_nonspecific_fraction(const std::vector<std::string>& sequences)
{
int lines = sequences.size();
int k=8;
int L=sequences[0].length();
int m=L-k+1;
// kmers is a set of pairs (kmer_id,count)
std::vector<boost::tuple<int, int> > kmers(pow(4,k), boost::make_tuple(0,0));
for (int i=0; i < pow(4,k); ++i)
boost::get<0>(kmers[i]) = i;
// count the number of kmers, only single strand considered here
for (int i=0; i < lines; ++i) {
const std::string& line = sequences[i];
for (int j=0; j < m; ++j) {
boost::get<1>(kmers[dna_to_number<size_t>(line.substr(j,k))])++;
boost::get<1>(kmers[dna_to_number<size_t>(reverse_complement(line.substr(j,k)))])++;
}
}
key_sort(kmers.begin(), kmers.end(), myget2); // sort by count
int a = ceil(0.25*pow(4,k));
int b = floor(0.75*pow(4,k));
int sum=0;
for (int i=a; i <= b; ++i) // number of kmers that have relative frequency in [0.25,0.75]
sum += boost::get<1>(kmers[i]);
return sum;
}
double
jussi_lambda(const std::vector<std::string>& s0,
const std::vector<std::string>& s1)
{
int L = s0[0].length();
int k = 8;
double f0=find_nonspecific_fraction(s0);
double f1=find_nonspecific_fraction(s1);
int size0 = s0.size()*(L-k+1);
int size1 = s1.size()*(L-k+1);
printf("\t\tTwo middle quads\tAll 8-mers\n");
printf("\tDataset 1:\t%i\t%i\n", (int)f0, size0);
printf("\tDataset 2:\t%i\t%i\n", (int)f1, size1);
f0 /= size0;
f1 /= size1;
printf("\tFreqs 0: %f Freqs 1: %f\n", f0, f1);
double lambda=f1/f0;
return lambda;
}
// returns set of sequences of length L that do not contain a subsequence
// close to the consensus by Hamming distance,
// current limit is ???
std::set<std::string>
get_unspecific_set_helper(const std::vector<std::string>& dataset, const std::string& consensus)
{
std::set<std::string> result;
int limit= 3; //ceil(consensus.length()/1.5);
std::string reverse_consensus = reverse_complement(consensus);
// find all unspecific sequences in dataset
for (int i=0; i < dataset.size(); ++i) {
if (min_hamming_distance(dataset[i], consensus) >= limit &&
(not use_two_strands || min_hamming_distance(dataset[i], reverse_consensus) >= limit))
result.insert(dataset[i]);
}
return result;
}
std::set<std::string>
get_unspecific_set(const std::vector<std::string>& dataset1,
const std::vector<std::string>& dataset2,
const std::string& consensus)
{
std::set<std::string> result1 = get_unspecific_set_helper(dataset1, consensus);
std::set<std::string> result2 = get_unspecific_set_helper(dataset2, consensus);
std::set<std::string> result;
printf("Dataset1 distinct unspecific sequences %zu\n", result1.size());
printf("Dataset2 distinct unspecific sequences %zu\n", result2.size());
//set_intersection(result1.begin(), result1.end(), result2.begin(), result2.end(), inserter(result, result.begin()));
set_union(result1.begin(), result1.end(), result2.begin(), result2.end(), inserter(result, result.begin()));
//printf("Result dataset unspecific %zu\n", result.size());
return result;
}
int
count_unspecific(const std::vector<std::string>& dataset, const std::set<std::string>& unspecific)
{
int count = 0;
for (int i=0; i < dataset.size(); ++i) {
if (unspecific.find(dataset[i]) != unspecific.end())
++count;
}
return count;
}
// compute lambda as the ratio of frequencies of an unspecific set
// between two generations, unspecific set is the union of
// two unspecific sets of the generations
double
hamming_lambda(const std::vector<std::string>& data1,
const std::vector<std::string>& data2,
const std::string& consensus, int generation)
{
//const std::vector<std::string>& data1=datasets[generation-1];
//const std::vector<std::string>& data2=datasets[generation];
std::set<std::string> unspecific_set =
get_unspecific_set(data1, data2, consensus);
printf("Size of unspecific set of generations %i and %i is %zu\n",
generation-1, generation, unspecific_set.size());
printf("Dataset1: Unspecific %i Total %zu\n",
count_unspecific(data1, unspecific_set), data1.size());
printf("Dataset2: Unspecific %i Total %zu\n",
count_unspecific(data2, unspecific_set), data2.size());
double unspec1 = count_unspecific(data1, unspecific_set) / (double)data1.size();
double unspec2 = count_unspecific(data2, unspecific_set) / (double)data2.size();
printf("Unspecific frequencies, generation %i: %f generation %i: %f ratio(lambda): %f\n",
generation-1, unspec1, generation, unspec2, unspec2/unspec1);
return unspec2/unspec1;
}