text: """
  Functions for manipulating pdb files.
  """
  
  import numpy as np
  
  from .polymod import get_connectivity
  
  
  def open_pdb(file_location):
      """This function reads in a pdb file and returns the atom names and coordinates.
  
      The pdb file must specify the atom elements in the last column, and follow
      the conventions outlined in the PDB format specification.
  
      """
  
      with open(file_location) as f:
          data = f.readlines()
  
      coordinates = []
      symbols = []
      bonds = []
      for line in data:
          if "ATOM" in line[0:6] or "HETATM" in line[0:6]:
              if line[76:79].strip():
                  raw_symbol = "".join(
                      [i for i in line[76:79].strip() if not i.isdigit()]
                  )
                  symbol = raw_symbol[0].upper() + raw_symbol[1:].lower()
                  symbols.append(symbol)
              elif line[12:17].strip():
                  raw_symbol = "".join(
                      [i for i in line[12:17].strip() if not i.isdigit()]
                  )
                  symbol = raw_symbol[0].upper() + raw_symbol[1:].lower()
                  symbols.append(symbol)
              atom_coords = [float(x) for x in line[30:55].split()]
              coordinates.append(atom_coords)
  
          elif "CONECT" in line[0:6]:
              atom1 = eval(line[6:11])
              for str_id in range(15, len(line), 5):
                  if line[str_id].isnumeric():
                      atom2 = eval(line[str_id - 4 : str_id + 1])  # noqa: E203
                      if ([atom1, atom2] not in bonds) and ([atom2, atom1] not in bonds):
                          bonds.append([atom1, atom2])
  
      coords = np.array(coordinates)
      symbols = np.array(symbols)
  
      bonds_file = open(".tmp/bonds.dat", "w")
      bonds_file.write("BONDS\n")
      bonds_file.write("\n")
      bond_id = 1
      for bond in bonds:
          atom1 = bond[0]
          atom2 = bond[1]
          bonds_file.write("%d 1 %d %d\n" % (bond_id, atom1, atom2))
          bond_id += 1
      bonds_file.close()
  
      return symbols, coords
  
  
  def write_pdb(
      file_location, symbols, coordinates, connect=True, connect_file=".tmp/bonds.dat"
  ):
      i_atom = 1
  
      RESIDUE_NAME = "UNK"
      CHAIN_ID = "A"
      RESIDUE_SEQ = 1
  
      # Write a pdb file given a file location, symbols, and coordinates.
      num_atoms = len(symbols)
  
      with open(file_location, "w+") as f:
          # f.write('{}\n'.format(num_atoms))
          # f.write('XYZ file\n')
          f.write("COMPND    %s\n" % file_location.split("/")[-1].split(".")[0])
          f.write("AUTHOR    GENERATED BY POLYMERXTAL\n")
  
          for i in range(num_atoms):
              pos = coordinates[i]
              #        1     7   13  18     23     31   39   47
              f.write(
                  "ATOM  %5d %4s %3s %1s%4d    %8.3f%8.3f%8.3f"
                  % (
                      i_atom,
                      symbols[i],
                      RESIDUE_NAME,
                      CHAIN_ID,
                      RESIDUE_SEQ,
                      pos[0],
                      pos[1],
                      pos[2],
                  )
                  + " " * 22
                  + "%2s\n" % symbols[i]
              )
              i_atom += 1
              if i_atom > 99999:
                  i_atom = 1
  
          if connect:
              connectivity = get_connectivity(connect_file)
              for main_atom in sorted(connectivity):
                  f.write(
                      "CONECT%5d%s\n"
                      % (main_atom, "".join(["%5d" % i for i in connectivity[main_atom]]))
                  )
  
              f.write(
                  "MASTER        0    0    0    0    0    0    0    0%5d    0%5d    0\n"
                  % (i_atom - 1, i_atom - 1)
              )
              f.write("END\n")
  
          else:
              #        1     7
              f.write("TER   %5d\n" % i_atom)
          # i_atom += 1
  