#! /usr/bin/env python

from sys import argv

d1 = dict()
d2 = dict()
s = 0
l1 = []
l2 = []
l = len(argv)
if argv[1] == "-h":
    print("Aligncomp - compare two different alignments of same sequences\n    ./aligncomp.py -h\n    ./aligncomp.py [OPTION]... FILE FILE\nAligncomp searches for columns that are identical in two alignments.\nReads FILE containing alignment in fasta format. With no additional \noptions given creates file 'res.txt' that contains numbers of found columns.\n\nOptions:\n   -p     shows percentage of columns that are presented in both alignments\n          for each of the alignments\n   -l     shows lenghts of the alignments\n   -b     shows coordinates of identical blocks. With -o shows only blocks\n          without gaps\n   -o     with -b shows coordinates of blocks that don't contain any gaps\n   NOTE: '-p' option should be stated before '-b' for correct results.")
else:
    a1 = str(argv[l-2])
    a2 = str(argv[l-1])

    ao1 = open(a1, mode = 'r')
    for i in ao1:
        if i.find(">") == 0:
            y = i.strip()
            d1[y] = ""
        elif i.find(">") != 0:
            d1[y] += str(i.strip())
    c = len(str(d1[y]))
    d1 = dict(sorted(d1.items()))
    for t in d1.keys():
        d1[t] = list(d1[t])
        num = 0
        for r in range(c):
            if d1[t][r] != "-":
                num += 1
                d1[t][r] = num
    ao2 = open(a2, mode = 'r')
    for j in ao2:
        if j.find(">") == 0:
            y = j.strip()
            d2[y] = ""
        elif j.find(">") != 0:
            d2[y] += str(j.strip())
    c2 = len(str(d2[y]))
    d2 = dict(sorted(d2.items()))
    for t in d2.keys():
        d2[t] = list(d2[t])
        num = 0
        for r in range(c2):
            if d2[t][r] != "-":
                num += 1
                d2[t][r] = num
    for x in range(c):
        u = []
        for w in d1.keys():
            u.append(d1[w][x])
        l1.append(u)
    for x in range(c2):
        u = []
        for w in d2.keys():
            u.append(d2[w][x])
        l2.append(u)
    ans = []
    for q in range(c):
        for h in range(c2): #check
            if l1[q] == l2[h]:
                ans.append((q + 1, h + 1))
                break
    with open("res.txt", mode='w') as out:
        for v in ans:
            print(v[0], end = ' ', file=out)
            print(v[1], file=out)
    for s in argv:
        if s == "-l":
            print(f"Lenght of {argv[l-2]}: {c}")
            print(f"Lenght of {argv[l-1]}: {c2}" )
        if s == "-p":
            pp1 = len(ans)/c*100
            pp2 = len(ans)/c2*100
            print(f"Percentage of columns in {argv[l-2]} identical to those of the second alignment: {pp1:.02f}%")
            print(f"Percentage of columns in {argv[l-1]} identical to those of the second alignment: {pp2:.02f}%")
        if s == "-b":
            for zero in argv:
                if zero == "-o":
                    de = []
                    for gap in ans:
                        if str(l1[gap[0]-1]).find("-") != -1:
                            de.append(gap)
                    for re in de:
                        ans.remove(re)
            da = len(ans)
            blocks1 = []
            blocks2 = []
            blocksta = ans[0]
            for e in range(da)[1:da-1]:
                if (((int(ans[e-1][0]) != int(ans[e][0]) - 1) and (int(ans[e-1][1]) != int(ans[e][1]) - 1)) and ((int(ans[e][0]) == int(ans[e+1][0]) - 1) and (int(ans[e][1]) == int(ans[e+1][1]) - 1))) or (((int(ans[e-1][0]) != int(ans[e][0]) - 1) and (int(ans[e-1][1]) != int(ans[e][1]) - 1)) and ((int(ans[e][0]) != int(ans[e+1][0]) - 1) and (int(ans[e][1]) != int(ans[e+1][1]) - 1))):
                    blockfin = ans[e-1]
                    blocks1.append((blocksta[0], blockfin[0]))
                    blocks2.append((blocksta[1], blockfin[1]))
                    blocksta = ans[e]
            blocks1.append((blocksta[0], ans[da-1][0]))
            blocks2.append((blocksta[1], ans[da-1][1]))
            print(argv[l-2], "blocks:")
            counter1 = 1
            counter2 = 1
            for n in blocks1:
                print(f"{counter1}: {n[0]} - {n[1]}")
                counter1 += 1
            print(argv[l-1], "blocks:")
            for n in blocks2:
                print(f"{counter2}: {n[0]} - {n[1]}")
                counter2 += 1
