File size: 25,483 Bytes
a3f3d91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
#if HAVE_CONFIG_H
#  include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <stdarg.h>

#include "freesasa.h"

#if STDC_HEADERS
extern int getopt(int, char * const *, const char *);
extern int optind, optopt;
extern char *optarg;
#endif

static char *program_name = "freesasa";

#if USE_XML
  #define XML_STRING "|xml"
#else
  #define XML_STRING ""
#endif

#if USE_JSON
  #define JSON_STRING "|json"
#else
  #define JSON_STRING ""
#endif

#define FORMAT_STRING "log|res|seq|pdb|rsa" XML_STRING JSON_STRING

enum {B_FILE, SELECT, UNKNOWN, RSA, RADII, DEPRECATED};

static int option_flag;

static struct option long_options[] = {
    {"lee-richards",         no_argument,       0, 'L'},
    {"shrake-rupley",        no_argument,       0, 'S'},
    {"probe-radius",         required_argument, 0, 'p'},
    {"resolution",           required_argument, 0, 'n'},
    {"help",                 no_argument,       0, 'h'},
    {"version",              no_argument,       0, 'v'},
    {"no-warnings",          no_argument,       0, 'w'},
    {"n-threads",            required_argument, 0, 't'},
    {"config-file",          required_argument, 0, 'c'},
    {"radius-from-occupancy",no_argument,       0, 'O'},
    {"hetatm",               no_argument,       0, 'H'},
    {"hydrogen",             no_argument,       0, 'Y'},
    {"separate-chains",      no_argument,       0, 'C'},
    {"separate-models",      no_argument,       0, 'M'},
    {"join-models",          no_argument,       0, 'm'},
    {"chain-groups",         required_argument, 0, 'g'},
    {"error-file",           required_argument, 0, 'e'},
    {"output",               required_argument, 0, 'o'},
    {"format",               required_argument, 0, 'f'},
    {"depth",                required_argument, 0, 'd'},
    {"select",               required_argument, &option_flag, SELECT},
    {"unknown",              required_argument, &option_flag, UNKNOWN},
    {"rsa",                  no_argument,       &option_flag, RSA},
    {"radii",                required_argument, &option_flag, RADII},
    {"deprecated",           no_argument,       &option_flag, DEPRECATED},
    // Deprecated options
    {"foreach-residue-type", no_argument,       0, 'r'},
    {"foreach-residue",      no_argument,       0, 'R'},
    {"print-as-B-values",    no_argument,       0, 'B'},
    {"no-log",               no_argument,       0, 'l'},
    {0,0,0,0}
};

#define NOARG_OPTIONS "hvwLSHYOCMm"
#define NOARG_DEPRECATED "BrRl"
#define ARG_OPTIONS "c:n:t:p:g:e:o:f:"
const char* options_string = ":" NOARG_OPTIONS NOARG_DEPRECATED ARG_OPTIONS;

// State of app (most settings are stored here)
struct cli_state {
    freesasa_parameters parameters;
    freesasa_classifier *classifier_from_file;
    const freesasa_classifier *classifier;
    int structure_options;
    int static_classifier;
    int no_rel;
    // chain groups
    int n_chain_groups;
    char** chain_groups;
    // selection commands
    int n_select;
    char** select_cmd;
    // output settings
    int output_format, output_depth;
    // Files
    FILE *input, *output, *errlog;

};
//static struct cli_state state;

struct analysis_results {
    freesasa_node *tree;
    freesasa_structure **structures;
    int n_structures;
};

// Defaults
static void
init_state(struct cli_state *state)
{
    *state = (struct cli_state) {
        .parameters = freesasa_default_parameters,
        .classifier_from_file = NULL,
        .classifier = NULL,
        .structure_options = 0,
        .static_classifier = 0,
        .no_rel = 0,
        .n_chain_groups = 0,
        .chain_groups = NULL,
        .n_select = 0,
        .select_cmd = 0,
        .output_format = 0,
        .output_depth = FREESASA_OUTPUT_CHAIN,
        .output = NULL,
        .errlog = NULL,
    };
}

