Back to index

python-biopython  1.60
Functions
Bio.PopGen.SimCoal.Template Namespace Reference

Functions

def exec_template
def process_para
def dupe
def get_xy_from_matrix
def get_step_2d
def generate_ssm2d_mat
def generate_island_mat
def generate_null_mat
def generate_join_events
def no_processor
def process_text
def generate_model
def get_demography_template
def _gen_loci
def get_chr_template
def generate_simcoal_from_template

Function Documentation

def Bio.PopGen.SimCoal.Template._gen_loci (   stream,
  loci 
) [private]

Definition at line 162 of file Template.py.

00162 
00163 def _gen_loci(stream, loci):
00164     stream.write('//Number of contiguous linkage blocks in chromosome\n')
00165     stream.write(str(len(loci)) + '\n')
00166     stream.write('//Per Block: Data type, No. of loci, Recombination rate to the right-side locus, plus optional parameters\n')
00167     for locus in loci:
00168         stream.write(' '.join([locus[0]] +
00169             map(lambda x: str(x), list(locus[1])
00170         )) + '\n')

Here is the caller graph for this function:

def Bio.PopGen.SimCoal.Template.dupe (   motif,
  times 
)

Definition at line 57 of file Template.py.

00057 
00058 def dupe(motif, times):
00059     ret_str = ''
00060     for i in range(1, times + 1):
00061         ret_str += motif + '\r\n'
00062     return ret_str

Definition at line 14 of file Template.py.

00014 
00015 def exec_template(template):
00016     executed_template = template
00017     match = re.search('!!!(.*?)!!!', executed_template, re.MULTILINE)
00018     #while len(match.groups())>0:
00019     while match:
00020         exec_result = str(eval(match.groups()[0]))
00021         executed_template = executed_template.replace(
00022                '!!!' + match.groups()[0] + '!!!',
00023                exec_result, 1)
00024         match = re.search('!!!(.*?)!!!', executed_template, re.MULTILINE)
00025         #match = patt.matcher(String(executed_template))
00026     return executed_template
00027     

Here is the caller graph for this function:

def Bio.PopGen.SimCoal.Template.generate_island_mat (   total_size,
  mig 
)

Definition at line 85 of file Template.py.

00085 
00086 def generate_island_mat(total_size, mig):
00087     mig_mat = ''
00088     for x in range(1, total_size + 1):
00089         for y in range(1, total_size + 1):
00090             if (x == y):
00091                 mig_mat += '0 '
00092             else:
00093                 mig_mat += '!!!' + str(mig) + '!!! '
00094         mig_mat += "\r\n"
00095     return mig_mat

def Bio.PopGen.SimCoal.Template.generate_join_events (   t,
  total_size,
  join_size,
  orig_size 
)

Definition at line 104 of file Template.py.

00104 
00105 def generate_join_events(t, total_size, join_size, orig_size):
00106     events = ''
00107     for i in range(1, total_size-1):
00108         events += str(t) + ' ' + str(i) + ' 0 1 1 0 1\r\n'
00109     events += str(t) + ' ' + str(total_size-1) + ' 0 1 ' + str(1.0*total_size*join_size/orig_size) + ' 0 1\r\n'
00110     return events

def Bio.PopGen.SimCoal.Template.generate_model (   par_stream,
  out_prefix,
  params,
  specific_processor = no_processor,
  out_dir = '.' 
)

Definition at line 132 of file Template.py.

00132 
00133     specific_processor = no_processor, out_dir = '.'):
00134     #prepare_dir()
00135     text = par_stream.read()
00136     out_file_prefix = sep.join([out_dir, out_prefix])
00137     return process_text(text, out_file_prefix, params, [], specific_processor)
00138 

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 96 of file Template.py.

00096 
00097 def generate_null_mat(total_size):
00098     null_mat = ''
00099     for x in range(1, total_size + 1):
00100         for y in range(1, total_size + 1):
00101             null_mat += '0 '
00102         null_mat += '\r\n'
00103     return null_mat

