--- title: Runner module keywords: fastai sidebar: home_sidebar summary: "API details." description: "API details." nb_path: "nbs/01_Runner.ipynb" ---
tped = '../seqtest/CACHE/seqtest.chr16.tped'
tfam = '../seqtest/CACHE/seqtest.tfam'
with open(tped) as tped_fh, open(tfam) as tfam_fh:
fams = parse_tfam(tfam_fh)
for line in tped_fh:
s = line.strip().split()
gene, gno = re.search(r'^(\S+?)(?:\[(\d+)\])?$', s[1]).groups()
print(gene,gno)
from SEQLinkage.Main import *
from __future__ import print_function
from SEQLinkage.Utils import *
from multiprocessing import Process, Queue
from collections import OrderedDict
import itertools
from copy import deepcopy
import sys, faulthandler, platform
import numpy as np
import os
if sys.version_info.major == 2:
from cstatgen import cstatgen_py2 as cstatgen
from cstatgen.egglib import Align
else:
from cstatgen import cstatgen_py3 as cstatgen
import egglib
from egglib import Align
args = Args().parser.parse_args('--fam data/mwe_normal_fam.csv --vcf data/first1000snp_full_samples.vcf.gz -f MERLIN --blueprint data/genemap.hg38.txt --freq AF --run-linkage -K 0.001 --moi AD -W 0 -M 1 --theta-max 0.5 --theta-inc 0.05'.split())
args = Args().parser.parse_args('--fam ../seqlinkage-example/seqlinkage-example.fam --vcf ../seqlinkage-example/seqlinkage-example.vcf.gz -f MERLIN --blueprint ../data/genemap.txt --freq EVSEAAF -o seqtest --run-linkage -K 0.001 --moi AD -W 0 -M 1 --theta-max 0.5 --theta-inc 0.05'.split())
args
checkParams(args)
if args.no_save:
cache = NoCache()
else:
cache = Cache(env.cache_dir, env.output, vars(args))
cache.setID('vcf')
args.run_linkage
cache.setID('analysis')
env.output
?cache.load
cache.cache_name
not args.vanilla and cache.check()
fmt = args.format[0]
args.blueprint
args.theta_inc
args.theta_max
args.output_limit
??run_linkage
LINKAGE/LINKAGE$ head LINKAGE.chr1/DDX11L1/1036/*
env.tmp_dir = './LINKAGE'
env.jobs=8
env.tmp_dir
env.output
with open(os.path.join(env.tmp_dir, 'LinkageRuntimeError.txt'), 'w') as runtime_err:
workdirs = glob.glob('{}/LINKAGE/{}.chr*'.format(env.tmp_dir, env.output))
print(workdirs)
linkage_worker(blueprint, workdirs[0], theta_inc, theta_max, runtime_err, to_plot)
tmp = {'a':1,'b':2}
tmp.values()
blueprint, theta_inc, theta_max, to_plot = args.blueprint, args.theta_inc, args.theta_max, True
import glob
workdirs = glob.glob('{}/LINKAGE/{}.chr*'.format(env., env.output))
workdir = '../seqtest/LINKAGE/seqtest.chr16/'
env.tmp_dir
env.outdir
genemap = {}
if blueprint:
with open(blueprint) as f:
for line in f.readlines():
chrID, start, end, gene = line.strip().split()[:4]
genemap[gene] = [chrID, int(start), int(end)]
with env.lock:
mkpath('{}/heatmap'.format(env.outdir))
lods_fh = open('{}/heatmap/{}.lods'.format(env.outdir, basename(workdir)), 'w')
hlods_fh = open('{}/heatmap/{}.hlods'.format(env.outdir, basename(workdir)), 'w')
famlods_fh = open('{}/heatmap/{}.family-lods'.format(env.outdir, basename(workdir)), 'w')
genes = list(filter(lambda g: g in genemap, map(basename, glob.glob(workdir + '/*'))))
gene = genes[0]
lods = {}
hlods = {}
fams = list(map(basename, filter(isdir, glob.glob('{}/{}/*'.format(workdir, gene)))))
fams
for fam in fams:
with cd('{}/{}/{}'.format(workdir, gene, fam)):
units = map(lambda x: re.sub(r'^(\d+?)\.PRE$', r'\1', x) ,glob.glob('*.PRE'))
for unit in units:
copy_file('{}.LOC'.format(unit), 'datafile.dat')
copy_file('{}.PRE'.format(unit), 'pedfile.pre')
step1 = runCommand(['makeped', 'pedfile.pre', 'pedfile.ped', 'n'],
show_stderr = False, return_zero = False)
if step1[1]:
if env.debug:
with env.lock:
print("makeped error:", step1[1], file = sys.stderr)
with env.makeped_counter.get_lock():
env.makeped_counter.value += 1
with env.lock:
errfile.write(step1[1])
continue
step2 = runCommand(['pedcheck', '-p', 'pedfile.ped', '-d', 'datafile.dat', '-c'],
show_stderr = False, return_zero = False)
if step2[1]:
lines = [x for x in step2[1].split('\n')
if not x.strip().startswith('*') and x.strip()]
if len(lines) > 0:
env.log('{} lines'.format(len(lines)))
with env.lock:
errfile.write(step2[1])
if env.debug:
with env.lock:
print("pedcheck error:", '\n'.join(lines), file = sys.stderr)
try:
copy_file('zeroout.dat', 'pedfile.dat')
except:
with env.pedcheck_counter.get_lock():
env.pedcheck_counter.value += 1
continue
step3 = runCommand('unknown', show_stderr = False, return_zero = False)
if step3[1]:
if env.debug:
with env.lock:
print("unkn error:", step3[1], file = sys.stderr)
with env.unknown_counter.get_lock():
env.unknown_counter.value += 1
with env.lock:
errfile.write(step3[1])
continue
step4 = runCommand('mlink', show_stderr = False, return_zero = False)
if step4[1]:
if env.debug:
with env.lock:
print("mlink error:", step4[1], file = sys.stderr)
with env.lock:
errfile.write(step4[1])
try:
copy_file('outfile.dat', '{}.out'.format(unit))
except:
with env.mlink_counter.get_lock():
env.mlink_counter.value += 1
continue
#clean linkage tmp files
for f in set(glob.glob('*.dat') + glob.glob('ped*') + ['names.tmp']):
os.remove(f)
#collect lod scores of different thelta for the fam
with open('{}.out'.format(unit)) as out:
raw = out.read()
for i in re.finditer(r'^THETAS\s+(0\.\d+)(?:\n.+?){7}LOD SCORE =\s+(-?\d+\.\d+)', raw, re.MULTILINE):
theta, lod = map(float, i.group(1,2))
#if float(lod) < 1e-6:
# lod = 0
if theta not in lods:
lods[theta] = {fam: lod}
elif fam not in lods[theta] or lod > lods[theta][fam]:
lods[theta][fam] = lod
lods
def run_linkage(blueprint, theta_inc, theta_max, to_plot = True):
try:
remove_tree(os.path.join(env.outdir, 'heatmap'))
except OSError:
pass
with open(os.path.join(env.tmp_dir, 'LinkageRuntimeError.txt'), 'w') as runtime_err:
workdirs = glob.glob('{}/LINKAGE/{}.chr*'.format(env.tmp_dir, env.output))
parmap(lambda x: linkage_worker(blueprint, x, theta_inc, theta_max, runtime_err, to_plot) , workdirs, env.jobs)
def linkage_worker(blueprint, workdir, theta_inc, theta_max, errfile, to_plot = True):
#env.log("Start running LINKAGE for {} ...".format(workdir), flush=True)
#hash genes into genemap
genemap = {}
if blueprint:
with open(blueprint) as f:
for line in f.readlines():
chrID, start, end, gene = line.strip().split()[:4]
genemap[gene] = [chrID, int(start), int(end)]
else:
tped = os.path.join(env.tmp_cache, basename(workdir) + '.tped')
with open(tped) as f:
for line in f.readlines():
items = line.strip().split()[:4]
chrID = items[0]
gene = items[1]
pos = items[3]
genemap[gene] = [chrID, int(pos), int(pos)+1]
with env.lock:
mkpath('{}/heatmap'.format(env.outdir))
lods_fh = open('{}/heatmap/{}.lods'.format(env.outdir, basename(workdir)), 'w')
hlods_fh = open('{}/heatmap/{}.hlods'.format(env.outdir, basename(workdir)), 'w')
famlods_fh = open('{}/heatmap/{}.family-lods'.format(env.outdir, basename(workdir)), 'w')
genes = list(filter(lambda g: g in genemap, map(basename, glob.glob(workdir + '/*'))))
for gene in sorted(genes, key=lambda g: genemap[g]):
lods = {}
hlods = {}
fams = map(basename, filter(isdir, glob.glob('{}/{}/*'.format(workdir, gene))))
for fam in fams:
with cd('{}/{}/{}'.format(workdir, gene, fam)):
units = map(lambda x: re.sub(r'^(\d+?)\.PRE$', r'\1', x) ,glob.glob('*.PRE'))
for unit in units:
copy_file('{}.LOC'.format(unit), 'datafile.dat')
copy_file('{}.PRE'.format(unit), 'pedfile.pre')
step1 = runCommand(['makeped', 'pedfile.pre', 'pedfile.ped', 'n'],
show_stderr = False, return_zero = False)
if step1[1]:
if env.debug:
with env.lock:
print("makeped error:", step1[1], file = sys.stderr)
with env.makeped_counter.get_lock():
env.makeped_counter.value += 1
with env.lock:
errfile.write(step1[1])
continue
step2 = runCommand(['pedcheck', '-p', 'pedfile.ped', '-d', 'datafile.dat', '-c'],
show_stderr = False, return_zero = False)
if step2[1]:
lines = [x for x in step2[1].split('\n')
if not x.strip().startswith('*') and x.strip()]
if len(lines) > 0:
env.log('{} lines'.format(len(lines)))
with env.lock:
errfile.write(step2[1])
if env.debug:
with env.lock:
print("pedcheck error:", '\n'.join(lines), file = sys.stderr)
try:
copy_file('zeroout.dat', 'pedfile.dat')
except:
with env.pedcheck_counter.get_lock():
env.pedcheck_counter.value += 1
continue
step3 = runCommand('unknown', show_stderr = False, return_zero = False)
if step3[1]:
if env.debug:
with env.lock:
print("unkn error:", step3[1], file = sys.stderr)
with env.unknown_counter.get_lock():
env.unknown_counter.value += 1
with env.lock:
errfile.write(step3[1])
continue
step4 = runCommand('mlink', show_stderr = False, return_zero = False)
if step4[1]:
if env.debug:
with env.lock:
print("mlink error:", step4[1], file = sys.stderr)
with env.lock:
errfile.write(step4[1])
try:
copy_file('outfile.dat', '{}.out'.format(unit))
except:
with env.mlink_counter.get_lock():
env.mlink_counter.value += 1
continue
#clean linkage tmp files
for f in set(glob.glob('*.dat') + glob.glob('ped*') + ['names.tmp']):
os.remove(f)
#collect lod scores of different thelta for the fam
with open('{}.out'.format(unit)) as out:
raw = out.read()
for i in re.finditer(r'^THETAS\s+(0\.\d+)(?:\n.+?){7}LOD SCORE =\s+(-?\d+\.\d+)', raw, re.MULTILINE):
theta, lod = map(float, i.group(1,2))
#if float(lod) < 1e-6:
# lod = 0
if theta not in lods:
lods[theta] = {fam: lod}
elif fam not in lods[theta] or lod > lods[theta][fam]:
lods[theta][fam] = lod
for theta in sorted(lods.keys()):
lods_fh.write('{} {} {} {}\n'.format(gene, ' '.join(map(str, genemap[gene])), theta, sum(lods[theta].values())))
for fam in lods[theta]:
famlods_fh.write('{} {} {} {} {}\n'.format(fam, gene, ' '.join(map(str, genemap[gene])), theta, lods[theta][fam]))
print(lods[theta].values())
res = minimize_scalar(hlod_fun(lods[theta].values(), -1), bounds=(0,1), method='bounded', options={'xatol':1e-8})
a = res.x
hlods_fh.write('{} {} {} {} {}\n'.format(gene, ' '.join(map(str, genemap[gene])), a, theta, hlod_fun(lods[theta].values())(a)))
with env.run_counter.get_lock():
env.run_counter.value += 1
if env.run_counter.value % (env.batch * env.jobs) == 0:
env.log('Linkage analysis for {:,d} units completed {{{:.2%}}} ...'.format(env.run_counter.value, float(env.run_counter.value)/env.success_counter.value), flush=True)
lods_fh.close()
hlods_fh.close()
famlods_fh.close()
if to_plot:
heatmap('{}/heatmap/{}.lods'.format(env.outdir, basename(workdir)), theta_inc, theta_max)
heatmap('{}/heatmap/{}.hlods'.format(env.outdir, basename(workdir)), theta_inc, theta_max)
#env.log("Finished running LINKAGE for {}.".format(workdir), flush=True)
hlod_fun(lods[theta].values())(a)
res = minimize_scalar(hlod_fun(lods[theta].values(), -1), bounds=(0,1), method='bounded', options={'xatol':1e-8})
a = res.x
a
??hlod_fun
run_linkage(args.blueprint, args.theta_inc, args.theta_max, args.output_limit)
import sys, os, subprocess, shutil, glob, shlex, re, hashlib, tempfile
try:
from cStringIO import StringIO ## for Python 2
import urlparse
except ImportError:
from io import StringIO ## for Python 3
from urllib import parse as urlparse
from contextlib import contextmanager
from multiprocessing import Pool, Process, Queue, Lock, Value, cpu_count
import itertools
from collections import OrderedDict, defaultdict, Counter
from shutil import rmtree as remove_tree
from zipfile import ZipFile
cpu_count()
def parmap(f, X, nprocs = cpu_count()):
def spawn(f):
def fun(q_in,q_out):
while True:
i,x = q_in.get()
if i is None:
break
q_out.put((i,f(x)))
return fun
#
q_in = Queue(1)
q_out = Queue()
proc = [Process(target=spawn(f),args=(q_in,q_out)) for _ in range(nprocs)]
for p in proc:
p.daemon = True
p.start()
sent = [q_in.put((i,x)) for i,x in enumerate(X)]
[q_in.put((None,None)) for _ in range(nprocs)]
res = [q_out.get() for _ in range(len(sent))]
[p.join() for p in proc]
return [x for i,x in sorted(res)]
if args.run_linkage:
cache.setID('analysis')
if not args.vanilla and cache.check():
env.log('Loading linkage analysis result from archive ...'.format(fmt.upper()))
cache.load(target_dir = env.output, names = ['heatmap'])
else:
env.log('Running linkage analysis ...'.format(fmt.upper()))
run_linkage(args.blueprint, args.theta_inc, args.theta_max, args.output_limit)
env.log('Linkage analysis succesfully performed for {:,d} units\n'.\
format(env.run_counter.value, fmt.upper()), flush = True)
if env.makeped_counter.value:
env.log('{} "makeped" runtime errors occurred'.format(env.makeped_counter.value))
if env.pedcheck_counter.value:
env.log('{} "pedcheck" runtime errors occurred'.format(env.pedcheck_counter.value))
if env.unknown_counter.value:
env.log('{} "unknown" runtime errors occurred'.format(env.unknown_counter.value))
if env.mlink_counter.value:
env.log('{} "mlink" runtime errors occurred'.format(env.mlink_counter.value))
cache.write(arcroot = 'heatmap', source_dir = os.path.join(env.output, 'heatmap'), mode = 'a')
html(args.theta_inc, args.theta_max, args.output_limit)
else:
env.log('Saving data to [{}]'.format(os.path.abspath(env.output)))
cache.load(target_dir = env.output, names = [fmt.upper() for fmt in args.format])
env.log('Saving data to [{}]'.format(os.path.abspath(env.output)))
cache.load(target_dir = env.output, names = [fmt.upper() for fmt in args.format])
def hinton(filename, max_weight=None, ax=None):
if ax is None:
ax = plt.gca()
matrix = np.random.rand(20, 20) - 0.5
if not max_weight:
max_weight = 2**np.ceil(np.log(np.abs(matrix).max())/np.log(2))
ax.patch.set_facecolor('gray')
ax.set_aspect('equal', 'box')
ax.xaxis.set_major_locator(plt.NullLocator())
ax.yaxis.set_major_locator(plt.NullLocator())
chrID = re.search(r'\.chr([0-9XY]+)\.', filename).group(1)
ax.set_title('Chromosome {}'.format(chrID))
for (x,y),w in np.ndenumerate(matrix):
color = 'white' if w > 0 else 'black'
size = np.sqrt(np.abs(w))
rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,
facecolor=color, edgecolor=color)
ax.add_patch(rect)
ax.autoscale_view()
ax.invert_yaxis()
plt.savefig(filename)
def heatmap(file, theta_inc, theta_max):
#env.log("Start ploting heatmap for {} ...".format(file), flush=True)
if os.path.getsize(file) == 0:
hinton('{}.png'.format(file))
return
lods = []
with open(file, 'r') as f:
for line in f.readlines():
theta,lod = line.split()[-2:]
if float(theta) >= theta_max:
print(line)
continue
lods.append(lod)
#add random number.
if max(lods) == min(lods):
#env.log('Max equals Min for [{}], No real heatmap will be generated.'.format(file))
hinton('{}.png'.format(file))
return
Num=int(round(theta_max/theta_inc))
lods = np.array(list(map(float,lods))).reshape((-1,Num))
chrID = re.search(r'\.chr([0-9XY]+)\.', file).group(1)
fig, ax = plt.subplots(1)
ax.set_title('Chromosome {}'.format(chrID))
plt.pcolormesh(fig,ax,lods.transpose(),
xticklabels=[''] * len(lods),
yticklabels=np.round(np.array(range(Num)) * theta_inc,2).tolist(),
cmap=brewer2mpl.get_map('Blues', 'Sequential', 9).mpl_colormap)
fig.savefig('{}.png'.format(file))
theta_inc
Num=int(round(theta_max/theta_inc))
Num
lods = ['0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0']
lods = np.array(list(map(float, lods))).reshape((-1,Num))
lods.shape
len(lods)
[''] * len(lods)
np.round(np.array(range(Num)) * theta_inc,2).tolist()
brewer2mpl.get_map('Blues', 'Sequential', 9).mpl_colormap[1]
lods = np.random.rand(4,10)
%matplotlib inline
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
plt.pcolormesh(lods)
dx, dy = 1, 0.05
# generate 2 2d grids for the x & y bounds
y, x = np.mgrid[slice(0, 0.5 + dy, dy),
slice(0, 4 + dx, dx)]
plt.pcolormesh(x,y,lods.transpose(),cmap=brewer2mpl.get_map('Blues', 'Sequential', 9).mpl_colormap)
file = '{}/heatmap/{}.lods'.format(env.outdir, basename(workdirs[0]))
chrID = re.search(r'\.chr([0-9XY]+)\.', file).group(1)
fig, ax = plt.subplots(1)
ax.set_title('Chromosome {}'.format(chrID))
plt.pcolormesh(fig,ax,lods.transpose(),
xticklabels=[''] * len(lods),
yticklabels=np.round(np.array(range(Num)) * theta_inc,2).tolist(),
cmap=brewer2mpl.get_map('Blues', 'Sequential', 9).mpl_colormap)
fig.savefig('{}.png'.format(file))
heatmap('{}/heatmap/{}.lods'.format(env.outdir, basename(workdirs[0])), theta_inc, theta_max)
#heatmap('{}/heatmap/{}.hlods'.format(env.outdir, basename(workdir)), theta_inc, theta_max)
workdirs
args = Args().parser.parse_args('--fam ../data/new_trim_ped_famless17_no:xx.fam --vcf /mnt/mfs/statgen/alzheimers-family/linkage_files/geno/full_sample/vcf/full_sample.vcf.gz --anno ../MWE/annotation/EFIGA_NIALOAD_chr21.hg38.hg38_multianno.csv --pop ../data/full_sample_fam_pop.txt -f MERLIN MEGA2 PLINK LINKAGE --build hg38 --freq AF -o ../data/fullchr21data -K 0.001 --moi AD -W 0 -M 1 -j 8'.split())
args
checkParams(args)
env.tmp_cache = '../data/fullchr21data/tmp/SEQLinkage_tmp_g18gdu7g/CACHE/'
tpeds = [os.path.join(env.tmp_cache, item) for item in os.listdir(env.tmp_cache) if item.startswith(env.output) and item.endswith('.tped')]
print(tpeds) #testing line
import concurrent
import time
start = time.perf_counter()
with concurrent.futures.ThreadPoolExecutor() as executor:
results = executor.map(do_something, range(10))
for result in results:
print(result)
tped, tfam, prev, wild_pen, muta_pen, inherit_mode, theta_max, theta_inc = tpeds[0], os.path.join(env.tmp_cache, "{}.tfam".format(env.output)), \
args.prevalence, args.wild_pen, args.muta_pen, \
args.inherit_mode, args.theta_max, args.theta_inc
out_base = '{}/LINKAGE/{}'.format(env.outdir, splitext(basename(tped))[0])
out_base = out_base+'test'
mkpath(out_base)
genes,gnos = [],[]
with open(tped) as tped_fh, open(tfam) as tfam_fh:
fams = parse_tfam(tfam_fh)
#parse per family per locus AF file
af = defaultdict(lambda: [])
#try to open the file for allele frequencies, otherwise use the default value
try:
with open(os.path.join(env.tmp_cache, basename(out_base) + '.freq')) as af_fh:
for line in af_fh:
s = line.strip().split()
freq = list(map(lambda x: max(1e-3, float(x)), s[2:]))
relativefreq = np.array(freq)/sum(freq)
af[(s[0],s[1])] = list(map(str, relativefreq))
except IOError:
env.error('freq info not properly read for [{}]'.format(basename(out_base)))
#parse tped
heter_pen = wild_pen
if inherit_mode == 'AD':
heter_pen = muta_pen
for line in tped_fh:
s = line.strip().split()
gene, gno = re.search(r'^(\S+?)(?:\[(\d+)\])?$', s[1]).groups()
genes.append(gene)
gnos.append(gno)
genes
out_base = '{}/LINKAGE/{}'.format(env.outdir, splitext(basename(tped))[0])
with open(tped) as tped_fh, open(tfam) as tfam_fh:
fams = parse_tfam(tfam_fh)
#parse per family per locus AF file
af = defaultdict(lambda: [])
#try to open the file for allele frequencies, otherwise use the default value
try:
with open(os.path.join(env.tmp_cache, basename(out_base) + '.freq')) as af_fh:
for line in af_fh:
s = line.strip().split()
freq = list(map(lambda x: max(1e-3, float(x)), s[2:]))
relativefreq = np.array(freq)/sum(freq)
af[(s[0],s[1])] = list(map(str, relativefreq))
except IOError:
env.error('freq info not properly read for [{}]'.format(basename(out_base)))
#parse tped
heter_pen = wild_pen
if inherit_mode == 'AD':
heter_pen = muta_pen
for line in tped_fh:
s = line.strip().split()
gene, gno = re.search(r'^(\S+?)(?:\[(\d+)\])?$', s[1]).groups()
if not gno:
gno = '0'
with env.format_counter.get_lock():
env.format_counter.value += 1
elif gno == '1':
with env.format_counter.get_lock():
env.format_counter.value += 1
if env.format_counter.value % (env.batch * env.jobs) == 0:
env.log('{:,d} units processed {{{:.2%}}} ...'.format(env.format_counter.value, float(env.format_counter.value)/env.success_counter.value), flush=True)
for fid in fams:
workdir = '{}/{}/{}'.format(out_base, gene, fid)
with env.lock:
mkpath(workdir)
#env.error("fid {} num {}\n".format(fid, fams[fid].get_member_ids()))
fam_af = af[(fid, s[1])]
if not fam_af:
#env.log('All missing in this family {} on {}[{}], skipped ...'.format(fid, gene, gno), flush=True)
with env.skipped_counter.get_lock():
env.skipped_counter.value += 1
removeEmptyDir(workdir)
continue
ids = fams[fid].get_sorted_ids()
idxes = list(map(lambda x: fams[fid].get_member_idx(x), ids))
gs = list(map(lambda x: s[2 * x + 4 : 2 * x + 6], idxes))
gs_num = len(set(filter(lambda x: x != '0', chain(*gs))))
if gs_num >= 10:
with env.skipped_counter.get_lock():
env.skipped_counter.value += 1
removeEmptyDir(workdir)
continue
with env.lock:
mkpath(workdir)
with open('{}/{}.PRE'.format(workdir, gno), 'w') as pre:
pre.write(''.join("{} {} {} {}\n".format(fid, fams[fid].print_member(pid), s[2*fams[fid].get_member_idx(pid) + 4], s[2*fams[fid].get_member_idx(pid) + 5]) for pid in ids))
with open('{}/{}.LOC'.format(workdir, gno), 'w') as loc:
loc.write("2 0 0 5\n")
loc.write("0 0.0 0.0 0\n")
loc.write("1 2\n")
loc.write("1 2\n")
loc.write(" {} {}\n".format(1 - prev, prev))
loc.write(" 1\n")
loc.write(" {} {} {}\n".format(wild_pen, heter_pen, muta_pen))
loc.write("3 {}\n".format(gs_num))
loc.write(' ' + ' '.join(fam_af) + "\n")
loc.write("0 0\n")
loc.write("0.0\n")
loc.write("1 {} {}\n".format(theta_inc, theta_max))
removeEmptyDir('{}/{}'.format(out_base, gene))
tped_fh.close()
tfam_fh.close()
removeEmptyDir('{}'.format(out_base))