示例#1
0
def align_bwt(reads, index, count, ref_seq):
    """aligns a read to a reference genome
    following the BWT index"""

    best_aln = reads  # contains alignments to report

    for i in range(len(reads)):  # for each one of the two ends
        # try forward
        read = best_aln[i].sequence
        aln = perfect_match_bwt(read, index, count)
        if aln != ".":
            best_aln[i].strand = "+"
    # try reverse
        else:
            read = u.rev(read)
            aln = perfect_match_bwt(read, index, count)
            if aln != ".":
                best_aln[i].strand = "-"
        # save positions
        if aln != ".":
            best_aln[i].aligned = True
            best_aln[i].position = aln

    SNPs, ins, dels, pos = 0, 0, 0, 0
    # if one end is not aligned, try local alignment within the expected region of the genome
    if best_aln[0].aligned and not best_aln[1].aligned:
        eor1 = best_aln[0].position + len(best_aln[0].sequence)
        refe_sl = ref_seq[eor1 + 75:eor1 + 175]
        read_sl = best_aln[1].sequence if best_aln[0].strand == "-" else u.rev(
            best_aln[1].sequence)
        SNPs, ins, dels, pos = local_align(refe_sl, read_sl)
        if SNPs != 0:
            best_aln[1].mismatch = SNPs
            best_aln[1].insertion = ins
            best_aln[1].deletion = dels
            best_aln[1].strand = "+" if best_aln[0].strand == "-" else "-"
            best_aln[1].aligned = True
            best_aln[1].position = pos + eor1 + 75
    elif best_aln[1].aligned and not best_aln[0].aligned:
        eor1 = best_aln[1].position
        refe_sl = ref_seq[eor1 - 175:eor1 - 75]
        read_sl = best_aln[0].sequence if best_aln[1].strand == "-" else u.rev(
            best_aln[0].sequence)
        SNPs, ins, dels, pos = local_align(refe_sl, read_sl)
        if SNPs != 0:
            best_aln[0].mismatch = SNPs
            best_aln[0].insertion = ins
            best_aln[0].deletion = dels
            best_aln[0].strand = "+" if best_aln[1].strand == "-" else "-"
            best_aln[0].aligned = True
            best_aln[0].position = pos + eor1 - 175

    # add to best alignments the information from the other end
    best_aln[0].pair_position = best_aln[1].position
    best_aln[0].pair_strand = best_aln[1].strand

    best_aln[1].pair_position = best_aln[0].position
    best_aln[1].pair_strand = best_aln[0].strand

    return best_aln
示例#2
0
文件: cc.py 项目: nikhilYadala/blurt
 def __init__(self, code=standard_cc):
     self.nu = code[0]
     self.g0 = util.rev(code[1], self.nu)
     self.g1 = util.rev(code[2], self.nu)
     self.output_map_1      = np.uint8(1 & (util.mul(self.g0, np.arange(1<<self.nu)) >> (self.nu-1)))
     self.output_map_2      = np.uint8(1 & (util.mul(self.g1, np.arange(1<<self.nu)) >> (self.nu-1)))
     self.states            = np.arange(1<<self.nu)
     self.state_inv_map     = ((np.arange(1<<self.nu) << 1) & ((1<<self.nu)-1))[:,np.newaxis] + np.array([0,1])[np.newaxis,:]
     self.state_inv_map_tag = np.tile(np.arange(1<<self.nu)[:,np.newaxis] >> (self.nu-1), (1,2))
     self.output_map_1_soft = self.output_map_1.astype(int)[np.newaxis,:] * 2 - 1
     self.output_map_2_soft = self.output_map_2.astype(int)[np.newaxis,:] * 2 - 1
示例#3
0
    def run(self):
        update = False

        try:
            if self.registry.last_block:
                current_prevhash = "%080x" % self.registry.last_block.hashPrevBlock
            else:
                current_prevhash = None

            log.debug("Checking for new block.")
            prevhash = util.rev((yield self.bitcoin_rpc.prevhash()))
            if prevhash and prevhash != current_prevhash:
                log.info("New block! Prevhash: %s" % prevhash)
                update = True

            elif Interfaces.timestamper.time(
            ) - self.registry.last_update >= settings.MERKLE_REFRESH_INTERVAL:
                log.info("Merkle update! Prevhash: %s" % prevhash)
                update = True

            if update:
                self.registry.update_block()

        except Exception:
            log.exception("UpdateWatchdog.run failed")
        finally:
            self.schedule()
示例#4
0
    def fill_from_rpc(self, data):
        '''Convert getblocktemplate result into BlockTemplate instance'''
        if 'height' not in data:
            log.info("Waiting for new work...")
            self.prevhash_hex = self.blank_hash
            self.broadcast_args = self.build_fake_broadcast_args()
            return

        txhashes = [None] + [
            util.ser_uint320(int(t['hash'], 16)) for t in data['transactions']
        ]
        mt = merkletree.MerkleTree(txhashes)

        self.height = data['height']
        self.nVersion = data['version']
        self.hashPrevBlock = int(data['previousblockhash'], 16)
        self.nBits = int(data['bits'], 16)
        self.nBitsHex = data['bits']

        self.hashMerkleRoot = 0
        self.nTime = 0
        self.nNonce = 0
        self.cbTxTime = int(self.timestamper.time())
        self.nTxTime = self.cbTxTime * 1000
        self.nHashCoin = 0
        self.sigchecksum = 0

        coinbase = CoinbaseTransaction(self.timestamper, self.coinbaser,
                                       data['coinbasevalue'],
                                       data['coinbaseaux']['flags'],
                                       data['height'],
                                       settings.COINBASE_EXTRAS, self.cbTxTime)

        self.vtx = [
            coinbase,
        ]
        for tx in data['transactions']:
            t = halfnode.CTransaction()
            t.deserialize(StringIO.StringIO(binascii.unhexlify(tx['data'])))
            self.vtx.append(t)

        self.curtime = data['curtime']
        self.timedelta = self.curtime - int(self.timestamper.time())
        self.merkletree = mt
        self.target = int((data['target']), 16)
        log.info("Block height: %i network difficulty: %s" %
                 (self.height, self.diff_to_t(self.target)))

        # Reversed prevhash
        #self.prevhash_bin = binascii.unhexlify(util.reverse_hash_80(data['previousblockhash']))
        self.prevhash_bin = binascii.unhexlify(
            util.rev(data['previousblockhash']))
        self.prevhash_hex = "%080x" % self.hashPrevBlock
        #log.info("%s\n", repr(self))

        self.broadcast_args = self.build_broadcast_args()
示例#5
0
    def submit_share(self, job_id, worker_name, session, extranonce1_bin, data,
                     difficulty):

        job = self.get_job(job_id)
        if job == None:
            raise SubmitException("Job '%s' not found" % job_id)

        ntime = util.flip(data[136:144])
        if not job.check_ntime(int(ntime, 16)):
            raise SubmitException("Ntime out of range")

        if not job.register_submit(data):
            log.info("Duplicate from %s, (%s %s)" % \
                    (worker_name, binascii.hexlify(extranonce1_bin), data))
            raise SubmitException("Duplicate share")

        hash_int = gapcoin_hash.getpowdiff(str(data))
        block_hash_bin = util.doublesha(binascii.unhexlify(data[0:168]))
        block_hash_hex = util.rev(binascii.hexlify(block_hash_bin))
        '''log.info("block_hash_hex %s" % block_hash_hex)
        log.info("shrint %s" % hash_int)
        log.info("jobint %s" % job.target)%f
        log.info("target %s" % difficulty)'''

        if hash_int < difficulty:
            raise SubmitException("Share less than target")

        share_diff = float(float(hash_int) / float(pow(2, 48)))

        if hash_int >= job.target:
            log.info("BLOCK CANDIDATE! %s" % block_hash_hex)

            extranonce2_bin = struct.pack('>L', 0)
            #self.last_block.vtx[0].set_extranonce(extranonce1_bin + extranonce2_bin)
            #txs = binascii.hexlify(util.ser_vector(self.last_block.vtx))

            job.vtx[0].set_extranonce(extranonce1_bin + extranonce2_bin)
            txs = binascii.hexlify(util.ser_vector(job.vtx))
            serialized = str(data) + str(txs)

            on_submit = self.bitcoin_rpc.submitblock(str(data), str(txs),
                                                     block_hash_hex)
            if on_submit:
                self.update_block()

            return (block_hash_hex, share_diff, on_submit)

        return (block_hash_hex, share_diff, None)
    def fill_from_rpc(self, data):
        '''Convert getblocktemplate result into BlockTemplate instance'''
        
        txhashes = [None] + [ util.ser_uint256(int(t['hash'], 16)) for t in data['transactions'] ]
        mt = merkletree.MerkleTree(txhashes)
        coinbase = CoinbaseTransaction(self.timestamper, self.coinbaser, data['coinbasevalue'],
                                              data['coinbaseaux']['flags'], data['height'],
                                              settings.COINBASE_EXTRAS)

        self.height = data['height']
        self.nVersion = data['version']
        self.hashPrevBlock = int(data['previousblockhash'], 16)
        self.nBits = int(data['bits'], 16)

        self.hashMerkleRoot = 0
        self.nTime = 0
        self.nNonce = 0
        self.vtx = [ coinbase, ]
        
        for tx in data['transactions']:
            t = halfnode.CTransaction()
            t.deserialize(StringIO.StringIO(binascii.unhexlify(tx['data'])))
            self.vtx.append(t)
            
        self.curtime = data['curtime']
        self.timedelta = self.curtime - int(self.timestamper.time()) 
        self.merkletree = mt
        self.target = int(data['bits'], 16)
        self.network_diff = round(float(self.target) / float(pow(2, 48)), 8)
        log.info("Block: %i network difficulty: %0.8f" % (self.height, self.network_diff))

        # Reversed prevhash
        self.prevhash_bin = binascii.unhexlify(util.rev(data['previousblockhash']))
        self.prevhash_hex = "%064x" % self.hashPrevBlock
        
        self.broadcast_args = self.build_broadcast_args()
