File: build-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 (337 lines) | stat: -rw-r--r-- 9,281 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//    Programmer:  Arthur L. Delcher
//          File:  build-fixed.cc
//  Last Updated:  Fri Jun  4 16:31:05 EDT 2004
//                
//  This program reads (from  stdin ) a set of fixed_length strings in
//  multi-fasta format.  It then builds and outputs to  stdout
//  a fixed-length interpolated context model (ICM) that matches the input.
//
//  Copyright (c) 2006 University of Maryland Center for Bioinformatics
//  & Computational Biology


#include  "build-fixed.hh"


static FILE  * Index_File_fp = NULL;
  // File containing a list of subscripts of strings to train model
static int  Model_Depth = DEFAULT_MODEL_DEPTH;
  // Maximum number of positions to use in Markov context
static int  Model_Len = DEFAULT_MODEL_LEN;
  // Width of Markov context and character to be predicted
static ICM_Model_t  Model_Type = UNKNOWN_TYPE;
  // Type of model
static int  * Permutation = NULL;
  // Describes how to re-order the characters before building the model
static int  Permutation_Len = 0;
  // Length of above permutation; must match length of input strings
static bool  Print_Binary = true;
  // Print model as a binary file iff this is true; otherwise print
  // as text file
static int  Special_Position = -1;
  // Designated position in model, e.g., for splice junction
static vector <char *>  Training_Data;
  // Holds the training strings


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



int  main
    (int argc, char * argv [])
  {
   int  string_ct;
     // Number of strings read from training file
   int  i;


   Parse_Command_Line (argc, argv);

   Read_Training_Data (stdin);
   string_ct = Training_Data . size ();

   if  (string_ct <= 0)
       {
        fprintf (stderr, "ERROR:  No strings read to train model\n");
        exit (EXIT_FAILURE);
       }

   if  (Index_File_fp != NULL)
       {
        // Read the file of subscripts, make a list of the strings
        // they refer to and use that for training.

        vector <char *>  list;
        int  sub;

        while  (fscanf (Index_File_fp, "%d", & sub) == 1)
          list . push_back (Training_Data [sub]);

        Training_Data = list;
        string_ct = Training_Data . size ();
       }

   Model_Len = strlen (Training_Data [0]);
   for  (i = 1;  i < string_ct;  i ++)
     if  (int (strlen (Training_Data [i])) != Model_Len)
         {
          fprintf (stderr, "ERROR:  String #%d has length = %d\n",
                   i, int (strlen (Training_Data [i])));
          fprintf (stderr, "        different from string #0 length = %d\n",
                   Model_Len);
          exit (EXIT_FAILURE);
         }
   if  (Permutation != NULL && Permutation_Len != Model_Len)
       {
        fprintf (stderr, "ERROR:  Permutation len = %d  string_len = %d\n",
                 Permutation_Len, Model_Len);
        exit (EXIT_FAILURE);
       }

   // create the model
   if  (Special_Position > Model_Len)
       {
        fprintf (stderr, "ERROR:  Bad special position = %d\n",
                 Special_Position);
       }
   Fixed_Length_ICM_Training_t  model (Model_Len, Model_Depth, Special_Position,
                                       Permutation, Model_Type);

   model . Train_Model (Training_Data);

   model . Output (stdout, Print_Binary);

   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)] .

  {
   char  * p;
   int  ch, errflg = FALSE;

   optarg = NULL;

   while  (! errflg
             && ((ch = getopt (argc, argv, "bd:hi:p:s:tv:")) != EOF))
     switch  (ch)
       {
        case  'b' :
          Print_Binary = true;
          break;
          
        case  'd' :
          Model_Depth = int (strtol (optarg, & p, 10));
          if  (p == optarg || Model_Depth <= 0)
              {
               fprintf (stderr, "Bad model depth value \"%s\"\n",
                        optarg);
               errflg = TRUE;
              }
          break;
          
        case  'h' :
          errflg = TRUE;
          break;

        case  'i' :
          Index_File_fp = File_Open (optarg, "r");
          break;

        case  'p' :
          {
           vector <int>  perm;
           int  i, j, n;

           for  (p = strtok (optarg, ", ");  p != NULL;  p = strtok (NULL, ", "))
             perm . push_back (atoi (p));
           n = perm . size ();
           Permutation = (int *) Safe_calloc (n, sizeof (int), __FILE__,
                                      __LINE__);
           for  (i = 0;  i < n;  i ++)
             if  (Permutation [perm [i]] == 0)
                 Permutation [perm [i]] = 1;
               else
                 {
                  fprintf (stderr, "ERROR:  Illegal permutation\n");
                  for  (j = 0;  j <= i;  j ++)
                    fprintf (stderr, " %d", perm [j]);
                  fprintf (stderr, " <-- duplicate\n");
                  exit (EXIT_FAILURE);
                 }
           for  (i = 0;  i < n;  i ++)
             if  (Permutation [i] == 0)
                 {
                  fprintf (stderr, "ERROR:  Illegal permutation--missing %d\n", i);
                  exit (EXIT_FAILURE);
                 }
           for  (i = 0;  i < n;  i ++)
             Permutation [i] = perm [i];
           Permutation_Len = n;
          }
          break;

        case  's' :
          Special_Position = strtol (optarg, NULL, 10);
          break;

#if  0    // ALD removed on 22 May 2006
        case  'T' :
          Model_Type = ICM_Model_t (strtol (optarg, NULL, 10));
          break;
#endif

        case  't' :
          Print_Binary = false;
          break;
          
        case  'v' :
          Verbose = int (strtol (optarg, & p, 10));
          if  (p == optarg)
              {
               fprintf (stderr, "Bad verbose value \"%s\"\n",
                        optarg);
               errflg = TRUE;
              }
          break;
          
        case  '?' :
          fprintf (stderr, "Unrecognized option -%c\n", optopt);

        default :
          errflg = TRUE;
       }

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

   return;
  }



static 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  Read_Training_Data
    (FILE  * fp)

// Read in training strings from  fp .  Format is multifasta, i.e., for
// each string a header line (starting with '>') followed by arbitrarily
// many data lines.  Save strings in global  Training_Data

  {
   char  * string = NULL, * tag = NULL;
   char  * p;
   long int  string_size = 0, tag_size = 0;

   while  (Read_String (fp, string, string_size, tag, tag_size))
     {
      p = strdup (string);
      Training_Data . push_back (p);
     }

   return;
  }



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>]  < <input-file>  > <output-file>\n"
           "\n"
           "Read sequences from  stdin  and output to  stdout \n"
           "the fixed-length interpolated context model built from them\n"
           "\n"
           "Options:\n"
           " -d <num>  Set depth of model to <num>\n"
           " -h        Print this message\n"
           " -i <fn>   Train using strings specified by subscripts in file\n"
           "           named <fn>\n"
           " -p n1,n2,...,nk  Permutation describing re-ordering of\n"
           "           character positions of input string to build model\n"
           " -s <num>  Specify special position in model\n"
           " -t        Output model as text (for debugging only)\n"
           " -v <num>  Set verbose level; higher is more diagnostic printouts\n"
           "\n",
           command);

   return;
  }