File: augustify.py

package info (click to toggle)
augustus 3.5.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 777,052 kB
  • sloc: cpp: 80,066; perl: 21,491; python: 4,368; ansic: 1,244; makefile: 1,141; sh: 171; javascript: 32
file content (608 lines) | stat: -rwxr-xr-x 27,830 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python3

import os
import errno
import os.path
import argparse
import re
import shutil
import subprocess
import logging
import random
import string
from difflib import SequenceMatcher
from inspect import currentframe, getframeinfo
from itertools import compress
import multiprocessing

__author__ = "Katharina J. Hoff"
__copyright__ = "Copyright 2022. All rights reserved."
__credits__ = "Mario Stanke, Anica Hoppe, Marnix Medema"
__license__ = "Artistic License"
__version__ = "1.0.3"
__email__ = "katharina.hoff@uni-greifswald.de"
__status__ = "development"

parser = argparse.ArgumentParser(
    description='augustify.py will either assign a majority vote Augustus ' +
                'parameter set to a given set of input sequences, or it ' +
                'will sort input sequences according to their respective ' +
                'best parameter sets and perform ab initio gene prediction ' +
                'with these parameter sets.')
parser.add_argument('-g', '--genome', required=True, type=str,
                    help='Genome fasta file (possibly softmasked)')
parser.add_argument('-p', '--parameter_list', required=True, type=str,
                    help='File that lists parameter sets to be tried on ' +
                    'input sequence (one parameter set name per line)')
parser.add_argument('-m', '--metagenomic_classification_outfile', type=str,
                    help='Output a tabulator separated text file that ' +
                    'assigns sequences to parameter sets (last column ' +
                    'contains probability).')
parser.add_argument('-P', '--prediction_file', type=str,
                    help='GFF file with gene predictions (only compatible ' +
                    'with option --metagenomic_classification_outfile/-m).')
parser.add_argument('-s', '--species', action='store_true',
                    help='Output the dominant and most suitable ' +
                    'parameter set name across all input sequences.')
parser.add_argument('-a', '--augustus_config_path', required=False, type=str,
                    help='Set path to the config directory of AUGUSTUS. \
                    If not given, will try to set augustus_config_path to \
                    environment variable AUGUSTUS_CONFIG_PATH. If this does not \
                    work, will try to set augustus_config_path to \
                    augustus_bin_path/../config/. \
                    The command-line argument --augustus_config_path has higher \
                    priority than the environment variable with the same name.')
parser.add_argument('-A', '--augustus_bin_path', required=False, type=str,
                    help='Set path to the AUGUSTUS directory that contains \
                    augustus binary. If not given, will try to locate the path \
                    with which(augustus)')
parser.add_argument('-t', '--threads', required=False, type=int, default=1,
                    help='Number of threads for running augustus. The number ' +
                    'of threads should not be greater than the number of ' +
                    'species parameter sets.')
args = parser.parse_args()

if ( (args.metagenomic_classification_outfile) and (args.species) ):
    print("Incompatible options selected: you must either specify " +
          "--metagenomic/-m OR --species/-s. You cannot run " +
          "augustify.py with both options!")
    exit(1)
elif ( (args.metagenomic_classification_outfile) and (args.species) ):
    print("Missing opion: you must either specify " +
          "--metagenomic/-m OR --species/-s.")

if ( (args.prediction_file is True) and (args.metagenomic_classification_outfile is not True)):
    print("Incompatible options selected: " +
          "--prediction_file/-P requires argument " +
          "--metagenomic_classification_outfile/-m!")
    exit(1)

if args.metagenomic_classification_outfile:
    if os.path.isfile(args.metagenomic_classification_outfile):
        print('File ' + args.metagenomic_classification_outfile +
            ' already exists. Please specify different file or delete file.')
        exit(1)

if args.prediction_file:
    if os.path.isfile(args.prediction_file):
        print('File ' + args.prediction_file +
            ' already exists. Please specify different file or delete file.')
        exit(1)

''' ******************* BEGIN FUNCTIONS *************************************'''

def normalize_float(manti, expo):
    """ Function that normalizes a float consisting of mantisse and exponent
        to a mantisse that has one digit in front of comma. Assumption is 
        that input manisse is either 0.xxxx or 1.xxx, i.e. the number of 
        digits in front of comma is already one in all cases."""
    if manti < 1:
        manti = manti*10
        expo = expo + 1
    return manti,expo


