##################################
#                                #
# Last modified 05/01/2013       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import math
from sets import Set

def run():

    if len(sys.argv) < 6:
        print 'usage: python %s bedfilename chrFieldID leftID rightID chromInfo outputfilename' % sys.argv[0]
        sys.exit(1)
    
    bedfile = sys.argv[1]
    chrFieldID = int(sys.argv[2])
    leftFieldID = int(sys.argv[3])
    rightFieldID = int(sys.argv[4])
    chromInfo = sys.argv[5]
    outfilename = sys.argv[6]

    RegionDict = {}

    listoflines = open(bedfile)
    for line in listoflines:
        if line[0]=='#':
            continue
        fields=line.strip().split('\t')
        chr=fields[chrFieldID]
        left=int(fields[leftFieldID])
        right=int(fields[rightFieldID])
        if RegionDict.has_key(chr):
            pass
        else:
            RegionDict[chr] = []
        RegionDict[chr].append((left,right))
        
    BedLength = 0

    for chr in RegionDict.keys():
        bases = []
        for (left,right) in RegionDict[chr]:
            for i in range(left,right):
                bases.append(i)
        bases = list(Set(bases))
        BedLength += len(bases)

    GenomeLength = 0

    listoflines = open(chromInfo)
    for line in listoflines:
        if line[0]=='#':
            continue
        fields=line.strip().split('\t')
        length=int(fields[1])
        GenomeLength+=length

    outfile = open(outfilename, 'w')

    outline='Total Bed bp:' + '\t'+ str(BedLength)
    outfile.write(outline+'\n')
    outline='Total Genome bp:' + '\t'+ str(GenomeLength)
    outfile.write(outline+'\n')
    outline='Fraction:' + '\t'+ str(BedLength/(GenomeLength+0.0))
    outfile.write(outline+'\n')
    outfile.close()
   
run()
