File: gthmkvtree.c

package info (click to toggle)
genomethreader 1.7.3%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 46,528 kB
  • sloc: ansic: 90,168; ruby: 1,769; makefile: 548; sh: 112; perl: 105
file content (270 lines) | stat: -rw-r--r-- 7,409 bytes parent folder | download | duplicates (3)
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
#include <stdbool.h>
#include "core/assert_api.h"
#include "core/compat_api.h"
#include "core/fa_api.h"
#include "core/fileutils_api.h"
#include "core/ma_api.h"
#include "gth/gthdef.h"
#include "gth/gthoutput.h"
#include "libgenomethreader/findfile.h"
#include "libgenomethreader/gthmkvtree.h"
#include "types.h"
#include "virtualdef.h"
#include "readvirt.pr"

/*
  This file contains all functions necessary to interface with mkvtree.
*/

#define NUMOFMKVTREEARGS        17

#define MKVTREEARGSIZE          512

#define CHECKOFFSET\
        gt_assert(offset < NUMOFMKVTREEARGS);

#define CHECKRVAL\
        gt_assert(rval < MKVTREEARGSIZE);

#define POLYA_SMAPFILE                  "TransDNAX"
#define POLYA_PL_VALUE                  7

#define SAVE_SMAP_OPTION\
        CHECKOFFSET;\
        rval =  snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE,\
                         "-smap");\
        CHECKRVAL;\
        CHECKOFFSET;\
        rval =  snprintf(gthmkvtreeinfo->argv[offset++] , MKVTREEARGSIZE, "%s",\
                         smapfile);\
        CHECKRVAL

/*
  This structure stores the argument vector and the argument counter for
  the mkvtree call.
*/
typedef struct
{
  Argctype argc;                /* number of arguments for mkvtree */
  char *argv[NUMOFMKVTREEARGS]; /* argument vector for mkvtree */
} Gthmkvtreeinfo;

static int setsmapfile(char *smapfile, const char *mapping, GtError *err)
{
  int rval, had_err = 0;
  gt_error_check(err);

  GtStr *path = gt_str_new();
  had_err = gth_find_file(mapping, GTHDATAENVNAME, GTHDATADIRNAME, path, err);
  if (!had_err) {
   rval = snprintf(smapfile, PATH_MAX+1, "%s", gt_str_get(path));
   gt_assert(rval < PATH_MAX + 1);
  }
  gt_str_delete(path);
  return had_err;
}

