How does legumes tell helpful and harmful invading bacteria apart

Please fix the punctuation.
How does legumes tell helpful and harmful invading bacteria apart?