示例#1
0
def fill(t, p):
    """Takes a flattened list of Atom objects and adds hydrogen atoms to them where appropriate.

    t: list of Atom objects
    p: Parser object"""
    res = copy.copy(t)

    for j in range(len(t)):
        atom = t[j]
        if isinstance(atom, Atom.Atom):
            num_h = 4 - len(
                atom.bonds
            )  #Since Atom objects cannot have more than 4 bonds, the only spaces that remain must belong to hydrogens.
            # print num_h
            if num_h > 0:

                for n in range(
                        num_h
                ):  #for every space that remains in atom.bonds, append a Hydrogen object to the bonds list.
                    hyd = Atom.Hydrogen('H' + str(p.i))
                    res.append(hyd)
                    final_atom = res[j]
                    final_atom.bonds.append(hyd)
                    p.i += 1
        else:
            fill(t[j], p)
    return res
示例#2
0
def fill_hydrogens(t):
    """Takes a flattened list of Atom objects (from the function flatten()) and fills in all the missing hydrogen Atoms such that each Atom object has four bonds.

    t: list of Atom objects
    Returns: list of Atom objects.
    """
    i = 0
    res = copy.copy(t)

    #for debugging/testing purposes -- can delete later! This variable will keep count of the number of hydrogens added at a given point in time.
    c = 0

    for j in range(len(t)):
        atom = t[j]
        num_h = 4 - len(
            atom.bonds
        )  #Since Atom objects cannot have more than 4 bonds, the only spaces that remain must belong to hydrogens.
        # print num_h
        if num_h > 0:

            #Again, these two lines are for debugging purposes only. Delete them if you do not want extra output.
            c += num_h
            print c

            for n in range(
                    num_h
            ):  #for every space that remains in atom.bonds, append a Hydrogen object to the bonds list.
                print('Adding a Hydrogen')
                hyd = Atom.Hydrogen('H' + str(i))
                res.append(hyd)
                final_atom = res[j]
                final_atom.bonds.append(hyd)
                i += 1
    return res