def parlindromic(s):
    s = str(s)
    if (s == rev(s)):
        return True
    else:
        return False
示例#8
0
    def submit_share(self, job_id, worker_name, session, extranonce1_bin, extranonce2, ntime, nonce,
                     difficulty):
        '''Check parameters and finalize block template. If it leads
           to valid block candidate, asynchronously submits the block
           back to the bitcoin network.
        
            - extranonce1_bin is binary. No checks performed, it should be from session data
            - job_id, extranonce2, ntime, nonce - in hex form sent by the client
            - difficulty - decimal number from session, again no checks performed
            - submitblock_callback - reference to method which receive result of submitblock()
        '''
        
        # Check if extranonce2 looks correctly. extranonce2 is in hex form...
        if len(extranonce2) != self.extranonce2_size * 2:
            raise SubmitException("Incorrect size of extranonce2. Expected %d chars" % (self.extranonce2_size*2))
        
        # Check for job
        job = self.get_job(job_id)
        if job == None:
            raise SubmitException("Job '%s' not found" % job_id)
                
        # Check if ntime looks correct
        if len(ntime) != 8:
            raise SubmitException("Incorrect size of ntime. Expected 8 chars")

        if not job.check_ntime(int(ntime, 16)):
            raise SubmitException("Ntime out of range")
        
        # Check nonce        
        if len(nonce) != 8:
            raise SubmitException("Incorrect size of nonce. Expected 8 chars")
        
        # Check for duplicated submit
        if not job.register_submit(extranonce1_bin, extranonce2, ntime, nonce):
            log.info("Duplicate from %s, (%s %s %s %s)" % \
                    (worker_name, binascii.hexlify(extranonce1_bin), extranonce2, ntime, nonce))
            raise SubmitException("Duplicate share")
        
        # Now let's do the hard work!
        # ---------------------------
        
        # 0. Some sugar
        extranonce2_bin = binascii.unhexlify(extranonce2)
        ntime_bin = binascii.unhexlify(ntime)
        nonce_bin = binascii.unhexlify(nonce)
                
        # 1. Build coinbase
        coinbase_bin = job.serialize_coinbase(extranonce1_bin, extranonce2_bin)
        coinbase_hash = util.doublesha(coinbase_bin)
        
        # 2. Calculate merkle root
        merkle_root_bin = job.merkletree.withFirst(coinbase_hash)
        merkle_root_int = util.uint256_from_str(merkle_root_bin)
                
        # 3. Serialize header with given merkle, ntime and nonce
        header_bin = job.serialize_header(merkle_root_int, ntime_bin, nonce_bin)
    
        # 4. Reverse header and compare it with target of the user
        if settings.COINDAEMON_ALGO == 'scrypt':
            hash_bin = ltc_scrypt.getPoWHash(''.join([ header_bin[i*4:i*4+4][::-1] for i in range(0, 20) ]))
        elif settings.COINDAEMON_ALGO  == 'scrypt-jane':
            hash_bin = yac_scrypt.getPoWHash(''.join([ header_bin[i*4:i*4+4][::-1] for i in range(0, 20) ]), int(ntime, 16))
        elif settings.COINDAEMON_ALGO == 'quark':
            hash_bin = quark_hash.getPoWHash(''.join([ header_bin[i*4:i*4+4][::-1] for i in range(0, 20) ]))
	elif settings.COINDAEMON_ALGO == 'skeinhash':
            hash_bin = skeinhash.skeinhash(''.join([ header_bin[i*4:i*4+4][::-1] for i in range(0, 20) ]))
        else:
            hash_bin = util.doublesha(''.join([ header_bin[i*4:i*4+4][::-1] for i in range(0, 20) ]))

        hash_int = util.uint256_from_str(hash_bin)
        scrypt_hash_hex = "%064x" % hash_int
        header_hex = binascii.hexlify(header_bin)
        if settings.COINDAEMON_ALGO == 'scrypt' or settings.COINDAEMON_ALGO == 'scrypt-jane':
            header_hex = header_hex+"000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000"
        elif settings.COINDAEMON_ALGO == 'quark':
            header_hex = header_hex+"000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000"
        else: pass
                 
        target_user = self.diff_to_target(difficulty)
        if hash_int > target_user:
            raise SubmitException("Share is above target")

        # Mostly for debugging purposes
        target_info = self.diff_to_target(100000)
        if hash_int <= target_info:
            log.info("Yay, share with diff above 100000")

        # Algebra tells us the diff_to_target is the same as hash_to_diff
        share_diff = int(self.diff_to_target(hash_int))

        on_submit = None
        mm_submit = None
        
        if settings.SOLUTION_BLOCK_HASH:
        # Reverse the header and get the potential block hash (for scrypt only) only do this if we want to send in the block hash to the shares table
            block_hash_bin = util.doublesha(''.join([ header_bin[i*4:i*4+4][::-1] for i in range(0, 20) ]))
            block_hash_hex = block_hash_bin[::-1].encode('hex_codec')        

        # 5. Compare hash with target of the network
        if hash_int <= job.target:
            # Yay! It is block candidate! 
            log.info("We found a block candidate! %s" % scrypt_hash_hex)

            # Reverse the header and get the potential block hash (for scrypt only) 
            #if settings.COINDAEMON_ALGO == 'scrypt' or settings.COINDAEMON_ALGO == 'sha256d':
            #   if settings.COINDAEMON_Reward == 'POW':
            block_hash_bin = util.doublesha(''.join([ header_bin[i*4:i*4+4][::-1] for i in range(0, 20) ]))
            block_hash_hex = block_hash_bin[::-1].encode('hex_codec')
            #else:   block_hash_hex = hash_bin[::-1].encode('hex_codec')
            #else:  block_hash_hex = hash_bin[::-1].encode('hex_codec')
            # 6. Finalize and serialize block object 
            job.finalize(merkle_root_int, extranonce1_bin, extranonce2_bin, int(ntime, 16), int(nonce, 16))
            
            if not job.is_valid():
                # Should not happen
                log.exception("FINAL JOB VALIDATION FAILED!(Try enabling/disabling tx messages)")
                            
            # 7. Submit block to the network
            serialized = binascii.hexlify(job.serialize())
	    on_submit = self.bitcoin_rpc.submitblock(serialized, block_hash_hex, scrypt_hash_hex)
            if on_submit:
                self.update_block()

           
        # 8. Compare hash with target of mm network
        if hash_int <= job.mm_target:
            log.info("We found a mm block candidate! %s" % scrypt_hash_hex)
            coinbase_hex = binascii.hexlify(coinbase_bin)
            branch_count = job.merkletree.branchCount()
            branch_hex = job.merkletree.branchHex()
            parent_hash = util.rev("%064x" % hash_int)
            parent_header = util.flip(header_hex)
            submission = coinbase_hex + parent_hash + branch_count + branch_hex + "000000000000000000" + parent_header;
            mm_submit = self.mm_rpc.getauxblock(self.mm_hash,submission)

            log.debug("Coinbase:%s",coinbase_hex)
            log.debug("Branch Count:%s",branch_count)
            log.debug("Branch Hex:%s",branch_hex)
            log.debug("Parent Hash:%s",parent_hash)
            log.debug("Parent Header:%s",parent_header)
            log.debug("MM Hash:%s",self.mm_hash)
            log.debug(" AuxPow:%s",submission)
            log.debug("    Res:"+str(mm_submit))

            
        if settings.SOLUTION_BLOCK_HASH:
            return (header_hex, block_hash_hex, self.mm_hash, share_diff, on_submit, mm_submit)
        else:
            return (header_hex, scrypt_hash_hex, self.mm_hash, share_diff, on_submit, mm_submit)
示例#9
0
def align_trivial(reads,
                  genome):  # takes a list with two reads, and a ref genome
    reference = genome.getSequence()[0]  # for now takes only first chromosome
    read1_fw = reads[0].sequence
    read1_rv = u.rev(read1_fw)
    read2_fw = reads[1].sequence
    read2_rv = u.rev(read2_fw)
    all_alng = {
        0: [],
        1: []
    }  # stores all valid alignments for each end of the read
    best_aln = reads  # contains alignments to report
    d = 0  # max number of mismatches allowed when aligning

    # iterate only once over regions of the genome of the size of the reads
    for i in range(len(reference) - len(read1_fw) + 1):
        ref = reference[i:(i + len(read1_fw))]

        # check end_1
        alignment = copy.deepcopy(reads[0])
        # forward
        diff = HammingDistance(read1_fw, ref, d)
        if diff != None:
            alignment.aligned = True
            alignment.position = i
            alignment.strand = "+"
            alignment.mismatch = diff if len(diff) > 0 else ["."]
            all_alng[0].append(alignment)
    # reverse
        else:
            diff = HammingDistance(read1_rv, ref, d)
            if diff != None:
                alignment.aligned = True
                alignment.position = i
                alignment.strand = "-"
                alignment.mismatch = diff if len(diff) > 0 else ["."]
                all_alng[0].append(alignment)

    # check end_2
        alignment = copy.deepcopy(reads[1])
        # forward
        diff = HammingDistance(read2_fw, ref, d)
        if diff != None:
            alignment.aligned = True
            alignment.position = i
            alignment.strand = "+"
            alignment.mismatch = diff if len(diff) > 0 else ["."]
            all_alng[1].append(alignment)
    # reverse
        else:
            diff = HammingDistance(read2_rv, ref, d)
            if diff != None:
                alignment.aligned = True
                alignment.position = i
                alignment.strand = "-"
                alignment.mismatch = diff if len(diff) > 0 else ["."]
                all_alng[1].append(alignment)

    # check if there were any valid alignments for end_1
    if len(all_alng[0]) > 0:
        # from all valid alignments, get the best for end_1
        best_aln[0] = all_alng[0][0]
        for j in range(len(all_alng[0])):
            if len(all_alng[0][j].mismatch) <= len(best_aln[0].mismatch):
                best_aln[0] = all_alng[0][j]
    # check if there were any valid alignments for end_2
    if len(all_alng[1]) > 0:
        # from all valid alignments, get the best for end_2
        best_aln[1] = all_alng[1][0]
        for j in range(len(all_alng[1])):
            if len(all_alng[1][j].mismatch) <= len(best_aln[1].mismatch):
                best_aln[1] = all_alng[1][j]

    # add to best alignments the information from the other end
    best_aln[0].pair_position = best_aln[1].position
    best_aln[0].pair_strand = best_aln[1].strand

    best_aln[1].pair_position = best_aln[0].position
    best_aln[1].pair_strand = best_aln[0].strand

    return best_aln
