def compute_allele_frequencies(self, using=None):
        "Computes the allele frequencies across all samples in this cohort."
        if using is None:
            using = router.db_for_write(self.__class__)
        cursor = connections[using].cursor()

        with transition(self, 'Recomputed Allele Frequencies'):
            with transaction.commit_manually(using):
                # TODO update to use model._meta.db_table..
                try:
                    # Update count on cohort instance
                    cursor.execute('''
                        UPDATE "cohort" SET "count" = (
                            SELECT COUNT(id)
                            FROM "cohort_sample"
                            WHERE "cohort_id" = "cohort"."id"
                        ) WHERE "id" = %s
                    ''', [self.id])

                    # Calculate frequencies for all variants associated with
                    # all samples in this cohort
                    cursor.execute('''
                        INSERT INTO cohort_variant (cohort_id, variant_id, af)
                        (
                            SELECT c.id, r.variant_id,
                                COUNT(r.id) / c."count"::float
                            FROM sample_result r
                                INNER JOIN sample s ON (r.sample_id = s.id)
                                INNER JOIN cohort_sample cs ON
                                    (cs.sample_id = s.id)
                                INNER JOIN cohort c ON (cs.cohort_id = c.id)
                            WHERE c.id = %s
                            GROUP BY c.id, r.variant_id, c."count"
                        )
                    ''', [self.id])
                    transaction.commit()
                except DatabaseError, e:
                    transaction.rollback()
                    log.exception(e)
                    raise