Topological analysis reveals state transitions in human gut and marine bacterial communities