static void
release_state(struct cli_state *state)
{
    if (state->classifier_from_file)
        freesasa_classifier_free(state->classifier_from_file);
    if (state->chain_groups) {
        for (int i = 0; i < state->n_chain_groups; ++i) {
            free(state->chain_groups[i]);
        }
    }
    if (state->select_cmd) {
        for (int i = 0; i < state->n_select; ++i) {
            free(state->select_cmd[i]);
        }
    }
    if (state->errlog) fclose(state->errlog);
    if (state->output) fclose(state->output);

}

static void
addresses(FILE *out)
{
    fprintf(out,
            "\n" REPORTBUG "\n"
            "Home page: " HOMEPAGE "\n");
}

static void
help(void)
{
    printf("\nUsage: %s [options] pdb-file ...", program_name);
    printf("\n       %s [options] < pdb-file", program_name);
    printf("\n       %s (-h | --help | -v | --version | --deprecated)\n", program_name);
    printf("\n"
           "Options: [--shrake-rupley | --lee-richards] --probe-radius=FLOAT\n"
           "  --resolution=INTEGER -n-threads=INTEGER\n"
           "  [--radius-from-occupancy | --config-file FILE | --radii=(protor|naccess)]\n"
           "  --hetatm --hydrogen [--separate-models | --join-models] [--separate-chains |\n"
           "  --chain-groups=STRING...] --unknown=(guess|skip|halt)\n"
           "  --output FILE --error-file FILE --no-warnings --select=STRING...\n"
           "  --format=(" FORMAT_STRING ")... \n"
           "  --depth=(structure|chain|residue|atom)\n");
    printf("\nPARAMETERS\n"
           "  -S --shrake-rupley           Use Shrake & Rupley algorithm\n"
           "  -L --lee-richards            Use Lee & Richards algorithm [default]\n");
    printf("  -p R --probe-radius=R        [default: %4.2f Å]\n"
           "  -n N --resolution=N          [S&R default: %d] [L&R default: %d]\n",
           FREESASA_DEF_PROBE_RADIUS, FREESASA_DEF_SR_N, FREESASA_DEF_LR_N);
    if (USE_THREADS) {
        printf(
           "  -t N --n-threads=N           [default: %d]\n",
           FREESASA_DEF_NUMBER_THREADS);
    }
    printf("\nRADIUS AND CLASS (maximum one of the following)\n"
           "  -O --radius-from-occupancy   Read atomic radii from Occupancy in PDB\n"
           "  -c FILE --config-file=FILE   Example files in 'share/'\n"
           "  --radii=(protor|naccess)     [default: protor]\n");
    printf("\nINPUT\n"
           "  -H --hetatm                  Include HETATM entries from input\n"
           "  -Y --hydrogen                Include hydrogen atoms, suppress warnings with -w\n"
           "  -m --join-models             Join all MODELs in input into one structure\n"
           "  -C --separate-chains         Calculate each chain separately\n"
           "  -M --separate-models         Calculate each MODEL separately\n"
           "  --unknown=(guess|skip|halt)  When unknown atom radius/class [default: guess]\n"
           "  -g G --chain-groups=G        Each group will be treated separately. Examples:\n"
            "                                 '-g A', '-g A+B', '-g A -g B', '-g AB+CD'\n");
    printf("\nOUTPUT\n"
           "  -w --no-warnings             Skip most warnings\n"
           "  -o FILE --output=FILE        Redirect output\n"
           "  -e FILE --error-file=FILE    Redirect errors\n"
           "  -f (...) --format=(" FORMAT_STRING ")\n"
           "                               Output format, can be repeated. [default: log]\n");
    if (USE_JSON || USE_XML) {
        printf(
           "  -d (...) --depth=(structure|chain|residue|atom)\n"
           "                               Depth of JSON and XML output [default: chain]\n");
    }
    printf("  --select=COMMAND             Select atoms using Pymol select syntax, can be\n"
           "                               repeated. Examples:\n"
           "                                  'AR, resn ala+arg', 'chain_A, chain A'\n");
    addresses(stdout);
}

static void
deprecated(void)
{
    fprintf(stderr,
            "These options will disappear in later versions of FreeSASA.\n"
            "Use --format instead\n\n"
            "  --rsa                         Equivalent to --format=rsa\n"
            "  -B  --print-as-B-values       Equivalent to --format=pdb\n"
            "  -r  --foreach-residue-type    Equivalent to --format=res\n"
            "  -R  --foreach-residue         Equivalent to --format=seq.\n"
            "  -l  --no-log                  Log suppressed if other format selected.\n"
            "                                Options has no effect."
            "\n");
}

static void
short_help(void)
{
    fprintf(stderr, "Run '%s -h' for usage instructions.\n",
            program_name);
    addresses(stderr);
}

static void
version(void)
{
    printf("%s\n", PACKAGE_STRING);
    printf("License: MIT <http://opensource.org/licenses/MIT>\n");
    printf("If you use this program for research, please cite:\n");
    printf("  Simon Mitternacht (2016) FreeSASA: An open source C\n"
           "  library for solvent accessible surface area calculations.\n"
           "  F1000Research 5:189.\n");
    addresses(stdout);
}

/** error handling **/

static void
err_msg(const char* prefix,
        const char* format,
        ...)
{
    va_list arg;
    va_start(arg, format);
    fprintf(stderr, "%s: %s: ", program_name, prefix);
    vfprintf(stderr, format, arg);
    fputc('\n', stderr);
    va_end(arg);
    fflush(stderr);
}

static void
exit_with_help(void) {
    fputc('\n', stderr);
    short_help();
    fputc('\n', stderr);
    fflush(stderr);
    exit(EXIT_FAILURE);
}

#define warn(...) err_msg("warning", __VA_ARGS__)
#define error(...) err_msg("error", __VA_ARGS__)
#define abort_msg(...) do {error(__VA_ARGS__); exit_with_help();} while(0)

static freesasa_structure **
get_structures(FILE *input,
               int *n,
               const struct cli_state *state)
{
   freesasa_structure **structures = NULL;

   *n = 0;
   if ((state->structure_options & FREESASA_SEPARATE_CHAINS) ||
       (state->structure_options & FREESASA_SEPARATE_MODELS)) {
       structures = freesasa_structure_array(input, n, state->classifier, state->structure_options);
       if (structures == NULL) abort_msg("invalid input");
       for (int i = 0; i < *n; ++i) {
           if (structures[i] == NULL) abort_msg("invalid input");
       }
   } else {
       structures = malloc(sizeof(freesasa_structure*));
       if (structures == NULL) {
           abort_msg("out of memory");
       }
       *n = 1;
       structures[0] = freesasa_structure_from_pdb(input, state->classifier, state->structure_options);
       if (structures[0] == NULL) {
           abort_msg("invalid input");
       }
   }

   // get chain-groups (if requested)
   if (state->n_chain_groups > 0) {
       int n2 = *n;
       for (int i = 0; i < state->n_chain_groups; ++i) {
           for (int j = 0; j < *n; ++j) {
               freesasa_structure* tmp = freesasa_structure_get_chains(structures[j], state->chain_groups[i]);
               if (tmp != NULL) {
                   ++n2;
                   structures = realloc(structures, sizeof(freesasa_structure*)*n2);
                   if (structures == NULL) abort_msg("out of memory");
                   structures[n2-1] = tmp;
               } else {
                   abort_msg("at least one of chain(s) '%s' not found", state->chain_groups[i]);
               }
           }
       }
       *n = n2;
   }

   return structures;
}

static freesasa_node *
run_analysis(FILE *input,
             const char *name,
             const struct cli_state *state)
{
    int name_len = strlen(name);
    freesasa_structure **structures = NULL;
    freesasa_node *tree = freesasa_tree_new();
    int n = 0;

    if (tree == NULL) abort_msg("failed to initialize result-tree");

    // read PDB file
    structures = get_structures(input, &n, state);
    if (n == 0) abort_msg("invalid input");

    // perform calculation on each structure
    for (int i = 0; i < n; ++i) {
        char name_i[name_len+10];
        freesasa_node *tmp_tree;

        strcpy(name_i,name);
        if (n > 1 && (state->structure_options & FREESASA_SEPARATE_MODELS))
            sprintf(name_i+strlen(name_i), ":%d", freesasa_structure_model(structures[i]));

        tmp_tree = freesasa_calc_tree(structures[i], &state->parameters, name_i);
        if (tmp_tree == NULL) abort_msg("can't calculate SASA");

        freesasa_node *structure_node =
            freesasa_node_children(freesasa_node_children(tmp_tree));
        const freesasa_result *result = freesasa_node_structure_result(structure_node);

        // Calculate selections for each structure
        if (state->n_select > 0) {
            for (int c = 0; c < state->n_select; ++c) {
                freesasa_selection *sel = freesasa_selection_new(state->select_cmd[c], structures[i], result);
                if (sel != NULL) {
                    freesasa_node_structure_add_selection(structure_node, sel);
                } else {
                    abort_msg("illegal selection");
                }
                freesasa_selection_free(sel);
            }
        }

        if (freesasa_tree_join(tree, &tmp_tree) != FREESASA_SUCCESS) {
            abort_msg("failed joining result-trees");
        }

        freesasa_structure_free(structures[i]);
    }

    free(structures);

    return tree;
}