示例#10
0
    def submit_share(self, job_id, worker_name, session, extranonce1_bin,
                     extranonce2, ntime, nonce, difficulty):
        '''Check parameters and finalize block template. If it leads
           to valid block candidate, asynchronously submits the block
           back to the bitcoin network.
        
            - extranonce1_bin is binary. No checks performed, it should be from session data
            - job_id, extranonce2, ntime, nonce - in hex form sent by the client
            - difficulty - decimal number from session, again no checks performed
            - submitblock_callback - reference to method which receive result of submitblock()
        '''

        # Check if extranonce2 looks correctly. extranonce2 is in hex form...
        if len(extranonce2) != self.extranonce2_size * 2:
            raise SubmitException(
                "Incorrect size of extranonce2. Expected %d chars" %
                (self.extranonce2_size * 2))

        # Check for job
        job = self.get_job(job_id)
        if job == None:
            raise SubmitException("Job '%s' not found" % job_id)

        # Check if ntime looks correct
        if len(ntime) != 8:
            raise SubmitException("Incorrect size of ntime. Expected 8 chars")

        if not job.check_ntime(int(ntime, 16)):
            raise SubmitException("Ntime out of range")

        # Check nonce
        if len(nonce) != 8:
            raise SubmitException("Incorrect size of nonce. Expected 8 chars")

        # Check for duplicated submit
        if not job.register_submit(extranonce1_bin, extranonce2, ntime, nonce):
            log.info("Duplicate from %s, (%s %s %s %s)" % \
                    (worker_name, binascii.hexlify(extranonce1_bin), extranonce2, ntime, nonce))
            raise SubmitException("Duplicate share")

        # Now let's do the hard work!
        # ---------------------------

        # 0. Some sugar
        extranonce2_bin = binascii.unhexlify(extranonce2)
        ntime_bin = binascii.unhexlify(ntime)
        nonce_bin = binascii.unhexlify(nonce)

        # 1. Build coinbase
        coinbase_bin = job.serialize_coinbase(extranonce1_bin, extranonce2_bin)
        coinbase_hash = util.doublesha(coinbase_bin)

        # 2. Calculate merkle root
        merkle_root_bin = job.merkletree.withFirst(coinbase_hash)
        merkle_root_int = util.uint256_from_str(merkle_root_bin)

        # 3. Serialize header with given merkle, ntime and nonce
        header_bin = job.serialize_header(merkle_root_int, ntime_bin,
                                          nonce_bin)

        # 4. Reverse header and compare it with target of the user
        if settings.COINDAEMON_ALGO == 'scrypt':
            hash_bin = ltc_scrypt.getPoWHash(''.join(
                [header_bin[i * 4:i * 4 + 4][::-1] for i in range(0, 20)]))
        elif settings.COINDAEMON_ALGO == 'scrypt-jane':
            hash_bin = yac_scrypt.getPoWHash(
                ''.join(
                    [header_bin[i * 4:i * 4 + 4][::-1] for i in range(0, 20)]),
                int(ntime, 16))
        elif settings.COINDAEMON_ALGO == 'quark':
            hash_bin = quark_hash.getPoWHash(''.join(
                [header_bin[i * 4:i * 4 + 4][::-1] for i in range(0, 20)]))
        elif settings.COINDAEMON_ALGO == 'skeinhash':
            hash_bin = skeinhash.skeinhash(''.join(
                [header_bin[i * 4:i * 4 + 4][::-1] for i in range(0, 20)]))
        else:
            hash_bin = util.doublesha(''.join(
                [header_bin[i * 4:i * 4 + 4][::-1] for i in range(0, 20)]))

        hash_int = util.uint256_from_str(hash_bin)
        scrypt_hash_hex = "%064x" % hash_int
        header_hex = binascii.hexlify(header_bin)
        if settings.COINDAEMON_ALGO == 'scrypt' or settings.COINDAEMON_ALGO == 'scrypt-jane':
            header_hex = header_hex + "000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000"
        elif settings.COINDAEMON_ALGO == 'quark':
            header_hex = header_hex + "000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000"
        else:
            pass

        target_user = self.diff_to_target(difficulty)
        if hash_int > target_user:
            raise SubmitException("Share is above target")

        # Mostly for debugging purposes
        target_info = self.diff_to_target(100000)
        if hash_int <= target_info:
            log.info("Yay, share with diff above 100000")

        # Algebra tells us the diff_to_target is the same as hash_to_diff
        share_diff = int(self.diff_to_target(hash_int))

        on_submit = None
        mm_submit = None

        if settings.SOLUTION_BLOCK_HASH:
            # Reverse the header and get the potential block hash (for scrypt only) only do this if we want to send in the block hash to the shares table
            block_hash_bin = util.doublesha(''.join(
                [header_bin[i * 4:i * 4 + 4][::-1] for i in range(0, 20)]))
            block_hash_hex = block_hash_bin[::-1].encode('hex_codec')

        # 5. Compare hash with target of the network
        if hash_int <= job.target:
            # Yay! It is block candidate!
            log.info("We found a block candidate! %s" % scrypt_hash_hex)

            # Reverse the header and get the potential block hash (for scrypt only)
            #if settings.COINDAEMON_ALGO == 'scrypt' or settings.COINDAEMON_ALGO == 'sha256d':
            #   if settings.COINDAEMON_Reward == 'POW':
            block_hash_bin = util.doublesha(''.join(
                [header_bin[i * 4:i * 4 + 4][::-1] for i in range(0, 20)]))
            block_hash_hex = block_hash_bin[::-1].encode('hex_codec')
            #else:   block_hash_hex = hash_bin[::-1].encode('hex_codec')
            #else:  block_hash_hex = hash_bin[::-1].encode('hex_codec')
            # 6. Finalize and serialize block object
            job.finalize(merkle_root_int, extranonce1_bin, extranonce2_bin,
                         int(ntime, 16), int(nonce, 16))

            if not job.is_valid():
                # Should not happen
                log.exception(
                    "FINAL JOB VALIDATION FAILED!(Try enabling/disabling tx messages)"
                )

            # 7. Submit block to the network
            serialized = binascii.hexlify(job.serialize())
            on_submit = self.bitcoin_rpc.submitblock(serialized,
                                                     block_hash_hex,
                                                     scrypt_hash_hex)
            if on_submit:
                self.update_block()

        # 8. Compare hash with target of mm network
        if hash_int <= job.mm_target:
            log.info("We found a mm block candidate! %s" % scrypt_hash_hex)
            coinbase_hex = binascii.hexlify(coinbase_bin)
            branch_count = job.merkletree.branchCount()
            branch_hex = job.merkletree.branchHex()
            parent_hash = util.rev("%064x" % hash_int)
            parent_header = util.flip(header_hex)
            submission = coinbase_hex + parent_hash + branch_count + branch_hex + "000000000000000000" + parent_header
            mm_submit = self.mm_rpc.getauxblock(self.mm_hash, submission)

            log.debug("Coinbase:%s", coinbase_hex)
            log.debug("Branch Count:%s", branch_count)
            log.debug("Branch Hex:%s", branch_hex)
            log.debug("Parent Hash:%s", parent_hash)
            log.debug("Parent Header:%s", parent_header)
            log.debug("MM Hash:%s", self.mm_hash)
            log.debug(" AuxPow:%s", submission)
            log.debug("    Res:" + str(mm_submit))

        if settings.SOLUTION_BLOCK_HASH:
            return (header_hex, block_hash_hex, self.mm_hash, share_diff,
                    on_submit, mm_submit)
        else:
            return (header_hex, scrypt_hash_hex, self.mm_hash, share_diff,
                    on_submit, mm_submit)
