本文整理汇总了Python中pyomo.environ.ConcreteModel.t_allele方法的典型用法代码示例。如果您正苦于以下问题:Python ConcreteModel.t_allele方法的具体用法?Python ConcreteModel.t_allele怎么用?Python ConcreteModel.t_allele使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyomo.environ.ConcreteModel
的用法示例。
在下文中一共展示了ConcreteModel.t_allele方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import t_allele [as 别名]
#.........这里部分代码省略.........
res_df = results.xs(results.index.values[0][1], level="Method")
res_df = res_df[res_df.apply(lambda x: any(x[a] > self.__thresh.get(a.name, -float("inf"))
for a in res_df.columns), axis=1)]
for tup in res_df.itertuples():
p = tup[0]
seq = str(p)
peps[seq] = p
for a, s in itr.izip(res_df.columns, tup[1:]):
if method in ["smm", "smmpmbec", "arb", "comblibsidney"]:
try:
thr = min(1., max(0.0, 1.0 - math.log(self.__thresh.get(a.name),
50000))) if a.name in self.__thresh else -float("inf")
except:
thr = 0
if s >= thr:
alleles_I.setdefault(a.name, set()).add(seq)
imm[seq, a.name] = min(1., max(0.0, 1.0 - math.log(s, 50000)))
else:
if s > self.__thresh.get(a.name, -float("inf")):
alleles_I.setdefault(a.name, set()).add(seq)
imm[seq, a.name] = s
prots = set(pr for pr in p.get_all_proteins())
cons[seq] = len(prots)
for prot in prots:
variations.append(prot.gene_id)
epi_var.setdefault(prot.gene_id, set()).add(seq)
self.__peptideSet = peps
#calculate conservation
variations = set(variations)
total = len(variations)
for e, v in cons.iteritems():
try:
cons[e] = v / total
except ZeroDivisionError:
cons[e] = 1
model = ConcreteModel()
#set definition
model.Q = Set(initialize=variations)
model.E = Set(initialize=set(peps.keys()))
model.A = Set(initialize=alleles_I.keys())
model.E_var = Set(model.Q, initialize=lambda mode, v: epi_var[v])
model.A_I = Set(model.A, initialize=lambda model, a: alleles_I[a])
#parameter definition
model.k = Param(initialize=self.__k, within=PositiveIntegers, mutable=True)
model.p = Param(model.A, initialize=lambda model, a: probs[a])
model.c = Param(model.E, initialize=lambda model, e: cons[e],mutable=True)
#threshold parameters
model.i = Param(model.E, model.A, initialize=lambda model, e, a: imm[e, a])
model.t_allele = Param(initialize=0, within=NonNegativeIntegers, mutable=True)
model.t_var = Param(initialize=0, within=NonNegativeIntegers, mutable=True)
model.t_c = Param(initialize=0.0, within=NonNegativeReals, mutable=True)
# Variable Definition
model.x = Var(model.E, within=Binary)
model.y = Var(model.A, within=Binary)
model.z = Var(model.Q, within=Binary)
# Objective definition
model.Obj = Objective(
rule=lambda model: sum(model.x[e] * sum(model.p[a] * model.i[e, a] for a in model.A) for e in model.E),
sense=maximize)
#Obligatory Constraint (number of selected epitopes)
model.NofSelectedEpitopesCov = Constraint(rule=lambda model: sum(model.x[e] for e in model.E) <= model.k)
#optional constraints (in basic model they are disabled)
model.IsAlleleCovConst = Constraint(model.A,
rule=lambda model, a: sum(model.x[e] for e in model.A_I[a]) >= model.y[a])
model.MinAlleleCovConst = Constraint(rule=lambda model: sum(model.y[a] for a in model.A) >= model.t_allele)
model.IsAntigenCovConst = Constraint(model.Q,
rule=lambda model, q: sum(model.x[e] for e in model.E_var[q]) >= model.z[q])
model.MinAntigenCovConst = Constraint(rule=lambda model: sum(model.z[q] for q in model.Q) >= model.t_var)
model.EpitopeConsConst = Constraint(model.E,
rule=lambda model, e: (1 - model.c[e]) * model.x[e] <= 1 - model.t_c)
#generate instance
self.instance = model
if self.__verbosity > 0:
print "MODEL INSTANCE"
self.instance.pprint()
#constraints
self.instance.IsAlleleCovConst.deactivate()
self.instance.MinAlleleCovConst.deactivate()
self.instance.IsAntigenCovConst.deactivate()
self.instance.MinAntigenCovConst.deactivate()
self.instance.EpitopeConsConst.deactivate()
示例2: __init__
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import t_allele [as 别名]
def __init__(self, cov, occ, groups_4digit, allele_table, beta, t_max_allele=2, solver="glpk", threads=1,
verbosity=0):
"""
Constructor
"""
self.__allele_table = allele_table
self.__beta = float(beta)
self.__t_max_allele = t_max_allele
self.__solver = SolverFactory(solver)
self.__threads = threads
self.__opts = {"threads": threads} if threads > 1 else {}
self.__verbosity = verbosity
self.__changed = True # model needs to know if it changed from last run or not
self.__ks = 1
self.__groups_4digit = groups_4digit
loci_alleles = defaultdict(list)
for type_4digit, group_alleles in groups_4digit.iteritems():
# print type_4digit, group_alleles
loci_alleles[type_4digit.split('*')[0]].extend(group_alleles)
loci = loci_alleles
self.__allele_to_4digit = {allele: type_4digit for type_4digit, group in groups_4digit.iteritems() for allele in
group}
'''
generates the basic ILP model
'''
model = ConcreteModel()
# init Sets
model.LociNames = Set(initialize=loci.keys())
model.Loci = Set(model.LociNames, initialize=lambda m, l: loci[l])
L = list(itertools.chain(*loci.values()))
reconst = {allele_id: 0.01 for allele_id in L if '_' in allele_id}
R = set([r for (r, _) in cov.keys()])
model.L = Set(initialize=L)
model.R = Set(initialize=R)
# init Params
model.cov = Param(model.R, model.L, initialize=lambda model, r, a: cov.get((r, a), 0))
model.reconst = Param(model.L, initialize=lambda model, a: reconst.get(a, 0))
model.occ = Param(model.R, initialize=occ)
model.t_allele = Param(initialize=self.__t_max_allele, mutable=True)
model.beta = Param(initialize=self.__beta,
validate=lambda val, model: 0.0 <= float(self.__beta) <= 0.999,
mutable=True)
model.nof_loci = Param(initialize=len(loci))
# init variables
model.x = Var(model.L, domain=Binary)
model.y = Var(model.R, domain=Binary)
model.re = Var(model.R, bounds=(0.0, None))
model.hetero = Var(bounds=(0.0, model.nof_loci))
# init objective
model.read_cov = Objective(
rule=lambda model: sum(model.occ[r] * (model.y[r] - model.beta * (model.re[r])) for r in model.R) - sum(
model.reconst[a] * model.x[a] for a in model.L), sense=maximize)
# init Constraints
model.max_allel_selection = Constraint(model.LociNames, rule=lambda model, l: sum(
model.x[a] for a in model.Loci[l]) <= model.t_allele)
model.min_allel_selection = Constraint(model.LociNames,
rule=lambda model, l: sum(model.x[a] for a in model.Loci[l]) >= 1)
model.is_read_cov = Constraint(model.R,
rule=lambda model, r: sum(model.cov[r, a] * model.x[a] for a in model.L) >=
model.y[r])
model.heterozygot_count = Constraint(
rule=lambda model: model.hetero >= sum(model.x[a] for a in model.L) - model.nof_loci)
# regularization constraints
model.reg1 = Constraint(model.R, rule=lambda model, r: model.re[r] <= model.nof_loci * model.y[r])
model.reg2 = Constraint(model.R, rule=lambda model, r: model.re[r] <= model.hetero)
model.reg3 = Constraint(model.R,
rule=lambda model, r: model.re[r] >= model.hetero - model.nof_loci * (1 - model.y[r]))
# generate constraint list for solution enumeration
model.c = ConstraintList()
# Generate instance. Used to be .create() but deprecated since,
# as ConcreteModels are instances on their own now.
self.__instance = model
示例3: __init__
# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import t_allele [as 别名]
#.........这里部分代码省略.........
"Either raise k or reduce epitopes to include.")
model.Include = Set(within=model.E, initialize=self.__included)
if overlap > 0:
def longest_common_substring(model):
result = []
for s1,s2 in itr.combinations(model.E,2):
if s1 != s2:
if s1 in s2 or s2 in s1:
result.append((s1,s2))
m = [[0] * (1 + len(s2)) for i in xrange(1 + len(s1))]
longest, x_longest = 0, 0
for x in xrange(1, 1 + len(s1)):
for y in xrange(1, 1 + len(s2)):
if s1[x - 1] == s2[y - 1]:
m[x][y] = m[x - 1][y - 1] + 1
if m[x][y] > longest:
longest = m[x][y]
x_longest = x
else:
m[x][y] = 0
if len(s1[x_longest - longest: x_longest]) >= overlap:
result.append((s1,s2))
return set(result)
model.O = Set(dimen=2, initialize=longest_common_substring)
# parameter definition
model.k = Param(initialize=self.__k, within=PositiveIntegers, mutable=True)
model.k_taa = Param(initialize=self.__k_taa, within=NonNegativeIntegers, mutable=True)
model.p = Param(model.A, initialize=lambda model, a: max(0, math.log(probs[a]+0.001,2)))
model.c = Param(model.E, initialize=lambda model, e: cons[e],mutable=True)
model.sigma = Param (model. E, model.A, initialize=lambda model, e, a: uncertainty.get((e,a), 0))
model.i = Param(model.E, model.A, initialize=lambda model, e, a: imm[e, a])
model.t_allele = Param(initialize=0, within=NonNegativeIntegers, mutable=True)
model.t_var = Param(initialize=0, within=NonNegativeIntegers, mutable=True)
model.t_c = Param(initialize=0.0, within=NonNegativeReals, mutable=True)
model.abd = Param(model.Q, initialize=lambda model, g: max(0, math.log(expression.get(g, 2)+0.001, 2)))
model.eps1 = Param(initialize=1e6, mutable=True)
model.eps2 = Param(initialize=1e6, mutable=True)
# variable Definition
model.x = Var(model.E, within=Binary)
model.y = Var(model.A, within=Binary)
model.z = Var(model.Q, within=Binary)
# objective definition
model.Obj1 = Objective(
rule=lambda model: -sum(model.x[e] * sum(model.abd[g] for g in model.G[e])
* sum(model.p[a] * model.i[e, a] for a in model.A) for e in model.E),
sense=minimize)
model.Obj2 = Objective(
rule=lambda model: sum(model.x[e]*sum(model.sigma[e,a] for a in model.A) for e in model.E),
sense=minimize)
# constraints
# obligatory Constraint (number of selected epitopes)
model.NofSelectedEpitopesCov1 = Constraint(rule=lambda model: sum(model.x[e] for e in model.E) >= model.k)
model.NofSelectedEpitopesCov2 = Constraint(rule=lambda model: sum(model.x[e] for e in model.E) <= model.k)
model.NofSelectedTAACov = Constraint(rule=lambda model: sum(model.x[e] for e in model.TAA) <= model.k_taa)
# optional constraints (in basic model they are disabled)
model.IsAlleleCovConst = Constraint(model.A,
rule=lambda model, a: sum(model.x[e] for e in model.A_I[a]) >= model.y[a])
model.MinAlleleCovConst = Constraint(rule=lambda model: sum(model.y[a] for a in model.A) >= model.t_allele)