def Bio.PopGen.SimCoal.Template.generate_simcoal_from_template (   model,
  chrs,
  params,
  out_dir = '.',
  tp_dir = None 
)
   Writes a complete SimCoal2 template file.

   This joins together get_demography_template and get_chr_template,
   which are feed into generate_model
   Please check the three functions for parameters (model from
     get_demography_template, chrs from get_chr_template and
     params from generate_model).

Definition at line 200 of file Template.py.

00200 
00201 def generate_simcoal_from_template(model, chrs, params, out_dir = '.', tp_dir=None):
00202     '''
00203        Writes a complete SimCoal2 template file.
00204 
00205        This joins together get_demography_template and get_chr_template,
00206        which are feed into generate_model
00207        Please check the three functions for parameters (model from
00208          get_demography_template, chrs from get_chr_template and
00209          params from generate_model).
00210     '''
00211     stream = open(out_dir + sep + 'tmp.par', 'w')
00212     get_demography_template(stream, model, tp_dir)
00213     get_chr_template(stream, chrs)
00214     stream.close()
00215     #par_stream = open(out_dir + sep + 'tmp.par', 'r')
00216     #print par_stream.read()
00217     #par_stream.close()
00218     par_stream = open(out_dir + sep + 'tmp.par', 'r')
00219     generate_model(par_stream, model, params, out_dir = out_dir)
00220     par_stream.close()
00221 
00222 

Here is the call graph for this function:

Here is the caller graph for this function:

def Bio.PopGen.SimCoal.Template.generate_ssm2d_mat (   x_max,
  y_max,
  mig 
)

Definition at line 77 of file Template.py.

00077 
00078 def generate_ssm2d_mat(x_max, y_max, mig):
00079     mig_mat = ''
00080     for x in range(1, x_max*y_max + 1):
00081         for y in range(1, x_max*y_max + 1):
00082             mig_mat += get_step_2d(x_max, y_max, x, y, mig)
00083         mig_mat += "\r\n"
00084     return mig_mat

Here is the call graph for this function:

def Bio.PopGen.SimCoal.Template.get_chr_template (   stream,
  chrs 
)
    Writes a Simcoal2 loci template part.

    stream - Writable stream.
    chr    - Chromosome list.

    Current loci list:
      [(chr_repeats,[(marker, (params))])]
      chr_repeats --> Number of chromosome repeats
      marker  --> 'SNP', 'DNA', 'RFLP', 'MICROSAT'
      params  --> Simcoal2 parameters for markers (list of floats
        or ints - if to be processed by generate_model)

Definition at line 171 of file Template.py.

00171 
00172 def get_chr_template(stream, chrs):
00173     '''
00174         Writes a Simcoal2 loci template part.
00175 
00176         stream - Writable stream.
00177         chr    - Chromosome list.
00178 
00179         Current loci list:
00180           [(chr_repeats,[(marker, (params))])]
00181           chr_repeats --> Number of chromosome repeats
00182           marker  --> 'SNP', 'DNA', 'RFLP', 'MICROSAT'
00183           params  --> Simcoal2 parameters for markers (list of floats
00184             or ints - if to be processed by generate_model)
00185     '''
00186     num_chrs = reduce(lambda x, y: x + y[0], chrs, 0)
00187     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')
00188     if len(chrs) > 1 or num_chrs == 1:
00189         stream.write(str(num_chrs) + ' 1\n')
00190     else:
00191         stream.write(str(num_chrs) + ' 0\n')
00192     for chr in chrs:
00193         repeats = chr[0]
00194         loci = chr[1]
00195         if len(chrs) == 1:
00196             _gen_loci(stream, loci)
00197         else:
00198             for i in range(repeats):
00199                 _gen_loci(stream, loci)

Here is the call graph for this function:

Here is the caller graph for this function:

def Bio.PopGen.SimCoal.Template.get_demography_template (   stream,
  model,
  tp_dir = None 
)
    Gets a demograpy template.
 
    Most probably this model needs to be sent to GenCases.
 
    stream - Writable stream.
    param  - Template file.
    tp_dir - Directory where to find the template, if None
             use an internal template

Definition at line 139 of file Template.py.

