##################################
#                                #
# Last modified 2017/09/11       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import string
import os

def run():

    if len(sys.argv) < 3:
        print 'usage: python %s hits_motifs all_motifs outfile' % sys.argv[0]
        print '\tNote: the script will only work properly with sorted wiggle files, i.e. all entries for each chromosome are conecutive'
        sys.exit(1)

    hit_motifs = sys.argv[1]
    all_motifs = int(sys.argv[2])
    outfilename = sys.argv[3]

    HitMotifDict = {}
    AllMotifDict = {}

    linelist = open(hit_motifs)
    for line in linelist:
        if line.startswith('#') or line.strip() == '':
            continue
        fields = line.strip().split('\t')
        chr = fields[0]
        if HitMotifDict.has_key(chr):
            pass
        else:
            HitMotifDict[chr] = {}
            HitMotifDict['+'] = []
            HitMotifDict['-'] = []
        left = int(fields[1])
        right = int(fields[2])
        strand = fields[3]
        HitMotifDictp[chr][strand].append((left,right))

    linelist = open(all_motifs)
    for line in linelist:
        if line.startswith('#') or line.strip() == '':
            continue
        fields = line.strip().split('\t')
        chr = fields[0]
        if AllMotifDict.has_key(chr):
            pass
        else:
            AllMotifDict[chr] = {}
            AllMotifDict['+'] = []
            AllMotifDict['-'] = []
        left = int(fields[1])
        right = int(fields[2])
        strand = fields[6]
        AllMotifDictp[chr][strand].append((left,right))

    for chr in AllMotifDict.keys():
        AllMotifDict['+'].sort()
        AllMotifDict['-'].sort()

    for chr in HitMotifDict.keys():
        HitMotifDict['+'].sort()
        HitMotifDict['-'].sort()

    outfile = open(outfilename,'w')
    
    outline = '#chr\tleft\tright\tstrand\tleft_same_strand_dist\tleft_opp_strand_dist\tright_same_strand_dist\tright_opp_strand_dist'
    outfile.write(outline + '\n')

    chromosomes = HitMotifDict.keys()
    chromosomes.sort()

    for chr in chromosomes:
        for (left,right) in HitMotifDict[chr]['+']:
            
        

    outfile.close()

run()

