Calculate a simple consensus sequence for the block.
979161
def get_consensus(block):
    """"""
    from collections import Counter

    # Take aligned (non-insert) chars from all rows; transpose
    columns = zip(*[[c for c in row['seq'] if not c.islower()]
                    for row in block['sequences']])
    cons_chars = [Counter(col).most_common()[0][0] for col in columns]
    cons_chars = [c if c != '-' else 'X' for c in cons_chars]
    assert len(cons_chars) == block['query_length']
    cons_sequence = {
        'index': 1,
        'id': 'consensus',
        'description': '',
        'dbxrefs': {},
        'phylum': '',
        'taxchar': '',
        'head_len': None,
        'tail_len': None,
        'head_seq': '',
        'tail_seq': '',
        'length': block['query_length'],
        'seq': ''.join(cons_chars),
    }
    return cons_sequence
305227
def persist_block(self,
                      block: 'BaseBlock'
                      ) -> Tuple[Tuple[Hash32, ...], Tuple[Hash32, ...]]:
        
        with self.db.atomic_batch() as db:
            return self._persist_block(db, block)
779577
def rpc_get_consensus_at( self, block_id, **con_info ):
        
        if not check_block(block_id):
            return {'error': 'Invalid block height', 'http_status': 400}

        db = get_db_state(self.working_dir)
        consensus = db.get_consensus_at( block_id )
        db.close()
        return self.success_response( {'consensus': consensus} )
739709
def commit(self):
        """"""

        self.abort_if_abci_chain_is_not_synced()

        data = self.block_txn_hash.encode('utf-8')

        # register a new block only when new transactions are received
        if self.block_txn_ids:
            self.bigchaindb.store_bulk_transactions(self.block_transactions)

        block = Block(app_hash=self.block_txn_hash,
                      height=self.new_height,
                      transactions=self.block_txn_ids)
        # NOTE: storing the block should be the last operation during commit
        # this effects crash recovery. Refer BEP#8 for details
        self.bigchaindb.store_block(block._asdict())

        logger.debug('Commit-ing new block with hash: apphash=%s ,'
                     'height=%s, txn ids=%s', data, self.new_height,
                     self.block_txn_ids)

        if self.events_queue:
            event = Event(EventTypes.BLOCK_VALID, {
                'height': self.new_height,
                'transactions': self.block_transactions
            })
            self.events_queue.put(event)

        return ResponseCommit(data=data)
774988
def find_consensus(bases):
    
    nucs = ['A', 'T', 'G', 'C', 'N']
    total = sum([bases[nuc] for nuc in nucs if nuc in bases])
    # save most common base as consensus (random nuc if there is a tie)
    try:
        top = max([bases[nuc] for nuc in nucs if nuc in bases])
    except:
        bases['consensus'] = ('N', 'n/a')
        bases['consensus frequency'] = 'n/a'
        bases['reference frequency'] = 'n/a'
        return bases
    top = [(nuc, bases[nuc]) for nuc in bases if bases[nuc] == top]
    if top[0][1] == 0:
        bases['consensus'] = ('n/a', 0)
    else:
        bases['consensus'] = random.choice(top)
    if total == 0:
        c_freq = 'n/a'
        ref_freq = 'n/a'
    else:
        c_freq = float(bases['consensus'][1]) / float(total)
        if bases['ref'] not in bases:
            ref_freq = 0
        else:
            ref_freq = float(bases[bases['ref']]) / float(total)
    bases['consensus frequency'] = c_freq
    bases['reference frequency'] = ref_freq
    return bases
810316
def step(self):
        """"""

        # If the nproc parameter of __init__ is zero, just iterate
        # over the K consensus instances instead of using
        # multiprocessing to do the computations in parallel. This is
        # useful for debugging and timing comparisons.
        if self.nproc == 0:
            for k in range(self.xstep.cri.K):
                step_group(k)
        else:
            self.pool.map(step_group, range(self.xstep.cri.K))

        ccmod_ystep()
        ccmod_ustep()
        cbpdn_setdict()
554190
def get_block_from_consensus(consensus_hash, hostport=None, proxy=None):
    
    assert hostport or proxy, 'Need hostport or proxy'
    if proxy is None:
        proxy = connect_hostport(hostport)

    consensus_schema = {
        'type': 'object',
        'properties': {
            'block_id': {
                'anyOf': [
                    {
                        'type': 'integer',
                        'minimum': 0,
                    },
                    {
                        'type': 'null',
                    },
                ],
            },
        },
        'required': [
            'block_id'
        ],
    }

    schema = json_response_schema( consensus_schema )
    resp = {}
    try:
        resp = proxy.get_block_from_consensus(consensus_hash)
        resp = json_validate( schema, resp )
        if json_is_error(resp):
            log.error("Failed to find block ID for %s" % consensus_hash)
            return resp

    except ValidationError as ve:
        if BLOCKSTACK_DEBUG:
            log.exception(ve)

        resp = {'error': 'Server response did not match expected schema.  You are likely communicating with an out-of-date Blockstack node.', 'http_status': 502}
        return resp
    
    except socket.timeout:
        log.error("Connection timed out")
        resp = {'error': 'Connection to remote host timed out.', 'http_status': 503}
        return resp

    except socket.error as se:
        log.error("Connection error {}".format(se.errno))
        resp = {'error': 'Connection to remote host failed.', 'http_status': 502}
        return resp

    except Exception as ee:
        if BLOCKSTACK_DEBUG:
            log.exception(ee)

        log.error("Caught exception while connecting to Blockstack node: {}".format(ee))
        resp = {'error': 'Failed to contact Blockstack node.  Try again with `--debug`.', 'http_status': 500}
        return resp

    return resp['block_id']