00139 
00140 def get_demography_template(stream, model, tp_dir = None):
00141     '''
00142         Gets a demograpy template.
00143  
00144         Most probably this model needs to be sent to GenCases.
00145  
00146         stream - Writable stream.
00147         param  - Template file.
00148         tp_dir - Directory where to find the template, if None
00149                  use an internal template
00150     '''
00151     if tp_dir == None:
00152         #Internal Template
00153         f = open(sep.join([builtin_tpl_dir, model + '.par']), 'r')
00154     else:
00155         #External template
00156         f = open(sep.join([tp_dir, model + '.par']), 'r')
00157     l = f.readline()
00158     while l!='':
00159         stream.write(l)
00160         l = f.readline()
00161     f.close()

Here is the call graph for this function:

Here is the caller graph for this function:

def Bio.PopGen.SimCoal.Template.get_step_2d (   x_max,
  y_max,
  x,
  y,
  mig 
)

Definition at line 68 of file Template.py.

00068 
00069 def get_step_2d(x_max, y_max, x, y, mig):
00070     my_x,    my_y    = get_xy_from_matrix(x_max, y_max, y)
00071     other_x, other_y = get_xy_from_matrix(x_max, y_max, x)
00072         
00073     if (my_x-other_x)**2 + (my_y-other_y)**2 == 1:
00074         return str(mig) + ' '
00075     else:
00076         return '0 '

Here is the call graph for this function:

Here is the caller graph for this function:

def Bio.PopGen.SimCoal.Template.get_xy_from_matrix (   x_max,
  y_max,
  pos 
)

Definition at line 63 of file Template.py.

00063 
00064 def get_xy_from_matrix(x_max, y_max, pos):
00065     y = (pos-1) / x_max
00066     x = (pos-1) % x_max
00067     return x, y

Here is the caller graph for this function:

Definition at line 111 of file Template.py.

00111 
00112 def no_processor(in_string):
00113     return in_string

Here is the call graph for this function:

def Bio.PopGen.SimCoal.Template.process_para (   in_string,
  out_file_prefix,
  para_list,
  curr_values 
)

Definition at line 28 of file Template.py.

00028 
00029 def process_para(in_string, out_file_prefix, para_list, curr_values):
00030     if (para_list == []):
00031         template = in_string
00032         f_name = out_file_prefix
00033         #f_name += '_' + str(total_size)
00034         for tup in curr_values:
00035             name, val = tup
00036             f_name += '_' + str(val) 
00037             #reg = re.compile('\?' + name, re.MULTILINE)
00038             #template = re.sub(reg, str(val), template)
00039             template = template.replace('?'+name, str(val))
00040         f = open(f_name + '.par', 'w')
00041         #executed_template = template
00042         executed_template = exec_template(template)
00043         clean_template =  executed_template.replace('\r\n','\n').replace('\n\n','\n')
00044         f.write(clean_template)
00045         f.close()
00046         return [f_name]
00047     else:
00048         name, rng = para_list[0]
00049         fnames = []
00050         for val in rng:
00051             new_values = [(name, val)]
00052             new_values.extend(curr_values)
00053             more_names = process_para(in_string, out_file_prefix, para_list[1:], new_values)
00054             fnames.extend(more_names)
00055         return fnames
00056 

Here is the call graph for this function:

Here is the caller graph for this function:

def Bio.PopGen.SimCoal.Template.process_text (   in_string,
  out_file_prefix,
  para_list,
  curr_values,
  specific_processor 
)

Definition at line 115 of file Template.py.

00115 
00116                  specific_processor):
00117     text = specific_processor(in_string)
00118     return process_para(text, out_file_prefix, para_list, [])
00119 
00120 #def prepare_dir():
00121 #    try:
00122 #        mkdir(sep.join([Config.dataDir, 'SimCoal'])) #Should exist, but...
00123 #    except OSError:
00124 #        pass #Its ok if already exists
00125 #    try:
00126 #        mkdir(sep.join([Config.dataDir, 'SimCoal', 'runs']))
00127 #    except OSError:
00128 #        pass #Its ok if already exists
00129     
00130 
#sep is because of jython

Here is the call graph for this function:

Here is the caller graph for this function: