# convert e.txt file into R usable files

f = open("e.txt", "r")
lf= f.readlines()
f.close()

dPop = {}

cursign = ""
for l in lf:
    if "#" in l:
        cursign = tuple(l.split()[2:])
        dPop[cursign] = []
    else:
        lsi = l[:-1].split()
        if len(lsi) > 0:
            if cursign == "":
                print "problem with header detection"
            else:
                dPop[cursign] += [lsi]

print dPop

# create 1 file by trait, with 1 line per pop
# direction of number of pop vertically ? where is the south ?
for s in dPop:
    g = open("e_"+"_".join(s)+".txt", "w")
    y =0
    haut = len(dPop[s])
    for p in dPop[s]:
        larg = len(p)
        x=0
        for t in p:
            g.write(str(larg*(haut-y-1)+x)+"\t"+t+"\n")
            x += 1
        y += 1
    g.close()
