File: score-fixed.cc

package info (click to toggle)
tigr-glimmer 3.02b-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 13,948 kB
  • sloc: cpp: 24,416; awk: 232; csh: 220; makefile: 147; sh: 51
file content (241 lines) | stat: -rw-r--r-- 6,372 bytes parent folder | download | duplicates (12)
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
//    Programmer:  Arthur L. Delcher
//          File:  score-fixed.cc
//  Last Updated:  Mon Jun  7 10:35:06 EDT 2004
//
//  Compute scores for a set of fixed length strings using
//  the model in the file on the command line.


#include  "score-fixed.hh"


static char  * Pos_Model_Path;
  // Name of file containing the positive model
static char  * Neg_Model_Path;
  // Name of file containing the negative model
static bool  Simple_Output = false;
  // If true, output is just string number (starting with 0)
  // and either 1 or -1 indicating which model scores higher
static bool  Use_Neg_ICM_Model = false;
  // If true then negative model is usual (streaming) ICM
  // instead of fixed-length ICM
static bool  Use_Null_Neg_Model = false;
  // If true then negative model is ignored and value
  // from it is automatically zero


//**ALD  Gets rid of make undefined reference error
int  Unused = Filter ('a');



int  main
    (int argc, char * argv [])

  {
   Fixed_Length_ICM_t  pos_model;
   ICM_t  neg_icm_model;
   Fixed_Length_ICM_t  neg_fixed_model;
   char  * string = NULL, * tag = NULL;
   long int  string_size = 0, tag_size = 0;
   int  string_num = 0;

   Parse_Command_Line (argc, argv);

   pos_model . read (Pos_Model_Path);
   fprintf (stderr, "pos model  len = %d  special = %d  type = %d\n",
            pos_model . getModelLength (),
            pos_model . getSpecialPosition (),
            pos_model . getModelType ());
   if  (Use_Null_Neg_Model)
       fprintf (stderr, "Using null negative model\n");
   else if  (Use_Neg_ICM_Model)
       neg_icm_model . Read (Neg_Model_Path);
     else
       {
        neg_fixed_model . read (Neg_Model_Path);
        fprintf (stderr, "neg model  len = %d  special = %d  type = %d\n",
                 neg_fixed_model . getModelLength (),
                 neg_fixed_model . getSpecialPosition (),
                 neg_fixed_model . getModelType ());
       }

   while  (Read_String (stdin, string, string_size, tag, tag_size))
     {
      double  pos_score, neg_score;
      double  avg_pos_score, avg_neg_score;
      int  len;

      string_num ++;
      len = strlen (string);

      pos_score = pos_model . score (string);
      if  (Use_Null_Neg_Model)
          neg_score = 0.0;
      else if  (Use_Neg_ICM_Model)
          neg_score = neg_icm_model . Score_String (string, strlen (string), 1);
        else
          neg_score = neg_fixed_model . score (string);

      avg_pos_score = pos_score / len;
      avg_neg_score = neg_score / len;

      if  (Simple_Output)
          printf ("%6d %3d\n", string_num - 1,
                  pos_score >= neg_score ? 1 : -1);
        else
          printf ("%5d:  %10.4f %9.5f   %10.4f %9.5f   %9.5f\n",
                  string_num, pos_score, avg_pos_score,
                  neg_score, avg_neg_score, avg_pos_score - avg_neg_score );
     }

   return  0;
  }



static void  Parse_Command_Line
    (int argc, char * argv [])

//  Get options and parameters from command line with  argc
//  arguments in  argv [0 .. (argc - 1)] .

  {
   bool  errflg = false;
   int  ch;

   optarg = NULL;

   while  (! errflg
             && ((ch = getopt (argc, argv, "hINs")) != EOF))
     switch  (ch)
       {
        case  'h' :
          errflg = true;
          break;

        case  'I' :
          Use_Neg_ICM_Model = true;
          break;

        case  'N' :
          Use_Null_Neg_Model = true;
          break;

        case  's' :
          Simple_Output = true;
          break;

        case  '?' :
          fprintf (stderr, "Unrecognized option -%c\n", optopt);

        default :
          errflg = TRUE;
       }

   if  (errflg || (Use_Null_Neg_Model && optind > argc - 1)
          || (! Use_Null_Neg_Model && optind != argc - 2))
       {
        Usage (argv [0]);
        exit (EXIT_FAILURE);
       }

   Pos_Model_Path = argv [optind ++];
   if  (! Use_Null_Neg_Model)
       Neg_Model_Path = argv [optind ++];

   return;
  }



int  Read_String
    (FILE * fp, char * & s, long int & s_size, char * & tag,
     long int & tag_size)

//  Read next string from  fp  (assuming FASTA format) into  s [0 .. ]
//  which has  s_size  characters.  Allocate extra memory if needed
//  and adjust  s_size  accordingly.  Return  TRUE  if successful,  FALSE
//  otherwise (e.g., EOF).  Put FASTA header line into  tag [0 .. ]
//  (and adjust  tag_size  if needed).

  {
   int  ch, ct;

   while  ((ch = fgetc (fp)) != EOF && ch != '>')
     ;

   if  (ch == EOF)
       return  FALSE;

   ct = 0;
   while  ((ch = fgetc (fp)) != EOF && ch != '\n' && isspace (ch))
     ;
   if  (ch == EOF)
       return  FALSE;
   if  (ch != '\n' && ! isspace (ch))
       ungetc (ch, fp);
   while  ((ch = fgetc (fp)) != EOF && ch != '\n')
     {
      if  (ct >= tag_size - 1)
          {
           tag_size += INCR_SIZE;
           tag = (char *) Safe_realloc (tag, tag_size);
          }
      tag [ct ++] = char (ch);
     }
   tag [ct ++] = '\0';

   ct = 0;
   while  ((ch = fgetc (fp)) != EOF && ch != '>')
     {
      if  (isspace (ch))
          continue;

      if  (ct >= s_size - 1)
          {
           s_size += INCR_SIZE;
           s = (char *) Safe_realloc (s, s_size);
          }
      s [ct ++] = char (ch);
     }
   s [ct ++] = '\0';

   if  (ch == '>')
       ungetc (ch, fp);

   return  TRUE;
  }



static void  Usage
    (char * command)

//  Print to stderr description of options and command line for
//  this program.   command  is the command that was used to
//  invoke it.

  {
   fprintf (stderr,
           "USAGE:  %s [options]  <pos-model>  <neg-model>  < input-file\n"
           "\n"
           "Read sequences from  stdin  and score them using fixed-length\n"
           "model in file  <model> .  Output scores to  stdout.\n"
           "Output columns are:  sequence number, positive total score,\n"
           "  positive score per base, negative total score,\n"
           "  negative score per base, delta positive/negative per-base scores.\n"
           "\n"
           "Options:\n"
           " -h        Print this message\n"
           " -I        Negative model is regular ICM, not fixed-length ICM\n"
           " -N        Use NULL negative model, i.e., constant zero\n"
           " -s        Output simple format of string num and 1 or -1\n"
           "\n",
           command);

   return;
  }