#!/usr/bin/python
import os, sys

def replace_by_word(origin_word, replace_word):
    nLines1 = []
    nLines2 = []
    f_name = file_name
    f_id = open(f_name, 'r')
    nLines1 = f_id.readlines()
    f_id.close()
    os.remove(f_name)
    nLen = len(nLines1)
    kk=0
    while kk < nLen:
        line = nLines1[kk]
        nLines2.append(line.replace(origin_word, replace_word))
        kk = kk+1
        #print line
    f_id = open(f_name, 'w')
    f_id.writelines(nLines2)
    f_id.close()


#==========================================================================
# start program
#==========================================================================

# check if argument is there
if len(sys.argv) == 3:
        new_chain = sys.argv[2]
        file_name = sys.argv[1]
        replace_by_word("X", new_chain)
        
else:
    message = """
ERROR: missing pdb filename as argument
usage: %s file.pdb""" %(sys.argv[0])
    sys.exit(message)
#==========================================================================

