Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logical operators #856

Closed
finswimmer opened this issue Aug 7, 2018 · 5 comments
Closed

Logical operators #856

finswimmer opened this issue Aug 7, 2018 · 5 comments

Comments

@finswimmer
Copy link

Hello,

the manual says these logical operators can be used in filtering expressions:

&& (same as &), ||, |

I read it like: "It doesn't matter if one use & or &&."

But in the later examples there seems to be a different behaviour:

FMT/DP>10  & FMT/GQ>10 .. both conditions must be satisfied within one sample
FMT/DP>10 && FMT/GQ>10 .. the conditions can be satisfied in different samples

Furthermore I don't understand the difference between | and ||. Can someone please give me an example and usecase for both to get a better understanding of this?

The manual says something about "select [...] samples". But which bcftools subcommand is able to select samples based on expressions?

I've posted this question yesterday on biostars. But until now there are just a few guesses about the differences. So I decided to ask directly on the source :)

fin swimmer

@pd3
Copy link
Member

pd3 commented Aug 15, 2018

Say your VCF contains the per-sample depth and genotype quality annotations and you want to include only sites where one or more samples have big enough coverage (DP>10) and genotype quality (GQ>20). The expression -i 'FMT/DP>10 & FMT/GQ>20' selects sites where the conditions are satisfied within the same sample:

$ bcftools query -i'FMT/DP>10 & FMT/GQ>20' -f'%POS[\t%SAMPLE:DP=%DP GQ=%GQ]\n' file.bcf
49979   SampleA:DP=10 GQ=50     SampleB:DP=20 GQ=40

On the other hand, if you need to include sites where both conditions met but not necessarily in the same sample, use the && operator rather than &:

$ bcftools query -i'FMT/DP>10 && FMT/GQ>20' -f'%POS[\t%SAMPLE:DP=%DP GQ=%GQ]\n' file.bcf
31771   SampleA:DP=10 GQ=50     SampleB:DP=40 GQ=20
49979   SampleA:DP=10 GQ=50     SampleB:DP=20 GQ=40

This example is taken from http://samtools.github.io/bcftools/howtos/filtering.html

@pd3 pd3 closed this as completed Aug 15, 2018
@finswimmer
Copy link
Author

Hello @pd3 ,

I somehow miss the notification about your response here. So please excuse my late response.

As I wrote on biostars unfortunately this is not an answer on the question what's the difference between | and || and when to use what. The example on the manual page is unclear to me.

So please let me aks you to reopen this question.

fin swimmer

@pd3 pd3 reopened this Aug 27, 2018
@pd3
Copy link
Member

pd3 commented Aug 27, 2018

Well, sorry to demonstrate the difference on & and && instead of | of ||, but it's the same priniciple.

The manual page says it all:

QUAL>10 |  FMT/GQ>10   .. true for sites with QUAL>10 or a sample with GQ>10, but selects only samples with GQ>10
QUAL>10 || FMT/GQ>10   .. true for sites with QUAL>10 or a sample with GQ>10, plus selects all samples at such sites

Or you can try to run yourself:

$ bcftools query -f'[%POS %SAMPLE %DP\n]\n' -i'FMT/DP=19 | FMT/DP="."' test/view.filter.vcf 
3162006 A 19

3162007 A .
3162007 B .

$ bcftools query -f'[%POS %SAMPLE %DP\n]\n' -i'FMT/DP=19 || FMT/DP="."' test/view.filter.vcf 
3162006 A 19
3162006 B 1

3162007 A .
3162007 B .

@finswimmer
Copy link
Author

Hey @pd3 ,

thanks for reopening and your additional informations. Your example showed it quite clear.

It would be very good if the manual will be expanded by such a practical example as you show.

Further more the part "&& (same as &)" should be edited, as it is not the same.

fin swimmer

PS: For the sake of completeness I will edit your answer on biostars and copy&paste the answer from here to there.

@pd3
Copy link
Member

pd3 commented Aug 29, 2018

Thank you. I'll update the documentation as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants