-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.h
132 lines (118 loc) · 2.95 KB
/
util.h
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
/*
* DPPDiv version 1.0b source code (git: 9c0ac3d2258f89827cfe9ba2b5038f0f656b82c1)
* Copyright 2009-2011
* Tracy Heath(1,2,3) (NSF postdoctoral fellowship in biological informatics DBI-0805631)
* Mark Holder(1)
* John Huelsenbeck(2)
*
* (1) Department of Ecology and Evolutionary Biology, University of Kansas, Lawrence, KS 66045
* (2) Integrative Biology, University of California, Berkeley, CA 94720-3140
* (3) email: tracyh@berkeley.edu
*
* DPPDiv 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 (the file gpl.txt included with this
* distribution or http://www.gnu.org/licenses/gpl.txt for more
* details.
*
* Some of this code is from publicly available source by John Huelsenbeck
*/
#ifndef UTIL_H
#define UTIL_H
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
inline std::string getLineFromFile(std::string fileName, int lineNum) {
std::ifstream fileStream(fileName.c_str());
if (!fileStream){
std::cerr << "Cannot open file \"" + fileName + "\"" << std::endl;
exit(1);
}
std::string linestring = "";
int line = 0;
while( getline(fileStream, linestring).good() ){
line++;
if (line == lineNum)
break;
}
fileStream.close();
if (line != lineNum){
std::cerr << "The file \"" + fileName + "\" has " << line << " lines. Could not find line " << lineNum << std::endl;
exit(1);
}
return linestring;
}
inline double expNumTables(double a, int n) {
double expectedNum = 0.0;
for (int i=1; i<=n; i++)
expectedNum += ( 1.0 / (i - 1.0 + a) );
expectedNum *= a;
return expectedNum;
}
inline double calculateFromPriorMean(double target, int n) {
double a = 0.000001;
double ea = expNumTables(a, n);
bool goUp;
if (target <= 1.0)
target = 1.01;
if (ea < target)
goUp = true;
else
goUp = false;
double increment = 0.1;
while ( fabs(ea - target) > 0.000001 )
{
if (ea < target && goUp == true)
{
a += increment;
}
else if (ea > target && goUp == false)
{
a -= increment;
}
else if (ea < target && goUp == false)
{
increment /= 2.0;
goUp = true;
a += increment;
}
else
{
increment /= 2.0;
goUp = false;
a -= increment;
}
ea = expNumTables(a, n);
}
return a;
}
inline void normalizeVector(std::vector<double> &v) {
int n = (int)v.size();
double lnC = v[0];
for (int i=1; i<n; i++)
{
if (v[i] > lnC)
lnC = v[i];
}
for (int i=0; i<n; i++)
v[i] -= lnC;
double sum = 0.0;
for (int i=0; i<n; i++)
{
if ( v[i] < -300.0 )
v[i] = 0.0;
else
v[i] = exp( v[i] );
sum += v[i];
}
for (int i=0; i<n; i++)
v[i] /= sum;
}
#endif