def add_hydrogens(self, formal_charges=None):
new_edges = list(self.edges)<EOL>counter = self.num_vertices<EOL>for i in range(self.num_vertices):<EOL><INDENT>num_elec = self.numbers[i]<EOL>if formal_charges is not None:<EOL><INDENT>num_elec -= int(formal_charges[i])<EOL><DEDENT>if num_elec >= <NUM_LIT:5> and num_elec <= <NUM_LIT:9>:<EOL><INDENT>num_hydrogen = num_elec - <NUM_LIT:10> + <NUM_LIT:8><EOL><DEDENT>elif num_elec >= <NUM_LIT> and num_elec <= <NUM_LIT>:<EOL><INDENT>num_hydrogen = num_elec - <NUM_LIT> + <NUM_LIT:8><EOL><DEDENT>elif num_elec == <NUM_LIT>:<EOL><INDENT>num_hydrogen = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>continue<EOL><DEDENT>if num_hydrogen > <NUM_LIT:4>:<EOL><INDENT>num_hydrogen = <NUM_LIT:8> - num_hydrogen<EOL><DEDENT>for n in self.neighbors[i]:<EOL><INDENT>bo = self.orders[self.edge_index[frozenset([i, n])]]<EOL>if bo <= <NUM_LIT:0>:<EOL><INDENT>bo = <NUM_LIT:1><EOL><DEDENT>num_hydrogen -= int(bo)<EOL><DEDENT>for j in range(num_hydrogen):<EOL><INDENT>new_edges.append((i, counter))<EOL>counter += <NUM_LIT:1><EOL><DEDENT><DEDENT>new_numbers = np.zeros(counter, int)<EOL>new_numbers[:self.num_vertices] = self.numbers<EOL>new_numbers[self.num_vertices:] = <NUM_LIT:1><EOL>new_orders = np.zeros(len(new_edges), int)<EOL>new_orders[:self.num_edges] = self.orders<EOL>new_orders[self.num_edges:] = <NUM_LIT:1><EOL>result = MolecularGraph(new_edges, new_numbers, new_orders)<EOL>return result<EOL>
Returns a molecular graph where hydrogens are added explicitely

           When the bond order is unknown, it assumes bond order one. If the
           graph  has an attribute formal_charges, this routine will take it
           into account when counting the number of hydrogens to be added. The
           returned graph will also have a formal_charges attribute.

           This routine only adds hydrogen atoms for a limited set of atoms from
           the periodic system: B, C, N, O, F, Al, Si, P, S, Cl, Br.