def organizeControl(inputFilePath, outputFilePath, check_margin_size):
    """
        script for organizing control junction information
    """
    hIN = open(inputFilePath, 'r')
    hOUT = open(outputFilePath, 'w')
    mergedBedpeInfo = {}
    mergedreadNums = {}
    num = 1
    for line in hIN:
        F = line.rstrip('\n').split('\t')
        match = 0
        delList = []
        for key in sorted(mergedBedpeInfo):
            (tchr1, tstart1, tend1, tchr2, tstart2, tend2, tdir1, tdir2,
                inseqSize) = key.split('\t')
            tsamples, treadNums = mergedBedpeInfo[key].split('\t')
            if tend1 == '18606952':
                print(key)
            if F[0] != tchr1 or int(F[2]) > int(tend1) + check_margin_size:
                print('\t'.join([tchr1, tstart1, tend1, tchr2, tstart2,
                    tend2, 'controlJunction_' + str(num), inseqSize, tdir1,
                    tdir2, tsamples, treadNums]), file=hOUT)
                num = num + 1
                delList.append(key)
                continue
            elif F[0] == tchr1 and F[3] == tchr2 and F[8] == tdir1 and F[9
                ] == tdir2:
                flag = 0
                if F[8] == '+':
                    expectedDiffSize = int(F[2]) - int(tend1) + (int(F[7]) -
                        int(inseqSize))
                    if F[9] == '+' and int(F[5]) == int(tend2) - int(
                        expectedDiffSize) or F[9] == '-' and int(F[5]) == int(
                        tend2) + int(expectedDiffSize):
                        flag = 1
                else:
                    expectedDiffSize = int(F[2]) - int(tend1) + (int(
                        inseqSize) - int(F[7]))
                    if F[9] == '+' and int(F[5]) == int(tend2) + int(
                        expectedDiffSize) or F[9] == '-' and int(F[5]) == int(
                        tend2) - int(expectedDiffSize):
                        flag = 1
                if flag == 1:
                    match = 1
                    newKey = '\t'.join([F[0], F[1], F[2], F[3], F[4], F[5],
                        F[8], F[9], F[7]])
                    newSamples = tsamples + ';' + F[10]
                    newReadNums = treadNums + ';' + F[11]
                    if F[7] < inseqSize:
                        mergedBedpeInfo[newKey] = '\t'.join([newSamples,
                            newReadNums])
                        delList.append(key)
                    else:
                        mergedBedpeInfo[key] = '\t'.join([newSamples,
                            newReadNums])
        for item in delList:
            del mergedBedpeInfo[item]
        if match == 0:
            newKey = '\t'.join([F[0], F[1], F[2], F[3], F[4], F[5], F[8], F
                [9], F[7]])
            mergedBedpeInfo[newKey] = F[10] + '\t' + F[11]
    hIN.close()
    for key in sorted(mergedBedpeInfo):
        (tchr1, tstart1, tend1, tchr2, tstart2, tend2, tdir1, tdir2, inseqSize
            ) = key.split('\t')
        tsamples, treadNums = mergedBedpeInfo[key].split('\t')
        print('\t'.join([tchr1, tstart1, tend1, tchr2, tstart2, tend2, 
            'controlJunction_' + str(num), inseqSize, tdir1, tdir2,
            tsamples, treadNums]), file=hOUT)
        num = num + 1
    hOUT.close()