static FILE*
fopen_werr(const char* filename,
           const char* mode) 
{
    FILE *f = fopen(filename, mode);
    if (f == NULL) {
        abort_msg("could not open file '%s'; %s",
                  filename, strerror(errno));
    }
    return f;
}

static void
state_add_chain_groups(const char* cmd, struct cli_state *state) 
{
    int err = 0;
    char *str;
    const char *token;

    // check that string is valid
    for (size_t i = 0; i < strlen(cmd); ++i) {
        char a = cmd[i];
        if (a != '+' && 
            !(a >= 'a' && a <= 'z') && !(a >= 'A' && a <= 'Z') &&
            !(a >= '0' && a <= '9')) {
            error("character '%c' not valid chain ID in --chain-groups, "
                  "valid characters are [A-z0-9] and '+' as separator", a);
            ++err;
        }
    }

    //extract chain groups
    if (err == 0) {
        str = strdup(cmd);
        token = strtok(str, "+");
        while (token) {
            ++state->n_chain_groups;
            state->chain_groups = realloc(state->chain_groups,sizeof(char*)*state->n_chain_groups);
            if (state->chain_groups == NULL) { abort_msg("out of memory");}
            state->chain_groups[state->n_chain_groups-1] = strdup(token);
            token = strtok(0, "+");
        }
        free(str);
    } else {
        abort_msg("aborting");
    }
}

static void
state_add_select(const char* cmd, struct cli_state *state) 
{
    ++state->n_select;
    state->select_cmd = realloc(state->select_cmd, sizeof(char*)*state->n_select);
    if (state->select_cmd == NULL) {
        abort_msg("out of memory");
    }
    state->select_cmd[state->n_select-1] = strdup(cmd);
    if (state->select_cmd[state->n_select-1] == NULL) {
        abort_msg("out of memory");
    }
}

static void
state_add_unknown_option(const char *optarg, struct cli_state *state)
{
    if (strcmp(optarg, "skip") == 0) {
        state->structure_options |= FREESASA_SKIP_UNKNOWN;
        return;
    } 
    if(strcmp(optarg, "halt") == 0) {
        state->structure_options |= FREESASA_HALT_AT_UNKNOWN;
        return;
    } 
    if(strcmp(optarg, "guess") == 0) {
        return; //default
    }
    abort_msg("unknown alternative to option --unknown: '%s'", optarg);
}

static int
parse_output_format(const char *optarg)
{
    if (strcmp(optarg, "log") == 0) {
        return FREESASA_LOG;
    }
    if (strcmp(optarg, "res") == 0) {
        return FREESASA_RES;
    }
    if (strcmp(optarg, "seq") == 0) {
        return FREESASA_SEQ;
    } 
    if (strcmp(optarg, "rsa") == 0) {
        return FREESASA_RSA;
    }
    if (strcmp(optarg, "json") == 0) {
        if (USE_JSON) {
            return FREESASA_JSON;
        } else {
            abort_msg("program was built without JSON support");
        }
    }
    if (strcmp(optarg, "xml") == 0) {
        if (USE_XML) {
            return FREESASA_XML;
        } else {
            abort_msg("program was built without XML support");
        }
    }
    if (strcmp(optarg, "pdb") == 0) {
        return FREESASA_PDB;
    }
    abort_msg("unknown output format: '%s'", optarg);
    return FREESASA_FAIL; // to avoid compiler warnings
}

