Package Bio :: Package PopGen :: Package SimCoal :: Module Template
[hide private]
[frames] | no frames]

Source Code for Module Bio.PopGen.SimCoal.Template

  1  # Copyright 2007 by Tiago Antao.  All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5   
  6  from __future__ import print_function 
  7   
  8  from os import sep 
  9  import re 
 10  from functools import reduce 
 11   
 12  from Bio.PopGen.SimCoal import builtin_tpl_dir 
 13   
 14  __docformat__ = "restructuredtext en" 
 15   
16 -def exec_template(template):
17 executed_template = template 18 match = re.search('!!!(.*?)!!!', executed_template, re.MULTILINE) 19 # while len(match.groups())>0: 20 while match: 21 exec_result = str(eval(match.groups()[0])) 22 executed_template = executed_template.replace( 23 '!!!' + match.groups()[0] + '!!!', 24 exec_result, 1) 25 match = re.search('!!!(.*?)!!!', executed_template, re.MULTILINE) 26 # match = patt.matcher(String(executed_template)) 27 return executed_template
28 29
30 -def process_para(in_string, out_file_prefix, para_list, curr_values):
31 if (para_list == []): 32 template = in_string 33 f_name = out_file_prefix 34 # f_name += '_' + str(total_size) 35 for tup in curr_values: 36 name, val = tup 37 f_name += '_' + str(val) 38 # reg = re.compile('\?' + name, re.MULTILINE) 39 # template = re.sub(reg, str(val), template) 40 template = template.replace('?'+name, str(val)) 41 with open(f_name + '.par', 'w') as f: 42 # executed_template = template 43 executed_template = exec_template(template) 44 clean_template = executed_template.replace('\r\n', '\n').replace('\n\n', '\n') 45 f.write(clean_template) 46 return [f_name] 47 else: 48 name, rng = para_list[0] 49 fnames = [] 50 for val in rng: 51 new_values = [(name, val)] 52 new_values.extend(curr_values) 53 more_names = process_para(in_string, out_file_prefix, para_list[1:], new_values) 54 fnames.extend(more_names) 55 return fnames
56 57
58 -def dupe(motif, times):
59 ret_str = '' 60 for i in range(1, times + 1): 61 ret_str += motif + '\r\n' 62 return ret_str
63 64
65 -def get_xy_from_matrix(x_max, y_max, pos):
66 y = (pos-1) / x_max 67 x = (pos-1) % x_max 68 return x, y
69 70
71 -def get_step_2d(x_max, y_max, x, y, mig):
72 my_x, my_y = get_xy_from_matrix(x_max, y_max, y) 73 other_x, other_y = get_xy_from_matrix(x_max, y_max, x) 74 75 if (my_x-other_x)**2 + (my_y-other_y)**2 == 1: 76 return str(mig) + ' ' 77 else: 78 return '0 '
79 80
81 -def generate_ssm2d_mat(x_max, y_max, mig):
82 mig_mat = '' 83 for x in range(1, x_max*y_max + 1): 84 for y in range(1, x_max*y_max + 1): 85 mig_mat += get_step_2d(x_max, y_max, x, y, mig) 86 mig_mat += "\r\n" 87 return mig_mat
88 89
90 -def generate_island_mat(total_size, mig):
91 mig_mat = '' 92 for x in range(1, total_size + 1): 93 for y in range(1, total_size + 1): 94 if (x == y): 95 mig_mat += '0 ' 96 else: 97 mig_mat += '!!!' + str(mig) + '!!! ' 98 mig_mat += "\r\n" 99 return mig_mat
100 101
102 -def generate_null_mat(total_size):
103 null_mat = '' 104 for x in range(1, total_size + 1): 105 for y in range(1, total_size + 1): 106 null_mat += '0 ' 107 null_mat += '\r\n' 108 return null_mat
109 110
111 -def generate_join_events(t, total_size, join_size, orig_size):
112 events = '' 113 for i in range(1, total_size-1): 114 events += str(t) + ' ' + str(i) + ' 0 1 1 0 1\r\n' 115 events += str(t) + ' ' + str(total_size-1) + ' 0 1 ' + str(1.0*total_size*join_size/orig_size) + ' 0 1\r\n' 116 return events
117 118
119 -def no_processor(in_string):
120 return in_string
121 122
123 -def process_text(in_string, out_file_prefix, para_list, curr_values, 124 specific_processor):
125 text = specific_processor(in_string) 126 return process_para(text, out_file_prefix, para_list, [])
127 128 129 # def prepare_dir(): 130 # try: 131 # mkdir(sep.join([Config.dataDir, 'SimCoal'])) # Should exist, but... 132 # except OSError: 133 # pass # Its ok if already exists 134 # try: 135 # mkdir(sep.join([Config.dataDir, 'SimCoal', 'runs'])) 136 # except OSError: 137 # pass # Its ok if already exists 138 139 140 # sep is because of jython
141 -def generate_model(par_stream, out_prefix, params, 142 specific_processor=no_processor, out_dir='.'):
143 # prepare_dir() 144 text = par_stream.read() 145 out_file_prefix = sep.join([out_dir, out_prefix]) 146 return process_text(text, out_file_prefix, params, [], specific_processor)
147 148
149 -def get_demography_template(stream, model, tp_dir=None):
150 ''' 151 Gets a demograpy template. 152 153 Most probably this model needs to be sent to GenCases. 154 155 - stream - Writable stream. 156 - param - Template file. 157 - tp_dir - Directory where to find the template, if None 158 use an internal template 159 ''' 160 if tp_dir is None: 161 # Internal Template 162 filename = sep.join([builtin_tpl_dir, model + '.par']) 163 else: 164 # External template 165 filename = sep.join([tp_dir, model + '.par']) 166 with open(filename, 'r') as f: 167 l = f.readline() 168 while l!='': 169 stream.write(l) 170 l = f.readline()
171 172
173 -def _gen_loci(stream, loci):
174 stream.write('//Number of contiguous linkage blocks in chromosome\n') 175 stream.write(str(len(loci)) + '\n') 176 stream.write('//Per Block: Data type, No. of loci, Recombination rate to the right-side locus, plus optional parameters\n') 177 for locus in loci: 178 stream.write(' '.join([locus[0]] + 179 [str(x) for x in list(locus[1])]) + '\n')
180 181
182 -def get_chr_template(stream, chrs):
183 ''' 184 Writes a Simcoal2 loci template part. 185 186 stream - Writable stream. 187 chr - Chromosome list. 188 189 Current loci list: 190 191 - [(chr_repeats,[(marker, (params))])] 192 193 - chr_repeats --> Number of chromosome repeats 194 - marker --> 'SNP', 'DNA', 'RFLP', 'MICROSAT' 195 - params --> Simcoal2 parameters for markers (list of floats 196 or ints - if to be processed by generate_model) 197 ''' 198 num_chrs = reduce(lambda x, y: x + y[0], chrs, 0) 199 stream.write('//Number of independent (unlinked) chromosomes, and "chromosome structure" flag: 0 for identical structure across chromosomes, and 1 for different structures on different chromosomes.\n') 200 if len(chrs) > 1 or num_chrs == 1: 201 stream.write(str(num_chrs) + ' 1\n') 202 else: 203 stream.write(str(num_chrs) + ' 0\n') 204 for chr in chrs: 205 repeats = chr[0] 206 loci = chr[1] 207 if len(chrs) == 1: 208 _gen_loci(stream, loci) 209 else: 210 for i in range(repeats): 211 _gen_loci(stream, loci)
212 213
214 -def generate_simcoal_from_template(model, chrs, params, out_dir='.', tp_dir=None):
215 ''' 216 Writes a complete SimCoal2 template file. 217 218 This joins together get_demography_template and get_chr_template, 219 which are feed into generate_model 220 Please check the three functions for parameters (model from 221 get_demography_template, chrs from get_chr_template and 222 params from generate_model). 223 ''' 224 with open(out_dir + sep + 'tmp.par', 'w') as stream: 225 get_demography_template(stream, model, tp_dir) 226 get_chr_template(stream, chrs) 227 # with open(out_dir + sep + 'tmp.par', 'r') as par_stream: 228 # print par_stream.read() 229 with open(out_dir + sep + 'tmp.par', 'r') as par_stream: 230 generate_model(par_stream, model, params, out_dir=out_dir)
231