#!/usr/bin/env python
# coding: utf-8


import argparse
import re

def parse_gjf_file(file_path):
    location = []
    with open(file_path, 'r') as file:
        for line in file:
            if 'PDBName' not in line:
                continue
            split_contents = re.findall('[^(]+',line)
            if len(split_contents) == 2:
                if 0 < len(re.findall('[^-]+',split_contents[0])) < 3:
                    location.append('('+re.findall('[^\n]+',split_contents[1])[0])
    return location


def parse_resp_file(file_path):
    atomtypes = []
    with open(file_path, 'r') as file:
        for line in file:
            parts = re.findall('[^ ]+',line)
            atomtype = re.findall('[A-Za-z]+', parts[0])[0] + '-' + parts[1]
            atomtype = re.findall('[^\n]+',atomtype)[0]
            atomtypes.append(atomtype)
    
    return atomtypes

def main():
    parser = argparse.ArgumentParser(description='Process GJF and RESP files.')
    parser.add_argument('-g', metavar='gjf_file', type=str, help='Path to the GJF file')
    parser.add_argument('-r', metavar='resp_file', type=str, help='Path to the RESP file')
    args = parser.parse_args()

    gjf_location = parse_gjf_file(args.g)
    resp_atomtypes = parse_resp_file(args.r)

    if len(gjf_location)==len(resp_atomtypes):
        for i in range(len(resp_atomtypes)):
            output = ' ' + resp_atomtypes[i] + gjf_location[i]
            print(output)
    else:
        print('The number of atoms in resp file is not the same with that in gjf file. Please '
              'double check or contact with administer.')

if __name__ == '__main__':
    main()