static int
parse_output_depth(const char *optarg) {
    if (strcmp("structure", optarg) == 0) {
        return FREESASA_OUTPUT_STRUCTURE;
    }
    if (strcmp("chain", optarg) == 0) {
        return FREESASA_OUTPUT_CHAIN;
    }
    if (strcmp("residue", optarg) == 0) {
        return FREESASA_OUTPUT_RESIDUE;
    }
    if (strcmp("atom", optarg) == 0) {
        return FREESASA_OUTPUT_ATOM;
    }
    abort_msg("output depth '%s' not allowed, "
              "can only be 'structure', 'chain', 'residue' or 'atom'",
              optarg);
    return FREESASA_FAIL; // to avoid compiler warnings
}

static void
state_set_static_classifier(const char *optarg, struct cli_state *state)
{
    if (strcmp("naccess", optarg) == 0) {
        state->classifier = &freesasa_naccess_classifier;
    } else if (strcmp("protor", optarg) == 0) {
        state->classifier = &freesasa_protor_classifier;
    } else {
        abort_msg("config '%s' not allowed, "
                  "can only be 'protor' or 'naccess')", optarg);
    }
    state->static_classifier = 1;
}

// Parse command line arguments and transform state
// accordingly. Parameter state assumed to be initialized to default.
static int
parse_arg(int argc, char **argv, struct cli_state *state)
{
    int alg_set = 0;
    char opt;
    int n_opt = 'z'+1;
    char opt_set[n_opt];
    int option_index = 0;

    memset(opt_set, 0, n_opt);

    while ((opt = getopt_long(argc, argv, options_string,
                              long_options, &option_index)) != -1) {
        opt_set[(int)opt] = 1;
        // Assume arguments starting with dash are actually missing arguments
        if (optarg != NULL && optarg[0] == '-') {
            if (option_index > 0) abort_msg("missing argument? Value '%s' cannot be argument to '--%s'.\n",
                                            program_name,optarg, long_options[option_index].name);
            else abort_msg("missing argument? Value '%s' cannot be argument to '-%c'.\n",
                           optarg,opt);
        }
        switch(opt) {
        case 0:
            switch(long_options[option_index].val) {
            case SELECT:
                state_add_select(optarg, state);
                break;
            case UNKNOWN:
                state_add_unknown_option(optarg, state);
                break;
            case RSA:
                state->output_format = FREESASA_RSA;
                break;
            case RADII:
                state_set_static_classifier(optarg, state);
                break;
            case DEPRECATED:
                deprecated();
                exit(EXIT_SUCCESS);
            default:
                abort(); // what does this even mean?
            }
            break;
        case 'h':
            help();
            exit(EXIT_SUCCESS);
        case 'v':
            version();
            exit(EXIT_SUCCESS);
        case 'e': 
            state->errlog = fopen_werr(optarg, "w");
            freesasa_set_err_out(state->errlog);
            break;
        case 'o':
            if (state->output != NULL) {
                abort_msg("option --output can only be set once");
            }
            state->output = fopen_werr(optarg, "w");
            break;
        case 'f':
            state->output_format |= parse_output_format(optarg);
            break;
        case 'd':
            state->output_depth = parse_output_depth(optarg);
            break;
        case 'w':
            freesasa_set_verbosity(FREESASA_V_NOWARNINGS);
            break;
        case 'c': {
            FILE *cf = fopen_werr(optarg, "r");
            state->classifier = state->classifier_from_file = freesasa_classifier_from_file(cf);
            if (state->classifier_from_file == NULL) abort_msg("can't read file '%s'", optarg);
            state->no_rel = 1;
            break;
        }
        case 'n':
            state->parameters.shrake_rupley_n_points = atoi(optarg);
            state->parameters.lee_richards_n_slices = atoi(optarg);
            if (state->parameters.shrake_rupley_n_points <= 0)
                abort_msg("resolution needs to be at least 1 (20 recommended minum for S&R, 5 for L&R)");
            break;
        case 'S':
            state->parameters.alg = FREESASA_SHRAKE_RUPLEY;
            ++alg_set;
            break;
        case 'L':
            state->parameters.alg = FREESASA_LEE_RICHARDS;
            ++alg_set;
            break;
        case 'p':
            state->parameters.probe_radius = atof(optarg);
            if (state->parameters.probe_radius <= 0)
                abort_msg("probe radius must be 0 or larger");
            break;
        case 'H':
            state->structure_options |= FREESASA_INCLUDE_HETATM;
            break;
        case 'Y':
            state->structure_options |= FREESASA_INCLUDE_HYDROGEN;
            break;
        case 'O':
            state->structure_options |= FREESASA_RADIUS_FROM_OCCUPANCY;
            state->no_rel = 1;
            break;
        case 'M':
            state->structure_options |= FREESASA_SEPARATE_MODELS;
            break;
        case 'm':
            state->structure_options |= FREESASA_JOIN_MODELS;
            break;
        case 'C':
            state->structure_options |= FREESASA_SEPARATE_CHAINS;
            break;
        case 'g':
            state_add_chain_groups(optarg, state);
            break;
        case 't':
            if (USE_THREADS) {
                state->parameters.n_threads = atoi(optarg);
                if (state->parameters.n_threads < 1) abort_msg("number of threads must be 1 or larger");
            } else {
                abort_msg("option '-t' only defined if program compiled with thread support");
            }
            break;
        // Deprecated options
        case 'r':
            warn("option '-r' deprecated, use '-f res' or '--format=res' instead");
            state->output_format |= FREESASA_RES;
            break;
        case 'R':
            warn("option '-R' deprecated, use '-f seq' or '--format=seq' instead");
            state->output_format |= FREESASA_SEQ;
            break;
        case 'B':
            warn("option '-B' deprecated, use '-f pdb' or '--format=pdb' instead");
            state->output_format |= FREESASA_PDB;
            break;
        case 'l':
            warn("option '-l' deprecated, has no effect.");
            break;
            // Errors
        case ':':
            abort_msg("option '-%c' missing argument");
            break;
        case '?':
        default:
            if (optopt == 0) {
                abort_msg("unknown option '%s'", argv[optind-1]);
            } else {
                abort_msg("unknown option '-%c'", optopt);
            }
            break;
        }
    }
    if (state->output == NULL) state->output = stdout;
    if (alg_set > 1) abort_msg("multiple algorithms specified");
    if (state->output_format == 0) state->output_format = FREESASA_LOG;
    if (opt_set['m'] && opt_set['M']) abort_msg("the options -m and -M can't be combined");
    if (opt_set['g'] && opt_set['C']) abort_msg("the options -g and -C can't be combined");
    if (opt_set['c'] && state->static_classifier) abort_msg("the options -c and --radii cannot be combined");
    if (opt_set['O'] && state->static_classifier) abort_msg("the options -O and --radii cannot be combined");
    if (opt_set['c'] && opt_set['O']) abort_msg("the options -c and -O can't be combined");
    if (state->output_format == FREESASA_RSA && (opt_set['c'] || opt_set['O'])) {
        warn("will skip REL columns in RSA when custom atomic radii selected");
    }
    if (state->output_format == FREESASA_RSA && (opt_set['C'] || opt_set['M']))
        abort_msg("the RSA format can not be used with the options -C or -M, "
                  "it does not support several results in one file");
    if (state->output_format & FREESASA_LOG) {
        fprintf(state->output, "## %s ##\n", PACKAGE_STRING);
    }

    return optind;
}

int
main(int argc,
     char **argv) 
{
    struct cli_state state;
    FILE *input = NULL;
    int optind = 0;
    
    freesasa_node *tree = freesasa_tree_new();
    if (tree == NULL) abort_msg("error initializing calculation");

    init_state(&state);

    optind = parse_arg(argc, argv, &state);
    
    if (argc > optind) {
        for (int i = optind; i < argc; ++i) {
            freesasa_node *tmp;
            input = fopen_werr(argv[i], "r");
            tmp = run_analysis(input, argv[i], &state);
            freesasa_tree_join(tree, &tmp);
            fclose(input);
        }
    } else {
        if (!isatty(STDIN_FILENO)) {
            freesasa_node *tmp;
            tmp = run_analysis(stdin, "stdin", &state);
            freesasa_tree_join(tree, &tmp);
        }
        else abort_msg("no input", program_name);
    }

    freesasa_tree_export(state.output, tree, state.output_format | state.output_depth | (state.no_rel ? FREESASA_OUTPUT_SKIP_REL : 0));
    freesasa_node_free(tree);

    release_state(&state);

    return EXIT_SUCCESS;
}