import collections as col

with open("FT.txt", "r") as FT:
    table = FT.read().split("\n")
with open("SD_weaker_result.tsv", "r") as sd_file:
    rows = sd_file.read().split("\n")[1 : -1]

CDS = col.defaultdict(int)

for row in rows:
    row = row.split("\t")
    repl = row[0]
    strand = row[1]
    start = row[4]
    end = row[5]
    prot = row[6]
    if row[8] == "1":
        CDS[(repl, strand, start, end, prot)] = 1
    else:
        continue


with open("SD_weaker_summary.tsv", "w") as sumfile:
    sumfile.write("Replicon\tStrand\tCDS start\tCDS end\tProtein\tSD\n")
    for s in table:
        if s[ : 3] == "CDS":
            info = s.split("\t")
            prot = str(int(info[1] == "with_protein"))
            chr = info[6]
            start = info[7]
            end = info[8]
            strand = info[9]
            line = "\t".join(map(str, (repl, strand, start, end, prot, CDS[(repl, strand, start, end, prot)])))
            sumfile.write(line + "\n")
        else:
            continue