def _integrate(self, prev, dt, eta0, q, g0=np.eye(4), intermediate=False): self.xi[0, :] = np.array([0, 0, 0, 0, 0, 1]) self.eta[0, :] = eta0 # integration over the body (don't need the initial point as the initial values are determined already) # g_half = g0 # known initial condition g_half = expm(dt / 2 * se(prev.eta[0, :])) for i in range(self.N - 1): # averaging over steps to get half step values xi_half = (self.xi[i, :] + prev.xi[i, :]) / 2 eta_half = (self.eta[i, :] + prev.eta[i, :]) / 2 # implicit midpoint approximation xi_dot = (self.xi[i, :] - prev.xi[i, :]) / dt eta_dot = (self.eta[i, :] - prev.eta[i, :]) / dt # external loads A_bar = 0 B_bar = 0 # viscosity B_bar += self.V @ xi_dot # other loads for load in self.loads: A, B = load.dist_load(g_half, xi_half, eta_half, xi_dot, eta_dot, self, q) A_bar += A B_bar += B if intermediate and i == self.N - 2: for load in self.loads: W = load.tip_load(g_half, xi_half, eta_half, xi_dot, eta_dot, self, q) B_bar += W # spatial derivatives xi_der = np.linalg.inv(self.K - A_bar) @ ( (self.M @ eta_dot) - (adjoint(eta_half).T @ self.M @ eta_half) + (adjoint(xi_half).T @ self.K @ (xi_half - self.xi_ref)) + B_bar) eta_der = xi_dot - (adjoint(xi_half) @ eta_half) # explicit Euler step xi_half_next = xi_half + self.ds * xi_der eta_half_next = eta_half + self.ds * eta_der g_half = g_half @ expm(se(self.ds * xi_half)) # determine next step from half step value self.xi[i + 1, :] = 2 * xi_half_next - prev.xi[i + 1, :] self.eta[i + 1, :] = 2 * eta_half_next - prev.eta[i + 1, :] # midpoint RKMK to step the g values for i in range(self.N): self.g[i, :] = flatten( unflatten(prev.g[i, :]) @ expm( se(dt * (self.eta[i, :] + prev.eta[i, :]) / 2))) return g_half
def benchmark_method(method="delta"): # or MILC for org in ["Mycobacterium_smegmatis"]:#problem_species:#validation_orgs: print org gbk_filename = get_genome_filename(org,'gbk') genome = get_genome(org) cdss = get_cdss(genome) Method = MILC if method == "MILC" else Delta method = Method(org) org_exp_dict = master_exp_dict[org] scores = [] expss = [] # list of lists; one for each replicates for i,cds in enumerate(cdss): if i % 100 == 0: print i locus_tag = head(cds.qualifiers['locus_tag']) try: gene = head(cds.qualifiers['gene']) except: gene = None if locus_tag in org_exp_dict: exp_scores = org_exp_dict[locus_tag] elif gene in org_exp_dict: exp_scores = org_exp_dict[gene] try: seq = str(cds.extract(genome).seq) method_score = method.score(seq) expss.append(exp_scores) scores.append(method_score) except: print "tag failed:",locus_tag print "recovered scores for: ",len(scores),"tags" print "distinct scores:",len(set(scores)) if method == "MILC": scores = [-x for x in scores] #flip scores so they correlate #positively w/ expression spearmans = [spearmanr(scores,exps)[0] for exps in transpose(expss)] spearman_mean = mean(spearmans) spearman_se = se(spearmans) print "Correlation:",org,spearman_mean,spearman_se#,milc.num_ribosomals
def benchmark_cat(): """Compute correlation with expression for the CDC method of Zhang BMC Bioinformatics 2012""" for org in validation_orgs: print org try: gbk_filename = get_genome_filename(org,'gbk') genome = get_genome(org) cdss = get_cdss(genome) ncid = org2nc_id(org) cat_filename = os.path.join("index_results",ncid+"_CAT",ncid+".cat") with open(cat_filename) as f: lines = [line.split("\t") for line in f.readlines()[1:]] labels,cdcs = transpose([(fields[0],fields[10]) for fields in lines]) matches = [re.search(r":(c?)(\d+)-(\d+)",label).groups() for label in labels] locations = [((int(start),int(stop)) if c == '' else (int(stop) - 1,int(start))) for (c,start,stop) in matches] cat_dict = {location:float(cdc) for location,cdc in zip(locations,cdcs)} org_exp_dict = master_exp_dict[org] # a dictionary of form {(start,stop):[locus tags]} location2lt = {(feature.location.start+1,feature.location.end): feature.qualifiers['locus_tag'][0] for feature in genome.features if ('locus_tag' in feature.qualifiers)} correlates = [(cdc,org_exp_dict[location2lt[location]]) for location,cdc in cat_dict.items() if location in location2lt and location2lt[location] in org_exp_dict] cdcs,exps = transpose(correlates) rhos = [spearmanr(cdcs,map(lambda xs:xs[i],exps))[0] for i in range(len(exps[0]))] print "num correlates:",len(correlates) print "Correlation:",org,mean(rhos),se(rhos) except: print "Failed on:",org
def _initRod(self): # setup g, xi, and eta for the initial configuration g = np.zeros((self.N, 12)) xi = np.zeros((self.N, 6)) eta = np.zeros((self.N, 6)) # set xi and eta for i in range(self.N): s = self.ds * i xi[i, :] = self.xi_init(s) eta[i, :] = self.eta_init(s) # integrate G G = np.eye(4) g[0, :] = flatten(G) for i in range(1, self.N): G = G @ expm(se(self.ds * xi[i - 1, :])) g[i, :] = flatten(G) # set state self.g = g self.xi = xi self.eta = eta