Skip to content

Commit 96f6282

Browse files
committed
Adds a way to provide a random seed to STRUCTURE
1 parent 7902732 commit 96f6282

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

structure_threader/argparser.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ def argument_parser(args):
128128
misc_opts.add_argument("--extra_opts", dest="extra_options", type=str,
129129
required=False,
130130
help="Add extra arguments to pass to the "
131-
"wrapped program here.\nExample: "
132-
"prior=logistic seed=123",
131+
"wrapped program here (between quotes).\nExamples: "
132+
"\"prior=logistic seed=123\" for fastStructure\n"
133+
"\"-D 12345\" for STRUCTURE",
133134
metavar="string", default="")
134135

135136
plot_opts.add_argument("--no_plots", dest="noplot", type=bool,
@@ -246,9 +247,11 @@ def argument_sanity(arguments, parser):
246247

247248
# Handle argparse limitations with "--" options.
248249
if arguments.extra_options != "":
249-
arguments.extra_options = "--{0}".format(arguments.extra_options)
250-
arguments.extra_options = \
251-
" --".join(arguments.extra_options.split())
250+
if "-st" not in sys.argv:
251+
arguments.extra_options = \
252+
"--{0}".format(arguments.extra_options)
253+
arguments.extra_options = \
254+
" --".join(arguments.extra_options.split())
252255

253256
# fastStructure is really only usefull with either a pop or indfile...
254257
if "-fs" in sys.argv and\

structure_threader/structure_threader.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,14 @@ def runprogram(wrapped_prog, iterations, arg):
7272
"""
7373
worker_status = (None, None)
7474

75-
k_val, rep_num = iterations
75+
try:
76+
seed, k_val, rep_num = iterations
77+
except ValueError:
78+
k_val, rep_num = iterations
79+
seed = None
7680

7781
if wrapped_prog == "structure": # Run STRUCTURE
78-
cli, output_file = sw.str_cli_generator(arg, k_val, rep_num)
82+
cli, output_file = sw.str_cli_generator(arg, k_val, rep_num, seed)
7983

8084
elif wrapped_prog == "maverick": # Run MavericK
8185
mav_params = mw.mav_params_parser(arg.params)
@@ -122,10 +126,10 @@ def structure_threader(wrapped_prog, arg):
122126

123127
if wrapped_prog != "structure":
124128
arg.replicates = [1]
129+
jobs = list(itertools.product(arg.k_list, arg.replicates))[::-1]
125130
else:
126131
sw.str_param_checker(arg)
127-
128-
jobs = list(itertools.product(arg.k_list, arg.replicates))[::-1]
132+
jobs = sw.seed_generator(arg.extra_options, arg.k_list, arg.replicates)
129133

130134
# This allows us to pass partial arguments to a function so we can later
131135
# use it with multiprocessing map().

structure_threader/wrappers/structure_wrapper.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/python3
22

3-
# Copyright 2017 Francisco Pina Martins <[email protected]>
3+
# Copyright 2017-2018 Francisco Pina Martins <[email protected]>
44
# This file is part of structure_threader.
55
# structure_threader is free software: you can redistribute it and/or modify
66
# it under the terms of the GNU General Public License as published by
@@ -17,24 +17,33 @@
1717

1818
import os
1919
import logging
20+
import itertools
21+
import random
22+
2023

2124
try:
2225
import colorer.colorer as colorer
2326
except ImportError:
2427
import structure_threader.colorer.colorer as colorer
2528

2629

27-
def str_cli_generator(arg, k_val, rep_num):
30+
def str_cli_generator(arg, k_val, rep_num, seed):
2831
"""
2932
Generates and returns command line for running STRUCTURE.
3033
"""
3134
output_file = os.path.join(arg.outpath, "str_K" + str(k_val) + "_rep" +
3235
str(rep_num))
3336
cli = [arg.external_prog, "-K", str(k_val), "-i", arg.infile, "-o",
3437
output_file]
38+
39+
if seed is not None:
40+
cli += ["-D", seed]
41+
3542
if arg.params is not None:
3643
cli += arg.params
3744

45+
print(cli)
46+
3847
return cli, output_file
3948

4049

@@ -54,3 +63,20 @@ def str_param_checker(arg):
5463
touch = open(extraparams, 'w')
5564
touch.close()
5665
arg.params = ["-m", mainparams, "-e", extraparams]
66+
67+
68+
def seed_generator(extra_options, k_list, replicates):
69+
"""
70+
Uses a user input seed value to generate *N* seeds, one for each run.
71+
Takes a seed value and the number of iterations as input and returns a
72+
job list: [(seed, K, replicate), ...].
73+
"""
74+
jobs = list(itertools.product(k_list, replicates))[::-1]
75+
76+
extra_options = extra_options.split()
77+
if "-D" in extra_options:
78+
seed = int(extra_options[extra_options.index("-D") + 1])
79+
random.seed(seed)
80+
jobs = [(str(random.randrange(10000000)),) + x for x in jobs]
81+
82+
return jobs

0 commit comments

Comments
 (0)