示例#11
0
def pileup(aligns, ref_genome):
    """take a file with aligned reads and extract variants."""
    print("Generating Pileup from file: ")
    print("   " + aligns)

    # open aligned reads file, and pileup file
    reads_f = open(aligns, 'r')
    pileup_f = open(aligns + ".plp", "w")

    # set variables
    genome = ref_genome.getSequence()[0]
    pile = [0] * len(genome)
    sym = {"A": 0, "C": 1, "G": 2, "T": 3}
    let = {0: "A", 1: "C", 2: "G", 3: "T"}
    donor = [list(pile), list(pile), list(pile), list(pile)]
    end = False  # flag for end of reads
    window = 1000  # size of the window to slide

    # write with the expected format
    pileup_f.write(">" + list(ref_genome.genome.keys())[0] + "\n")
    pileup_f.write(">SNP\n")

    start = 0
    reads = []
    # keep function running while there are reads in file
    while not end:

        # collect the information of all the reads inside the window
        for read in reads_f:
            read = read.strip().split('\t')
            seq, init, pos = read[0], int(read[3]), read[2]
            if pos == "-":
                seq = u.rev(seq)  # reverse if inverted
            reads.append((seq, init))  # save the read
            if init >= start + window:  # stop with the first that scapes
                break  #           the end of the window

        for read in reads:
            seq = read[0]  # retrieve the reads from array
            init = read[1]
            for i in range(len(seq)):  # position by position of each read
                donor[sym[seq[i]]][init + i] += 1  #  add to PILEUP!

        # call variants:
        depth = 0
        dom = 0
        final = min(len(genome), start + window)
        for j in range(start, final):  # for each position in the window ...
            don = genome[j]  # by default, the donor is = to reference
            for nt in range(4):  # for each nucleotide... (ACGT)
                depth += donor[nt][j]  # add to sequencing depth for position j
                if dom < donor[nt][j]:
                    dom = donor[nt][
                        j]  # save the max depth for any nucleotide in pos j
                    don = let[nt]  #  and the identity of that nucleotide
            if don != genome[j]:  # if donor and reference diverge...
                if depth > 2:  # QC: and donor has >2 reads
                    if float(dom) / float(
                            depth) > 0.6:  # QC: and freq(nt) >0.6
                        #print(genome[j],donor[0][j],donor[1][j],donor[2][j],donor[3][j],don,j)
                        pileup_f.write(genome[j] + "," + don + "," + str(j) +
                                       "\n")
            depth = 0
            dom = 0
        print(len(reads))
        # reset variables or finish outputing
        if len(genome) > start + window:
            start += window
            reads = []
        else:
            end = True

    pileup_f.close()
    return 1
