1

I have a mysql "problem" that I'm having difficulty wrapping my head around.

I have a table of strings from a database (actually genotypes, but shouldn't be relevant) that can exist in any one to three samples. I would like to count the number of unique alleles for each sample (s_id) for each catalog id (c_id). For instance given this table below:

id   batch_id  catalog_id   sample_id   tag_id      allele  depth
309     1       324             1         323         TCGC  244
1449616 1       324             2         7961        TCGC  192
2738325 1       324             2        1168472      CCGG  31
3521555 1       324             3        221716       TAAC  29

So far, I've been able to construct the following code:

CREATE TABLE danumbers2
SELECT catalog_id,
count(case when sample_id = '1' and allele != 'consensus' then sample_id end) as SAMPLE1,
count(case when sample_id = '2' and allele != 'consensus' then sample_id end) as SAMPLE2,
count(case when sample_id = '3' and allele != 'consensus' then sample_id end) as SAMPLE3,
sum(case when sample_id = '1' and allele != 'consensus' then depth end) as DEPTH1,
sum(case when sample_id = '2' and allele != 'consensus' then depth end) as DEPTH2,
sum(case when sample_id = '3' and allele != 'consensus' then depth end) as DEPTH3,
count(distinct allele) AS ALLELECOUNT

from matches as danumbers
group by catalog_id

CREATE TABLE thehitlist_all
SELECT catalog_id,SAMPLE1,SAMPLE2,SAMPLE3,DEPTH1,DEPTH2,DEPTH3,ALLELECOUNT
FROM danumbers
WHERE(SAMPLE1>1 SAMPLE2>1 AND SAMPLE3>1 AND ALLELECOUNT>1 AND DEPTH2>10 AND DEPTH3>10)

Which gives this result:

catalog_id  SAMPLE1 SAMPLE2 SAMPLE3 DEPTH1  DEPTH2  DEPTH3  ALLELECOUNT
324           1    2        1    244     223     29     4

The result is essentially a catalog_id sorted count of the total number of alleles in each sample, with a count of the total distinct alleles for each catalog id. What I'm interested in calculating (but can't seem to figure out!) is the "unique" alleles that are not shared between samples. In other words, to find diagnostic "alleles" for each sample at each catalog id.

So for the example above data above, I'd like the table to look as such:

catalog_id  SAMPLE1 SAMPLE2 SAMPLE3 ALLELECOUNT
324           0    1        1       2

Any thoughts would be greatly appreciated! Please let me know if I can provide any more information, etc.

1
  • perhaps a conditional statement nested in count distinct? Jul 30, 2012 at 18:33

2 Answers 2

2

You can simply add the other column name in the COUNT(DISTINCT...:

COUNT(DISTINCT s_id, allele) AS ALLELECOUNT

Which will count the unique combinations of s_id and allele together.

3
  • This seems to give the same result as before... for c_id 4, the ALLELECOUNT is still 22. What i'd like is a distinct count for each sample the number of non-shared elements of the distinct allele set. Jul 29, 2012 at 19:42
  • 1
    @jasongallant, that's because there's 22 distinct alleles in your sample data, so it appears to count all rows.
    – Zane Bien
    Jul 30, 2012 at 9:29
  • Ok, I see what you're saying @Zane Bien, and many thanks for the input. But this doesn't really solve the problem I'm working towards. Perhaps another (simpler) example will illustrate this better. I've edited the example above to reflect this more simply! Jul 30, 2012 at 17:47
0

This will give you the full records of those matches whose alleles are diagnostic within a catalog_id:

select good.*
from matches good
  left join matches dq on
    dq.catalog_id = good.catalog_id and
    dq.allele = good.allele and
    dq.sample_id != good.sample_id
where dq.catalog_id is null

From here, you should be able to dump to a temp table and summarize easily using techniques similar to what you've already illustrated. You may be able to skip the temp table and go straight to summary if you want to.

It will only filter out those rows where the allele is found for more than one sample per catalog. If the same allele is found for the same sample in the same catalog, then this will still return a row for it. If you want to choose those where the allele is only found for one RECORD per catalog (as opposed to one sample per catalog), then you would change the dq.sample_id != good.sample_id to dq.id != good.id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.