def run(self, counter): if not self._PCR_mixtures: return print('PCR Simulation: calculating equilibrium in the system ' 'and running the simulation...') pcr = self._new_PCR() if len(self._PCR_mixtures) > 1: counter.set_subwork(2, (len(self._PCR_mixtures), 0.01)) results_path = self._pcrM.ParallelPCR(counter[0], self._abort_event, pcr, self._PCR_mixtures.values()) if not results_path or self.aborted(): return results = from_shelf(results_path) counter[1].done() else: pcr = run_pcr_from_file(pcr, counter) results = (pcr(self._PCR_mixtures.itervalues().next()),) if results is None or self.aborted(): return for res in results: if not res: continue hit_id, obj_val, products, reaction_end = res self._max_objective_value = max(self._max_objective_value, obj_val) self._products[hit_id] = products self._reaction_ends[hit_id] = reaction_end #filter out hits without products self._products = dict(hit for hit in self._products.items() if hit[1]) self._nonzero = len(self._products) > 0 print 'PCR Simulation: done.'
def run(self, counter): if not self._PCR_mixtures: return print( 'PCR Simulation: calculating equilibrium in the system ' 'and running the simulation...') pcr = self._new_PCR() if len(self._PCR_mixtures) > 1: counter.set_subwork(2, (len(self._PCR_mixtures), 0.01)) results_path = self._pcrM.ParallelPCR(counter[0], self._abort_event, pcr, self._PCR_mixtures.values()) if not results_path or self.aborted(): return results = from_shelf(results_path) counter[1].done() else: pcr = run_pcr_from_file(pcr, counter) results = (pcr(self._PCR_mixtures.itervalues().next()), ) if results is None or self.aborted(): return for res in results: if not res: continue hit_id, obj_val, products, reaction_end = res self._max_objective_value = max(self._max_objective_value, obj_val) self._products[hit_id] = products self._reaction_ends[hit_id] = reaction_end #filter out hits without products self._products = dict(hit for hit in self._products.items() if hit[1]) self._nonzero = len(self._products) > 0 print 'PCR Simulation: done.'
def _extract_annealings(*annealings_list): fwd_annealings = [] rev_annealings = [] for filename in annealings_list: ann = from_shelf(filename) if not ann: continue if ann[0]: fwd_annealings.extend(ann[0]) if ann[1]: rev_annealings.extend(ann[1]) return fwd_annealings,rev_annealings
def _extract_annealings(*annealings_list): fwd_annealings = [] rev_annealings = [] for filename in annealings_list: ann = from_shelf(filename) if not ann: continue if ann[0]: fwd_annealings.extend(ann[0]) if ann[1]: rev_annealings.extend(ann[1]) return fwd_annealings, rev_annealings
def process_sequences(seqs, _depth): if _depth == 0: return with user_message('RingBlast: processing %d sequences of the %d ring.' % (len(seqs), depth-_depth+1), '\n'): next_ring = blast_filter_fetch(seqs) if not next_ring: return to_check = [] next_to_process = [] for n in next_ring: next_seqs = from_shelf(n) if not next_seqs: continue for ns in next_seqs: sid = self.base_sid(ns) if sid in extended_set: #FIXME: need to merge sequences properly, instead of replacing if len(extended_set[sid]) < len(ns): extended_set[sid] = ns else: to_check.append(ns) if not to_check or not check_sequences(to_check, next_to_process): return if next_to_process: process_sequences(next_to_process, _depth-1)
def ring_blast(self, query, db='nr', evalue=0.001, blast_filter=None, depth=1, command='blastn', **kwargs): '''Perform a blast search with the given query to obtain the core set of hits. Make another search with each hit as a query. If results of the second search contain new hits, check if these are reciprocal by yet another search with them and checking that results contain hits from the core set and if they are, add the to the final set. ''' if isinstance(query, SeqRecord): query = [query] def blast_filter_fetch(seqs): @MultiprocessingBase.data_mapper @shelf_result def worker(s): r = self.blast_seq(s, db, evalue, command) if r and blast_filter: blast_filter(r) if r: return self.fetch_results(r, db, what='alignment') return None results = [] total = len(seqs) prg = ProgressCounter('Performing blast search for %d sequences:' % total, total) @MultiprocessingBase.results_assembler def assembler(i, res): if res: results.append(res) prg.count() with prg: if not self.parallelize2(1, worker, assembler, seqs): return None return results with user_message('RingBlast: building a core set of sequences.', '\n'): core_seqs = blast_filter_fetch(query) if not core_seqs: return None core_seqs = self.unique_seqs(chain.from_iterable(from_shelf(r) for r in core_seqs)) extended_set = dict((self.base_sid(s), s) for s in core_seqs) if depth <= 0: return core_seqs core_db = self.format_tmp_db(core_seqs, command.endswith('n')) def check_sequences(seqs, next_to_process): total = len(seqs) prg = ProgressCounter('RingBlast: checking %d new sequences:' % total, total) @MultiprocessingBase.data_mapper def worker(seq): res = self.blast_seq(seq, core_db, 100, command) if res and blast_filter: blast_filter(res) return bool(res), seq @MultiprocessingBase.results_assembler def assembler(i, res): prg.count() if not res[0]: return seq = res[1] extended_set[self.base_sid(seq)] = seq next_to_process.append(seq) with prg: return self.parallelize2(1, worker, assembler, seqs) def process_sequences(seqs, _depth): if _depth == 0: return with user_message('RingBlast: processing %d sequences of the %d ring.' % (len(seqs), depth-_depth+1), '\n'): next_ring = blast_filter_fetch(seqs) if not next_ring: return to_check = [] next_to_process = [] for n in next_ring: next_seqs = from_shelf(n) if not next_seqs: continue for ns in next_seqs: sid = self.base_sid(ns) if sid in extended_set: #FIXME: need to merge sequences properly, instead of replacing if len(extended_set[sid]) < len(ns): extended_set[sid] = ns else: to_check.append(ns) if not to_check or not check_sequences(to_check, next_to_process): return if next_to_process: process_sequences(next_to_process, _depth-1) process_sequences(core_seqs, depth) return extended_set.values()
def ring_blast(self, query, db='nr', evalue=0.001, blast_filter=None, depth=1, command='blastn', **kwargs): '''Perform a blast search with the given query to obtain the core set of hits. Make another search with each hit as a query. If results of the second search contain new hits, check if these are reciprocal by yet another search with them and checking that results contain hits from the core set and if they are, add the to the final set. ''' if isinstance(query, SeqRecord): query = [query] def blast_filter_fetch(seqs): @MultiprocessingBase.data_mapper @shelf_result def worker(s): r = self.blast_seq(s, db, evalue, command, **kwargs) if r and blast_filter: blast_filter(r) if r: return self.fetch_results(r, db, what='alignment') return None results = [] total = len(seqs) prg = ProgressCounter('Performing blast search for %d sequences:' % total, total) @MultiprocessingBase.results_assembler def assembler(i, res): if res: results.append(res) prg.count() with prg: if not self.parallelize2(1, worker, assembler, seqs): return None return results with user_message('RingBlast: building a core set of sequences.', '\n'): core_seqs = blast_filter_fetch(query) if not core_seqs: return None core_seqs = self.unique_seqs(chain.from_iterable(from_shelf(r) for r in core_seqs)) extended_set = dict((self.base_sid(s), s) for s in core_seqs) if depth <= 0: return core_seqs core_db = self.format_tmp_db(core_seqs, command.endswith('n')) def check_sequences(seqs, next_to_process): total = len(seqs) prg = ProgressCounter('RingBlast: checking %d new sequences:' % total, total) @MultiprocessingBase.data_mapper def worker(seq): res = self.blast_seq(seq, core_db, 100, command) if res and blast_filter: blast_filter(res) return bool(res), seq @MultiprocessingBase.results_assembler def assembler(i, res): prg.count() if not res[0]: return seq = res[1] extended_set[self.base_sid(seq)] = seq next_to_process.append(seq) with prg: return self.parallelize2(1, worker, assembler, seqs) def process_sequences(seqs, _depth): if _depth == 0: return with user_message('RingBlast: processing %d sequences of the %d ring.' % (len(seqs), depth-_depth+1), '\n'): next_ring = blast_filter_fetch(seqs) if not next_ring: return to_check = [] next_to_process = [] for n in next_ring: next_seqs = from_shelf(n) if not next_seqs: continue for ns in next_seqs: sid = self.base_sid(ns) if sid in extended_set: #FIXME: need to merge sequences properly, instead of replacing if len(extended_set[sid]) < len(ns): extended_set[sid] = ns else: to_check.append(ns) if not to_check or not check_sequences(to_check, next_to_process): return if next_to_process: process_sequences(next_to_process, _depth-1) process_sequences(core_seqs, depth) return extended_set.values()