示例#12
0
def plcp_bits(rate, octets):
    plcp_rate = util.rev(rate.encoding, 4)
    plcp = plcp_rate | (octets << 5)
    parity = (util.mul(plcp, 0x1FFFF) >> 16) & 1
    plcp |= parity << 17
    return util.shiftout(np.array([plcp]), 18)
示例#13
0
 data = sym[ofdm.format.dataSubcarriers]
 pilots = sym[ofdm.format.pilotSubcarriers] * next(pilotPolarity) * ofdm.format.pilotTemplate
 kalman_u = kalman_state.update(np.sum(pilots))
 data *= kalman_u
 pilots *= kalman_u
 if i==0: # signal
     signal_bits = data.real>0
     signal_bits = interleaver.interleave(signal_bits, ofdm.format.Nsc, 1, reverse=True)
     scrambled_plcp_estimate = code.decode(signal_bits*2-1, 18)
     plcp_estimate = scrambler.scramble(scrambled_plcp_estimate, int(ofdm.format.Nsc*.5), scramblerState=0)
     parity = (np.sum(plcp_estimate) & 1) == 0
     if not parity:
         return None, None, 0
     plcp_estimate = util.shiftin(plcp_estimate[:18], 18)[0]
     try:
         encoding_estimate = util.rev(plcp_estimate & 0xF, 4)
         rate_estimate = [r.encoding == encoding_estimate for r in wifi_802_11_rates].index(True)
     except ValueError:
         return None, None, 0
     r_est = wifi_802_11_rates[rate_estimate]
     Nbpsc, constellation_estimate = r_est.Nbpsc, r_est.constellation
     Ncbps, Nbps = r_est.Ncbps, r_est.Nbps
     length_octets = (plcp_estimate >> 5) & 0xFFF
     length_bits = length_octets * 8
     length_coded_bits = (length_bits+16+6)*2
     length_symbols = (length_coded_bits+Ncbps-1) // Ncbps
     signal_bits = code.encode(scrambled_plcp_estimate)
     dispersion = data - qam.bpsk.symbols[interleaver.interleave(signal_bits, ofdm.format.Nsc, 1)]
     dispersion = np.var(dispersion)
 else:
     if drawingCalls is not None:
示例#14
0
def pileup(aligns, ref_genome):
    """take a file with aligned reads and extract variants."""
    print("Generating Pileup from file: ")
    print("   " + aligns)

    # open aligned reads file, and pileup file
    reads_f = open(aligns, 'r')

    # set variables
    genome = ref_genome.getSequence()[0]
    snps = {}
    dels = {}  # format: key=position, vals=(ref, (alt,) support)
    insr = {}

    # collect the information of all the reads inside the window
    for read in reads_f:
        read = read.strip().split('\t')
        seq, init, pos = read[0], int(read[3]), read[2]
        if pos == "-":
            seq = u.rev(seq)  # reverse if inverted

        # get differences if any:
        # SNPs
        if read[4] != ".":
            for i in map(int, read[4].split(",")):
                # add support if it exists (position and allele)
                if (init + i) in snps:
                    if seq[i] == snps[init + i][1]:
                        # pos_on_ref      ref_allele  alt_allele         support +1
                        snps[init + i] = (genome[init + i], seq[i],
                                          (snps[init + i][2] + 1))
                else:  # or create the event if it doesn't
                    snps[init + i] = (genome[init + i], seq[i], 1)

        # deletions
        if read[5] != ".":
            pos_d = list(map(int, read[5].split(",")))
            ale_d = list(map(int, read[6].split(",")))
            for i in range(len(pos_d)):
                pi_del = init + pos_d[i] + 1
                pf_del = init + pos_d[i] + 1 + ale_d[i]
                # add support if it exists (position and allele)
                if pi_del in dels:
                    if dels[pi_del][0] == genome[pi_del:pf_del]:
                        dels[pi_del][1] += 1
                else:  # or create the event if it doesn't
                    dels[pi_del] = [genome[pi_del:pf_del], 1]

        # insertions
        if read[7] != ".":
            pos_i = list(map(int, read[7].split(",")))
            ale_i = list(map(int, read[8].split(",")))
            for i in range(len(pos_i)):
                pi_ins = pos_i[i] + 1
                pf_ins = pos_i[i] + 1 + ale_i[i]
                p_geno = pos_i[i] + 1 + init
                # add support if it exists (position and allele)
                if p_geno in insr:
                    if insr[p_geno][0] == seq[pi_ins:pf_ins]:
                        insr[p_geno][1] += 1
                else:  # or create the event if it doesn't
                    insr[p_geno] = [seq[pi_ins:pf_ins], 1]

        # write with the expected format
    pileup_f = open(aligns + ".plp", "w")
    pileup_f.write(">" + list(ref_genome.genome.keys())[0] + "\n")

    pileup_f.write(">INS\n")
    insr_pos = list(insr.keys())
    insr_pos.sort()
    for j in insr_pos:
        if insr[j][1] > 2:
            pileup_f.write(insr[j][0] + "," + str(j) + "\n")

    pileup_f.write(">DEL\n")
    dels_pos = list(dels.keys())
    dels_pos.sort()
    for j in dels_pos:
        if dels[j][1] > 2:
            pileup_f.write(dels[j][0] + "," + str(j) + "\n")

    pileup_f.write(">SNP\n")
    snps_pos = list(snps.keys())
    snps_pos.sort()
    for j in snps_pos:
        if snps[j][2] > 3:
            pileup_f.write(snps[j][0] + "," + snps[j][1] + "," + str(j) + "\n")

    pileup_f.close()

    return 1
