Hello all,

`bcftools` have these logical operators that can be used in filtering expressions:

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

What's the difference between `||` and `|`? Can someone provide an example and/or usecase for clarification?

The [manual][1] have this example:

    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

But this is not clear to me.

fin swimmer


  [1]: http://www.htslib.org/doc/bcftools.html#expressions
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

---
**EDIT: (inserted by a mod)**

*Answer given on [github][1]:*

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 .


  [1]: https://github.com/samtools/bcftools/issues/856#issuecomment-416140784


