import re


def extract_energies(pdb_file):
	""" Extract energies from the header of the PDB file, according to HADDOCK formatting """
	vdw = .0
	elec = .0
	desolv = .0
	air = .0
	bsa = .0

	vdw_elec_air_regex = r"\s(\-?\d*\.?\d{1,}|0\b)"  # Known error: 8.754077E-02 is matched as 8.754077 which is
	# not relevant for the I/O benchmark but should be taken into account

	desolv_regex = r"(\-?\d*\.?\d*)$"
	bsa_regex = r"(\-?\d*\.?\d*)$"

	f = open(pdb_file, 'r')
	for line in f:

		if 'REMARK energies' in line:
			# print(line)
			total, bonds, angles, improper, dihe, vdw, elec, air, cdih, coup, rdcs, vean, dani, xpcs, rg = re.findall(
				vdw_elec_air_regex, line)
			vdw = float(vdw)
			elec = float(elec)
			air = float(elec)

		if 'REMARK Desolvation' in line:
			# print(line)
			desolv = float(re.findall(desolv_regex, line)[0])

		if 'REMARK buried surface area' in line:
			# print(line)
			bsa = float(re.findall(bsa_regex, line)[0])
			break

	f.close()

	return vdw, elec, desolv, air, bsa