File: seqpcc.c

package info (click to toggle)
ncbi-tools6 6.1.20170106%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 468,492 kB
  • sloc: ansic: 1,474,204; pascal: 6,740; cpp: 6,248; xml: 3,390; sh: 2,139; perl: 1,084; csh: 508; makefile: 437; javascript: 198; ruby: 93; lisp: 81
file content (237 lines) | stat: -rw-r--r-- 4,931 bytes parent folder | download | duplicates (14)
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
#include <objres.h>
#include <objseq.h>
#include <seqport.h>
#include <seqpcc.h>

/* defines */
#define F7   7
#define F24 24
#define F42 42
#define PI  3.14159

/* functions - internal */

static FloatHi PPCC (FloatHi score)
{
  FloatHi   meang = 0.77;
  FloatHi   stdg  = 0.20;
  FloatHi   meanc = 1.63;
  FloatHi   stdc  = 0.24;
  FloatHi   constg, constc;
  FloatHi   evalg, evalc;
  FloatHi   Gg, Gc;
  FloatHi   prob;

  constg = 1 / (stdg * 2 * PI);
  constc = 1 / (stdc * 2 * PI);
  evalg = (((score - meang) / stdg) * ((score - meang) / stdg)) / 2;
  evalc = (((score - meanc) / stdc) * ((score - meanc) / stdc)) / 2;
  evalg = exp (evalg);
  evalc = exp (evalc);
  Gg = constg / evalg;
  Gc = constc / evalc;
  prob = Gc / ((30 * Gg) + Gc);
  return prob;
}

static FloatHi nroot (FloatHi fv, Int4 wv)
{
  FloatHi  fi = 1.0;

  fi = fi / (FloatHi) wv;
  fv = pow (fv, fi);
  return fv;
}

static void PCC (SeqPortPtr spp, Int4 start, Int4 end, Int4 window,
                 FloatHi scr[F24][F42], Char res[F24], FloatHiPtr fptr)
{
  Int4        i, n, col;
  Int4        stop;
  Char        aa;
  Boolean     flagMatch;
  FloatHi     temp;
  FloatHiPtr  fheadptr;

  fheadptr = fptr;
  for (i = start; i < end; i++)
    *fptr++ = 0.0;
  fptr = fheadptr;
  stop = end - window;
  for (i = start; i < stop; i++)
  {
    SeqPortSeek (spp, i, SEEK_SET);
    temp = 1.0;
    for (col = 0; col < window; col++)
    {
      aa = SeqPortGetResidue (spp);
      flagMatch = FALSE;
      n = 0;
      while (res[n] != '\0')
      {
        if (aa == res[n])
        {
          flagMatch = TRUE;
          break;
        }
        n++;
      }
      if (flagMatch)
        temp *= scr[n][col];
    }
    temp = nroot (temp, window);
    temp = PPCC (temp);
    if (temp < 0.0)
      temp = 0.0;
    temp *= 100.0;
    fheadptr = fptr;
    for (n = 0; n < window; n++)
    {
      if (temp > *fptr)
        *fptr = temp;
      fptr++;
    }
    fptr = fheadptr;
    fptr++;
  }
  return;
}

static SeqGraphPtr PCCGraph (SeqPortPtr spp, Int4 start, Int4 end, Int4 window,
                             FloatHi scr[F24][F42], Char res[F24])
{
  SeqGraphPtr  sgp;
  Int4         gwidth = 500;
  FloatHiPtr   fptr;

  if ((sgp = SeqGraphNew ()) != NULL)
  {
/* type and number of values and compression */
    sgp->numval = end + 1;
    sgp->compr = (Int4) (sgp->numval / gwidth);
    if ((sgp->numval%gwidth) != 0)
      sgp->compr += 1;
/* graph type */
    sgp->flags[2] = 1;
    sgp->values = (Pointer) MemNew ((sizeof (FloatHi)) * sgp->numval);
/* min/max */
    sgp->max.realvalue = 100.0;
    sgp->min.realvalue = 0.0;
    sgp->axis.realvalue = 0.0;
/* scaling */
    sgp->flags[1] = 1;
    sgp->a = 2;
    sgp->b = 0;
/* do it */
    fptr = (FloatHiPtr) sgp->values;
    PCC (spp, start, end, window, scr, res, fptr);
  }
  return sgp;
}

static Int4 ReadPCC (CharPtr res, FloatHi scr[F24][F42], Int4 window)
{
  FILE    *fin;
  Int2    i, n, val, count;
  Int2    numcol;
  FloatHi score;
  Char    buf[PATH_MAX], buff[PATH_MAX];
  CharPtr bptr;

  static Char c[] = {'A','B','C','D','E','F','G','H','I','K','L','M',
                     'N','P','Q','R','S','T','V','W','X','Y','Z','*'};

  numcol = F7;

  if (!FindPath ("ncbi", "ncbi", "data", buf, sizeof (buf)))
    return 0;
  FileBuildPath (buf, NULL, "KSpcc.dat");
  if ((fin = FileOpen (buf, "r")) == NULL)
  {
    if ((fin = FileOpen ("KSpcc.dat", "r")) == NULL)
    {
      return 0;
    }
  }

  val = 0;
  while ((FileGets (buff, sizeof (buff), fin)) != NULL)
  {
    if (buff[0] == ';')
      continue;
    res[val] = c[val];
    bptr = buff;
    for (n = 0; n < numcol; n++)
    {
      sscanf (bptr, "%lf", &score);
      scr[val][n] = score;
      bptr += 7;
    }
    val++;
    if (val == F24)
      break;
  }
  count = val;

  for (n = numcol; n < window; n++)
  {
    if (n == F42)
      break;
    val = (Int2) (n % numcol);
    for (i = 0; i < F24; i++)
    {
      scr[i][n] = scr[i][val];
    }
  }

  FileClose (fin);
  return (Int4) count;
}

/* functions - external */

extern SeqGraphPtr PCCProc (BioseqPtr bsp, SeqFeatPtr sfp, Int4 window)
{
  SeqIdPtr     psip;
  BioseqPtr    pbsp;
  Int4         start, end;
  FloatHi      scr[F24][F42];
  Char         res[F24];
  SeqPortPtr   spp;
  SeqGraphPtr  sgp = NULL;

  if (bsp != NULL)
  {
    pbsp = bsp;

    if (!ISA_aa (pbsp->mol))
      return sgp;
  }
  else if (sfp != NULL)
  {
    if (sfp->data.choice != SEQFEAT_CDREGION)
      return sgp;

    psip = SeqLocId (sfp->product);
    pbsp = BioseqFind (psip);
    if (pbsp == NULL)
      return sgp;

    if (!ISA_aa (pbsp->mol))
      return sgp;
  }

  if (ReadPCC (res, scr, window) != F24)
    return sgp;

  start = 0;
  end = pbsp->length-1;
  if ((spp = SeqPortNew (pbsp, start, end, 0, Seq_code_iupacaa)) != NULL)
  {
    end -= window;
    sgp = PCCGraph (spp, start, end, window, scr, res);
    SeqPortFree (spp);
  }

  return sgp;
}