File: pdbSSM.c

package info (click to toggle)
theseus 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,152 kB
  • ctags: 2,447
  • sloc: ansic: 42,404; makefile: 250; sh: 131
file content (276 lines) | stat: -rw-r--r-- 7,071 bytes parent folder | download
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
    Theseus - maximum likelihood superpositioning of macromolecular structures

    Copyright (C) 2004-2014 Douglas L. Theobald

    This program 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.

    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 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.,
    59 Temple Place, Suite 330,
    Boston, MA  02111-1307  USA

    -/_|:|_|_\-
*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <time.h>
#include <limits.h>
#include "Error.h"
#include "pdbMalloc.h"
#include "Cds.h"
#include "PDBCds.h"
#include "pdbStats.h"
#include "distfit.h"
#include "DLTmath.h"
#include "pdbSSM.h"


/* SSM is Edgar-speak for Structure Similarity Matrix file type, quote:

For a file format, I suggest the following.

The file type is called SSM (structure similarity matrices).

The file contains text.

The first line is formatted as follows:

#SSM<n>

where <n>=N is the number of structures.

Following the first line are N sequences in FASTA format. Presumably you will
use the standard amino acid alphabet here, but you can use any symbols you like
-- e.g. an X at each position just as a placeholder. MUSCLE will use a sequence
for use in the output alignment and to get the length so that the similarity
matrix dimension is known in advance.

Following the FASTA data are N(N-1)/2 similarity matrices. A similarity matrix
is formatted as follows:

#MATRIX<i>,<j>
<matrix data>
#ENDMATRIX

where <i> and <j> are "structure indexes". A structure index=1, 2 ... N as
defined by the order they appear in the FASTA data section of the file.

The matrices must appear in order of increasing i, then within a given i in
order of increasing j, including only matrices in which i < j (of course, the
matrix for i,j contains the same data as the matrix for j,i). So, for example,
if N=4 then the order is:

#MATRIX1,2
...
#MATRIX1,3
...
#MATRIX1,4
...
#MATRIX2,3
...
#MATRIX2,4
...
#MATRIX3,4
...

The matrix data is formatted as follows.

The data contains L_i lines, where L_i is the length of the i'th structure.

One line contains L_j floating-point values, where L_j is the length of the j'th
structure. A floating-point value may be formatted in any way readable by the C
language atof() function.

The first value starts in the first column of the line, values are separated by
exactly one space character.
*/


SSM
*SSMInit(void)
{
    SSM            *ssm = NULL;

    ssm = (SSM *) malloc(sizeof(SSM));
    if (ssm == NULL)
    {
        perror("\n  ERROR");
        fprintf(stderr,
                "\n  ERROR198: could not allocate memory in function SSMInit(). \n");
        PrintTheseusTag();
        exit(EXIT_FAILURE);
    }

    ssm->L = NULL;
    ssm->mat = NULL;

    return(ssm);
}


void
SSMAlloc(SSM *ssm, CdsArray *cdsA)
{
    int         i, j, k, cnum;

    ssm->n = cnum = cdsA->cnum;

    ssm->L = (int *) malloc(cnum * sizeof(int));

    for (i = 0; i < cnum; ++i)
    {
        ssm->L[i] = cdsA->cds[i]->aalen;
        //printf("\nlength: %d", ssm->L[i]);
    }

    ssm->mat = (double ***) malloc(0.5 * cnum * (cnum - 1) * sizeof(double **));

    k = 0;
    for (i = 0; i < cnum; ++i)
    {
        for (j = i+1; j < cnum; ++j)
        {
            ssm->mat[k] = MatAlloc(ssm->L[i], ssm->L[j]);
            ++k;
        }
    }

//    printf("[[%d][%d]]\n", k, ssm->n);fflush(NULL);
}


void
SSMDestroy(SSM **ssm_ptr)
{
    SSM *ssm = *ssm_ptr;
    int         i;

    for (i = 0; i < ssm->n * (ssm->n - 1) / 2; ++i)
        MatDestroy(&(ssm->mat[i]));

    free(ssm->mat);
    ssm->mat = NULL;

    free(ssm->L);
    ssm->L = NULL;

    free(ssm);
    *ssm_ptr = NULL;
}


void
SSMCalc(SSM *ssm, CdsArray *cdsA)
{
    int         i, j, k, m, n, p, q;
    int         cnum = cdsA->cnum;
    double     *lnvar = malloc(cdsA->vlen * sizeof(double));
    double     *invvar = malloc(cdsA->vlen * sizeof(double));

    for (i = 0; i < cdsA->vlen; ++i)
        lnvar[i] = log(cdsA->var[i]);

    for (i = 0; i < cdsA->vlen; ++i)
        invvar[i] = 1.0 / cdsA->var[i];

    k = 0;
    for (i = 0; i < cnum; ++i)
    {
        for (j = i+1; j < cnum; ++j)
        {
            for (m = p = 0; m < cdsA->vlen; ++m)
            {
                if (cdsA->cds[i]->nu[m])
                {
                    for (n = q = 0; n < cdsA->vlen; ++n)
                    {
                        if (cdsA->cds[j]->nu[n])
                        {
/*                           printf("[%d][%d][%d]:%e %e %e %e %e %e\n", k, p, q, */
/*                                  sqrt(SqrCdsDist(cdsA->cds[i], m, cdsA->avecds, m)), */
/*                                  sqrt(SqrCdsDist(cdsA->cds[i], m, cdsA->avecds, n)), */
/*                                  sqrt(SqrCdsDist(cdsA->cds[j], n, cdsA->avecds, m)), */
/*                                  sqrt(SqrCdsDist(cdsA->cds[j], n, cdsA->avecds, n)), */
/*                                  lnvar[i], lnvar[j]); */
/*                           fflush(NULL); */

                            ssm->mat[k][p][q] =
                            (invvar[m] * SqrCdsDist(cdsA->cds[i], m, cdsA->avecds, m) +
                             invvar[n] * SqrCdsDist(cdsA->cds[i], m, cdsA->avecds, n) +
                             invvar[m] * SqrCdsDist(cdsA->cds[j], n, cdsA->avecds, m) +
                             invvar[n] * SqrCdsDist(cdsA->cds[j], n, cdsA->avecds, n) +
                             2.0 * (lnvar[i] + lnvar[j])) * -0.25;

//                           printf("[%d][%d][%d]:%e\n", k, p, q, ssm->mat[k][p][q]);
//                           fflush(NULL);

                            q++;
                        }
                    }
                    //printf("\nq:%d", q);

                    p++;
                }
            }
            //printf("\np:%d", p);
            k++;
        }
    }

    free(lnvar);
    free(invvar);
}


void
WriteSSM(SSM *ssm)
{
    int         i, j, k, m, n;
    FILE       *fp = NULL;

    fp = fopen("ssm.txt", "w");

    fprintf(fp, "#SSM%d\n", ssm->n);

    k = 0;
    for (i = 0; i < ssm->n; ++i)
    {
        for (j = i+1; j < ssm->n; ++j)
        {
            fprintf(fp, "#MATRIX%d,%d\n", i+1, j+1);

            for (m = 0; m < ssm->L[i]; ++m)
            {
                for (n = 0; n < ssm->L[j]; ++n)
                {
                    fprintf(fp, "% 14.2f ", ssm->mat[k][m][n]);
                }

                fprintf(fp, "\n");
            }

            fprintf(fp, "ENDMATRIX\n");
            k++;
        }
    }

    fprintf(fp, "\n");

    fclose(fp);
}