├── .gitignore ├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── lib └── genotypeMatch.js ├── package.json └── test ├── and.js ├── atLeast.js ├── doesntExist.js ├── exact.js ├── exists.js ├── genotypeMatch.js ├── has.js ├── none.js ├── not.js ├── only.js └── or.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.log 3 | node_modules 4 | build 5 | *.node 6 | components 7 | dna.txt 8 | dna.json -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.log 3 | node_modules 4 | build 5 | *.node 6 | components 7 | dna.txt 8 | dna.json -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 0.10 4 | - 0.12 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Fractal 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/genomejs/gql.png?branch=master)](https://travis-ci.org/genomejs/gql) 2 | 3 | [![NPM version](https://badge.fury.io/js/gql.png)](http://badge.fury.io/js/gql) 4 | 5 | ## Information 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
Packagegql
DescriptionQuery language for interpreting genome SNPs
Node Version>= 0.4
20 | 21 | ## Compatibility 22 | 23 | This query language is for use with the genomejs JSON format. See the [dna2json](https://github.com/genomejs/dna2json) repository for more information. 24 | 25 | Just a reminder: deletions are represented as `-`! 26 | 27 | ## Usage 28 | 29 | This example will create a query that determines if a person is sickle cell effected. 30 | 31 | ```javascript 32 | var gql = require('gql'); 33 | var dna = require('./genome.json'); 34 | 35 | var query = gql.or([ 36 | gql.exact('rs334', 'TT'), 37 | gql.exact('i3003137', 'AA') 38 | ]); 39 | 40 | var isMatch = query(dna); 41 | console.log(isMatch); // true or false 42 | ``` 43 | 44 | 45 | ## API 46 | 47 | All of these APIs return a boolean-returning function that acts as the truth test. These functions take in one argument, a DNA-JSON object. You can nest as deep as you want, or be as basic as you want. 48 | 49 | For example, checking if a person is immune to norovirus is pretty simple: 50 | 51 | ```js 52 | var dna = require('./my-dna.json'); 53 | 54 | var query = gql.exact('rs601338', 'AA'); 55 | var isImmune = query(dna); 56 | ``` 57 | 58 | ### Conditions 59 | 60 | #### exact(id, genotype) 61 | 62 | Evaluates to true if this was the only allele observed. 63 | 64 | ```javascript 65 | q.exact('rs2032651', 'D'); // will only match genotype D 66 | q.exact('rs2032651', 'AT'); // will only match genotype AT 67 | ``` 68 | 69 | #### has(id, genotype) 70 | 71 | Evaluates to true if the allele was observed at all. 72 | 73 | ```javascript 74 | q.has('rs2032651', 'A'); // will match GA, GAT, AB, etc. 75 | ``` 76 | 77 | #### exists(id) 78 | 79 | Evaluates to true if any allele has been observed. 80 | 81 | ```javascript 82 | q.exists('rs2032651'); 83 | ``` 84 | 85 | #### doesntExist(id) 86 | 87 | Evaluates to true if no allele has been observed. 88 | 89 | ```javascript 90 | q.doesntExist('rs2032651'); 91 | ``` 92 | 93 | #### not(condition) 94 | 95 | Inverts the result of another condition. 96 | 97 | ```javascript 98 | // evaluates to true if rs2032651 != AA 99 | var query = q.not(q.exact('rs2032651', 'AA')); 100 | 101 | query({ 102 | rs2032651: { 103 | genotype: 'TT' 104 | } 105 | }); // true 106 | 107 | query({ 108 | rs2032651: { 109 | genotype: 'AA' 110 | } 111 | }); // false 112 | ``` 113 | 114 | #### only(id, allele) 115 | 116 | Evaluates to true if only the allele was observed at the specified position. 117 | 118 | ```javascript 119 | q.only('rs2032651', 'A'); // will match A, AA, AAA, etc. 120 | ``` 121 | 122 | ### Aggregate Conditions 123 | 124 | #### or(conditions...) 125 | 126 | Evaluates to true if any of the given condition functions evaluate to true. 127 | 128 | ```javascript 129 | q.or([ 130 | q.has('rs2032651', 'A'), 131 | q.doesntExist('rs2032651') 132 | ]); 133 | ``` 134 | 135 | #### and(conditions...) 136 | 137 | Evaluates to true if all of the given condition functions evaluate to true. 138 | 139 | ```javascript 140 | q.and([ 141 | q.exists('rs2032652'), 142 | q.has('rs2032651', 'A') 143 | ]); 144 | ``` 145 | 146 | #### atLeast(number, conditions...) 147 | 148 | Evaluates to true if the number of given condition functions evaluating to true is equal to or greater than the given number. 149 | 150 | ```javascript 151 | q.atLeast(2, [ 152 | q.exists('rs2032652'), 153 | q.has('rs2032651', 'A'), 154 | q.exact('rs2032653', 'AT'), 155 | q.exact('rs2032654', 'GG') 156 | ]); 157 | ``` 158 | 159 | #### none(conditions...) 160 | 161 | Evaluates to true if all of the given condition functions evaluate to false. 162 | 163 | ```javascript 164 | q.none([ 165 | q.has('rs2032651', 'A'), 166 | q.doesntExist('rs2032651') 167 | ]); 168 | ``` 169 | 170 | ## LICENSE 171 | 172 | (MIT License) 173 | 174 | Copyright (c) 2013 Fractal 175 | 176 | Permission is hereby granted, free of charge, to any person obtaining 177 | a copy of this software and associated documentation files (the 178 | 'Software'), to deal in the Software without restriction, including 179 | without limitation the rights to use, copy, modify, merge, publish, 180 | distribute, sublicense, and/or sell copies of the Software, and to 181 | permit persons to whom the Software is furnished to do so, subject to 182 | the following conditions: 183 | 184 | The above copyright notice and this permission notice shall be 185 | included in all copies or substantial portions of the Software. 186 | 187 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 188 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 189 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 190 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 191 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 192 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 193 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 194 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var roughMatch = require('./lib/genotypeMatch'); 2 | 3 | function callFn(data) { 4 | return function(fn) { 5 | return fn(data); 6 | }; 7 | } 8 | 9 | module.exports = logic = { 10 | // higher-level, operates on nested conditions 11 | and: function(conditions){ 12 | if (!Array.isArray(conditions) || conditions.length === 0) { 13 | throw new Error('and must receive an array with conditions'); 14 | } 15 | return function(data){ 16 | return conditions.every(callFn(data)); 17 | }; 18 | }, 19 | atLeast: function(num, conditions){ 20 | if (typeof num !== 'number') { 21 | throw new Error('atLeast must receive the number of conditions to be met'); 22 | } 23 | if (!Array.isArray(conditions) || conditions.length === 0) { 24 | throw new Error('atLeast must receive an array with conditions'); 25 | } 26 | return function(data){ 27 | var matches = conditions.filter(callFn(data)); 28 | return (matches.length >= num); 29 | }; 30 | }, 31 | only: function(k, v){ 32 | if (typeof v !== 'string') { 33 | throw new Error('only can only check for strings'); 34 | } 35 | if (v.length !== 1) { 36 | throw new Error('only accepts only one allele') 37 | } 38 | return function(data){ 39 | var snp = data[k]; 40 | if (!(snp && snp.genotype)) { 41 | return false; 42 | } 43 | return snp.genotype.split('').every(function(allele) { 44 | return allele === v; 45 | }); 46 | }; 47 | }, 48 | or: function(conditions){ 49 | if (!Array.isArray(conditions) || conditions.length === 0) { 50 | throw new Error('or must receive an array with conditions'); 51 | } 52 | return function(data){ 53 | return conditions.some(callFn(data)); 54 | }; 55 | }, 56 | not: function(condition){ 57 | if (typeof condition !== 'function') { 58 | throw new Error('not must receive a function'); 59 | } 60 | return function(data){ 61 | return !condition(data); 62 | }; 63 | }, 64 | 65 | // lower-level 66 | exists: function(k){ 67 | if (typeof k !== 'string') { 68 | throw new Error('exists must receive a key name'); 69 | } 70 | return function(data){ 71 | return data[k] != null && data[k].genotype != null; 72 | }; 73 | }, 74 | has: function(k, v){ 75 | if (typeof v !== 'string') { 76 | throw new Error('has can only check for strings'); 77 | } 78 | if (v.length !== 1) { 79 | throw new Error('has only accepts one allele, use exact instead') 80 | } 81 | return function(data){ 82 | var snp = data[k]; 83 | return !!(snp && snp.genotype && snp.genotype.indexOf(v) !== -1); 84 | }; 85 | }, 86 | exact: function(k, v){ 87 | if (typeof v !== 'string') { 88 | throw new Error('exact can only check for strings'); 89 | } 90 | return function(data){ 91 | var snp = data[k]; 92 | return !!(snp && snp.genotype && roughMatch(snp.genotype, v)); 93 | }; 94 | }, 95 | 96 | // sugar 97 | none: function(conditions){ 98 | if (!Array.isArray(conditions) || conditions.length === 0) { 99 | throw new Error('none must receive an array with conditions'); 100 | } 101 | return logic.not(logic.or(conditions)); 102 | }, 103 | doesntExist: function(k){ 104 | if (typeof k !== 'string') { 105 | throw new Error('doesntExist must receive a key name'); 106 | } 107 | return logic.not(logic.exists(k)); 108 | } 109 | }; 110 | -------------------------------------------------------------------------------- /lib/genotypeMatch.js: -------------------------------------------------------------------------------- 1 | module.exports = function(a, b){ 2 | if (a.length !== b.length) return false; // short exit if possible 3 | if (a === b) return true; // exact match 4 | if (a[0] === b[1] && a[1] === b[0]) return true; // reverse match 5 | return false; 6 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gql", 3 | "description": "Query language for interpreting genome SNPs", 4 | "version": "1.1.2", 5 | "homepage": "http://github.com/genomejs/gql", 6 | "repository": "git://github.com/genomejs/gql.git", 7 | "author": "Fractal (http://wearefractal.com/)", 8 | "main": "./index.js", 9 | "tags": [ 10 | "genomejs", 11 | "genoset", 12 | "gs144", 13 | "dna", 14 | "genetics", 15 | "genes", 16 | "query", 17 | "language" 18 | ], 19 | "devDependencies": { 20 | "mocha": "^2.2.1", 21 | "should": "^5.2.0" 22 | }, 23 | "scripts": { 24 | "test": "mocha" 25 | }, 26 | "engines": { 27 | "node": ">= 0.4.0" 28 | }, 29 | "licenses": [ 30 | { 31 | "type": "MIT", 32 | "url": "http://github.com/genomejs/gql/raw/master/LICENSE" 33 | } 34 | ] 35 | } 36 | -------------------------------------------------------------------------------- /test/and.js: -------------------------------------------------------------------------------- 1 | var gql = require('../'); 2 | var should = require('should'); 3 | require('mocha'); 4 | 5 | describe('and()', function() { 6 | it('should match with one true condition', function() { 7 | var truth = function(data) { 8 | should.exist(data); 9 | return true; 10 | }; 11 | var fn = gql.and([truth]); 12 | fn(123).should.equal(true); 13 | }); 14 | it('should match with two true conditions', function() { 15 | var truth = function(data) { 16 | should.exist(data); 17 | return true; 18 | }; 19 | var fn = gql.and([truth, truth]); 20 | fn(123).should.equal(true); 21 | }); 22 | it('should not match with one false condition', function() { 23 | var truth = function(data) { 24 | should.exist(data); 25 | return false; 26 | }; 27 | var fn = gql.and([truth]); 28 | fn(123).should.equal(false); 29 | }); 30 | it('should not match with one false and one true condition', function() { 31 | var truth = function(data) { 32 | should.exist(data); 33 | return true; 34 | }; 35 | var truth2 = function(data) { 36 | should.exist(data); 37 | return false; 38 | }; 39 | var fn = gql.and([truth, truth2]); 40 | fn(123).should.equal(false); 41 | }); 42 | }); 43 | -------------------------------------------------------------------------------- /test/atLeast.js: -------------------------------------------------------------------------------- 1 | var gql = require('../'); 2 | var should = require('should'); 3 | require('mocha'); 4 | 5 | describe('atLeast()', function() { 6 | it('should match with one true condition', function() { 7 | var truth = function(data) { 8 | should.exist(data); 9 | return true; 10 | }; 11 | var fn = gql.atLeast(1, [truth]); 12 | fn(123).should.equal(true); 13 | }); 14 | it('should match with two true conditions', function() { 15 | var truth = function(data) { 16 | should.exist(data); 17 | return true; 18 | }; 19 | var fn = gql.atLeast(2, [truth, truth]); 20 | fn(123).should.equal(true); 21 | }); 22 | it('should not match with one false condition', function() { 23 | var truth = function(data) { 24 | should.exist(data); 25 | return false; 26 | }; 27 | var fn = gql.atLeast(1, [truth]); 28 | fn(123).should.equal(false); 29 | }); 30 | it('should not match with two false conditions', function() { 31 | var truth = function(data) { 32 | should.exist(data); 33 | return false; 34 | }; 35 | var fn = gql.atLeast(1, [truth, truth]); 36 | fn(123).should.equal(false); 37 | }); 38 | it('should match with one false and one true condition', function() { 39 | var truth = function(data) { 40 | should.exist(data); 41 | return true; 42 | }; 43 | var truth2 = function(data) { 44 | should.exist(data); 45 | return false; 46 | }; 47 | var fn = gql.atLeast(1, [truth, truth2]); 48 | fn(123).should.equal(true); 49 | }); 50 | }); -------------------------------------------------------------------------------- /test/doesntExist.js: -------------------------------------------------------------------------------- 1 | var gql = require('../'); 2 | var should = require('should'); 3 | require('mocha'); 4 | 5 | describe('doesntExist()', function() { 6 | it('should not match with two alleles', function() { 7 | var data = { 8 | rs1234: { 9 | genotype: 'AA' 10 | } 11 | }; 12 | var fn = gql.doesntExist('rs1234'); 13 | fn(data).should.equal(false); 14 | }); 15 | it('should not match with one allele', function() { 16 | var data = { 17 | rs1234: { 18 | genotype: 'A' 19 | } 20 | }; 21 | var fn = gql.doesntExist('rs1234'); 22 | fn(data).should.equal(false); 23 | }); 24 | it('should match with no data', function() { 25 | var data = {}; 26 | var fn = gql.doesntExist('rs1234'); 27 | fn(data).should.equal(true); 28 | }); 29 | it('should match with no genotype', function() { 30 | var data = { 31 | rs1234: { 32 | genotype: null 33 | } 34 | }; 35 | var fn = gql.doesntExist('rs1234'); 36 | fn(data).should.equal(true); 37 | }); 38 | }); -------------------------------------------------------------------------------- /test/exact.js: -------------------------------------------------------------------------------- 1 | var gql = require('../'); 2 | var should = require('should'); 3 | require('mocha'); 4 | 5 | describe('exact()', function() { 6 | it('should match with two alleles', function() { 7 | var data = { 8 | rs1234: { 9 | genotype: 'AA' 10 | } 11 | }; 12 | var fn = gql.exact('rs1234', 'AA'); 13 | fn(data).should.equal(true); 14 | }); 15 | it('should match with one alleles', function() { 16 | var data = { 17 | rs1234: { 18 | genotype: 'A' 19 | } 20 | }; 21 | var fn = gql.exact('rs1234', 'A'); 22 | fn(data).should.equal(true); 23 | }); 24 | it('should not match with one allele when two given', function() { 25 | var data = { 26 | rs1234: { 27 | genotype: 'AA' 28 | } 29 | }; 30 | var fn = gql.exact('rs1234', 'A'); 31 | fn(data).should.equal(false); 32 | }); 33 | it('should not match with two alleles when one given', function() { 34 | var data = { 35 | rs1234: { 36 | genotype: 'A' 37 | } 38 | }; 39 | var fn = gql.exact('rs1234', 'AA'); 40 | fn(data).should.equal(false); 41 | }); 42 | }); 43 | -------------------------------------------------------------------------------- /test/exists.js: -------------------------------------------------------------------------------- 1 | var gql = require('../'); 2 | var should = require('should'); 3 | require('mocha'); 4 | 5 | describe('exists()', function() { 6 | it('should match with two alleles', function() { 7 | var data = { 8 | rs1234: { 9 | genotype: 'AA' 10 | } 11 | }; 12 | var fn = gql.exists('rs1234'); 13 | fn(data).should.equal(true); 14 | }); 15 | it('should match with one allele', function() { 16 | var data = { 17 | rs1234: { 18 | genotype: 'A' 19 | } 20 | }; 21 | var fn = gql.exists('rs1234'); 22 | fn(data).should.equal(true); 23 | }); 24 | it('should not match with no data', function() { 25 | var data = {}; 26 | var fn = gql.exists('rs1234'); 27 | fn(data).should.equal(false); 28 | }); 29 | it('should not match with no genotype', function() { 30 | var data = { 31 | rs1234: { 32 | genotype: null 33 | } 34 | }; 35 | var fn = gql.exists('rs1234'); 36 | fn(data).should.equal(false); 37 | }); 38 | }); -------------------------------------------------------------------------------- /test/genotypeMatch.js: -------------------------------------------------------------------------------- 1 | var genotypeMatch = require('../lib/genotypeMatch'); 2 | var should = require('should'); 3 | require('mocha'); 4 | 5 | describe('lib/genotypeMatch()', function() { 6 | it('should match straight forward with two alleles', function(done) { 7 | var a = 'AB'; 8 | var b = 'AB'; 9 | 10 | genotypeMatch(a,b).should.equal(true); 11 | done(); 12 | }); 13 | 14 | it('should match straight forward with one allele', function(done) { 15 | var a = 'A'; 16 | var b = 'A'; 17 | 18 | genotypeMatch(a,b).should.equal(true); 19 | done(); 20 | }); 21 | 22 | it('should not match straight forward with one allele', function(done) { 23 | var a = 'B'; 24 | var b = 'A'; 25 | 26 | genotypeMatch(a,b).should.equal(false); 27 | done(); 28 | }); 29 | 30 | it('should match reverse with two alleles', function(done) { 31 | var a = 'AB'; 32 | var b = 'BA'; 33 | 34 | genotypeMatch(a,b).should.equal(true); 35 | done(); 36 | }); 37 | 38 | it('should not match straight forward with two alleles', function(done) { 39 | var a = 'AB'; 40 | var b = 'GT'; 41 | 42 | genotypeMatch(a,b).should.equal(false); 43 | done(); 44 | }); 45 | }); -------------------------------------------------------------------------------- /test/has.js: -------------------------------------------------------------------------------- 1 | var gql = require('../'); 2 | var should = require('should'); 3 | require('mocha'); 4 | 5 | describe('has()', function() { 6 | it('should throw error with two alleles', function() { 7 | var data = { 8 | rs1234: { 9 | genotype: 'AA' 10 | } 11 | }; 12 | try { 13 | var fn = gql.has('rs1234', 'AA'); 14 | } catch(err) { 15 | should.exist(err); 16 | } 17 | }); 18 | it('should match with one alleles', function() { 19 | var data = { 20 | rs1234: { 21 | genotype: 'A' 22 | } 23 | }; 24 | var fn = gql.has('rs1234', 'A'); 25 | fn(data).should.equal(true); 26 | }); 27 | it('should match with one allele when two given', function() { 28 | var data = { 29 | rs1234: { 30 | genotype: 'AT' 31 | } 32 | }; 33 | var fn = gql.has('rs1234', 'T'); 34 | fn(data).should.equal(true); 35 | }); 36 | it('should not match with wrong allele', function() { 37 | var data = { 38 | rs1234: { 39 | genotype: 'A' 40 | } 41 | }; 42 | var fn = gql.has('rs1234', 'T'); 43 | fn(data).should.equal(false); 44 | }); 45 | }); -------------------------------------------------------------------------------- /test/none.js: -------------------------------------------------------------------------------- 1 | var gql = require('../'); 2 | var should = require('should'); 3 | require('mocha'); 4 | 5 | describe('none()', function() { 6 | it('should not match with one true condition', function() { 7 | var truth = function(data) { 8 | should.exist(data); 9 | return true; 10 | }; 11 | var fn = gql.none([truth]); 12 | fn(123).should.equal(false); 13 | }); 14 | it('should not match with two true conditions', function() { 15 | var truth = function(data) { 16 | should.exist(data); 17 | return true; 18 | }; 19 | var fn = gql.none([truth, truth]); 20 | fn(123).should.equal(false); 21 | }); 22 | it('should match with one false condition', function() { 23 | var truth = function(data) { 24 | should.exist(data); 25 | return false; 26 | }; 27 | var fn = gql.none([truth]); 28 | fn(123).should.equal(true); 29 | }); 30 | it('should not match with one false and one true condition', function() { 31 | var truth = function(data) { 32 | should.exist(data); 33 | return true; 34 | }; 35 | var truth2 = function(data) { 36 | should.exist(data); 37 | return false; 38 | }; 39 | var fn = gql.none([truth, truth2]); 40 | fn(123).should.equal(false); 41 | }); 42 | }); -------------------------------------------------------------------------------- /test/not.js: -------------------------------------------------------------------------------- 1 | var gql = require('../'); 2 | var should = require('should'); 3 | require('mocha'); 4 | 5 | describe('not()', function() { 6 | it('should invert a truthy function', function() { 7 | var fn = function(d) { 8 | should.exist(d); 9 | return true; 10 | }; 11 | var inverseFn = gql.not(fn); 12 | inverseFn(123).should.equal(false); 13 | }); 14 | it('should invert a falsey function', function() { 15 | var fn = function(d) { 16 | should.exist(d); 17 | return false; 18 | }; 19 | var inverseFn = gql.not(fn); 20 | inverseFn(123).should.equal(true); 21 | }); 22 | }); -------------------------------------------------------------------------------- /test/only.js: -------------------------------------------------------------------------------- 1 | var gql = require('../'); 2 | var should = require('should'); 3 | require('mocha'); 4 | 5 | describe('only()', function() { 6 | it('should throw error with two alleles', function() { 7 | var data = { 8 | rs1234: { 9 | genotype: 'AA' 10 | } 11 | }; 12 | try { 13 | var fn = gql.only('rs1234', 'AA'); 14 | } catch(err) { 15 | should.exist(err); 16 | } 17 | }); 18 | it('should match with one allele', function() { 19 | var data = { 20 | rs1234: { 21 | genotype: 'A' 22 | } 23 | }; 24 | var fn = gql.only('rs1234', 'A'); 25 | fn(data).should.equal(true); 26 | }); 27 | it('should match with two alleles', function() { 28 | var data = { 29 | rs1234: { 30 | genotype: 'AA' 31 | } 32 | }; 33 | var fn = gql.only('rs1234', 'A'); 34 | fn(data).should.equal(true); 35 | }); 36 | it('should match with three alleles', function() { 37 | var data = { 38 | rs1234: { 39 | genotype: 'AAA' 40 | } 41 | }; 42 | var fn = gql.only('rs1234', 'A'); 43 | fn(data).should.equal(true); 44 | }); 45 | it('should not match with wrong allele', function() { 46 | var data = { 47 | rs1234: { 48 | genotype: 'A' 49 | } 50 | }; 51 | var fn = gql.only('rs1234', 'T'); 52 | fn(data).should.equal(false); 53 | }); 54 | it('should not match with wrong allele', function() { 55 | var data = { 56 | rs1234: { 57 | genotype: 'AA' 58 | } 59 | }; 60 | var fn = gql.only('rs1234', 'T'); 61 | fn(data).should.equal(false); 62 | }); 63 | }); 64 | -------------------------------------------------------------------------------- /test/or.js: -------------------------------------------------------------------------------- 1 | var gql = require('../'); 2 | var should = require('should'); 3 | require('mocha'); 4 | 5 | describe('or()', function() { 6 | it('should match with one true condition', function() { 7 | var truth = function(data) { 8 | should.exist(data); 9 | return true; 10 | }; 11 | var fn = gql.or([truth]); 12 | fn(123).should.equal(true); 13 | }); 14 | it('should match with two true conditions', function() { 15 | var truth = function(data) { 16 | should.exist(data); 17 | return true; 18 | }; 19 | var fn = gql.or([truth, truth]); 20 | fn(123).should.equal(true); 21 | }); 22 | it('should not match with one false condition', function() { 23 | var truth = function(data) { 24 | should.exist(data); 25 | return false; 26 | }; 27 | var fn = gql.or([truth]); 28 | fn(123).should.equal(false); 29 | }); 30 | it('should not match with two false conditions', function() { 31 | var truth = function(data) { 32 | should.exist(data); 33 | return false; 34 | }; 35 | var fn = gql.or([truth, truth]); 36 | fn(123).should.equal(false); 37 | }); 38 | it('should match with one false and one true condition', function() { 39 | var truth = function(data) { 40 | should.exist(data); 41 | return true; 42 | }; 43 | var truth2 = function(data) { 44 | should.exist(data); 45 | return false; 46 | }; 47 | var fn = gql.or([truth, truth2]); 48 | fn(123).should.equal(true); 49 | }); 50 | }); --------------------------------------------------------------------------------