def create_random_string():
    """ Function that creates a random string added to the logfile name
        and tmp dir name """
    letters = string.ascii_lowercase
    randomString = ''.join(random.choice(letters) for i in range(8))
    tmp = "tmp_" + randomString + "/"
    log = "augustify_log_" + randomString
    # if directory or log_file exists, create a new random string
    while(os.path.exists(tmp) or os.path.exists(log)):
        randomString = ''.join(random.choice(letters) for i in range(8))
        tmp = "tmp" + randomString + "/"
        log = "augustify_log_" + randomString
    return(randomString)


def create_log_file_name(randomString):
    """ Function that creates a log file with a random name """
    log = "augustify_log_" + randomString
    return(log)


def create_tmp_dir(randomString):
    """ Function that creates a directory for temporary files with a random name """
    tmp = "tmp_" + randomString + "/"
    os.mkdir(tmp)
    logger.info("Creating directory " + tmp + ".")
    return(tmp)


def find_tool(toolname):
    """ Function that tries to locate a tool with which """
    if shutil.which(toolname) is not None:
        toolbinary = shutil.which(toolname)
    else:
        frameinfo = getframeinfo(currentframe())
        logger.info('Error in file ' + frameinfo.filename + ' at line ' +
                str(frameinfo.lineno) + ': '
                + "Unable to locate binary of " + toolname + "!")
        exit(1)
    return toolbinary


def check_tool_in_given_path(given_path, toolname):
    """ If TOOL_PATH is provided as command-line option, check whether the
    required binary is executable in that directory; return tool binary
    with full path. """
    if not re.search(r"/$", given_path):
        given_path = given_path + "/"
    toolbinary = given_path + toolname
    toolbinary = os.path.abspath(toolbinary)
    if not(os.access(toolbinary, os.X_OK)):
        frameinfo = getframeinfo(currentframe())
        logger.info('Error in file ' + frameinfo.filename + ' at line ' +
                    str(frameinfo.lineno) + ': ' + toolbinary +
                    ' is not executable!')
        exit(1)
    return toolbinary


def run_process(args_lst, prc_out, prc_err):
    ''' Function that runs a subprocess with arguments and specified STDOUT and STDERR '''
    try:
        logger.info("Trying to execute the following command:")
        logger.info(" ".join(args_lst))
        result = subprocess.run(args_lst, stdout=prc_out, stderr=prc_err)
        logger.info("Succeeded in executing command.")
        if(result.returncode == 0):
            return(result)
        else:
            frameinfo = getframeinfo(currentframe())
            logger.info('Error in file ' + frameinfo.filename + ' at line ' +
                  str(frameinfo.lineno) + ': ' + 'Return code of subprocess was ' +
                str(result.returncode) + str(result.args))
            quit(1)
    except subprocess.CalledProcessError as grepexc:
        frameinfo = getframeinfo(currentframe())
        print('Error in file ' + frameinfo.filename + ' at line ' +
              str(frameinfo.lineno) + ': ' + 'Failed executing: ',
              " ".join(grepexc.args))
        print("Error code: ", grepexc.returncode, grepexc.output)
        quit(1)

