1 | ////////////////////////////////////////////////////////////// |
---|
2 | // BPPMatrix.hpp |
---|
3 | // |
---|
4 | // save the Base Pairing Probability Matrix for each sequences |
---|
5 | ////////////////////////////////////////////////////////////// |
---|
6 | |
---|
7 | #ifndef __BBPMatrix_HPP__ |
---|
8 | #define __BBPMatrix_HPP__ |
---|
9 | |
---|
10 | #include <iostream> |
---|
11 | #include <string> |
---|
12 | #include "SparseMatrix.h" |
---|
13 | #include "McCaskill.hpp" |
---|
14 | #include "nrutil.h" |
---|
15 | |
---|
16 | |
---|
17 | |
---|
18 | using namespace std; |
---|
19 | |
---|
20 | namespace MXSCARNA{ |
---|
21 | class BPPMatrix { |
---|
22 | private: |
---|
23 | |
---|
24 | int seqLength; // sequence length; |
---|
25 | float cutOff; // the threshold of probability |
---|
26 | |
---|
27 | BPPMatrix() {} |
---|
28 | public: |
---|
29 | SparseMatrix bppMat; // base pairing probability matrix 1-origin(1..seqLength) |
---|
30 | BPPMatrix(int bppmode, const string &seq, int seqLength, float inCutOff = 0.0001) : seqLength (seqLength), cutOff(inCutOff) { |
---|
31 | if( bppmode == 'r' ) // by katoh |
---|
32 | readBPPMatrix(seq); |
---|
33 | else if( bppmode == 'w' ) |
---|
34 | setandwriteBPPMatrix(seq); |
---|
35 | else |
---|
36 | setBPPMatrix(seq); |
---|
37 | } |
---|
38 | BPPMatrix(int seqLength, float inCutOff, const Trimat<float> & tmpBppMat) : seqLength(seqLength), cutOff(inCutOff) { |
---|
39 | bppMat.SetSparseMatrix(seqLength, seqLength, tmpBppMat, cutOff); |
---|
40 | } |
---|
41 | |
---|
42 | |
---|
43 | void setBPPMatrix(const string &seq) { |
---|
44 | const char *tmpSeq = seq.c_str(); |
---|
45 | McCaskill mc(seqLength, &tmpSeq[1]); |
---|
46 | mc.calcPartitionFunction(); |
---|
47 | Trimat<float> tmpBppMat(seqLength + 1); |
---|
48 | |
---|
49 | for (int i = 0; i < seqLength; i++) { |
---|
50 | for(int j = i; j < seqLength; j++) { |
---|
51 | tmpBppMat.ref(i+1, j+1) = mc.getProb(i,j); |
---|
52 | } |
---|
53 | } |
---|
54 | bppMat.SetSparseMatrix(seqLength, seqLength, tmpBppMat, cutOff); |
---|
55 | } |
---|
56 | |
---|
57 | void setandwriteBPPMatrix(const string &seq) { // by katoh |
---|
58 | const char *tmpSeq = seq.c_str(); |
---|
59 | McCaskill mc(seqLength, &tmpSeq[1]); |
---|
60 | mc.calcPartitionFunction(); |
---|
61 | Trimat<float> tmpBppMat(seqLength + 1); |
---|
62 | float tmpbp; |
---|
63 | |
---|
64 | fprintf( stdout, ">\n" ); |
---|
65 | for (int i = 0; i < seqLength; i++) { |
---|
66 | for(int j = i; j < seqLength; j++) { |
---|
67 | tmpbp = mc.getProb(i,j); |
---|
68 | if (tmpbp > 0.0001) |
---|
69 | fprintf( stdout, "%d %d %50.40f\n", i, j, tmpbp ); |
---|
70 | tmpBppMat.ref(i+1, j+1) = tmpbp; |
---|
71 | } |
---|
72 | } |
---|
73 | bppMat.SetSparseMatrix(seqLength, seqLength, tmpBppMat, cutOff); |
---|
74 | } |
---|
75 | |
---|
76 | static void fgets_IGNORE_RESULT(char *buf, int size, FILE *fp) { |
---|
77 | char *result = fgets(buf, size, fp); |
---|
78 | if (!result) { |
---|
79 | fprintf(stderr, "fgets() fails, but we ignore that fact[3].\n"); |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | void readBPPMatrix(const string &seq) { // by katoh |
---|
84 | const char *tmpSeq = seq.c_str(); |
---|
85 | char oneline[1000]; |
---|
86 | int onechar; |
---|
87 | float prob; |
---|
88 | int posi, posj; |
---|
89 | static FILE *bppfp = NULL; |
---|
90 | |
---|
91 | |
---|
92 | if( bppfp == NULL ) |
---|
93 | { |
---|
94 | bppfp = fopen( "_bpp", "r" ); |
---|
95 | if( bppfp == NULL ) |
---|
96 | { |
---|
97 | fprintf( stderr, "Cannot open _bpp.\n" ); |
---|
98 | exit( 1 ); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | Trimat<float> tmpBppMat(seqLength + 1); |
---|
103 | fgets_IGNORE_RESULT( oneline, 999, bppfp ); |
---|
104 | if( oneline[0] != '>' ) |
---|
105 | { |
---|
106 | fprintf( stderr, "Format error\n" ); |
---|
107 | exit( 1 ); |
---|
108 | } |
---|
109 | while( 1 ) |
---|
110 | { |
---|
111 | onechar = getc( bppfp ); |
---|
112 | ungetc( onechar, bppfp ); |
---|
113 | if( onechar == '>' || onechar == EOF ) break; |
---|
114 | |
---|
115 | fgets_IGNORE_RESULT( oneline, 999, bppfp ); |
---|
116 | sscanf( oneline, "%d %d %f", &posi, &posj, &prob ); |
---|
117 | // fprintf( stderr, "%d %d -> %f\n", posi, posj, prob ); |
---|
118 | tmpBppMat.ref(posi+1, posj+1) = prob; |
---|
119 | } |
---|
120 | |
---|
121 | bppMat.SetSparseMatrix(seqLength, seqLength, tmpBppMat, cutOff); |
---|
122 | } |
---|
123 | |
---|
124 | float GetProb(int i, int j) { |
---|
125 | return bppMat.GetValue(i,j); |
---|
126 | } |
---|
127 | |
---|
128 | float GetLength() const { |
---|
129 | return seqLength; |
---|
130 | } |
---|
131 | |
---|
132 | void updateBPPMatrix(const Trimat<float> &inbppMat) { |
---|
133 | bppMat.SetSparseMatrix(seqLength, seqLength, inbppMat, cutOff); |
---|
134 | } |
---|
135 | }; |
---|
136 | } |
---|
137 | #endif // __BPPMatrix_HPP__ |
---|