-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.h
158 lines (122 loc) · 4.44 KB
/
base.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/******************************************************************
* Copyright (c) 2007
* All rights reserved.
* Filename: base.h
* Abstract: Declaration of base class for KaKs methods.
* Version: 1.0
* Author: Zhang Zhang ([email protected])
* Date: Feb.2, 2005
******************************************************************/
#pragma warning(disable:4786) //Disable warning when using vector
#if !defined(BASE_H)
#define BASE_H
#define CODONLENGTH 3 //Length of codon
#define DNASIZE 4 //A C G T
#define XSIZE DNASIZE*DNASIZE //Size of one group AXX (X=A,C,G,T)
#define CODON 64 //Codon Size
#define NULL 0 //Zero
#define NA -1 //Not Available
#define NCODE 23 //Number of genetic codes
#define NNCODE NCODE*2 //Double of the number genetic codes
#define SMALLVALUE 1e-10 //Value near to zero
#define NUMBER_OF_RATES 6 //Number of substitution rates
#define MODELCOUNT 14 //Number of candidate models
#define gammap(x,alpha) (alpha*(1-pow(x,-1/alpha)))
#define square(a) ((a)*(a))
#define min2(a,b) ((a)<(b)?(a):(b))
#define max2(a,b) ((a)>(b)?(a):(b))
#define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))
/* Stanard lib of C++ */
#include<string>
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<cstdlib>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<ctype.h>
#include<cctype>
#include<valarray>
using namespace std;
/*****Global variables*****/
extern const char* transl_table[]; //Genetic codon table
extern int genetic_code; //ID of codon table from 1 to 23
//End of Global variables
/* Convert one type to any other type */
template<class out_type,class in_value>
out_type CONVERT(const in_value & t) {
stringstream stream;
//Put the value 't' into the stream
stream<<t;
out_type result;
//Put the stream into the 'result'
stream>>result;
return result;
}
class Base {
public:
Base();
/* Write the content into file */
bool writeFile(string output_filename, const char* result);
/* Parse results */
string parseOutput();
/* Format string for outputing into file */
void addString(string &result, string str, string flag="\t");
/* Generate a radnom integer */
int getRandom();
double getRandDouble();
int getRandInt(int begin, int end);
int setRandSeed();
int countChar(string str, char ch);
/* Convert a char-T,C,A,G into a digit 0,1,2,3, respectively */
int convertChar(char ch);
/* Convert a digit-0,1,2,3 into a char T,C,A,G, respectively */
char convertInt(int ch);
/* Convert a string to uppercase */
string stringtoUpper(string str);
string Trim(string str);
string filterString(string str);
string replaceAll(string str, char from, char to);
string replaceAll(string str, char from);
/* Calculate the amino acid of codon by codon */
char getAminoAcid(string codon);
/* Calculate the amino acid of codon by codon's id*/
char getAminoAcid(int id);
/* Get the number of stop codon in a given genetic code table */
int getNumNonsense(int genetic_code);
/* Return the codon's id from codon table */
int getID(string codon);
/* Return a codon according to the id */
string getCodon(int IDcodon);
/* Sum array's elements */
double sumArray(double x[], int end, int begin=0);
int sumArray(int x[], int end, int begin=0);
/* Init value to array */
int initArray(double x[], int n, double value=0.0);
int initArray(int x[], int n, int value=0);
/* Elements in array are mutipled by scale */
int scaleArray(double scale, double x[], int n);
/* Sqrt of the sum of the elements' square */
double norm(double x[], int n);
/* Copy array's values one by one: to[] = from[] */
int copyArray(double from[], double to[], int n);
/* Sum of 'n' products multiplied by two elements x[], y[] */
double innerp(double x[], double y[], int n);
/* Set x[i,j]=0 when x!=j and x[i,j]=1 when x=j */
int initIdentityMatrix(double x[], int n);
bool removeCntrl(string name, vector<string> &str);
bool checkValid(string name, vector<string> &str);
int readAXT(string seqfile, vector<string> &seqname, vector<string> &seq);
/* Read fasta file */
int readFasta(string seqfile, vector<string> &seqname, vector<string> &seq);
vector<string> split(const char *str, char c);
int read_input(string seqfile, vector <double> &patient_info, vector <vector <double> > &seq2d);
/* Convert a set of aligned sequences into a consensus sequence */
string generalInput(vector<string> seq);
string RGB(int r, int g, int b);
};
#endif