def run_process_stdinput(args_lst, prc_in):
    ''' Function that runs a subprocess with arguments and input from STDIN '''
    try:
        logger.info("Trying to execute the following command with input from STDIN:")
        logger.info(" ".join(args_lst))
        result = subprocess.run(args_lst, stdin=prc_in, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        logger.info("Succeeded in executing command.")
        if(result.returncode == 0):
            return(result)
        else:
            frameinfo = getframeinfo(currentframe())
            logger.info('Error in file ' + frameinfo.filename + ' at line ' +
                  str(frameinfo.lineno) + ': ' + "Return code of subprocess was " +
                str(result.returncode) + str(result.args))
            quit(1)
    except subprocess.CalledProcessError as grepexc:
        frameinfo = getframeinfo(currentframe())
        print('Error in file ' + frameinfo.filename + ' at line ' +
              str(frameinfo.lineno) + ': ' + 'Failed executing: ',
              " ".join(grepexc.args))
        print("Error code: ", grepexc.returncode, grepexc.output)
        quit(1)

def work_augustus(cmd_ext_lst):
    ''' Function that keeps running AUGUSTUS to compute emiprobs until it 
    returns a result or until the entire sequence has been tried; walk 
    the input in steps 4000 nt windows, overlapping 200 nt, if possible; 
    we do this because AUGUSTUS sometimes crashes when computing the desired
    probability; problem should ultimately be fixed in AUGUSTUS '''
    segmlen = 15000 # These parameters need to be evaluated, later!
    stepwidth = 7500 # These parameters need to be evaluated, later!
    cmd1 = cmd_ext_lst[0:7]
    cmd2 = cmd_ext_lst[7:9]
    seqlen = cmd_ext_lst[-1]
    if seqlen<=segmlen:
        cmd = cmd1 + cmd2
        return subprocess.run(cmd, stdout=subprocess.PIPE, 
            stderr=subprocess.PIPE, shell=False)
    else:
        curr_start = 1
        curr_end = segmlen
        returncode = 1
        while (curr_start < curr_end) and (curr_end - curr_start + 1 >= segmlen) and not(returncode == 0):
            cmd = cmd1 + ["--predictionStart=" + str(curr_start), "--predictionEnd=" + str(curr_end)] + cmd2
            result = subprocess.run(cmd, stdout=subprocess.PIPE, 
                stderr=subprocess.PIPE, shell=False)
            returncode = result.returncode
            curr_start = curr_start + stepwidth
            if (curr_end + stepwidth) <= seqlen:
                curr_end = curr_end+stepwidth
            else:
                curr_end = seqlen
        return result

def augustify_seq(hindex, header, seqs, tmp, params):
    ''' Function that runs a subprocess with arguments and specified STDOUT and STDERR '''
    logger.info("Processing sequence: " + header)
    # store length for loop (emiprobs bug)
    currlen = len(seqs)
    # sequence files for prediction
    try:
        with open(tmp + "seq" + str(hindex) + ".fa", "w") as seq_handle:
            seq_handle.write(header)
            seq_handle.write(seqs)
    except IOError:
        frameinfo = getframeinfo(currentframe())
        logger.info('Error in file ' + frameinfo.filename + ' at line ' +
                    str(frameinfo.lineno) + ': ' + "Could not open file " +
                    tmp + "seq" + str(hindex) + ".fa" + " for writing!")

    # construct augustus calls
    calls = []
    for species in params:
        curr_call =  [augustus, '--AUGUSTUS_CONFIG_PATH=' + augustus_config_path, 
                     '--species=' + species.rstrip(), '--genemodel=complete', 
                     '--emiprobs=on',
                     '--softmasking=0', tmp + "seq" + str(hindex) + ".fa",
                     '--outfile=' + tmp + "seq" + str(hindex) + "_" + species.rstrip() + ".gff",
                     '--errfile=' + tmp + "seq" + str(hindex) + "_" + species.rstrip() + ".err", currlen]
        calls.append(curr_call)

    # execute processes in parallel
    logger.info("Executing the following commands: ")
    logger.info(calls)
    if __name__ == '__main__':
        with multiprocessing.Pool(processes=args.threads) as pool:
            results = pool.map(work_augustus, calls)
    logger.info("Finished parallel execution!")
    # parse results and find max prob for this sequence
    results = {}
    for species in params:
        try:
            with open(tmp + "seq" + str(hindex) + "_" + species.rstrip() + ".gff", "r") as spec_result_handle:
                for line in spec_result_handle:
                    thismatch1 = re.search(r'\# joint probability of gene structure and sequence in \S+ model: (\d+\.\d+)$', line)
                    thismatch2 = re.search(r'\# joint probability of gene structure and sequence in \S+ model: (\d+\.\d+)e(-\d+)', line)
                    thismatch3 = re.search(r'\# joint probability of gene structure and sequence in \S+ model: 0$', line)
                    thismatch4 = re.search(r'\# joint probability of gene structure and sequence in \S+ model: (\d+)e(-\d+)', line)
                    
                    if thismatch1:
                        mantisse,exponent = normalize_float(float(thismatch1.group(1)), int(0))
                        results[species.rstrip()] = {'mantisse' : mantisse,
                                                     'exponent' : exponent,
                                                     'original' : thismatch1.group(1)}
                    elif thismatch2:
                        mantisse,exponent = normalize_float(float(thismatch2.group(1)), int(thismatch2.group(2)))
                        results[species.rstrip()] = {'mantisse' : mantisse,
                                                     'exponent' : exponent,
                                                     'original' : thismatch2.group(1) + 'e' + thismatch2.group(2)}
                    elif thismatch3:
                        results[species.rstrip()] = {'mantisse' : float(0),
                                                     'exponent' : int(0),
                                                     'original' : 0}
                    elif thismatch4:
                        mantisse,exponent = normalize_float(float(thismatch4.group(1)), int(thismatch4.group(2)))
                        results[species.rstrip()] = {'mantisse' : mantisse,
                                                     'exponent' : exponent,
                                                     'original' : thismatch4.group(1) + 'e' + thismatch4.group(2)}
            
                if species.rstrip() not in results:
                    # AUGUSTUS may not predict any gene at all, hence no probability available
                    results[species.rstrip()] = {'mantisse' : float(0),
                                                 'exponent' : int(0),
                                                 'original' : 0}

        except IOError:
            frameinfo = getframeinfo(currentframe())
            logger.info('Error in file ' + frameinfo.filename + ' at line ' +
                    str(frameinfo.lineno) + ': ' + "Could not open file " +
                    tmp + "seq" + str(hindex) + "_" + species.rstrip() + ".gff" + " for reading!")

        try:
            os.remove(tmp + "seq" + str(hindex) + '_' + species.rstrip() + '.gff')
        except OSError:
            pass

        try:
            os.remove(tmp + 'seq' + str(hindex) + '_' + species.rstrip() + '.err')
        except OSError:
            pass

    # determine maximum exponent
    max_exp = float('-inf')
    max_mant = 0.0
    max_species = "undef"
    for species in results:
        if not(results[species]['mantisse'] == 0):
            if (results[species]['exponent'] == max_exp) and (results[species]['mantisse'] > max_mant):
                max_mant = results[species]['mantisse']
                max_species = species
            elif results[species]['exponent'] > max_exp:
                max_exp = results[species]['exponent']
                max_mant = results[species]['mantisse']
                max_species = species

    thismatch = re.search(r'>(\S+)', header.rstrip())
    # write result if appropriate
    if args.metagenomic_classification_outfile:
        try:
            with open(args.metagenomic_classification_outfile, "a+") as classify_handle:
                if not (max_mant == 0):
                    classify_handle.write(thismatch.group(1) + "\t" + max_species +
                                        "\t" + results[max_species]['original'] + "\n")
                else: # this case should not occur anymore unless you use only completely unfitting parameter sets
                    classify_handle.write(thismatch.group(1) + "\t" + "undef" +
                                        "\t" + str(0) + "\n")
        except IOError:
            frameinfo = getframeinfo(currentframe())
            logger.info('Error in file ' + frameinfo.filename + ' at line ' +
                        str(frameinfo.lineno) + ': ' + 'Could not open file ' +
                        args.metagenomic_classification_outfile + ' for writing!')
            
    # run augustus with all gene models for the selected species
    if args.prediction_file and not(max_mant == 0):
        curr_call = [augustus, '--AUGUSTUS_CONFIG_PATH=' + augustus_config_path,
                     '--species=' + max_species,
                     '--softmasking=1', tmp + 'seq' + str(hindex) + '.fa',
                     '--outfile=' + tmp + 'seq' + str(hindex) + '_' + max_species + '_max.gff',
                     '--errfile=' + tmp + 'seq' + str(hindex) + '_' + max_species + '_max.err']
        aug_res = subprocess.run(curr_call, stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE, shell=False)
        try:
            with open(tmp + "seq" + str(hindex) + "_" + max_species + "_max.gff", "r") as aug_handle:
                try:
                    with open(tmp + "all_preds.gff", "a+") as gff_handle:
                        for line in aug_handle:
                            gff_handle.write(line)
                except IOError:
                    frameinfo = getframeinfo(currentframe())
                    logger.info('Error in file ' + frameinfo.filename + ' at line ' +
                                str(frameinfo.lineno) + ': ' + 'Could not open file ' +
                                tmp + 'all_preds.gff' + ' for writing!')
        except IOError:
            frameinfo = getframeinfo(currentframe())
            logger.info('Error in file ' + frameinfo.filename + ' at line ' +
                        str(frameinfo.lineno) + ': ' + 'Could not open file ' +
                        tmp + 'seq' + str(hindex) + '_' + max_species + 
                        '_max.gff' + ' for reading!')

        try:
            os.remove(tmp + 'seq' + str(hindex) + '_' + max_species + '_max.gff')
        except OSError:
            pass

        try:
            os.remove(tmp + 'seq' + str(hindex) + '_' + max_species + '_max.err')
        except OSError:
            pass

        try:
            os.remove(tmp + 'seq' + str(hindex) + '.fa')
        except OSError:
            pass

    if max_mant == 0:
        return "undef"
    else:
        return max_species

''' ******************* END FUNCTIONS *************************************'''

### Create log file and tmp directory for saving files that can be removed afterwards ###
rString = create_random_string()
log = create_log_file_name(rString)
logger = logging.getLogger("")
logger.setLevel(logging.INFO)
fh = logging.FileHandler(log)
fh.setLevel(logging.INFO)
formatter = logging.Formatter("%(message)s")
fh.setFormatter(formatter)
logger.addHandler(fh)
tmp = create_tmp_dir(rString)

### Check whether provided threads are available
count = len(os.sched_getaffinity(0))
if args.threads > count:
    frameinfo = getframeinfo(currentframe())
    logger.info('Warning in file ' + frameinfo.filename + ' at line ' +
                str(frameinfo.lineno) + ': ' + 'You provided ' +
                str(args.threads) + ' threads but your system has only ' +
                str(count) + ' cores. Decreasing to that number.')
    args.threads = count

### Find augustus_bin_path ###
if args.augustus_bin_path:
    augustus = check_tool_in_given_path(args.augustus_bin_path, "augustus")
else:
    augustus = find_tool("augustus")

if args.prediction_file:
    ### Find join_aug_preds.pl - used only if option prediction_file is set
    perl = find_tool("perl")
    if args.augustus_bin_path:
        join_aug_pred = check_tool_in_given_path(args.augustus_bin_path + "../scripts/", "join_aug_pred.pl")
    else:
        join_aug_pred = find_tool("join_aug_pred.pl")


### Find augustus_config_path ###
augustus_config_path = ""
if args.augustus_config_path:
    augustus_config_path = args.augustus_config_path
else:
    logger.info("Trying to find environment variable " +
                "AUGUSTUS_CONFIG_PATH")
    if os.environ.get('AUGUSTUS_CONFIG_PATH') is not None:
        test_augustus_config_path = os.environ.get('AUGUSTUS_CONFIG_PATH')
        if os.path.exists(test_augustus_config_path):
            augustus_config_path = test_augustus_config_path
            logger.info("Found environment variable AUGUSTUS_CONFIG_PATH " +
                        "and set augustus_config_path to environment " +
                        "variable AUGUSTUS_CONFIG_PATH")
if augustus_config_path == "":
    logger.info(
        "Did not find environment variable AUGUSTUS_CONFIG_PATH " +
        "(either variable does not exist, or the path given in variable " +
        "does not exist). Will try to set this variable in a different " +
        "way. \n" +
        "Trying to set augustus_config_path to " +
        "augustus_bin_path/../config/")
    augustus_bin_path = augustus
    augustus_bin_path = re.sub(r'/augustus', '/', augustus_bin_path)
    test_augustus_config_path = augustus_bin_path + "../config/"
    if os.path.exists(test_augustus_config_path):
        augustus_config_path = test_augustus_config_path
    else:
        frameinfo = getframeinfo(currentframe())
        logger.info('Error in file ' + frameinfo.filename + ' at line ' +
                    str(frameinfo.lineno) + ': ' + 'Unable to set ' +
                    'augustus_config_path!')
        exit(1)

### Read species parameter names
params = [];
try:
    with open(args.parameter_list, "r") as params_handle:
        for line in params_handle:
            params.append(line)
except IOError:
    frameinfo = getframeinfo(currentframe())
    logger.info('Error in file ' + frameinfo.filename + ' at line ' +
                str(frameinfo.lineno) + ': ' + 'Could not open file ' +
                args.parameter_list + ' for reading!')


### Check whether given parameter sets are valid
must_delete = [];
for species_set in params:
    species_set = species_set.rstrip()
    curr_spec_set_file = augustus_config_path +  '/species/' + species_set + '/' + species_set + '_exon_probs.pbl'
    # print("Checking for " + curr_spec_set_file)
    if os.path.isfile(curr_spec_set_file):
        must_delete.append(True)
    else:
        must_delete.append(False)
        frameinfo = getframeinfo(currentframe())
        logger.info('Warning in file ' + frameinfo.filename + ' at line ' +
                str(frameinfo.lineno) + ': ' + 'Species parameter set ' + species_set +
                    ' does not exist in ' + augustus_config_path +
                    '. Will ignore this parameter set!')
params = list(compress(params, must_delete))

### Check whether more than 1 set remains
if len(params) < 2:
    frameinfo = getframeinfo(currentframe())
    if len(params) == 0:
        logger.info('Error in file ' + frameinfo.filename + ' at line ' +
                str(frameinfo.lineno) + ': ' +
                'Remaining number of parameter sets is zero. ' +
                'Please run augustus outside of augustify.py with ' +
                'a parameter set!')
    else:
        logger.info('Error in file ' + frameinfo.filename + ' at line ' +
                   str(frameinfo.lineno) + ': ' +
                    'Remaining number of parameter sets is one. ' +
                    'Please run augustus outside of augustify.py with ' +
                    'only one parameter set (here: ' + params[0] +')!')
    exit(1)

### Check whether number of threads is appropriate
if args.threads > len(params):
    frameinfo = getframeinfo(currentframe())
    logger.info('Warning in file ' + frameinfo.filename + ' at line ' +
                str(frameinfo.lineno) + ': ' +
                'Number of threads is with ' + str(args.threads) +
                ' greater than number of species parameter sets (here: ' +
                str(len(params)) + '). ' +
                'Decreasing number of threads to ' + str(len(params)) +
                '.')
    args.threads = len(params)


### Open genome file, loop over single sequences
seq_to_spec = {}
try:
    with open(args.genome, "r") as genome_handle:
        curr_seq = ""
        curr_header = ""
        hindex = 0;
        for line in genome_handle:
            if re.match(r'^>', line):
                hindex = hindex + 1
                if len(curr_seq) > 0:
                    fitting_species = augustify_seq(hindex, curr_header, curr_seq, tmp, params)
                    seq_to_spec[curr_header] = fitting_species
                curr_header = line;
                curr_seq = "";
            else:
                curr_seq += line
        # process the last sequence
        if hindex > 0 and len(curr_seq) > 0:
            fitting_species = augustify_seq(hindex, curr_header, curr_seq, tmp, params)
            seq_to_spec[curr_header] = fitting_species
        else:
            logger.info('Wrong formatted genome fasta file ' + args.genome + ' !')

except IOError:
    frameinfo = getframeinfo(currentframe())
    logger.info('Error in file ' + frameinfo.filename + ' at line ' +
                str(frameinfo.lineno) + ': ' + 'Could not open file ' +
                args.genome + ' for reading!')

### Compute majority vote: most often selected parameter set wins
if(args.species):
    count_specs = {}
    for seq in seq_to_spec:
        if seq_to_spec[seq] not in count_specs:
            count_specs[seq_to_spec[seq]] = 1
        else:
            count_specs[seq_to_spec[seq]] = count_specs[seq_to_spec[seq]] + 1

    max_setcount = 0
    max_name = ""
    for spec in count_specs:
        if count_specs[spec] > max_setcount:
            max_setcount = count_specs[spec]
            max_name = spec

    print("Best species: " + max_name)

### Merge augustus predictions
if args.prediction_file:
    try:
        with open(tmp + "all_preds.gff", "r") as aug_single_handle:
            subprcs_args = [perl, join_aug_pred]
            result = run_process_stdinput(subprcs_args, aug_single_handle)
            try:
                with open(args.prediction_file, "w") as aug_merged_handle:
                    aug_merged_handle.write(result.stdout.decode('utf-8'))
            except IOError:
                frameinfo = getframeinfo(currentframe())
                logger.info('Error in file ' + frameinfo.filename + ' at line ' +
                            str(frameinfo.lineno) + ': ' + 'Could not open file ' +
                            args.prediction_file + ' for writing!')
    except IOError:
        frameinfo = getframeinfo(currentframe())
        logger.info('Error in file ' + frameinfo.filename + ' at line ' +
                    str(frameinfo.lineno) + ': ' + 'Could not open file ' +
                    tmp + 'all_preds.gff' + ' for reading!')

### Cleanup
shutil.rmtree(tmp)