static int initGthmkvtreeinfo(Gthmkvtreeinfo *gthmkvtreeinfo,
                              const char *filename, bool creatednaindex,
                              bool transdnax, bool addsuffix, Uint demand,
                              const char *progname, const char *proteinsmap,
                              GtError *err)
{
  char smapfile[PATH_MAX+1];
  Uint i, offset = 0;
  Sint rval;

  gt_error_check(err);
  gt_assert(!(!creatednaindex && transdnax));

  /* init argv */
  for (i = 0; i < NUMOFMKVTREEARGS; i++) {
    gthmkvtreeinfo->argv[i] = gt_malloc(sizeof (char) * MKVTREEARGSIZE);
    gthmkvtreeinfo->argv[i][0] = '\0';
  }

  /* save program name */
  CHECKOFFSET;
  rval =  snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE, "%s",
                   progname);
  CHECKRVAL;

  /* save alphabet type */
  if (creatednaindex) {
    /* set DNA alphabet */
    if (transdnax) {
      if (setsmapfile(smapfile, POLYA_SMAPFILE, err))
        return -1;
      SAVE_SMAP_OPTION;
    }
    else {
      CHECKOFFSET;
      rval =  snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE, "-dna");
      CHECKRVAL;
    }
  }
  else {
    /* set protein alphabet */
    if (strcmp(proteinsmap, "protein") == 0) {
      CHECKOFFSET;
      rval = snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE,
                      "-protein");
      CHECKRVAL;
    }
    else {
      if (setsmapfile(smapfile, proteinsmap, err))
        return -1;
      SAVE_SMAP_OPTION;
    }
  }

  /* save verbose flag */
  CHECKOFFSET;
  rval =  snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE, "-v");
  CHECKRVAL;

  /* set flags for different tables */
  if (demand & OISTAB) {
    CHECKOFFSET;
    rval =  snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE, "-ois");
    CHECKRVAL;
  }
  if (demand & TISTAB) {
    CHECKOFFSET;
    rval =  snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE, "-tis");
    CHECKRVAL;
  }

  if (demand & BCKTAB) {
    CHECKOFFSET;
    rval =  snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE, "-bck");
    CHECKRVAL;
    CHECKOFFSET;
    rval =  snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE, "-pl");
    CHECKRVAL;
  }

  if (demand & SUFTAB) {
    CHECKOFFSET;
    rval =  snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE, "-suf");
    CHECKRVAL;
  }
  if (demand & BWTTAB) {
    CHECKOFFSET;
    rval =  snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE, "-bwt");
    CHECKRVAL;
  }
  if (demand & LCPTAB) {
    CHECKOFFSET;
    rval =  snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE, "-lcp");
    CHECKRVAL;
  }

  if (demand & STI1TAB) {
    CHECKOFFSET;
    rval =  snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE, "-sti1");
    CHECKRVAL;
  }

  /* save filename for which index needs to be computed */
  CHECKOFFSET;
  rval = snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE, "-db");
  CHECKRVAL;
  CHECKOFFSET;
  rval = snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE, "%s",
                  filename);
  CHECKRVAL;

  /* save name of index */
  CHECKOFFSET;
  rval =  snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE ,
                   "-indexname");
  CHECKRVAL;

  if (addsuffix) {
    if (transdnax) {
      CHECKOFFSET;
      rval =  snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE, "%s.%s",
                       filename, POLYASUFFIX);
      CHECKRVAL;
    }
    else if (!creatednaindex) {
      CHECKOFFSET;
      rval =  snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE, "%s.%s",
                       filename, proteinsmap);
      CHECKRVAL;
    }
    else {
      CHECKOFFSET;
      rval =  snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE, "%s.%s",
                       filename, DNASUFFIX);
      CHECKRVAL;
    }
  }
  else {
    CHECKOFFSET;
    rval =  snprintf(gthmkvtreeinfo->argv[offset++], MKVTREEARGSIZE, "%s",
                     filename);
    CHECKRVAL;
  }

  /* save argument counter (argc) */
  gthmkvtreeinfo->argc = (Argctype) offset;

  return 0;
}

static void freeGthmkvtreeinfo(Gthmkvtreeinfo *gthmkvtreeinfo)
{
  unsigned int i;
  if (!gthmkvtreeinfo) return;
  /* free argv */
  for (i = 0; i < NUMOFMKVTREEARGS; i++)
    gt_free(gthmkvtreeinfo->argv[i]);
}

/* The following function is used to interface with mkvtree.
  <filename> is the name of the file for which an index needs to be
  constructred. */
int gthcallmkvtree(const char *filename, const char *progname,
                   const char *proteinsmap, bool creatednaindex, bool transdnax,
                   bool addsuffix, bool oistab, bool completeindex,
                   GthOutput *out, GtError *err)
{
  Gthmkvtreeinfo gthmkvtreeinfo;
  Virtualtree virtualtree;
  Uint demand = TISTAB;

  gt_error_check(err);

  if (oistab)
    demand |= OISTAB;
  if (completeindex)
    demand |= SUFTAB | BWTTAB | LCPTAB | BCKTAB | STI1TAB;

  /* init argc and argv for mkvtree */
  if (initGthmkvtreeinfo(&gthmkvtreeinfo, filename, creatednaindex, transdnax,
                         addsuffix, demand, progname, proteinsmap, err)) {
    return -1;
  }

  if (out && out->showverbose)
    out->showverbose("call mkvtree to compute index");

  /* call mkvtree */
  makeemptyvirtualtree(&virtualtree);
  if (callmkvtree(gthmkvtreeinfo.argc, (const char**) gthmkvtreeinfo.argv, true,
                  &virtualtree, true, out ? out->showverboseVM : NULL)) {
    fprintf(stderr,"%s\n", messagespace());
    exit(EXIT_FAILURE);
  }
  if (freevirtualtree(&virtualtree)) {
    fprintf(stderr,"%s\n", messagespace());
    exit(EXIT_FAILURE);
  }

  /* free space */
  freeGthmkvtreeinfo(&gthmkvtreeinfo);

  return 0;
}