import pandas as pd
import argparse

parser = argparse.ArgumentParser(description='Convert eigenstrat pos file and pileup file chrms to numbers', usage="proj_path")
parser.add_argument('proj_path', type=str, help='path to project root name')

arg = parser.parse_args()

chrms = pd.read_csv(arg.proj_path + '.allChrmsInOrder.txt',names=['chr'])

chrms['newname'] = range(1,chrms.shape[0]+1)

chrms['newname'] = chrms['newname'].astype(str)

dct = chrms.set_index('chr').to_dict()

dct = dct['newname']

#rewrite the positions.snp file

out = open(arg.proj_path+'.numberedChr.snp','w')
with open(arg.proj_path+'.snp') as f:
    for line in f:
        fields = line.strip().split()
        fields[1] = dct[fields[1]]
        out.write('\t'.join(fields) + '\n')

#rewrite the pilup.txt file

out = open(arg.proj_path+'.numberedChr.txt','w')
with open(arg.proj_path+'.txt') as f:
   for line in f:
        fields = line.strip().split()
        fields[0] = dct[fields[0]]
        out.write('\t'.join(fields) + '\n')
