#输入命令python bot.py 来源文件夹路径 输出文件夹路径

import re
import sys
import os

pattern1=re.compile(r">(.*?)@(.*)")
pattern2=re.compile(r"-+")
pattern3=re.compile(r"[A-Za-z]+")


def getOrganizdDS(lis):
    dictDS={}
    length=len(lis)
    for i in range(length):
        m=pattern1.match(lis[i])
        if m!=None:
            if m.group(1) in dictDS.keys():
                if i+1<length and lis[i+1][0]!=">":
                    dictDS[m.group(1)].append([m.group(2),lis[i+1]])
                else:
                    dictDS[m.group(1)].append([m.group(2),""])
            else:
                if i+1<length and lis[i+1][0]!=">":
                    dictDS[m.group(1)]=[[m.group(2),lis[i+1]]]
                else:
                    dictDS[m.group(1)]=[[m.group(2),""]]
    return dictDS

def getMinIndex(lis,lis2):
    if len(lis)==0:
        return None
    else:
        final=[]
        minlist=[]
        while True:
            k=0
            while lis[k] in minlist:
                k+=1
                if k==len(lis):
                    return final
            min=lis[k]
            index=k
            indexlis=[]
            for i in range(len(lis)):
                if lis[i] not in minlist:
                    if lis[i]<min:
                        min=lis[i]
                        indexlis=[i]
                        continue
                    if lis[i]==min:
                        done=0
                        for s in range(len(indexlis)):
                            if lis2[i]<lis2[indexlis[s]]:
                                indexlis.insert(s,i)
                                done=1
                                break
                        if done==0:
                            indexlis.append(i)
                        continue
                    else:
                        continue
            minlist.append(min)
            final.append(indexlis)


def ranking(species, dictDS):
    lis=dictDS[species]
    count1=[]
    count2=[]
    res1=[]
    res2=[]
    for i in lis:
        lis1=pattern2.findall(i[1])
        lis2=pattern3.findall(i[1])
        sum=0
        for j in lis1:
            sum+=len(j)
        res1.append(sum)
        res2.append(len(lis2))
    indexlis=getMinIndex(res1,res2)
    changeele=[]
    for i in indexlis:
        for j in i:
            changeele.append(lis[j])
    dictDS[species]=changeele

def writeRes(dictDS,filenameRes):
    f = open(filenameRes,"w")
    for i in dictDS.keys():
        f.write(">"+i+"\n")
        f.write(dictDS[i])
    f.close()

def merge(dictDS):
    resDict={}
    for species in dictDS.keys():
        newstr=""
        length=len(dictDS[species])
        first=list(dictDS[species][0][1][:-1])
        for i in range(len(first)):
            for j in range(length):
                lis=list(dictDS[species][j][1][:-1])
                if lis[i]!="-":
                    newstr+=lis[i]
                    break
                else:
                    if j==length-1:
                        newstr+="-"
        newstr+="\n"
        if length==1:
            resDict[species+"@"+dictDS[species][0][0]]=newstr
            continue
        resDict[species+"@new"]=newstr
    return resDict

path1,path2 = sys.argv[1:3]

files= os.listdir(path1)
for filename in files:
    filepath=path1+"/"+filename
    filenameRes=path2+"/"+filename
    with open(filepath) as ds:
        dslist=ds.readlines()
    DS=getOrganizdDS(dslist)
    for species in DS.keys():
        ranking(species,DS)
    DS=merge(DS)
    writeRes(DS,filenameRes)



