本文整理汇总了Python中utils.get_region函数的典型用法代码示例。如果您正苦于以下问题:Python get_region函数的具体用法?Python get_region怎么用?Python get_region使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_region函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: write_glfo
def write_glfo(output_dir, glfo, only_genes=None, debug=False):
if debug:
print ' writing glfo to %s%s' % (output_dir, '' if only_genes is None else (' (restricting to %d genes)' % len(only_genes)))
if os.path.exists(output_dir + '/' + glfo['chain']):
remove_glfo_files(output_dir, glfo['chain']) # also removes output_dir
os.makedirs(output_dir + '/' + glfo['chain'])
for fname in glfo_fasta_fnames(glfo['chain']):
with open(output_dir + '/' + glfo['chain'] + '/' + fname, 'w') as outfile:
for gene in glfo['seqs'][utils.get_region(fname)]:
if only_genes is not None and gene not in only_genes:
continue
outfile.write('>' + gene + '\n')
outfile.write(glfo['seqs'][utils.get_region(fname)][gene] + '\n')
for fname in glfo_csv_fnames():
with open(output_dir + '/' + glfo['chain'] + '/' + fname, 'w') as codonfile:
writer = csv.DictWriter(codonfile, ('gene', 'istart'))
writer.writeheader()
for gene, istart in glfo[utils.get_codon(fname) + '-positions'].items():
if only_genes is not None and gene not in only_genes:
continue
writer.writerow({'gene' : gene, 'istart' : istart})
# make sure there weren't any files lingering in the output dir when we started
# NOTE this will ignore the dirs corresponding to any *other* chains (which is what we want now, I think)
unexpected_files = set(glob.glob(output_dir + '/' + glfo['chain'] + '/*')) - set([output_dir + '/' + glfo['chain'] + '/' + fn for fn in glfo_fnames(glfo['chain'])])
if len(unexpected_files) > 0:
raise Exception('unexpected file(s) while writing germline set: %s' % (' '.join(unexpected_files)))
开发者ID:Annak17,项目名称:partis,代码行数:28,代码来源:glutils.py
示例2: add_some_snps
def add_some_snps(snps_to_add, glfo, remove_template_genes=False, debug=False):
"""
Generate some snp'd genes and add them to glfo, specified with <snps_to_add>.
e.g. [{'gene' : 'IGHV3-71*01', 'positions' : (35, None)}, ] will add a snp at position 35 and at a random location.
The resulting snp'd gene will have a name like IGHV3-71*01+C35T.T47G
"""
templates_to_remove = set()
for isnp in range(len(snps_to_add)):
snpinfo = snps_to_add[isnp]
gene, positions = snpinfo['gene'], snpinfo['positions']
print ' adding %d %s to %s' % (len(positions), utils.plural_str('snp', len(positions)), gene)
seq = glfo['seqs'][utils.get_region(gene)][gene]
assert utils.get_region(gene) == 'v'
cpos = glfo['cyst-positions'][gene]
snpfo = None
itry = 0
while snpfo is None or snpfo['gene'] in glfo['seqs'][utils.get_region(gene)]:
if itry > 0:
print ' already in glfo, try again'
if itry > 99:
raise Exception('too many tries while trying to generate new snps -- did you specify a lot of snps on the same position?')
snpfo = generate_snpd_gene(gene, cpos, seq, positions)
itry += 1
if remove_template_genes:
templates_to_remove.add(gene)
add_new_allele(glfo, snpfo, remove_template_genes=False, debug=debug) # *don't* remove the templates here, since we don't know if there's another snp later that needs them
remove_the_stupid_godamn_template_genes_all_at_once(glfo, templates_to_remove) # works fine with zero-length <templates_to_remove>
开发者ID:Annak17,项目名称:partis,代码行数:32,代码来源:glutils.py
示例3: write_glfo
def write_glfo(output_dir, glfo, only_genes=None, debug=False):
if debug:
print " writing glfo to %s%s" % (
output_dir,
"" if only_genes is None else (" (restricting to %d genes)" % len(only_genes)),
)
if os.path.exists(output_dir + "/" + glfo["chain"]):
remove_glfo_files(output_dir, glfo["chain"]) # also removes output_dir
os.makedirs(output_dir + "/" + glfo["chain"])
for fname in glfo_fasta_fnames(glfo["chain"]):
with open(output_dir + "/" + glfo["chain"] + "/" + fname, "w") as outfile:
for gene in glfo["seqs"][utils.get_region(fname)]:
if only_genes is not None and gene not in only_genes:
continue
outfile.write(">" + gene + "\n")
outfile.write(glfo["seqs"][utils.get_region(fname)][gene] + "\n")
with open(output_dir + "/" + glfo["chain"] + "/" + extra_fname, "w") as csvfile:
writer = csv.DictWriter(csvfile, csv_headers)
writer.writeheader()
for region, codon in utils.conserved_codons[glfo["chain"]].items():
for gene, istart in glfo[codon + "-positions"].items():
if only_genes is not None and gene not in only_genes:
continue
writer.writerow({"gene": gene, codon + "_position": istart})
# make sure there weren't any files lingering in the output dir when we started
# NOTE this will ignore the dirs corresponding to any *other* chains (which is what we want now, I think)
unexpected_files = set(glob.glob(output_dir + "/" + glfo["chain"] + "/*")) - set(
[output_dir + "/" + glfo["chain"] + "/" + fn for fn in glfo_fnames(glfo["chain"])]
)
if len(unexpected_files) > 0:
raise Exception("unexpected file(s) while writing germline set: %s" % (" ".join(unexpected_files)))
开发者ID:psathyrella,项目名称:partis,代码行数:34,代码来源:glutils.py
示例4: read_fasta_file
def read_fasta_file(seqs, fname, skip_pseudogenes, aligned=False):
n_skipped_pseudogenes = 0
for seq_record in SeqIO.parse(fname, 'fasta'):
linefo = [p.strip() for p in seq_record.description.split('|')]
# first get gene name
if linefo[0][:2] != 'IG': # if it's an imgt file, with a bunch of header info (and the accession number first)
gene = linefo[imgt_info_indices.index('gene')]
functionality = linefo[imgt_info_indices.index('functionality')]
if functionality not in functionalities:
raise Exception('unexpected functionality %s in %s' % (functionality, fname))
if skip_pseudogenes and functionality == 'P':
n_skipped_pseudogenes += 1
continue
else: # plain fasta with just the gene name after the '>'
gene = linefo[0]
utils.split_gene(gene) # just to check if it's a valid gene name
if not aligned and utils.get_region(gene) != utils.get_region(os.path.basename(fname)): # if <aligned> is True, file name is expected to be whatever
raise Exception('gene %s from %s has unexpected region %s' % (gene, os.path.basename(fname), utils.get_region(gene)))
# then the sequence
seq = str(seq_record.seq).upper()
if not aligned:
seq = utils.remove_gaps(seq)
if len(seq.strip(''.join(utils.expected_characters))) > 0: # return the empty string if it only contains expected characters
raise Exception('unexpected character %s in %s (expected %s)' % (seq.strip(''.join(utils.expected_characters)), seq, ' '.join(utils.expected_characters)))
seqs[utils.get_region(gene)][gene] = seq
if n_skipped_pseudogenes > 0:
print ' skipped %d pseudogenes' % n_skipped_pseudogenes
开发者ID:Annak17,项目名称:partis,代码行数:31,代码来源:glutils.py
示例5: convert_to_duplicate_name
def convert_to_duplicate_name(glfo, gene):
for equivalence_class in duplicate_names[utils.get_region(gene)]:
if gene in equivalence_class:
for alternate_name in equivalence_class:
if alternate_name != gene and alternate_name in glfo["seqs"][utils.get_region(gene)]:
# print 'converting %s --> %s' % (gene, alternate_name)
return alternate_name
raise Exception("couldn't find alternate name for %s" % gene)
开发者ID:psathyrella,项目名称:partis,代码行数:8,代码来源:glutils.py
示例6: prepare_bppseqgen
def prepare_bppseqgen(self, seq, chosen_tree, n_leaf_nodes, gene, reco_event, seed):
""" write input files and get command line options necessary to run bppseqgen on <seq> (which is a part of the full query sequence) """
if len(seq) == 0:
return None
# write the tree to a tmp file
workdir = self.workdir + '/' + utils.get_region(gene)
os.makedirs(workdir)
treefname = workdir + '/tree.tre'
reco_seq_fname = workdir + '/start-seq.txt'
leaf_seq_fname = workdir + '/leaf-seqs.fa'
if n_leaf_nodes == 1: # add an extra leaf to one-leaf trees so bppseqgen doesn't barf (when we read the output, we ignore the second leaf)
lreg = re.compile('t1:[0-9]\.[0-9][0-9]*')
leafstr = lreg.findall(chosen_tree)
assert len(leafstr) == 1
leafstr = leafstr[0]
chosen_tree = chosen_tree.replace(leafstr, '(' + leafstr + ',' + leafstr + '):0.0')
with opener('w')(treefname) as treefile:
treefile.write(chosen_tree)
self.write_mute_freqs(gene, seq, reco_event, reco_seq_fname)
env = os.environ.copy()
env["LD_LIBRARY_PATH"] += ':' + self.args.partis_dir + '/packages/bpp/lib'
# build up the command line
# docs: http://biopp.univ-montp2.fr/apidoc/bpp-phyl/html/classbpp_1_1GTR.html that page is too darn hard to google
bpp_binary = self.args.partis_dir + '/packages/bpp/bin/bppseqgen'
if not os.path.exists(bpp_binary):
raise Exception('bpp not found in %s' % os.path.dirname(bpp_binary))
command = bpp_binary # NOTE should I use the "equilibrium frequencies" option?
command += ' alphabet=DNA'
command += ' --seed=' + str(seed)
command += ' input.infos=' + reco_seq_fname # input file (specifies initial "state" for each position, and possibly also the mutation rate at that position)
command += ' input.infos.states=state' # column name in input file BEWARE bio++ undocumented defaults (i.e. look in the source code)
command += ' input.tree.file=' + treefname
command += ' input.tree.format=Newick'
command += ' output.sequence.file=' + leaf_seq_fname
command += ' output.sequence.format=Fasta'
if self.args.mutate_from_scratch:
command += ' model=JC69'
command += ' input.infos.rates=none' # BEWARE bio++ undocumented defaults (i.e. look in the source code)
if self.args.flat_mute_freq is not None:
command += ' rate_distribution=Constant'
else:
command += ' rate_distribution=Gamma(n=4,alpha=' + self.mute_models[utils.get_region(gene)]['gamma']['alpha']+ ')'
else:
command += ' input.infos.rates=rate' # column name in input file
pvpairs = [p + '=' + v for p, v in self.mute_models[utils.get_region(gene)]['gtr'].items()]
command += ' model=GTR(' + ','.join(pvpairs) + ')'
return {'cmd_str' : command, 'outfname' : leaf_seq_fname, 'workdir' : workdir, 'other-files' : [reco_seq_fname, treefname], 'env' : env}
开发者ID:psathyrella,项目名称:partis,代码行数:52,代码来源:recombinator.py
示例7: plot
def plot(self, base_plotdir, cyst_positions=None, tryp_positions=None):
if not self.finalized:
self.finalize()
plotdir = base_plotdir + '/mute-freqs'
utils.prep_dir(plotdir + '/plots', multilings=('*.csv', '*.svg'))
for region in utils.regions:
utils.prep_dir(plotdir + '/' + region + '/plots', multilings=('*.csv', '*.svg'))
utils.prep_dir(plotdir + '/' + region + '-per-base/plots', multilings=('*.csv', '*.png'))
for gene in self.counts:
counts, plotting_info = self.counts[gene], self.plotting_info[gene]
sorted_positions = sorted(counts)
hist = TH1D('hist_' + utils.sanitize_name(gene), '',
sorted_positions[-1] - sorted_positions[0] + 1,
sorted_positions[0] - 0.5, sorted_positions[-1] + 0.5)
for position in sorted_positions:
hist.SetBinContent(hist.FindBin(position), counts[position]['freq'])
hi_diff = abs(counts[position]['freq'] - counts[position]['freq_hi_err'])
lo_diff = abs(counts[position]['freq'] - counts[position]['freq_lo_err'])
err = 0.5*(hi_diff + lo_diff)
hist.SetBinError(hist.FindBin(position), err)
plotfname = plotdir + '/' + utils.get_region(gene) + '/plots/' + utils.sanitize_name(gene) + '.svg'
xline = None
if utils.get_region(gene) == 'v' and cyst_positions is not None:
xline = cyst_positions[gene]['cysteine-position']
elif utils.get_region(gene) == 'j' and tryp_positions is not None:
xline = int(tryp_positions[gene])
plotting.draw(hist, 'int', plotdir=plotdir + '/' + utils.get_region(gene), plotname=utils.sanitize_name(gene), errors=True, write_csv=True, xline=xline, draw_str='e') #, cwidth=4000, cheight=1000)
paramutils.make_mutefreq_plot(plotdir + '/' + utils.get_region(gene) + '-per-base', utils.sanitize_name(gene), plotting_info)
# for region in utils.regions:
# utils.prep_dir(plotdir + '/' + region + '/tmp/plots', multilings=('*.csv', '*.svg'))
# for gene in self.tmpcounts:
# for position in self.tmpcounts[gene]:
# roothist = plotting.make_hist_from_my_hist_class(self.tmpcounts[gene][position]['muted'], gene + '_' + str(position))
# plotting.draw(roothist, 'int', plotdir=plotdir + '/' + utils.get_region(gene) + '/tmp', plotname=utils.sanitize_name(gene) + '_' + str(position), errors=True, write_csv=True) #, cwidth=4000, cheight=1000)
# make mean mute freq hists
hist = plotting.make_hist_from_my_hist_class(self.mean_rates['all'], 'all-mean-freq')
plotting.draw(hist, 'float', plotname='all-mean-freq', plotdir=plotdir, stats='mean', bounds=(0.0, 0.4), write_csv=True)
for region in utils.regions:
hist = plotting.make_hist_from_my_hist_class(self.mean_rates[region], region+'-mean-freq')
plotting.draw(hist, 'float', plotname=region+'-mean-freq', plotdir=plotdir, stats='mean', bounds=(0.0, 0.4), write_csv=True)
check_call(['./bin/makeHtml', plotdir, '3', 'null', 'svg'])
# then write html file and fix permissiions
for region in utils.regions:
check_call(['./bin/makeHtml', plotdir + '/' + region, '1', 'null', 'svg'])
check_call(['./bin/makeHtml', plotdir + '/' + region + '-per-base', '1', 'null', 'png'])
check_call(['./bin/permissify-www', plotdir]) # NOTE this should really permissify starting a few directories higher up
开发者ID:matsengrp,项目名称:bioboxmixcr,代码行数:51,代码来源:mutefreqer.py
示例8: plot
def plot(self, plotdir, only_csv=False, only_overall=False):
if not self.finalized:
self.finalize()
overall_plotdir = plotdir + '/overall'
for gene in self.freqs:
if only_overall:
continue
freqs = self.freqs[gene]
if len(freqs) == 0:
if gene not in glutils.dummy_d_genes.values():
print ' %s no mutefreqer obs for %s' % (utils.color('red', 'warning'), utils.color_gene(gene))
continue
sorted_positions = sorted(freqs.keys())
genehist = Hist(sorted_positions[-1] - sorted_positions[0] + 1, sorted_positions[0] - 0.5, sorted_positions[-1] + 0.5, xtitle='position', ytitle='mut freq', title=gene)
for position in sorted_positions:
hi_diff = abs(freqs[position]['freq'] - freqs[position]['freq_hi_err'])
lo_diff = abs(freqs[position]['freq'] - freqs[position]['freq_lo_err'])
err = 0.5*(hi_diff + lo_diff)
genehist.set_ibin(genehist.find_bin(position), freqs[position]['freq'], error=err)
xline = None
figsize = [7, 4]
if utils.get_region(gene) in utils.conserved_codons[self.glfo['chain']]:
codon = utils.conserved_codons[self.glfo['chain']][utils.get_region(gene)]
xline = self.glfo[codon + '-positions'][gene]
if utils.get_region(gene) == 'v':
figsize[0] *= 3.5
elif utils.get_region(gene) == 'j':
figsize[0] *= 2
plotting.draw_no_root(self.per_gene_mean_rates[gene], plotdir=plotdir + '/per-gene/' + utils.get_region(gene), plotname=utils.sanitize_name(gene), errors=True, write_csv=True, only_csv=only_csv, shift_overflows=True)
# per-position plots:
plotting.draw_no_root(genehist, plotdir=plotdir + '/per-gene-per-position/' + utils.get_region(gene), plotname=utils.sanitize_name(gene), errors=True, write_csv=True, xline=xline, figsize=figsize, only_csv=only_csv, shift_overflows=True)
# # per-position, per-base plots:
# paramutils.make_mutefreq_plot(plotdir + '/' + utils.get_region(gene) + '-per-base', utils.sanitize_name(gene), plotting_info) # needs translation to mpl UPDATE fcn is fixed, but I can't be bothered uncommenting this at the moment
# make mean mute freq hists
for rstr in ['all', 'cdr3'] + utils.regions:
if rstr == 'all':
bounds = (0.0, 0.4)
else:
bounds = (0.0, 0.6 if rstr == 'd' else 0.4)
plotting.draw_no_root(self.mean_rates[rstr], plotname=rstr+'_mean-freq', plotdir=overall_plotdir, stats='mean', bounds=bounds, write_csv=True, only_csv=only_csv, shift_overflows=True)
plotting.draw_no_root(self.mean_n_muted[rstr], plotname=rstr+'_mean-n-muted', plotdir=overall_plotdir, stats='mean', write_csv=True, only_csv=only_csv, shift_overflows=True)
if not only_csv: # write html file and fix permissiions
for substr in self.subplotdirs:
plotting.make_html(plotdir + '/' + substr)
开发者ID:psathyrella,项目名称:partis,代码行数:48,代码来源:mutefreqer.py
示例9: generate_snpd_gene
def generate_snpd_gene(gene, cpos, seq, positions):
assert utils.get_region(gene) == "v" # others not yet handled
def choose_position():
snp_pos = None
while snp_pos is None or snp_pos in snpd_positions or not utils.codon_ok("cyst", tmpseq, cpos, debug=True):
snp_pos = random.randint(10, len(seq) - 15) # note that randint() is inclusive
tmpseq = seq[:snp_pos] + "X" + seq[snp_pos + 1 :] # for checking cyst position
return snp_pos
snpd_positions = set() # only used if a position wasn't specified (i.e. was None) in <snps_to_add>
mutfo = OrderedDict()
for snp_pos in positions:
if snp_pos is None:
snp_pos = choose_position()
snpd_positions.add(snp_pos)
new_base = None
while new_base is None or new_base == seq[snp_pos]:
new_base = utils.nukes[random.randint(0, len(utils.nukes) - 1)]
print " %3d %s --> %s" % (snp_pos, seq[snp_pos], new_base)
mutfo[snp_pos] = {"original": seq[snp_pos], "new": new_base}
seq = seq[:snp_pos] + new_base + seq[snp_pos + 1 :]
assert utils.codon_ok("cyst", seq, cpos, debug=True) # this is probably unnecessary
snpd_name, mutfo = get_new_allele_name_and_change_mutfo(gene, mutfo)
return {"template-gene": gene, "gene": snpd_name, "seq": seq}
开发者ID:psathyrella,项目名称:partis,代码行数:27,代码来源:glutils.py
示例10: make_transition_plot
def make_transition_plot(self, gene_name, model):
""" NOTE shares a lot with make_mutefreq_plot() in python/paramutils.py """
fig, ax = plotting.mpl_init()
fig.set_size_inches(plotting.plot_ratios[utils.get_region(gene_name)])
ibin = 0
print utils.color_gene(utils.unsanitize_name(gene_name))
legend_colors = set() # add a color to this the first time you plot it
for state in model.states:
# bin label
ax.text(-0.5 + ibin, -0.075, paramutils.simplify_state_name(state.name), rotation='vertical', size=8)
sorted_to_states = {}
for name in state.transitions.keys():
if name.find('IG') == 0:
sorted_to_states[name] = int(paramutils.simplify_state_name(name))
else:
sorted_to_states[name] = name
sorted_to_states = sorted(sorted_to_states.items(), key=operator.itemgetter(1))
total = 0.0
for to_state, simple_to_state in sorted_to_states:
prob = state.transitions[to_state]
alpha = 0.6
width = 3
if 'insert' in str(simple_to_state):
label = 'insert'
color = '#3498db' # blue
elif str(simple_to_state) == 'end':
label = 'end'
color = 'red'
else: # regional/internal states
assert to_state.find('IG') == 0
label = 'internal'
color = 'green'
label_to_use = None
if color not in legend_colors:
label_to_use = label
legend_colors.add(color)
# horizontal line at height total+prob
ax.plot([-0.5 + ibin, 0.5 + ibin], [total + prob, total + prob], color=color, linewidth=width, alpha=alpha, label=label_to_use)
# vertical line from total to total + prob
ax.plot([ibin, ibin], [total + 0.01, total + prob], color=color, alpha=alpha, linewidth=width)
midpoint = 0.5*(prob + 2*total)
# ax.text(ibin, midpoint, paramutils.simplify_state_name(to_state)) # nicely labels the midpoint of the chunk between lines, but there isn't really room for it
total += prob
ibin += 1
ax.get_xaxis().set_visible(False)
plotting.mpl_finish(ax, self.base_plotdir + '/transitions', gene_name, ybounds=(-0.01, 1.01), xbounds=(-3, len(model.states) + 3), leg_loc=(0.95, 0.1), adjust={'left' : 0.1, 'right' : 0.8}, leg_prop={'size' : 8})
开发者ID:psathyrella,项目名称:partis,代码行数:60,代码来源:plot-hmms.py
示例11: add_new_allele
def add_new_allele(glfo, newfo, remove_template_genes, debug=False):
"""
Add a new allele to <glfo>, specified by <newfo> which is of the
form: {'template-gene' : 'IGHV3-71*01', 'gene' : 'IGHV3-71*01+C35T.T47G', 'seq' : 'ACTG yadda yadda CGGGT'}
If <remove_template_genes>, we also remove 'template-gene' from <glfo>.
"""
template_gene = newfo['template-gene']
region = utils.get_region(template_gene)
if template_gene not in glfo['seqs'][region]:
raise Exception('unknown template gene %s' % template_gene)
new_gene = newfo['gene']
if region == 'v':
glfo['cyst-positions'][new_gene] = glfo['cyst-positions'][template_gene]
elif region == 'j':
glfo['tryp-positions'][new_gene] = glfo['tryp-positions'][template_gene]
glfo['seqs'][region][new_gene] = newfo['seq']
if debug:
print ' adding new allele to glfo:'
print ' template %s %s' % (glfo['seqs'][region][template_gene], utils.color_gene(template_gene))
print ' new %s %s' % (utils.color_mutants(glfo['seqs'][region][template_gene], newfo['seq']), utils.color_gene(new_gene))
if remove_template_genes:
remove_gene(glfo, template_gene, debug=True)
开发者ID:Annak17,项目名称:partis,代码行数:28,代码来源:glutils.py
示例12: add_new_allele
def add_new_allele(self, gene, fitfo, n_candidate_snps, debug=False):
# figure out what the new nukes are
old_seq = self.glfo['seqs'][utils.get_region(gene)][gene]
new_seq = old_seq
mutfo = {}
for pos in sorted(fitfo['candidates'][n_candidate_snps]):
obs_counts = {nuke : self.counts[gene][pos][n_candidate_snps][nuke] for nuke in utils.nukes} # NOTE it's super important to only use the counts from sequences with <n_candidate_snps> total mutations
sorted_obs_counts = sorted(obs_counts.items(), key=operator.itemgetter(1), reverse=True)
original_nuke = self.mfreqer.counts[gene][pos]['gl_nuke']
new_nuke = None
for nuke, _ in sorted_obs_counts: # take the most common one that isn't the existing gl nuke
if nuke != original_nuke:
new_nuke = nuke
break
print ' %3d (%s --> %s)' % (pos, original_nuke, new_nuke),
assert old_seq[pos] == original_nuke
mutfo[pos] = {'original' : original_nuke, 'new' : new_nuke}
new_seq = new_seq[:pos] + new_nuke + new_seq[pos+1:]
new_name, mutfo = glutils.get_new_allele_name_and_change_mutfo(gene, mutfo)
print ''
print ' %s %s' % (old_seq, utils.color_gene(gene))
print ' %s %s' % (utils.color_mutants(old_seq, new_seq), utils.color_gene(new_name))
# and add it to the set of new alleles for this gene
self.new_allele_info.append({
'template-gene' : gene,
'gene' : new_name,
'seq' : new_seq,
'aligned-seq' : None
})
开发者ID:Annak17,项目名称:partis,代码行数:31,代码来源:allelefinder.py
示例13: generate_snpd_gene
def generate_snpd_gene(gene, cpos, seq, positions):
assert utils.get_region(gene) == 'v' # others not yet handled
def choose_position():
snp_pos = None
while snp_pos is None or snp_pos in snpd_positions or not utils.check_conserved_cysteine(tmpseq, cpos, debug=True, assert_on_fail=False):
snp_pos = random.randint(10, len(seq) - 15) # note that randint() is inclusive
tmpseq = seq[: snp_pos] + 'X' + seq[snp_pos + 1 :] # for checking cyst position
return snp_pos
snpd_positions = set() # only used if a position wasn't specified (i.e. was None) in <snps_to_add>
mutfo = OrderedDict()
for snp_pos in positions:
if snp_pos is None:
snp_pos = choose_position()
snpd_positions.add(snp_pos)
new_base = None
while new_base is None or new_base == seq[snp_pos]:
new_base = utils.nukes[random.randint(0, len(utils.nukes) - 1)]
print ' %3d %s --> %s' % (snp_pos, seq[snp_pos], new_base)
mutfo[snp_pos] = {'original' : seq[snp_pos], 'new' : new_base}
seq = seq[: snp_pos] + new_base + seq[snp_pos + 1 :]
utils.check_conserved_cysteine(seq, cpos)
snpd_name, mutfo = get_new_allele_name_and_change_mutfo(gene, mutfo)
return {'template-gene' : gene, 'gene' : snpd_name, 'seq' : seq}
开发者ID:Annak17,项目名称:partis,代码行数:26,代码来源:glutils.py
示例14: plot
def plot(self, base_plotdir, only_csv=False):
if not self.finalized:
self.finalize(debug=debug)
plotdir = base_plotdir + '/allele-finding'
for old_gene_dir in glob.glob(plotdir + '/*'): # has to be a bit more hackey than elsewhere, since we have no way of knowing what genes might have had their own directories written last time we wrote to this dir
if not os.path.isdir(old_gene_dir):
raise Exception('not a directory: %s' % old_gene_dir)
utils.prep_dir(old_gene_dir, wildlings=('*.csv', '*.svg'))
os.rmdir(old_gene_dir)
utils.prep_dir(plotdir, wildlings=('*.csv', '*.svg'))
if only_csv: # not implemented
return
start = time.time()
for gene in self.plotvals:
if utils.get_region(gene) != 'v':
continue
for position in self.plotvals[gene]:
if position not in self.fitted_positions[gene]: # we can make plots for the positions we didn't fit, but there's a *lot* of them and they're slow
continue
# if 'allele-finding' not in self.TMPxyvals[gene][position] or self.TMPxyvals[gene][position]['allele-finding'] is None:
# continue
plotting.make_allele_finding_plot(plotdir + '/' + utils.sanitize_name(gene), gene, position, self.plotvals[gene][position])
print ' allele finding plot time: %.1f' % (time.time()-start)
开发者ID:Annak17,项目名称:partis,代码行数:28,代码来源:allelefinder.py
示例15: add_new_allele
def add_new_allele(glfo, newfo, remove_template_genes=False, debug=False):
"""
Add a new allele to <glfo>, specified by <newfo> which is of the
form: {'gene' : 'IGHV3-71*01+C35T.T47G', 'seq' : 'ACTG yadda yadda CGGGT', 'template-gene' : 'IGHV3-71*01'}
If <remove_template_genes>, we also remove 'template-gene' from <glfo>.
"""
template_gene = newfo["template-gene"]
region = utils.get_region(template_gene)
if template_gene not in glfo["seqs"][region]:
raise Exception("unknown template gene %s" % template_gene)
new_gene = newfo["gene"]
if region == "v":
glfo["cyst-positions"][new_gene] = glfo["cyst-positions"][template_gene]
elif region == "j":
glfo["tryp-positions"][new_gene] = glfo["tryp-positions"][template_gene]
glfo["seqs"][region][new_gene] = newfo["seq"]
if debug:
print " adding new allele to glfo:"
print " template %s %s" % (glfo["seqs"][region][template_gene], utils.color_gene(template_gene))
print " new %s %s" % (
utils.color_mutants(glfo["seqs"][region][template_gene], newfo["seq"]),
utils.color_gene(new_gene),
)
if remove_template_genes:
remove_gene(glfo, template_gene, debug=True)
开发者ID:psathyrella,项目名称:partis,代码行数:31,代码来源:glutils.py
示例16: __init__
def __init__(self, base_indir, outdir, gene_name, naivety, glfo, args):
self.region = utils.get_region(gene_name)
self.raw_name = gene_name # i.e. unsanitized
self.germline_seqs = glfo['seqs'] # all germline alleles
self.germline_seq = self.germline_seqs[self.region][gene_name] # germline sequence for this hmm
self.indir = base_indir
self.args = args
self.cyst_positions = glfo['cyst-positions']
self.tryp_positions = glfo['tryp-positions']
# parameters with values that I more or less made up
self.precision = '16' # number of digits after the decimal for probabilities
self.eps = 1e-6 # NOTE I also have an eps defined in utils, and they should in principle be combined
self.n_max_to_interpolate = 20
self.min_mean_unphysical_insertion_length = {'fv' : 1.5, 'jf' : 25} # jf has to be quite a bit bigger, since besides account for the variation in J length from the tryp position to the end, it has to account for the difference in cdr3 lengths
self.erosion_pseudocount_length = 10 # if we're closer to the end of the gene than this, make sure erosion probability isn't zero
# self.insert_mute_prob = 0.0
# self.mean_mute_freq = 0.0
self.outdir = outdir
self.naivety = naivety
self.smallest_entry_index = -1 # keeps track of the first state that has a chance of being entered from init -- we want to start writing (with add_internal_state) from there
# self.insertions = [ insert for insert in utils.index_keys if re.match(self.region + '._insertion', insert) or re.match('.' + self.region + '_insertion', insert)] OOPS that's not what I want to do
self.insertions = []
if self.region == 'v':
self.insertions.append('fv')
elif self.region == 'd':
self.insertions.append('vd')
elif self.region == 'j':
self.insertions.append('dj')
self.insertions.append('jf')
self.erosion_probs = {}
self.insertion_probs = {}
self.insertion_content_probs = {}
self.n_occurences = utils.read_overall_gene_probs(self.indir, only_gene=gene_name, normalize=False) # how many times did we observe this gene in data?
replacement_genes = None
if self.n_occurences < self.args.min_observations_to_write: # if we didn't see it enough, average over all the genes that find_replacement_genes() gives us
if self.args.debug:
print ' only saw it %d times, use info from other genes' % self.n_occurences
replacement_genes = utils.find_replacement_genes(self.indir, self.args.min_observations_to_write, gene_name, single_gene=False, debug=self.args.debug)
self.read_erosion_info(gene_name, replacement_genes) # try this exact gene, but...
self.read_insertion_info(gene_name, replacement_genes)
if self.naivety == 'M': # mutate if not naive
self.mute_freqs, self.mute_obs = paramutils.read_mute_info(self.indir, this_gene=gene_name, approved_genes=replacement_genes)
self.track = Track('nukes', utils.nukes)
self.saniname = utils.sanitize_name(gene_name)
self.hmm = HMM(self.saniname, self.track.getdict()) # pass the track as a dict rather than a Track object to keep the yaml file a bit more readable
self.hmm.extras['gene_prob'] = max(self.eps, utils.read_overall_gene_probs(self.indir, only_gene=gene_name)) # if we really didn't see this gene at all, take pity on it and kick it an eps
mean_freq_hist = Hist(fname=self.indir + '/all-mean-mute-freqs.csv')
self.hmm.extras['overall_mute_freq'] = mean_freq_hist.get_mean()
开发者ID:antibodyome,项目名称:partis,代码行数:59,代码来源:hmmwriter.py
示例17: finalize
def finalize(self, debug=False):
assert not self.finalized
self.mfreqer.finalize()
start = time.time()
gene_results = {'not_enough_obs_to_fit' : set(), 'didnt_find_anything_with_fit' : set(), 'new_allele' : set()}
if debug:
print '\nlooking for new alleles:'
for gene in sorted(self.mfreqer.counts):
if utils.get_region(gene) != 'v':
continue
if debug:
print '\n%s (observed %d %s)' % (utils.color_gene(gene), self.gene_obs_counts[gene], utils.plural_str('time', self.gene_obs_counts[gene]))
positions_to_try_to_fit, xyvals = self.get_positions_to_fit(gene, gene_results, debug=debug)
if positions_to_try_to_fit is None:
continue
fitfo = {n : {} for n in ('min_snp_ratios', 'candidates')}
for istart in range(1, self.n_max_snps):
if debug:
if istart == 1:
print ' resid. / ndof'
print ' position ratio (m=0 / m>%5.2f) muted / obs ' % self.big_y_icpt_bounds[0]
print ' %d %s' % (istart, utils.plural_str('snp', istart))
subxyvals = {pos : {k : v[istart : istart + self.max_fit_length] for k, v in xyvals[pos].items()} for pos in positions_to_try_to_fit}
self.fit_istart(gene, istart, positions_to_try_to_fit, subxyvals, fitfo, debug=debug)
if istart not in fitfo['candidates']: # if it didn't get filled, we didn't have enough observations to do the fit
break
istart_candidates = []
if debug:
print ' evaluating each snp hypothesis'
print ' snps min ratio'
for istart in fitfo['candidates']:
if debug:
print ' %2d %9s' % (istart, fstr(fitfo['min_snp_ratios'][istart])),
if self.is_a_candidate(gene, fitfo, istart, debug=debug):
istart_candidates.append(istart)
if len(istart_candidates) > 0:
n_candidate_snps = min(istart_candidates) # add the candidate with the smallest number of snps to the germline set, and run again (if the firs
gene_results['new_allele'].add(gene)
print '\n found a new allele candidate separated from %s by %d %s at %s:' % (utils.color_gene(gene), n_candidate_snps,
utils.plural_str('snp', n_candidate_snps), utils.plural_str('position', n_candidate_snps)),
self.add_new_allele(gene, fitfo, n_candidate_snps, debug=debug)
else:
gene_results['didnt_find_anything_with_fit'].add(gene)
if debug:
print ' no new alleles'
if debug:
print 'found new alleles for %d %s (there were also %d without new alleles, and %d without enough observations to fit)' % (len(gene_results['new_allele']), utils.plural_str('gene', len(gene_results['new_allele'])),
len(gene_results['didnt_find_anything_with_fit']), len(gene_results['not_enough_obs_to_fit']))
print ' allele finding time: %.1f' % (time.time()-start)
self.finalized = True
开发者ID:Annak17,项目名称:partis,代码行数:59,代码来源:allelefinder.py
示例18: remove_gene
def remove_gene(glfo, gene, debug=False):
""" remove <gene> from <glfo> """
if debug:
print ' removing %s from glfo' % utils.color_gene(gene)
region = utils.get_region(gene)
if region in utils.conserved_codons:
del glfo[utils.conserved_codons[region] + '-positions'][gene]
del glfo['seqs'][region][gene]
开发者ID:Annak17,项目名称:partis,代码行数:8,代码来源:glutils.py
示例19: plot
def plot(self, base_plotdir, cyst_positions=None, tryp_positions=None, only_csv=False):
if not self.finalized:
self.finalize()
plotdir = base_plotdir + '/mute-freqs'
overall_plotdir = plotdir + '/overall'
utils.prep_dir(overall_plotdir, multilings=('*.csv', '*.svg'))
for region in utils.regions:
utils.prep_dir(plotdir + '/' + region, multilings=('*.csv', '*.svg'))
# utils.prep_dir(plotdir + '/' + region + '-per-base/plots', multilings=('*.csv', '*.png'))
if self.tigger:
utils.prep_dir(plotdir + '/tigger', multilings=('*.csv', '*.svg'))
for gene in self.freqs:
freqs = self.freqs[gene]
sorted_positions = sorted(freqs.keys())
genehist = Hist(sorted_positions[-1] - sorted_positions[0] + 1, sorted_positions[0] - 0.5, sorted_posi
|
请发表评论