示例#15
0
    def submit_share(self, job_id, worker_name, session, extranonce1_bin,
                     extranonce2, ntime, nonce, difficulty):

        # Check for job
        job = self.get_job(job_id)
        if job == None:
            raise SubmitException("Job '%s' not found" % job_id)

        nonce = util.rev(nonce)
        ntime = util.rev(ntime)
        extranonce2_bin = binascii.unhexlify(extranonce2)
        ntime_bin = binascii.unhexlify(ntime)
        nonce_bin = binascii.unhexlify(nonce)

        # Check if extranonce2 looks correctly. extranonce2 is in hex form...
        if len(extranonce2) != self.extranonce2_size * 2:
            raise SubmitException(
                "Incorrect size of extranonce2. Expected %d chars" %
                (self.extranonce2_size * 2))

        # Check if ntime looks correct
        if len(ntime) != 8:
            raise SubmitException("Incorrect size of ntime. Expected 8 chars")

        if not job.check_ntime(int(ntime, 16)):
            raise SubmitException("Ntime out of range")

        # Check nonce
        if len(nonce) != 8:
            raise SubmitException("Incorrect size of nonce. Expected 8 chars")

        # Check for duplicated submit
        if not job.register_submit(extranonce1_bin, extranonce2, ntime, nonce):
            log.info("Duplicate from %s, (%s %s %s %s)" % \
                    (worker_name, binascii.hexlify(extranonce1_bin), extranonce2, ntime, nonce))
            raise SubmitException("Duplicate share")

        # 1. Build coinbase
        coinbase_bin = job.serialize_coinbase(extranonce1_bin, extranonce2_bin)
        coinbase_hash = kshake320_hash.getHash320(coinbase_bin)

        # 2. Calculate merkle root
        merkle_root_bin = job.merkletree.withFirst(coinbase_hash)
        merkle_root_int = util.uint320_from_str(merkle_root_bin)

        # 3. Serialize header with given merkle, ntime and nonce
        header_bin = job.serialize_header(merkle_root_bin, int(ntime, 16),
                                          int(nonce, 16))

        header_hex = binascii.hexlify(header_bin)
        header_hex = header_hex + "0000000000000000"

        # 4. Reverse header and compare it with target of the user
        hash_bin = kshake320_hash.getPoWHash(header_bin)
        hash_int = util.uint320_from_str(hash_bin)
        hash_hex = "%080x" % hash_int
        block_hash_hex = hash_bin[::-1].encode('hex_codec')

        target_user = float(self.diff_to_target(difficulty))
        if hash_int > target_user:
            log.info("ABOVE TARGET!")
            raise SubmitException("Share above target")

        target_info = self.diff_to_target(50)
        if hash_int <= target_info:
            log.info("YAY, share with diff above 50")

        # Algebra tells us the diff_to_target is the same as hash_to_diff
        share_diff = float(self.diff_to_target(hash_int))

        if hash_int <= job.target:
            # Yay! It is block candidate!
            log.info("BLOCK CANDIDATE! %s" % block_hash_hex)

            # Finalize and serialize block object
            job.finalize(merkle_root_int, extranonce1_bin, extranonce2_bin,
                         int(ntime, 16), int(nonce, 16))

            if not job.is_valid():
                # Should not happen
                log.info("FINAL JOB VALIDATION FAILED!")

            # Submit block to the network
            '''serialized = binascii.hexlify(job.serialize())
            on_submit = self.bitcoin_rpc.submitblock(str(serialized), block_hash_hex)'''

            job.vtx[0].set_extranonce(extranonce1_bin + extranonce2_bin)
            txs = binascii.hexlify(util.ser_vector(job.vtx))
            on_submit = self.bitcoin_rpc.submitblock_wtxs(
                str(header_hex), str(txs), block_hash_hex)
            '''if on_submit:
                self.update_block()'''

            return (block_hash_hex, share_diff, on_submit)

        return (block_hash_hex, share_diff, None)