1 /* $Id: $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's offical duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================*/
25
26 /*****************************************************************************
27
28 File name: njn_localmaxstatmatrix.cpp
29
30 Author: John Spouge
31
32 Contents:
33
34 ******************************************************************************/
35
36 #include "njn_localmaxstatmatrix.hpp"
37 #include "njn_localmaxstatutil.hpp"
38 #include "njn_memutil.hpp"
39
40
41 using namespace Njn;
42
init(size_t dimMatrix_,size_t dimMatrix2_)43 void LocalMaxStatMatrix::init (size_t dimMatrix_, size_t dimMatrix2_)
44 {
45 if (dimMatrix2_ == 0) dimMatrix2_ = dimMatrix_;
46
47 if (dimMatrix_ > 0 && dimMatrix2_ > 0)
48 {
49 d_scoreMatrix_p = MemUtil::newMatrix <long int> (dimMatrix_, dimMatrix2_);
50 d_p_p = new double [dimMatrix_];
51 d_p2_p = new double [dimMatrix2_];
52 }
53
54 d_dimMatrix = dimMatrix_;
55 d_dimMatrix2 = dimMatrix2_;
56 }
57
free2()58 void LocalMaxStatMatrix::free2 ()
59 {
60 if (getDimMatrix () > 0 && getDimMatrix2 () > 0)
61 {
62 MemUtil::deleteMatrix <long int> (d_scoreMatrix_p, getDimMatrix (), getDimMatrix2 ());
63 d_scoreMatrix_p = 0;
64 delete [] d_p_p; d_p_p = 0;
65 delete [] d_p2_p; d_p2_p = 0;
66 }
67
68 d_dimMatrix = 0;
69 d_dimMatrix2 = 0;
70 }
71
copy(size_t dimMatrix_,const long int * const * scoreMatrix_,const double * p_,const double * p2_,size_t dimMatrix2_)72 void LocalMaxStatMatrix::copy (
73 size_t dimMatrix_, // #(distinct values) of scores & probabilities (which are paired)
74 const long int *const *scoreMatrix_, // score matrix [0...dimMatrix_)[0...dimMatrix_)
75 const double *p_, // probability of "letters" p_ [0...dimMatrix_)
76 const double *p2_, // probability of "letters" p2_ [0...dimMatrix_), the second (j) set of letter-probabilities
77 size_t dimMatrix2_) // #(distinct letters) in the second alphabet
78 {
79 if (! p2_) p2_ = p_;
80 if (dimMatrix2_ == 0) dimMatrix2_ = dimMatrix_;
81
82 free2 ();
83 init (dimMatrix_, dimMatrix2_);
84
85 if (getDimMatrix () == 0)
86 {
87 LocalMaxStat::copy (0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
88 return;
89 }
90
91 size_t i = 0;
92 size_t j = 0;
93
94 d_dimMatrix = dimMatrix_;
95 d_dimMatrix2 = dimMatrix2_;
96
97 for (i = 0; i < getDimMatrix (); i++)
98 {
99 memcpy (d_scoreMatrix_p [i], scoreMatrix_ [i], sizeof (long int) * getDimMatrix2 ());
100 }
101
102 memcpy (d_p_p, p_, sizeof (double) * getDimMatrix ());
103 memcpy (d_p2_p, p2_, sizeof (double) * getDimMatrix2 ());
104
105 size_t dim = 0;
106 long int *score = 0;
107 double *p = 0;
108
109 double **probMatrix = MemUtil::newMatrix <double> (getDimMatrix (), getDimMatrix2 ());
110
111 for (i = 0; i != getDimMatrix (); i++)
112 {
113 for (j = 0; j != getDimMatrix2 (); j++)
114 {
115 probMatrix [i][j] = p_ [i] * p2_ [j];
116 }
117 }
118
119 LocalMaxStatUtil::flatten (getDimMatrix (), getScoreMatrix (), probMatrix, &dim, &score, &p, getDimMatrix2 ());
120 LocalMaxStat::copy (dim, score, p);
121
122 delete [] p; p = 0;
123 delete [] score; score = 0;
124 MemUtil::deleteMatrix <double> (probMatrix, getDimMatrix (), getDimMatrix2 ());
125 }
126
copy(LocalMaxStat localMaxStat_,size_t dimMatrix_,const long int * const * scoreMatrix_,const double * p_,const double * p2_,size_t dimMatrix2_)127 void LocalMaxStatMatrix::copy (
128 LocalMaxStat localMaxStat_, // base object
129 size_t dimMatrix_, // #(distinct values) of scores & probabilities (which are paired)
130 const long int *const *scoreMatrix_, // score matrix [0...dimMatrix_)[0...dimMatrix_)
131 const double *p_, // probability of "letters" p_ [0...dimMatrix_)
132 const double *p2_, // probability of "letters" p2_ [0...dimMatrix_), the second (j) set of letter-probabilities
133 size_t dimMatrix2_) // #(distinct letters) in the second alphabet
134 {
135 if (! p2_) p2_ = p_;
136 if (dimMatrix2_ == 0) dimMatrix2_ = dimMatrix_;
137
138 free2 ();
139 init (dimMatrix_);
140
141 size_t i = 0;
142 /*sls deleted size_t j = 0;*/
143
144 d_dimMatrix = dimMatrix_;
145 d_dimMatrix2 = dimMatrix2_;
146
147 for (i = 0; i < getDimMatrix (); i++)
148 {
149 memcpy (d_scoreMatrix_p [i], scoreMatrix_ [i], sizeof (long int) * getDimMatrix2 ());
150 }
151
152 memcpy (d_p_p, p_, sizeof (double) * getDimMatrix ());
153 memcpy (d_p2_p, p2_, sizeof (double) * getDimMatrix2 ());
154
155 LocalMaxStat::copy (localMaxStat_);
156 }
157
158