+ value = Math.round(mu - sensitivity/this.epsilon*Math.signum(u)*Math.log(1-2*Math.abs(u)));
+ }
+
+ return value;
+ }
+
+ public long addNoise2Count(long count, long cipherId, HashSet<Integer> degrees, GenomicQueryTypeType qt) throws I2B2Exception {
+ if (this.epsilon == 0.0)
+ return count;
+
+ ResultSet res = null;
+ try {
+ getNoisyCount.setLong(1, cipherId);
+ getNoisyCount.setString(5, qt.name());
+
+ for (Integer d : degrees) {
+ getNoisyCount.setInt(2, d);
+ res = getNoisyCount.executeQuery();
+
+ if (res.next()) {
+ long noisyCount = res.getLong(1);
+ res.close();
+ return noisyCount;
+ }
+
+ res.close();
+ }
+
+ return count+this.getNextLong(1);
+
+ } catch(SQLException e) {
+ e.printStackTrace();
+ log.debug("Fail to insert noisy count into db");
+ } finally {
+ try {
+ res.close();
+ } catch (SQLException e) {
+ // do nothing
+ }
+ }
+
+ return count+this.getNextLong(1);
+ }
+
+ public long addNoise2Result(long result, long cipherId, int degree, GenomicQueryTypeType qt, long delta, long modulus, Long count) throws I2B2Exception {
+ if (this.epsilon == 0.0)
+ return result;
+
+ ResultSet res = null;
+ try {
+ getNoisyResult.setLong(1, cipherId);
+ getNoisyResult.setInt(2, degree);
+
+ getNoisyResult.setString(5, qt.name());
+ res = getNoisyResult.executeQuery();
+ if (res.next())
+ return res.getLong(1);
+ else {
+ long noisyResult = result+this.getNextLong(1)*delta;
+ if (noisyResult >= modulus)
+ noisyResult -= modulus;
+ else if (noisyResult < 0)
+ noisyResult += modulus;
+
+ insertEntry.setLong(1, cipherId);
+ insertEntry.setInt(2, degree);
+
+ insertEntry.setString(5, qt.name());
+ if (count == null)
+ insertEntry.setNull(6, Types.BIGINT);
+ else
+ insertEntry.setLong(6, count.longValue());
+ insertEntry.setLong(7, noisyResult);
+ insertEntry.execute();
+
+ return noisyResult;
+ }
+ } catch(SQLException e) {
+ e.printStackTrace();
+ log.info("Fail to insert noisy result into db");
+ //public_key = new pk_t(*secret_key, *evaluation_key, plaintextModulus); // Need to implement new constructor which initialize the key except its value
+ res << "insert into gen_encrypted_genotypes (cipher_id, pid_gen, cipher_gt1, cipher_gt2, no_calls) values" << endl;
+ }else{
+ res << "insert into gen_encrypted_genotypes (cipher_id, pid_gen, cipher_gt1, cipher_gt2, cipher_gt3, cipher_gt4, cipher_gt5) values" << endl;
+ }
+
+ }
+
+ // parse variant information
+ for (size_t i=degree;i<n;i++) {
+ // read variant information
+ readBlock(container[0], &ifs); // chromosome
+ readBlock(container[1], &ifs); // position
+ readBlock(container[2], &ifs); // rs_number
+ readBlock(container[3], &ifs); // reference
+ readBlock(container[4], &ifs); // alternate
+
+ string select_query="",insert_query="";
+ if (*container[2] == '.') {
+ select_query="SELECT COUNT(*) FROM public.gen_variants WHERE chromosome=\'"+string(container[0])+"\' AND \"position\"="+string(container[1])+" AND rs_number=\'"+string(container[2])+"\' and reference=\'"+string(container[3])+"\' AND alternate=\'"+string(container[4])+"\';";
+ select_query="SELECT COUNT(*) FROM public.gen_variants WHERE chromosome=\'"+string(container[0])+"\' AND \"position\"="+string(container[1])+" AND rs_number=\'"+string(container[2])+"\' AND reference=\'"+string(container[3])+"\' AND alternate=\'"+string(container[4])+"\';";
+##INFO=<ID=AC,Number=A,Type=Integer,Description="Allele count in genotypes, for each ALT allele, in the same order as listed">
+##INFO=<ID=AF,Number=A,Type=Float,Description="Allele Frequency, for each ALT allele, in the same order as listed">
+##INFO=<ID=AN,Number=1,Type=Integer,Description="Total number of alleles in called genotypes">
+##INFO=<ID=ASP,Number=0,Type=Flag,Description="Is Assembly specific. This is set if the variant only maps to one assembly">
+##INFO=<ID=ASS,Number=0,Type=Flag,Description="In acceptor splice site FxnCode = 73">
+##INFO=<ID=BaseQRankSum,Number=1,Type=Float,Description="Z-score from Wilcoxon rank sum test of Alt Vs. Ref base qualities">
+##INFO=<ID=CDA,Number=0,Type=Flag,Description="Variation is interrogated in a clinical diagnostic assay">
+##INFO=<ID=CFL,Number=0,Type=Flag,Description="Has Assembly conflict. This is for weight 1 and 2 variant that maps to different chromosomes on different assemblies.">
+##INFO=<ID=CLN,Number=0,Type=Flag,Description="Variant is Clinical(LSDB,OMIM,TPA,Diagnostic)">
+##INFO=<ID=ClippingRankSum,Number=1,Type=Float,Description="Z-score From Wilcoxon rank sum test of Alt vs. Ref number of hard clipped bases">
+##INFO=<ID=END,Number=1,Type=Integer,Description="Stop position of the interval">
+##INFO=<ID=FS,Number=1,Type=Float,Description="Phred-scaled p-value using Fisher's exact test to detect strand bias">
+##INFO=<ID=G5,Number=0,Type=Flag,Description=">5% minor allele frequency in 1+ populations">
+##INFO=<ID=G5A,Number=0,Type=Flag,Description=">5% minor allele frequency in each and all populations">
+##INFO=<ID=GCF,Number=0,Type=Flag,Description="Has Genotype Conflict Same (rs, ind), different genotype. N/N is not included.">
+##INFO=<ID=GENEINFO,Number=1,Type=String,Description="Pairs each of gene symbol:gene id. The gene symbol and id are delimited by a colon (:) and each pair is delimited by a vertical bar (|)">
+##INFO=<ID=GMAF,Number=1,Type=Float,Description="Global Minor Allele Frequency [0, 0.5]; global population is 1000GenomesProject phase 1 genotype data from 629 individuals, released in the 08-04-2010 dataset">
+##INFO=<ID=GNO,Number=0,Type=Flag,Description="Genotypes available. The variant has individual genotype (in SubInd table).">
+##INFO=<ID=HD,Number=0,Type=Flag,Description="Marker is on high density genotyping kit (50K density or greater). The variant may have phenotype associations present in dbGaP.">
+##INFO=<ID=HaplotypeScore,Number=1,Type=Float,Description="Consistency of the site with at most two segregating haplotypes">
+##INFO=<ID=InbreedingCoeff,Number=1,Type=Float,Description="Inbreeding coefficient as estimated from the genotype likelihoods per-sample when compared against the Hardy-Weinberg expectation">
+##INFO=<ID=KGPROD,Number=0,Type=Flag,Description="1000 Genome production phase">
+##INFO=<ID=KGPilot123,Number=0,Type=Flag,Description="1000 Genome discovery all pilots 2010(1,2,3)">
+##INFO=<ID=KGVAL,Number=0,Type=Flag,Description="1000 Genome validated by second method">
+##INFO=<ID=LSD,Number=0,Type=Flag,Description="Submitted from a locus-specific database">
+##INFO=<ID=MLEAC,Number=A,Type=Integer,Description="Maximum likelihood expectation (MLE) for the allele counts (not necessarily the same as the AC), for each ALT allele, in the same order as listed">
+##INFO=<ID=MLEAF,Number=A,Type=Float,Description="Maximum likelihood expectation (MLE) for the allele frequency (not necessarily the same as the AF), for each ALT allele, in the same order as listed">
+##INFO=<ID=MUT,Number=0,Type=Flag,Description="Is mutation (journal citation, explicit fact): a low frequency variation that is cited in journal and other reputable sources">
+##INFO=<ID=NEGATIVE_TRAIN_SITE,Number=0,Type=Flag,Description="This variant was used to build the negative training set of bad variants">
+##INFO=<ID=NOC,Number=0,Type=Flag,Description="Contig allele not present in variant allele list. The reference sequence allele at the mapped position is not present in the variant allele list, adjusted for orientation.">
+##INFO=<ID=NOV,Number=0,Type=Flag,Description="Rs cluster has non-overlapping allele sets. True when rs set has more than 2 alleles from different submissions and these sets share no alleles in common.">
+##INFO=<ID=NS,Number=1,Type=Integer,Description="Number of Samples With Data">
+##INFO=<ID=NSF,Number=0,Type=Flag,Description="Has non-synonymous frameshift A coding region variation where one allele in the set changes all downstream amino acids. FxnClass = 44">
+##INFO=<ID=NSM,Number=0,Type=Flag,Description="Has non-synonymous missense A coding region variation where one allele in the set changes protein peptide. FxnClass = 42">
+##INFO=<ID=NSN,Number=0,Type=Flag,Description="Has non-synonymous nonsense A coding region variation where one allele in the set changes to STOP codon (TER). FxnClass = 41">
+##INFO=<ID=PM,Number=0,Type=Flag,Description="Variant is Precious(Clinical,Pubmed Cited)">
+##INFO=<ID=PMC,Number=0,Type=Flag,Description="Links exist to PubMed Central article">
+##INFO=<ID=POSITIVE_TRAIN_SITE,Number=0,Type=Flag,Description="This variant was used to build the positive training set of good variants">
+##INFO=<ID=QD,Number=1,Type=Float,Description="Variant Confidence/Quality by Depth">
+##INFO=<ID=R3,Number=0,Type=Flag,Description="In 3' gene region FxnCode = 13">
+##INFO=<ID=R5,Number=0,Type=Flag,Description="In 5' gene region FxnCode = 15">
+##INFO=<ID=REF,Number=0,Type=Flag,Description="Has reference A coding region variation where one allele in the set is identical to the reference sequence. FxnCode = 8">
+##INFO=<ID=RSPOS,Number=1,Type=Integer,Description="Chr position reported in dbSNP">
+##INFO=<ID=RV,Number=0,Type=Flag,Description="RS orientation is reversed">
+##INFO=<ID=ReadPosRankSum,Number=1,Type=Float,Description="Z-score from Wilcoxon rank sum test of Alt vs. Ref read position bias">
+##INFO=<ID=S3D,Number=0,Type=Flag,Description="Has 3D structure - SNP3D table">
+##INFO=<ID=SYN,Number=0,Type=Flag,Description="Has synonymous A coding region variation where one allele in the set does not change the encoded amino acid. FxnCode = 3">
+##INFO=<ID=TPA,Number=0,Type=Flag,Description="Provisional Third Party Annotation(TPA) (currently rs from PHARMGKB who will give phenotype data)">
+##INFO=<ID=U3,Number=0,Type=Flag,Description="In 3' UTR Location is in an untranslated region (UTR). FxnCode = 53">
+##INFO=<ID=U5,Number=0,Type=Flag,Description="In 5' UTR Location is in an untranslated region (UTR). FxnCode = 55">
+##INFO=<ID=VLD,Number=0,Type=Flag,Description="Is Validated. This bit is set if the variant has 2+ minor allele count based on frequency or genotype data.">
+##INFO=<ID=VQSLOD,Number=1,Type=Float,Description="Log odds ratio of being a true variant versus being false under the trained gaussian mixture model">
+##INFO=<ID=WTD,Number=0,Type=Flag,Description="Is Withdrawn by submitter If one member ss is withdrawn by submitter, then this bit is set. If all member ss' are withdrawn, then the rs is deleted to SNPHistory">
+##INFO=<ID=culprit,Number=1,Type=String,Description="The annotation which was the worst performing in the Gaussian mixture model, likely the reason why the variant was filtered out">
+##INFO=<ID=dbSNPBuildID,Number=1,Type=Integer,Description="First dbSNP Build for RS">
+##INFO=<ID=set,Number=1,Type=String,Description="Source VCF for the merged record in CombineVariants">
+ if (v['Result Type'].endsWith('FREQUENCY') || v['Result Type'].endsWith('AF')) {
+ if(denominator.length==0) {
+ var result = (i2b2.GEN.ctrlr.decryptOneDegree(qi.getElementsByTagName("encrypted_value")[0].textContent, ct[parseInt(qi.getElementsByTagName("degree")[0].textContent)], i2b2.GEN.model.partialkey, i2b2.GEN.model.polynomialDegree, i2b2.GEN.model.plaintextModulus, i2b2.GEN.model.coefficientModulus)/queryResultVariantInformation[i].count.textContent).toFixed(6);
+ v.Result = (result >= 0.0 && result <= 1.0) ? result : 0.0;
+ }else {
+ var result = i2b2.GEN.ctrlr.decryptOneDegree(qi.getElementsByTagName("encrypted_value_denominator")[0].textContent, denominator[parseInt(qi.getElementsByTagName("degree")[0].textContent)], i2b2.GEN.model.partialkey, i2b2.GEN.model.polynomialDegree, i2b2.GEN.model.plaintextModulus, i2b2.GEN.model.coefficientModulus);
+ result = (i2b2.GEN.ctrlr.decryptOneDegree(qi.getElementsByTagName("encrypted_value")[0].textContent, ct[parseInt(qi.getElementsByTagName("degree")[0].textContent)], i2b2.GEN.model.partialkey, i2b2.GEN.model.polynomialDegree, i2b2.GEN.model.plaintextModulus, i2b2.GEN.model.coefficientModulus)/result).toFixed(6);
+ v.Result = (result >= 0.0 && result <= 1.0) ? result : 0.0;
+ }
+ } else {
+ var result = i2b2.GEN.ctrlr.decryptOneDegree(qi.getElementsByTagName("encrypted_value")[0].textContent, ct[parseInt(qi.getElementsByTagName("degree")[0].textContent)], i2b2.GEN.model.partialkey, i2b2.GEN.model.polynomialDegree, i2b2.GEN.model.plaintextModulus, i2b2.GEN.model.coefficientModulus);
+ v.Result = (result >= 0 && result <= 0.9*i2b2.GEN.model.plaintextModulus) ? result : 0;