import math
class Atom:

    def dist_atoms(self, atom2):
        return(math.sqrt((self.coord_x-atom2.coord_x)**2 +
                         (self.coord_y-atom2.coord_y)**2 +
                         (self.coord_z-atom2.coord_z)**2))


    def __init__(self, atom_id, atom_name, res_name, chain_id,
                 res_seq_nb, insertion_code, coordinates):
        self.atom_id = atom_id
        self.atom_name = atom_name
        self.res_name = res_name
        self.chain_id = chain_id
        self.res_seq_nb = res_seq_nb
        self.insertion_code = insertion_code
        self.coord_x = coordinates[0]
        self.coord_y = coordinates[1]
        self.coord_z = coordinates[2]
        self.coords = coordinates

class Residue:
    def __init__(self, atoms_list):
        self.atoms = {}
        for atom in atoms_list:
            self.atoms[atom.atom_name] = atom
            self.resid = atom.res_seq_nb
            self.res_name = atom.res_name
            self.chain_id = atom.chain_id
            self.insertion_code = atom.insertion_code
            
            
    def h_bond(self, res2):
        if("H" not in res2.atoms.keys()):
            return(False)
        # elementary charge in Coulomb
        #e = 1.602176634 * 10*math.exp(-19)
        # dimensionnal factor f
        f = 332
        q1 = 0.42
        q2 = 0.20
        # r, distance between O-N atoms, in angströms
        r_ON = self.atoms["O"].dist_atoms(res2.atoms["N"])
        # r, distance between C-H atoms, in angströms
        r_CH = self.atoms["C"].dist_atoms(res2.atoms["H"])
        # r, distance between O-H atoms, in angströms
        r_OH = self.atoms["O"].dist_atoms(res2.atoms["H"])
        # r, distance between C-N atoms, in angströms
        r_CN = self.atoms["C"].dist_atoms(res2.atoms["N"])
        # Electrostatic interaction energy, in kcal/mole
        E = q1*q2*(1/r_ON + 1/r_CH - 1/r_OH - 1/r_CN)*f
        return(E)
