#!/usr/bin/python

import sys
import os
import string
from xml.sax import make_parser
from xml.sax import ContentHandler

class alignxml( ContentHandler ):
    def __init__(self):
        self.tilesize = []
        self.alignments = dict()
        self.elementdata = ''
        self.elementstack = []
        self.tilea = None
        self.tileb = None
        self.attrs = None
        self.alns = []
    def startElement( self, name, attrs ):
#        print "starting element:", name, attrs
        self.elementstack.append( name )
        self.elementdata = ''
        self.attrs = attrs
        if( name == "alignment" ):
            self.alns = []
            self.tilea = None
            self.tileb = None
    def endElement( self, name ):
        self.elementdata = self.elementdata.strip()

        if( name == "rows" and self.elementstack[-2] == "image" ):
            self.tilesize = []
            b = self.elementdata.split()
            for i in b:
                self.tilesize.append( int(i) )
        elif( name == "tilea" and self.elementstack[-2] == "alignment"):
            self.tilea = self.elementdata.split()
        elif( name == "tileb" and self.elementstack[-2] == "alignment"):
            self.tileb = self.elementdata.split()
        elif( name == "transform" and self.elementstack[-2] == "alignment"):
            if( int(self.attrs['x']) > -10 and int(self.attrs['x']) < 10 and int(self.attrs['y']) > 20):
                self.alns.append( [ int(self.attrs['x']), int(self.attrs['y']), float(self.elementdata)] );
        elif( name == "alignment" ):
            self.alignments[ ( int(self.tilea[0]), int(self.tilea[1]), int(self.tileb[0]), int(self.tileb[1])) ] = self.alns
        el = self.elementstack.pop()
    def characters( self, ch ):
        self.elementdata = self.elementdata + ch
    def myprint(self):
        print self.tilesize
        for i in self.alignments:
            print (i[0], i[1], i[2], i[3]),
            for j in self.alignments[i]:
                print j,
            print ''
    def dotprint( self ):
        data = "digraph align{\nrankdir=LR\n"
        for i in range(0, len(self.tilesize) ):
            for j in range(0, self.tilesize[i]):
                data = data + '"N' + str(i) + "_" + str(j)
                data = data + '" [shape=box];\n'
        for i in self.alignments:
            if( len(self.alignments[i]) > 0 ):
                std = avg = 0
                for score in self.alignments[i]:
                    avg = avg + score[2]
                    avg = avg / (float( len(self.alignments[i]) ))
                for score in self.alignments[i]:
                    std = std + pow( avg-score[2], 2 )
                std = pow(std/( float( len(self.alignments[i]) ) ), 0.5 )
                for score in self.alignments[i]:
                    if( score[2] >= self.alignments[i][0][2]-(std) ):
                        data = data + "N" + str(i[0])+"_"+str(i[1]) + " -> "
                        data = data + "N" + str(i[2])+"_"+str(i[3])
                        data = data + "[ label=\"" + str(score[0]) + "," + str(score[1]) + ":" + str(score[2]) + "\"];\n"
        data = data + "}"
        return data
            

myfile = open( sys.argv[1] )

doch = alignxml()

myxml = make_parser()

myxml.setContentHandler( doch )

myxml.parse( myfile )

data = doch.dotprint( )
print data
