##################################
#                                #
# Last modified 2026/01/16       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import string
import os
import random

def run():

    if len(sys.argv) < 2:
        print 'usage: python %s input_alignment outfile_prefix' % sys.argv[0]
        sys.exit(1)

    input = sys.argv[1]
    outprefix = sys.argv[2]

    print input, outprefix

    linesList = []

    B1HDict = {}

    lineslist = open(input)
    for line in lineslist:
        if line.startswith('CWM:'):
            CWMname = line.strip().split(']')[0].split('[')[1].replace('=','_')
        if line.startswith('B1H:'):
            B1Hname = line.strip().split(']')[0].split('[')[1]
            if B1HDict.has_key(B1Hname):
                pass
            else:
                B1HDict[B1Hname] = 1
                cmd = 'mkdir ' + outprefix + '/' + B1Hname
                cmd = cmd.replace('//','/')
                print cmd 
                p = os.popen(cmd, "r")
                p = os.popen(cmd, "r")
        linesList.append(line)
        if line.startswith('============='):
            outfilename = outprefix + '/' + B1Hname + '/' + CWMname + '-vs-' + B1Hname +  '.alignment'
            outfilename = outfilename.replace('//','/')
            outfile = open(outfilename, 'w')
            for LL in linesList:
                outfile.write(LL)

            linesList = []

            outfile.close()

run()

