BASF Corp. has agreed to buy all the outstanding shares of common stock of Verenium Corp., a biotechnology company based in San Diego.