In [229]:
# convert two line binary data into binary file
from cStringIO import StringIO
import binascii
ifile = "/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/dna2_156"
fpbits=[]
with open(ifile,"r") as f:
        for l in f:
                fpbits.append(l)
                l=map(str.rstrip,l)
                m = ''.join(l)
                print len(m)
                print (m)
# strip newline character from fingerprint bits string
fpbits=map(str.rstrip,fpbits)
fp = ''.join(fpbits)
print (fp)
print len(fp)


156
010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110
156
110110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101111
010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110110110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101111
312

In [230]:
# create a function to read bits and write in binary file
def int2bin(fpbits):
    sio = StringIO(fpbits)
    outfile = '/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/testfp'
    f = open(outfile, 'wb')
    # when condition is true (1)
    while 1:
        # get the next 8 bits
        b = sio.read(8)
        # CHECK THE READING STRING
        #print ("stop")
        #print (b) 
        # check if hit EOF
        if not b:
            break

        # If less than 8 bits, pad with zeroes (0's) on the right
        if len(b) < 8:
            b = b + '0' * (8 - len(b))
            print (b)

        # Convert to integer (int)
        i = int(b, 2)
        #print (i)

        # Convert to character (chr)
        c = chr(i)
        #print (c)

        # Write the file
        f.write(c)
    f.close()

    #read file as binary data to check the input and output
    #outfile = '/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/testfp'
    binFile = open(outfile,'rb')
    binaryData = binFile.read(32)
    hexvalue = binascii.hexlify(binaryData)
    print hexvalue
    binary = binascii.a2b_hex(hexvalue)
    # print output file having padding bit and print input string
    print "".join("{:08b}".format(ord(i)) for i in binary)
    fpout = "".join("{:08b}".format(ord(i)) for i in binary)
    print (fpout)
    print (fpbits)
    # check string by matching
    # find padding and create original string by removal of padding
    print len(fpout),len(fpbits)
    padding = len(fpout) - len(fpbits)
    print padding
    if (padding == 0):
        fpout1 = fpout
    else:
        fpout1 = fpout[:-padding]
    print ("bit with padding", fpout1)
    # actual string
    print ("actual" , fpbits)
    print ("Is input and output string bits are equal?", "Ans:", (fpbits == fpout1))
    return f

In [ ]:


In [233]:
#ifile = "/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/dna2_156-0"
ifile = "/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/dna2_156"
# open file in read write mode of binary
fout = open('bigbin.bin','r+b')
fpbits=[]
with open(ifile,"r") as f:
    for l in f:
        fpbits.append(l)
        l=map(str.rstrip,l)
        m = ''.join(l)
        print len(m)
        print ("value" , m)
        fpfile = int2bin(m)
        #fout = fpfile.read()
        print("test file", fpfile, fpfile.name)
        fpfile1 = open (fpfile.name, 'rb')
        print("test file", fpfile1)
        fout.write(fpfile1.read())
        print (fout.read())
    fout.close()


156
('value', '010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110')
11100000
5b72db96dcb6e5b72db96dcb6e5b72db96dcb6e0
0101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100000
0101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100000
010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110
160 156
4
('bit with padding', '010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110')
('actual', '010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110')
('Is input and output string bits are equal?', 'Ans:', True)
('test file', <closed file '/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/testfp', mode 'wb' at 0x111518390>, '/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/testfp')
('test file', <open file '/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/testfp', mode 'rb' at 0x1115184b0>)

156
('value', '110110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101111')
11110000
db72db96dcb6e5b72db96dcb6e5b72db96dcb6f0
1101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011110000
1101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011110000
110110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101111
160 156
4
('bit with padding', '110110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101111')
('actual', '110110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101110010110110111001011011011100101101101111')
('Is input and output string bits are equal?', 'Ans:', True)
('test file', <closed file '/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/testfp', mode 'wb' at 0x111518420>, '/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/testfp')
('test file', <open file '/Users/rakesh/Research_Work/ipython_program/genetics/FP_ipython/testfp', mode 'rb' at 0x111518390>)


In [ ]:


In [ ]:


In [ ]:


In [ ]: