11#!/usr/bin/python3
22
3- # Copyright 2017 Francisco Pina Martins <f.pinamartins@gmail.com>
3+ # Copyright 2017-2018 Francisco Pina Martins <f.pinamartins@gmail.com>
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
1717
1818import os
1919import logging
20+ import itertools
21+ import random
22+
2023
2124try :
2225 import colorer .colorer as colorer
2326except 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