290560
def finalize(state, block):
    """"""

    if state.is_METROPOLIS():
        br = state.config['BYZANTIUM_BLOCK_REWARD']
        nr = state.config['BYZANTIUM_NEPHEW_REWARD']
    else:
        br = state.config['BLOCK_REWARD']
        nr = state.config['NEPHEW_REWARD']
        
    delta = int(br + nr * len(block.uncles))
    state.delta_balance(state.block_coinbase, delta)

    udpf = state.config['UNCLE_DEPTH_PENALTY_FACTOR']

    for uncle in block.uncles:
        r = int(br * (udpf + uncle.number - state.block_number) // udpf)
        state.delta_balance(uncle.coinbase, r)

    if state.block_number - \
            state.config['MAX_UNCLE_DEPTH'] in state.recent_uncles:
        del state.recent_uncles[state.block_number -
                                state.config['MAX_UNCLE_DEPTH']]
369492
def goto_block(self, block):
        
        old_block = self.basic_block
        term = block.terminator
        if term is not None:
            self.position_before(term)
        else:
            self.position_at_end(block)
        try:
            yield
        finally:
            self.position_at_end(old_block)
135750
def print_consensus(genomes):
    
    # generate consensus sequences
    cons = {} # cons[genome][sample][contig] = consensus
    for genome, contigs in list(genomes.items()):
        cons[genome] = {}
        for contig, samples in list(contigs.items()):
            for sample, stats in list(samples.items()):
                if sample not in cons[genome]:
                    cons[genome][sample] = {}
                seq = cons[genome][sample][contig] = []
                for pos, ps in enumerate(stats['bp_stats'], 1):
                    ref, consensus = ps['ref'], ps['consensus'][0]
                    if consensus == 'n/a':
                        consensus = ref.lower()
                    seq.append(consensus)
    # print consensus sequences
    for genome, samples in cons.items():
        for sample, contigs in samples.items():
            fn = '%s.%s.consensus.fa' % (genome, sample)
            f = open(fn, 'w')
            for contig, seq in contigs.items():
                print('>%s' % (contig), file = f)
                print(''.join(seq), file = f)
            f.close()
    return cons
1024445
def point_consensus(self, consensus_type):
        
        if "mean" in consensus_type:
            consensus_data = np.mean(self.data, axis=0)
        elif "std" in consensus_type:
            consensus_data = np.std(self.data, axis=0)
        elif "median" in consensus_type:
            consensus_data = np.median(self.data, axis=0)
        elif "max" in consensus_type:
            consensus_data = np.max(self.data, axis=0)
        elif "percentile" in consensus_type:
            percentile = int(consensus_type.split("_")[1])
            consensus_data = np.percentile(self.data, percentile, axis=0)
        else:
            consensus_data = np.zeros(self.data.shape[1:])
        consensus = EnsembleConsensus(consensus_data, consensus_type, self.ensemble_name,
                                      self.run_date, self.variable, self.start_date, self.end_date, self.units)
        return consensus
53585
def calculate_columns(sequence):
    
    columns = {}

    for row in sequence:
        for key in row.keys():
            if key not in columns:
                columns[key] = len(key)

            value_length = len(str(row[key]))
            if value_length > columns[key]:
                columns[key] = value_length

    return columns
1075034
def get_valid_consensus_hashes( self, block_id ):
        
        first_block_to_check = block_id - self.impl.get_valid_transaction_window()

        query = 'SELECT consensus_hash FROM snapshots WHERE block_id >= ? AND block_id <= ?;'
        args = (first_block_to_check,block_id)

        valid_consensus_hashes = []
        
        con = self.db_open(self.impl, self.working_dir)
        rows = self.db_query_execute(con, query, args, verbose=False)

        for r in rows:
            assert r['consensus_hash'] is not None
            assert isinstance(r['consensus_hash'], (str,unicode))

            valid_consensus_hashes.append(str(r['consensus_hash']))

        con.close()
        return valid_consensus_hashes
156660
def notify_block_new(self, block):
        """"""
        payload = block.SerializeToString()
        self._notify(
            "consensus_notifier_notify_block_new", payload, len(payload))
421934
def assemble(self):
        """"""
        first_block = ray.get(self.objectids[(0, ) * self.ndim])
        dtype = first_block.dtype
        result = np.zeros(self.shape, dtype=dtype)
        for index in np.ndindex(*self.num_blocks):
            lower = DistArray.compute_block_lower(index, self.shape)
            upper = DistArray.compute_block_upper(index, self.shape)
            result[[slice(l, u) for (l, u) in zip(lower, upper)]] = ray.get(
                self.objectids[index])
        return result
1332222
def merge_blocks(a_blocks, b_blocks):
    
    # Check sentinels for sequence length.
    assert a_blocks[-1][2] == b_blocks[-1][2] == 0 # sentinel size is 0
    assert a_blocks[-1] == b_blocks[-1]
    combined_blocks = sorted(list(set(a_blocks + b_blocks)))
    # Check for overlaps.
    i = j = 0
    for a, b, size in combined_blocks:
        assert i <= a
        assert j <= b
        i = a + size
        j = b + size
    return combined_blocks
627431
function blockxor(S, Si, D, len) {
        for (var i = 0; i < len; i++) {
            D[i] ^= S[Si + i]
        }
    }