import sys
import gzip
import os
chro_order=['shit']
for i in range(1,24):
        chro_order.append('chr'+str(i))
chro_order.append('chrX')
fn=sys.argv[1]
dfh=open(fn,'r')
rfh=open(fn.split('/')[-1]+'_CpG_mod_temp.txt','w')
sample=fn.split('_')[1]
total=0
for i in dfh:
        line=i.split()
	pos=str((int(line[1])+int(line[2]))/2)
        if line[0] in chro_order:
                        if chro_order.index(line[0])>=10:
                                index=str(chro_order.index(line[0])*10000000000+int(line[1]))+'_'+str(chro_order.index(line[0])*10000000000+int(line[2]))
                        if chro_order.index(line[0])<10:
                                index='0'+str(chro_order.index(line[0])*10000000000+int(line[1]))+'_'+'0'+str(chro_order.index(line[0])*10000000000+int(line[2]))
                        rfh.write(index+'_'+'_'.join(line)+'\n')
rfh.close()
os.system('sort -k 1b,1 '+fn.split('/')[-1]+'_CpG_mod_temp.txt > '+fn.split('/')[-1]+'_mod.txt')
os.system('rm '+fn.split('/')[-1]+'_CpG_mod_temp.txt')
