├── LICENSES ├── LICENSE_MTJ.txt ├── LICENSE_jna.txt ├── LICENSE_appacheCommons.txt ├── LICENSE_jopt.txt ├── LICENSE_OpenBLAS.txt └── LICENSE_jsc.txt ├── lib └── fortranlibs │ └── Makefile └── src └── ch └── unil └── genescore ├── vegas ├── WeightedChisquareAlgorithm.java ├── GeneDataEqtlZscoreHolder.java ├── DataInconsistencyException.java ├── GeneDataInterface.java ├── OverlappedGenomicElementStream.java ├── test │ ├── FileExportStub.java │ ├── FarebrotherTest.java │ ├── GeneResultsNoScoreTest.java │ ├── GeneResultsSnpsOutOfBoundsTest.java │ ├── SnpTest.java │ ├── GeneWithItsSnpsTest.java │ ├── FileParserTest.java │ ├── OverlappedCollectionStreamTest.java │ ├── DistributionMethodsTest.java │ ├── OverlappedGenomicElementTest.java │ └── AnalyticVegasTest.java ├── MvnPackDirectMapping.java ├── MaxEffVegas.java ├── GeneScoreEvaluator.java ├── OverlappedGenomicElementFileStream.java ├── GeneResultsNoScore.java ├── BedFileStream.java ├── OverlappedCollectionStream.java ├── WrappedSnpPositionStream.java ├── MvnPack.java ├── StreamMethods.java ├── GeneResultsScore.java ├── GeneResultsSnpsOutOfBounds.java ├── SnpWeightPairs.java ├── GeneWithItsSnps.java ├── DistributionMethods.java ├── SnpSerializedPositionStream.java ├── MaxSimulAndAnalyticVegas.java ├── GeneData.java ├── SnpPositionStream.java ├── EmpiricalPvalue.java ├── LoadedGenotypes.java ├── ReferencePopulationFakeSignal.java ├── VegasSimulationNew.java ├── OverlappedGenomicElement.java └── WritingMethods.java ├── main ├── test │ ├── ParserStub.java │ └── AllTests.java ├── ConvenienceMethods.java ├── ProgressMonitor.java └── FileExport.java ├── pathway ├── test │ └── RankSumTestTest.java ├── MetaGene.java ├── Damper.java └── EffTestCalculator.java └── gene ├── GeneIdMapping.java ├── GeneAnnotationBed.java ├── Chromosome.java ├── GeneAnnotationUcsc.java ├── GeneAnnotationGencode.java └── Gene.java /LICENSES/LICENSE_MTJ.txt: -------------------------------------------------------------------------------- 1 | Matrix-Toolkits-Java 2 | Copyright (C) 2003-2006 Bjørn-Ove Heimsund 3 | Copyright (C) 2006-2014 Samuel Halliday 4 | 5 | LICENSE:BSD -------------------------------------------------------------------------------- /LICENSES/LICENSE_jna.txt: -------------------------------------------------------------------------------- 1 | This copy of JNA is licensed under the 2 | Apache (Software) License, version 2.0 ("the License"). 3 | See the License for details about distribution rights, and the 4 | specific rights regarding derivate works. 5 | 6 | You may obtain a copy of the License at: 7 | 8 | http://www.apache.org/licenses/ 9 | 10 | A copy is also included in the downloadable source code package 11 | containing JNA, in file "ASL2.0", under the same directory 12 | as this file. 13 | -------------------------------------------------------------------------------- /LICENSES/LICENSE_appacheCommons.txt: -------------------------------------------------------------------------------- 1 | Licensed under the Apache License, Version 2.0 (the "License"); 2 | you may not use this file except in compliance with the License. 3 | You may obtain a copy of the License at 4 | 5 | http://www.apache.org/licenses/LICENSE-2.0 6 | 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | -------------------------------------------------------------------------------- /lib/fortranlibs/Makefile: -------------------------------------------------------------------------------- 1 | CC=gfortran 2 | SOURCES=mvtdstpack.f 3 | OBJECTS=$(SOURCES:.f=.o) 4 | LDFLAGS=-Wall -shared 5 | CFLAGS=-Wall -fPIC -frecursive 6 | UNAME_S := $(shell uname) 7 | print_vars: 8 | echo $(UNAME_S) 9 | 10 | all: libmvtpack.dylib 11 | 12 | libmvtpack.dylib: $(OBJECTS) 13 | $(CC) $(LDFLAGS) $(OBJECTS) -o $@ 14 | 15 | libmvtpack.so: $(OBJECTS) 16 | $(CC) $(LDFLAGS) $(OBJECTS) -o $@ 17 | 18 | libmvnpack.dlyb: mvndstpack.o 19 | $(CC) $(LDFLAGS) mvndstpack.o -o $@ 20 | 21 | .f.o: 22 | $(CC) $(CFLAGS) -c $< 23 | 24 | clean: 25 | rm -f *.o libmvnpack.dylib libmvtpack.dylib libmvtpack.so 26 | 27 | -------------------------------------------------------------------------------- /LICENSES/LICENSE_jopt.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/WeightedChisquareAlgorithm.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | public interface WeightedChisquareAlgorithm { 25 | 26 | int getIfault(); 27 | double probQsupx(double q); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/GeneDataEqtlZscoreHolder.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | public interface GeneDataEqtlZscoreHolder extends GeneDataInterface { 25 | 26 | void setEqtlZscores(double[] zScores); 27 | double[] getEqtlZscores(); 28 | } 29 | -------------------------------------------------------------------------------- /LICENSES/LICENSE_OpenBLAS.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011-2014, The OpenBLAS Project 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are 6 | met: 7 | 8 | 1. Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | 2. Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in 13 | the documentation and/or other materials provided with the 14 | distribution. 15 | 3. Neither the name of the OpenBLAS project nor the names of 16 | its contributors may be used to endorse or promote products 17 | derived from this software without specific prior written 18 | permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 29 | USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/DataInconsistencyException.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | public class DataInconsistencyException extends Exception{ 25 | 26 | private static final long serialVersionUID = -7777302296961543088L; 27 | 28 | public DataInconsistencyException(String msg){ 29 | super(msg); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/GeneDataInterface.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import java.util.ArrayList; 25 | 26 | import no.uib.cipr.matrix.DenseMatrix; 27 | 28 | public interface GeneDataInterface { 29 | 30 | public DenseMatrix getCorr(); 31 | public ArrayList getScores(); 32 | public double[] getWeights(); 33 | public void processData(); 34 | } 35 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/OverlappedGenomicElementStream.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | //TODO: orderCheck throw error from getNextAsOverlappedGenomicElement(); 24 | public interface OverlappedGenomicElementStream { 25 | 26 | public OverlappedGenomicElement getNextAsOverlappedGenomicElement(); 27 | public boolean streamOpen(); 28 | public void reOpenStream(); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/test/FileExportStub.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas.test; 23 | 24 | import java.util.ArrayList; 25 | 26 | import ch.unil.genescore.main.FileExport; 27 | 28 | class FileExportStub extends FileExport { 29 | ArrayList myStrs = new ArrayList(); 30 | boolean isClosed=false; 31 | @Override 32 | public void setWriter(String filename){}; 33 | @Override 34 | public void println(String str){ 35 | myStrs.add(str); 36 | }; 37 | @Override 38 | public void close(){isClosed=true;}; 39 | public boolean isclosed(){return isClosed;} 40 | public ArrayList getStrings(){return(myStrs);} 41 | } -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/MvnPackDirectMapping.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import com.sun.jna.Native; 25 | //import com.sun.jna.NativeLibrary; 26 | import com.sun.jna.ptr.DoubleByReference; 27 | import com.sun.jna.ptr.IntByReference; 28 | 29 | public class MvnPackDirectMapping {//implements MvnPack { 30 | 31 | static { 32 | 33 | 34 | Native.register("mvtpack"); 35 | } 36 | 37 | public static native void mvtdst_(IntByReference n,IntByReference df, double[] lower, double[] upper, int[] infin, double[] correl, double[] delta, 38 | IntByReference maxpts, DoubleByReference abseps, DoubleByReference releps, 39 | DoubleByReference error, DoubleByReference value, IntByReference inform); 40 | 41 | 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/main/test/ParserStub.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.main.test; 23 | 24 | import java.util.ArrayList; 25 | 26 | import ch.unil.genescore.main.FileParser; 27 | 28 | public class ParserStub extends FileParser { 29 | 30 | static ArrayList TestData_ = null; 31 | int counter = 0; 32 | 33 | public ParserStub() { 34 | } 35 | 36 | public ParserStub(String filename) { 37 | super(); 38 | } 39 | 40 | public static void setTestData(ArrayList TestData) { 41 | TestData_ = TestData; 42 | } 43 | 44 | @Override 45 | public String[] readLine() { 46 | 47 | if (counter < TestData_.size()) { 48 | String[] out = TestData_.get(counter).split("\t", -1); 49 | counter++; 50 | return out; 51 | } else 52 | return null; 53 | } 54 | 55 | @Override 56 | public void close() { 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/MaxEffVegas.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import java.util.ArrayList; 25 | 26 | import ch.unil.genescore.main.Settings; 27 | import ch.unil.genescore.vegas.MaxVegas.Status; 28 | import no.uib.cipr.matrix.DenseMatrix; 29 | import no.uib.cipr.matrix.UpperSymmDenseMatrix; 30 | 31 | public class MaxEffVegas extends MaxVegas { 32 | 33 | public MaxEffVegas(){ 34 | super(); 35 | pruningCutoff_=Settings.maxPruningCutoff_; 36 | } 37 | public MaxEffVegas(ArrayList snpScores, UpperSymmDenseMatrix ld, double[] weights) { 38 | super(snpScores, ld, weights); 39 | } 40 | 41 | public MaxEffVegas(ArrayList snpScores, DenseMatrix ld, double pruningCutoff) { 42 | super(snpScores, ld, pruningCutoff); 43 | 44 | } 45 | 46 | 47 | @Override 48 | protected void calculateGenescore(){ 49 | geneScore_=computeEffectivePval(); 50 | status_=Status.EFF_NR_OF_TEST_ADJ; 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/GeneScoreEvaluator.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | /** 25 | * Interface for classes that can evaluate gene scores 26 | */ 27 | public abstract class GeneScoreEvaluator { 28 | 29 | 30 | /** Compute the gene score / p-value, return true if success */ 31 | public abstract boolean computeScore(); 32 | /** Get the score(s) */ 33 | public abstract double[] getScore(); 34 | 35 | /** Get a string representation of the results */ 36 | public abstract String getResultsAsString(); 37 | /** Get a header line corresponding to getResultsAsString() */ 38 | public abstract String getResultsAsStringHeader(); 39 | public abstract void setDataFromGeneData(GeneData Dat); 40 | 41 | 42 | /** Get output to be printed on console after computing score for a gene */ 43 | public String getConsoleOutput() { return ""; } 44 | 45 | /** Get output to be printed for genes where no score could be computed (error) */ 46 | public String getNoScoreOutput() { return ""; } 47 | /** Get a header line corresponding to getNoScoreOutput() */ 48 | public String getNoScoreOutputHeader() { return ""; } 49 | 50 | /** returns string defining evaluator type */ 51 | public String getTypeString() { return ""; } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/OverlappedGenomicElementFileStream.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | public abstract class OverlappedGenomicElementFileStream { 25 | 26 | OverlappedGenomicElement currentElement_ = null; 27 | boolean streamInitialized_ = false; 28 | boolean streamOpen_= false; 29 | 30 | /** updates currentElement_ 31 | * @throws */ 32 | abstract protected void loadElement(); 33 | 34 | public OverlappedGenomicElement getNextAsOverlappedGenomicElement(){ 35 | if (!streamOpen_) 36 | throw new RuntimeException("never use on closed stream"); 37 | if (!streamInitialized_){//initial load 38 | 39 | loadElement(); 40 | streamInitialized_=true; 41 | } 42 | //System.out.println(currentElement_.mainElement_.id_); 43 | OverlappedGenomicElement elementToBeReturned = currentElement_; 44 | loadElement(); 45 | 46 | if (currentElement_!=null){ 47 | // System.out.println(currentElement_.mainElement_.id_); 48 | if (currentElement_.compareTo(elementToBeReturned)<0){ 49 | System.out.println(currentElement_.mainElement_.id_); 50 | System.out.println(elementToBeReturned.mainElement_.id_); 51 | throw new RuntimeException("stream not sorted properly"); 52 | } 53 | } 54 | return elementToBeReturned; 55 | 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/test/FarebrotherTest.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas.test; 23 | import static org.junit.Assert.assertEquals; 24 | 25 | import org.junit.AfterClass; 26 | import org.junit.BeforeClass; 27 | import org.junit.Test; 28 | 29 | import ch.unil.genescore.main.Settings; 30 | import ch.unil.genescore.vegas.Farebrother; 31 | import ch.unil.genescore.vegas.FarebrotherExperiment; 32 | 33 | 34 | public class FarebrotherTest { 35 | 36 | 37 | @BeforeClass 38 | public static void testSetup() { 39 | Settings.loadSettings(); 40 | } 41 | 42 | @AfterClass 43 | public static void testCleanup() { } 44 | 45 | 46 | // ============================================================================ 47 | // TESTS 48 | 49 | /** Test test statistic */ 50 | @Test 51 | public void FarebrotherTest() { 52 | Settings.requestedAbsolutePrecision_=1e-25; 53 | double w=1e-17; 54 | System.out.println(w); 55 | double q=1-w; 56 | double t=1-q; 57 | t=w/10.0; 58 | System.out.println(t); 59 | double[] lambda={4,3,2,1}; 60 | 61 | Farebrother myfarebrother = new Farebrother(lambda); 62 | double res = myfarebrother.probQsupx(300); 63 | System.out.println(res); 64 | FarebrotherExperiment myfarebrotherExp = new FarebrotherExperiment(lambda); 65 | double res2 = myfarebrotherExp.probQsupx(300); 66 | System.out.println(res2); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/GeneResultsNoScore.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import java.util.ArrayList; 25 | 26 | import ch.unil.genescore.main.FileExport; 27 | import ch.unil.genescore.main.Main; 28 | import ch.unil.genescore.main.Settings; 29 | 30 | public class GeneResultsNoScore { 31 | 32 | private ArrayList noScore_ = null; 33 | private FileExport exporter_ = null; 34 | public GeneResultsNoScore(){ 35 | noScore_ = new ArrayList(); 36 | exporter_ = new FileExport(); 37 | } 38 | 39 | public void add(String str){ 40 | noScore_.add(str); 41 | } 42 | 43 | public void setExporter(FileExport exp) { 44 | exporter_=exp; 45 | } 46 | 47 | public void setExporter(String additionalOutputFileSuffix) { 48 | String filename = Settings.outputDirectory_ + "/" + Settings.gwasName_+ additionalOutputFileSuffix + ".scoreComputeError" + Settings.chromFileExtension_ + ".txt"; 49 | exporter_.setWriter(filename); 50 | } 51 | 52 | 53 | public void writeResultsToFile(String additionalOutputFileSuffix){ 54 | 55 | if (noScore_.size() == 0) 56 | return; 57 | 58 | Main.warning("Gene score computation did not converge at specified precision for some genes"); 59 | setExporter(additionalOutputFileSuffix); 60 | String header = "chromosome\tstart\tend\tstrand\tgene_id\tsymbol\tScore\tStatus"; 61 | exporter_.println(header); 62 | for (String line : noScore_) 63 | exporter_.println(line); 64 | exporter_.close(); 65 | Main.println(""); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/test/GeneResultsNoScoreTest.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas.test; 23 | 24 | 25 | import static org.junit.Assert.*; 26 | 27 | import java.util.ArrayList; 28 | import java.util.LinkedHashMap; 29 | 30 | import org.junit.AfterClass; 31 | import org.junit.BeforeClass; 32 | import org.junit.Test; 33 | 34 | import ch.unil.genescore.gene.Gene; 35 | import ch.unil.genescore.main.FileExport; 36 | import ch.unil.genescore.main.Settings; 37 | import ch.unil.genescore.vegas.GeneResultsSnpsOutOfBounds; 38 | import ch.unil.genescore.vegas.GeneResultsNoScore; 39 | 40 | 41 | public class GeneResultsNoScoreTest { 42 | 43 | // ============================================================================ 44 | // SETUP 45 | 46 | @BeforeClass 47 | public static void testSetup() { 48 | Settings.loadSettings(); 49 | } 50 | 51 | @AfterClass 52 | public static void testCleanup() { 53 | } 54 | 55 | 56 | 57 | @Test 58 | public void writeResultsToFileTest(){ 59 | 60 | FileExportStub stub = new FileExportStub(); 61 | GeneResultsNoScore results = new GeneResultsNoScore(); 62 | results.add("g1"); 63 | results.add("g2"); 64 | 65 | results.setExporter(stub); 66 | 67 | results.writeResultsToFile("blub"); 68 | String str=stub.getStrings().get(0); 69 | System.out.print(str); 70 | ArrayList strs = stub.getStrings(); 71 | assertTrue(stub.getStrings().get(0).equals("chromosome\tstart\tend\tstrand\tgene_id\tsymbol\tScore\tStatus")); 72 | assertTrue(stub.getStrings().get(1).equals("g1")); 73 | assertTrue(stub.getStrings().get(2).equals("g2")); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/main/ConvenienceMethods.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.main; 23 | 24 | import java.util.ArrayList; 25 | 26 | public class ConvenienceMethods { 27 | /**converting between ArrayList and double[] or int[]*/ 28 | public static ArrayList doubleArToArList(double[] myList){ 29 | ArrayList arOut = new ArrayList(myList.length); 30 | for (double el : myList){ 31 | arOut.add(el); 32 | } 33 | return arOut; 34 | } 35 | public static double[] arListToDoubleAr(ArrayList myList){ 36 | double[] arOut = new double[myList.size()]; 37 | for (int i=0 ; i < myList.size(); i++){ 38 | arOut[i]=myList.get(i); 39 | } 40 | return arOut; 41 | } 42 | public static ArrayList intArToArList(int[] myList){ 43 | ArrayList arOut = new ArrayList(myList.length); 44 | for (int el : myList){ 45 | arOut.add(el); 46 | } 47 | return arOut; 48 | } 49 | public static int[] arListToIntAr(ArrayList myList){ 50 | int[] arOut = new int[myList.size()]; 51 | for (int i=0 ; i < myList.size(); i++){ 52 | arOut[i]=myList.get(i); 53 | } 54 | return arOut; 55 | } 56 | /**subindexing double[] with other int[]*/ 57 | public static double[] subIndexing(double[] ar,int[] indices){ 58 | double[] arOut = new double[indices.length]; 59 | for (int i=0; i3){ 55 | currentElement = new GenomicElement(nextLine[3]); 56 | } 57 | else { 58 | currentElement = new GenomicElement(fakeIds); 59 | } 60 | currentElement.setPosition(nextLine[0], Integer.parseInt(nextLine[1]), Integer.parseInt(nextLine[2]), true); 61 | currentElement_= new OverlappedGenomicElement(currentElement); 62 | } 63 | public boolean streamOpen(){ return streamOpen_;} 64 | @Override 65 | public void reOpenStream() { 66 | 67 | if(streamOpen()){throw new RuntimeException("never call reopen on open stream");} 68 | parser_ = new FileParser(filePath_,"\t"); 69 | streamOpen_=true; 70 | // TODO Auto-generated method stub 71 | 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/OverlappedCollectionStream.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import java.util.Collection; 25 | import java.util.Collections; 26 | import java.util.Iterator; 27 | //TODO: Dangerous class I think 28 | public class OverlappedCollectionStream 29 | implements OverlappedGenomicElementStream { 30 | 31 | Collection myCollection_ = null; 32 | OverlappedGenomicElement lastElement_ = null; 33 | OverlappedGenomicElement currentElement_ = null; 34 | boolean streamOpen_ = false; 35 | Iterator it_ = null; 36 | public OverlappedCollectionStream(Collection myCollection){ 37 | myCollection_ = myCollection; 38 | streamOpen_ = true; 39 | it_ = myCollection_.iterator(); 40 | } 41 | 42 | 43 | public OverlappedGenomicElement getNextAsOverlappedGenomicElement(){ 44 | if (!streamOpen_) 45 | throw new RuntimeException("never use on closed stream"); 46 | lastElement_ = currentElement_; 47 | if (it_.hasNext()){ 48 | currentElement_=it_.next(); 49 | 50 | } 51 | if (!it_.hasNext()) { 52 | streamOpen_ = false; 53 | } 54 | if (lastElement_!=null){ 55 | if (currentElement_.compareTo(lastElement_)<0) 56 | throw new RuntimeException("elements are not sorted right"); 57 | } 58 | return currentElement_; 59 | } 60 | public boolean streamOpen(){ 61 | return streamOpen_; 62 | } 63 | 64 | 65 | @Override 66 | public void reOpenStream() { 67 | if(streamOpen()){throw new RuntimeException("never call reopen on open stream");} 68 | OverlappedGenomicElement lastElement_ = null; 69 | OverlappedGenomicElement currentElement_ = null; 70 | streamOpen_ = true; 71 | it_ = myCollection_.iterator(); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/WrappedSnpPositionStream.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import java.io.DataInputStream; 25 | import java.util.ArrayList; 26 | 27 | import ch.unil.genescore.main.Main; 28 | 29 | public class WrappedSnpPositionStream 30 | //extends OverlappedGenomicElementFileStream 31 | implements OverlappedGenomicElementStream { 32 | 33 | private SnpPositionStream posStream_ = null; 34 | private int currentIndex_ = 0; 35 | private ArrayList filenames_ = null; 36 | private String binaryFileVersionID_ = null; 37 | 38 | WrappedSnpPositionStream(ArrayList filenames,String binaryFileVersionID){ 39 | filenames_ = filenames; 40 | binaryFileVersionID_=binaryFileVersionID; 41 | currentIndex_=0; 42 | posStream_ = new SnpPositionStream(filenames_.get(currentIndex_),binaryFileVersionID_); 43 | } 44 | @Override 45 | public OverlappedGenomicElement getNextAsOverlappedGenomicElement() { 46 | boolean currentStreamOpen = posStream_.streamOpen(); 47 | if (!currentStreamOpen) { 48 | if ((currentIndex_+1)==filenames_.size()) 49 | throw new RuntimeException("never call on closed stream."); 50 | currentIndex_++; 51 | posStream_ = new SnpPositionStream(filenames_.get(currentIndex_),binaryFileVersionID_); 52 | } 53 | return posStream_.getNextAsOverlappedGenomicElement(); 54 | } 55 | 56 | @Override 57 | public boolean streamOpen() { 58 | // TODO Auto-generated method stub 59 | return (posStream_.streamOpen() || (currentIndex_+1)!=filenames_.size()); 60 | } 61 | @Override 62 | public void reOpenStream() { 63 | if(streamOpen()){throw new RuntimeException("never call reopen on open stream");} 64 | currentIndex_=0; 65 | posStream_ = new SnpPositionStream(filenames_.get(currentIndex_),binaryFileVersionID_); 66 | } 67 | 68 | 69 | } 70 | 71 | 72 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/test/GeneResultsSnpsOutOfBoundsTest.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas.test; 23 | 24 | 25 | 26 | 27 | import static org.junit.Assert.*; 28 | 29 | import java.util.ArrayList; 30 | import java.util.LinkedHashMap; 31 | 32 | import org.junit.AfterClass; 33 | import org.junit.BeforeClass; 34 | import org.junit.Test; 35 | 36 | import ch.unil.genescore.gene.Gene; 37 | import ch.unil.genescore.main.FileExport; 38 | import ch.unil.genescore.main.Settings; 39 | import ch.unil.genescore.vegas.GeneResultsSnpsOutOfBounds; 40 | 41 | 42 | public class GeneResultsSnpsOutOfBoundsTest { 43 | 44 | 45 | // ============================================================================ 46 | // SETUP 47 | 48 | @BeforeClass 49 | public static void testSetup() { 50 | Settings.loadSettings(); 51 | } 52 | 53 | @AfterClass 54 | public static void testCleanup() { 55 | } 56 | 57 | 58 | 59 | @Test 60 | public void writeResultsToFileTest(){ 61 | 62 | FileExportStub stub = new FileExportStub(); 63 | LinkedHashMap map = new LinkedHashMap(); 64 | map.put(new Gene("g1"), 0); 65 | map.put(new Gene("g2"), 2); 66 | 67 | 68 | //String=results.getStrings().get(0); 69 | GeneResultsSnpsOutOfBounds results = new GeneResultsSnpsOutOfBounds(); 70 | results.setExporter(stub); 71 | results.setZeroSnpsOrAboveSnpLimit(map); 72 | results.writeResultsToFile("blub"); 73 | String str=stub.getStrings().get(0); 74 | System.out.print(str); 75 | ArrayList strs = stub.getStrings(); 76 | assertTrue(stub.getStrings().get(0).equals("gene_id\tsymbol\tSNPs")); 77 | assertTrue(stub.getStrings().get(1).equals("g1\tNA\t0")); 78 | assertTrue(stub.getStrings().get(2).equals("g2\tNA\t2")); 79 | assertTrue(stub.isclosed()); 80 | } 81 | 82 | 83 | } 84 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/main/test/AllTests.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.main.test; 23 | 24 | import org.junit.runner.RunWith; 25 | import org.junit.runners.Suite; 26 | import org.junit.runners.Suite.SuiteClasses; 27 | 28 | import ch.unil.genescore.gene.test.GenomicElementTest; 29 | import ch.unil.genescore.pathway.test.RankSumTestTest; 30 | import ch.unil.genescore.pathway.test.geneSetLibraryTest; 31 | import ch.unil.genescore.vegas.test.AnalyticVegasTest; 32 | import ch.unil.genescore.vegas.test.DistributionMethodsTest; 33 | import ch.unil.genescore.vegas.test.FarebrotherTest; 34 | import ch.unil.genescore.vegas.test.FileParserTest; 35 | import ch.unil.genescore.vegas.test.GeneResultsNoScoreTest; 36 | import ch.unil.genescore.vegas.test.GeneResultsSnpsOutOfBoundsTest; 37 | import ch.unil.genescore.vegas.test.MTJConvenienceMethodsTest; 38 | import ch.unil.genescore.vegas.test.MaxVegasTest; 39 | import ch.unil.genescore.vegas.test.OverlappedCollectionStreamTest; 40 | import ch.unil.genescore.vegas.test.OverlappedGenomicElementTest; 41 | import ch.unil.genescore.vegas.test.SnpTest; 42 | @RunWith(Suite.class) 43 | //@SuiteClasses({ GenomicElementTest.class, SnpTest.class, AnalyticVegasTest.class, PathwayMainTest.class,MTJConvenienceMethodsTest.class}) 44 | @SuiteClasses({ 45 | 46 | GenomicElementTest.class, 47 | 48 | 49 | RankSumTestTest.class, 50 | geneSetLibraryTest.class, 51 | FarebrotherTest.class, 52 | AnalyticVegasTest.class, 53 | DistributionMethodsTest.class, 54 | 55 | MaxVegasTest.class, 56 | MTJConvenienceMethodsTest.class, 57 | OverlappedCollectionStreamTest.class, 58 | OverlappedGenomicElementTest.class, 59 | 60 | SnpTest.class, 61 | GeneResultsSnpsOutOfBoundsTest.class, 62 | GeneResultsNoScoreTest.class, 63 | FileParserTest.class, 64 | 65 | 66 | 67 | 68 | }) 69 | public class AllTests { 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/test/SnpTest.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas.test; 23 | 24 | import static org.junit.Assert.assertEquals; 25 | 26 | import org.junit.AfterClass; 27 | import org.junit.BeforeClass; 28 | import org.junit.Test; 29 | 30 | import ch.unil.genescore.main.Settings; 31 | import ch.unil.genescore.vegas.Snp; 32 | 33 | import org.apache.commons.math3.distribution.NormalDistribution; 34 | 35 | public class SnpTest { 36 | 37 | 38 | // ============================================================================ 39 | // SETUP 40 | 41 | @BeforeClass 42 | public static void testSetup() { 43 | Settings.loadSettings(); 44 | } 45 | 46 | @AfterClass 47 | public static void testCleanup() { } 48 | 49 | 50 | // ============================================================================ 51 | // TESTS 52 | 53 | /** Test test statistic */ 54 | @Test 55 | public void testComputeChiSquaredStatistics() { 56 | 57 | // Computed chi2 statistics for 1e-1, 1e-2, ... using R: 58 | // > x <- 10^(-(1:32)) 59 | // > qchisq(x, 1, lower.tail=FALSE) 60 | double[] chi2Stat = { 2.705543, 6.634897, 10.827566, 15.136705, 19.511421, 23.928127, 28.373987, 32.841253, 37.324893, 41.821456, 61 | 46.328476, 50.844128, 55.367025, 59.896088, 64.430464, 68.969461, 73.512517, 78.059165, 82.609014, 87.161733, 62 | 91.717041, 96.274696, 100.834486, 105.396229, 109.959765, 114.524952, 119.091664, 123.659790, 128.229229, 132.799893, 63 | 137.371700, 141.944577 }; 64 | 65 | //Main.println("Comparing chi2 stat computed using:"); 66 | //Main.println("p-value\tR\tMath Commons"); 67 | 68 | for (int i=1; i<14; i++) { 69 | Snp snp = new Snp("snp1", Math.pow(10, -i)); 70 | snp.computeChiSquaredStatistics(); 71 | //Main.println("1e-" + i + "\t" + chi2Stat[i-1] + "\t" + snp.getChi2Stat()); 72 | assertEquals(chi2Stat[i-1], snp.getChi2Stat(), 1e-2); 73 | } 74 | } 75 | 76 | } 77 | 78 | 79 | -------------------------------------------------------------------------------- /LICENSES/LICENSE_jsc.txt: -------------------------------------------------------------------------------- 1 | Disclaimer 2 | 3 | The author accepts no legal responsibility whatsoever for the use of the JSC library, or any other free software on this site, by other parties; and makes no guarantees, expressed or implied, about its quality, reliability, or any other characteristic. The software is provided "as is" and its author makes no guarantees that the software is without bugs, or that it will run on any particular platform. 4 | 5 | The JSC library is incomplete and still under construction. The author reserves the right to restructure JSC; change, withdraw or rename any of its classes or interfaces as he sees fit, without notice. 6 | 7 | The JSC library, or any other software on this site, is not designed or intended for use in on-line control of aircraft, air traffic, aircraft navigation or aircraft communications; or in the design, construction, operation or maintenance of any nuclear facility; or for any other safety-critical application. You must not use or redistribute the software for such purposes. 8 | 9 | Although the site invites emailed questions, comments, bug reports, requests etc., the author does not guarantee to always respond to such communications. 10 | 11 | The author makes reasonable efforts to protect the integrity of his computer files. However, there is always a risk attached to downloading files from the Internet. Please ensure that anything downloaded is virus-checked. The author accepts no liability for any loss or damage caused by the downloading of files from this site or their use. You assume all risk for any damage to your computer system or loss of data that results from obtaining any content or software from this website, including any damages resulting from computer viruses. 12 | 13 | The author cannot guarantee that this website's links to other resources on the Internet are always current or working. The author is not responsible for the content of external sites and provision of a link should not be taken as endorsement of any kind. The inclusion of any company's name within this website's pages should not be construed as an endorsement of that company's products and/or services. 14 | 15 | Intellectual property rights 16 | 17 | This site and all the software it provides are copyright © Andrew James Bertie, 2004, unless otherwise indicated. You must not remove or modify any copyright or trademark notice, or other notice of ownership, from the software. Acknowledgement of A. J. Bertie's copyright, authorship and permission must appear with any permitted reuse of this site's content or software. This includes any applications you develop using the JSC library, or extensions of it. 18 | 19 | The JSC library, applets or any other software downloaded for free from this site cannot be leased, sold or otherwise used for commercial gain without the express permission of the author. This prohibition includes large scale or fee paying educational use. 20 | 21 | You are permitted to extend and reuse the JSC library through the usual Java object-orientated mechanisms of inheritance, composition and access provided to classes, interfaces, packages etc. by the JSC application programming interface which is documented on this site at API. However, you must not modify, take apart, decompile, or reverse engineer the original JSC library. The author is willing, however, to consider making useful modifications to the JSC API if requested. 22 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/MvnPack.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import com.sun.jna.Library; 25 | import com.sun.jna.ptr.DoubleByReference; 26 | import com.sun.jna.ptr.IntByReference; 27 | 28 | public interface MvnPack extends Library { 29 | 30 | /** 31 | * See http://www.math.wsu.edu/faculty/genz/software/fort77/mvtdstpack.f 32 | * @param n 33 | * @param lower 34 | * @param upper 35 | * @param infin 36 | * @param correl 37 | * @param maxpts 38 | * @param abseps 39 | * @param releps 40 | * @param error 41 | * @param value 42 | * @param inform 43 | */ 44 | void mvtdst_(IntByReference n,IntByReference df, double[] lower, double[] upper, int[] infin, double[] correl, double[] delta, 45 | IntByReference maxpts, DoubleByReference abseps, DoubleByReference releps, 46 | DoubleByReference error, DoubleByReference value, IntByReference inform); 47 | 48 | /** 49 | * See http://www.math.wsu.edu/faculty/genz/software/fort77/mvnexppack.f 50 | * @param n 51 | * @param lower 52 | * @param upper 53 | * @param infin 54 | * @param correl 55 | * @param maxpts 56 | * @param abseps 57 | * @param releps 58 | * @param error 59 | * @param value 60 | * @param inform 61 | */ 62 | // void mvnexp_(IntByReference n, double[] lower, double[] upper, int[] infin, double[] correl, 63 | // IntByReference maxpts, DoubleByReference abseps, DoubleByReference releps, 64 | // double[] error, double[] value, IntByReference inform); 65 | 66 | /** 67 | * See http://www.math.wsu.edu/faculty/genz/software/fort77/mvnxpppack.f 68 | * @param n 69 | * @param lower 70 | * @param upper 71 | * @param infin 72 | * @param correl 73 | * @param maxpts 74 | * @param abseps 75 | * @param releps 76 | * @param error 77 | * @param value 78 | * @param inform 79 | */ 80 | // void mvnxpp_(IntByReference n, double[] lower, double[] upper, int[] infin, double[] correl, 81 | // IntByReference maxpts, DoubleByReference abseps, DoubleByReference releps, 82 | // double[] error, double[] value, IntByReference inform); 83 | 84 | } 85 | 86 | 87 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/main/ProgressMonitor.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | 23 | package ch.unil.genescore.main; 24 | 25 | 26 | /** 27 | * Small class to output progress when running a loop 28 | */ 29 | public class ProgressMonitor { 30 | 31 | /** Total iterations */ 32 | private int totalIterations_ = 0; 33 | /** Output frequency */ 34 | private int freq_ = -1; 35 | /** Starting time */ 36 | private long t0_ = 0; 37 | /** The string written at each iteration */ 38 | private String asterisks_ = "*"; 39 | 40 | 41 | // ============================================================================ 42 | // PUBLIC METHODS 43 | 44 | /** Constructor */ 45 | public ProgressMonitor(int totalIterations) { 46 | 47 | totalIterations_ = totalIterations; 48 | 49 | freq_ = totalIterations / 40; 50 | if (freq_ == 0) { 51 | freq_ = 1; 52 | asterisks_ = ""; 53 | for (int i=0; i<40/totalIterations_; i++) 54 | asterisks_ += "*"; 55 | } 56 | 57 | t0_ = System.currentTimeMillis(); 58 | 59 | Main.println("|-------------- Progress --------------|"); 60 | } 61 | 62 | 63 | /** Constructor */ 64 | public ProgressMonitor(int totalIterations, int freq) { 65 | 66 | this(totalIterations); 67 | freq_ = freq; 68 | } 69 | 70 | 71 | // ---------------------------------------------------------------------------- 72 | 73 | /** Print progress */ 74 | public void iteration(int i) { 75 | 76 | if (i % freq_ == 0) 77 | System.out.print(asterisks_); 78 | } 79 | 80 | 81 | // ---------------------------------------------------------------------------- 82 | 83 | /** Estimated total runtime */ 84 | public void estimatedTotalRuntime(int i) { 85 | if (i % freq_ == 0) { 86 | long t1 = System.currentTimeMillis(); 87 | Main.println(i + "\tERT: \t" + Utils.chronometer(totalIterations_*(t1-t0_)/i)); 88 | } 89 | 90 | } 91 | 92 | 93 | // ---------------------------------------------------------------------------- 94 | 95 | /** Print progress */ 96 | public void done() { 97 | Main.print("\n\n"); 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/StreamMethods.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import java.io.BufferedInputStream; 25 | import java.io.BufferedOutputStream; 26 | import java.io.DataInputStream; 27 | import java.io.DataOutputStream; 28 | import java.io.FileInputStream; 29 | import java.io.FileOutputStream; 30 | import java.util.zip.GZIPInputStream; 31 | import java.util.zip.GZIPOutputStream; 32 | 33 | import ch.unil.genescore.main.Main; 34 | import ch.unil.genescore.main.Settings; 35 | 36 | public class StreamMethods { 37 | 38 | 39 | public static DataOutputStream openDataOutputStream(String filename , String binaryFileVersionID) { 40 | 41 | Main.println("Writing file: " + filename); 42 | 43 | try { 44 | FileOutputStream outfile = new FileOutputStream(filename); 45 | GZIPOutputStream gzip = new GZIPOutputStream(outfile); 46 | BufferedOutputStream buf = new BufferedOutputStream(gzip); 47 | DataOutputStream outStream = new DataOutputStream(buf); 48 | 49 | // Write the version, used as a check when reading files 50 | outStream.writeUTF(binaryFileVersionID); 51 | 52 | return outStream; 53 | 54 | } catch (Exception e) { 55 | throw new RuntimeException("Could not open binary output file: " + filename); 56 | } 57 | } 58 | 59 | /** Open a data input stream */ 60 | public static DataInputStream openDataInputStream(String filename, String binaryFileVersionID) { 61 | 62 | if (Settings.verbose_) 63 | Main.println("Reading file: " + filename); 64 | 65 | try { 66 | FileInputStream infile = new FileInputStream(filename); 67 | GZIPInputStream gzip = new GZIPInputStream(infile); 68 | BufferedInputStream buf = new BufferedInputStream(gzip); 69 | DataInputStream inStream = new DataInputStream(buf); 70 | 71 | // Check that the file starts with the right versionId 72 | String versionId = inStream.readUTF(); 73 | if (!versionId.equals(binaryFileVersionID)) 74 | throw new RuntimeException("Incompatible version ID of binary file, delete the file to create a new one"); 75 | 76 | return inStream; 77 | 78 | } catch (Exception e) { 79 | throw new RuntimeException("Could not open binary input file: " + filename); 80 | } 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/GeneResultsScore.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import java.util.ArrayList; 25 | 26 | import ch.unil.genescore.gene.Gene; 27 | import ch.unil.genescore.main.FileExport; 28 | import ch.unil.genescore.main.Settings; 29 | 30 | public class GeneResultsScore { 31 | 32 | private boolean headerWritten_ = false; 33 | protected FileExport exporter_ = null; 34 | 35 | public GeneResultsScore(){ 36 | exporter_ = new FileExport(); 37 | } 38 | 39 | 40 | protected String addDot(String withoutDot){ 41 | String outStr=withoutDot; 42 | if (withoutDot!="") 43 | outStr = "." + withoutDot; 44 | return outStr; 45 | } 46 | 47 | public void setExporter(String additionalOutputFileSuffix){ 48 | 49 | String filename = Settings.outputDirectory_ + "/" + Settings.gwasName_ + addDot(additionalOutputFileSuffix) + ".genescores" + Settings.chromFileExtension_ + ".txt"; 50 | exporter_.setWriter(filename); 51 | } 52 | public void writeLine(GeneScoreEvaluator evaluator, Gene gene){ 53 | 54 | if (!getHeaderWritten()) { 55 | getExporter().println("chromosome\tstart\tend\tstrand\tgene_id\tgene_symbol" + evaluator.getResultsAsStringHeader()); 56 | setHeaderWritten(true); 57 | } 58 | 59 | // Write results for this gene to file 60 | String nextLine = gene.toString(); 61 | nextLine += evaluator.getResultsAsString(); 62 | getExporter().println(nextLine); 63 | getExporter().flush(); 64 | } 65 | 66 | /**write GeneScoreEvaluator output directly without gene information.*/ 67 | public void writeLine(GeneScoreEvaluator evaluator){ 68 | 69 | if (!getHeaderWritten()) { 70 | getExporter().println(evaluator.getResultsAsStringHeader()); 71 | setHeaderWritten(true); 72 | } 73 | 74 | String nextLine = evaluator.getResultsAsString(); 75 | getExporter().println(nextLine); 76 | getExporter().flush(); 77 | } 78 | 79 | 80 | 81 | 82 | public FileExport getExporter() { 83 | return exporter_; 84 | } 85 | 86 | public boolean getHeaderWritten(){ 87 | return headerWritten_; 88 | } 89 | public void setHeaderWritten(boolean h){ 90 | headerWritten_=h; 91 | } 92 | 93 | public void setExporter(FileExport exporter) { 94 | this.exporter_ = exporter; 95 | } 96 | 97 | 98 | } 99 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/GeneResultsSnpsOutOfBounds.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import java.util.LinkedHashMap; 25 | 26 | import ch.unil.genescore.gene.Gene; 27 | import ch.unil.genescore.main.FileExport; 28 | import ch.unil.genescore.main.Main; 29 | import ch.unil.genescore.main.Settings; 30 | 31 | 32 | public class GeneResultsSnpsOutOfBounds { 33 | 34 | private LinkedHashMap zeroSnpsOrAboveSnpLimit_ = null; 35 | private FileExport exporter_ = null; 36 | 37 | public GeneResultsSnpsOutOfBounds(){ 38 | exporter_ = new FileExport(); 39 | zeroSnpsOrAboveSnpLimit_ = new LinkedHashMap(); 40 | } 41 | 42 | 43 | /** Write the list of genes that have zero or beyond max num snps */ 44 | public void writeResultsToFile(String additionalOutputFileSuffix) { 45 | 46 | if (getZeroSnpsOrAboveSnpLimit().size() == 0) 47 | return; 48 | 49 | Main.warning("Writing genes without SNPs or exceeding the maximum number of SNPs"); 50 | setExporter(additionalOutputFileSuffix); 51 | 52 | String header = "gene_id\tsymbol\tSNPs"; 53 | exporter_.println(header); 54 | for (Gene gene : getZeroSnpsOrAboveSnpLimit().keySet()) { 55 | String line = gene.id_; 56 | line += "\t" + gene.getSymbolOrNA(); 57 | line += "\t" + getZeroSnpsOrAboveSnpLimit().get(gene); 58 | exporter_.println(line); 59 | } 60 | exporter_.close(); 61 | Main.println(""); 62 | } 63 | 64 | public LinkedHashMap getZeroSnpsOrAboveSnpLimit() { 65 | return zeroSnpsOrAboveSnpLimit_; 66 | } 67 | 68 | 69 | public void setZeroSnpsOrAboveSnpLimit(LinkedHashMap zeroSnpsOrAboveSnpLimit) { 70 | this.zeroSnpsOrAboveSnpLimit_ = zeroSnpsOrAboveSnpLimit; 71 | } 72 | 73 | public void setExporter(FileExport exp) { 74 | exporter_=exp; 75 | } 76 | 77 | public void setExporter(String additionalOutputFileSuffix) { 78 | String filename = Settings.outputDirectory_ + "/" + Settings.gwasName_ + additionalOutputFileSuffix + ".numSnpError" + Settings.chromFileExtension_ + ".txt"; 79 | exporter_.setWriter(filename); 80 | } 81 | 82 | public void addToMap(GeneWithItsSnps geneWithItsSnps) { 83 | getZeroSnpsOrAboveSnpLimit().put(geneWithItsSnps.getGene(),geneWithItsSnps.getNrOfSnps()); 84 | } 85 | 86 | 87 | } 88 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/SnpWeightPairs.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import java.util.ArrayList; 25 | import java.util.HashMap; 26 | 27 | public class SnpWeightPairs { 28 | 29 | //Snp snp1; 30 | private ArrayList snps_=null; 31 | private ArrayList weights_ = null; 32 | 33 | public SnpWeightPairs(){ 34 | 35 | } 36 | 37 | 38 | public SnpWeightPairs(ArrayList snps,ArrayList weights){ 39 | 40 | snps_ = snps; 41 | weights_ = weights; 42 | } 43 | public void fuseSnpWeightPairs(SnpWeightPairs secondSnpWeightPairs){ 44 | HashMap snpHash = new HashMap(); 45 | HashMap weightHash = new HashMap(); 46 | String currentId=null; 47 | for (int i=0;i snps = new ArrayList(); 71 | ArrayList weights = new ArrayList(); 72 | for (String id : snpHash.keySet()){ 73 | snps.add(snpHash.get(id)); 74 | weights.add(weightHash.get(id)); 75 | } 76 | snps_ = snps; 77 | weights_ = weights; 78 | } 79 | 80 | public ArrayList getSnps(){return snps_;} 81 | public ArrayList getWeights(){return weights_;} 82 | public double[] getWeightsNotList(){ 83 | int n=weights_.size(); 84 | double[] outAr = new double[n]; 85 | for (int i=0 ; i < n ; i++) 86 | outAr[i]=weights_.get(i); 87 | return outAr; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/GeneWithItsSnps.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import java.util.ArrayList; 25 | import java.util.Collection; 26 | import java.util.Iterator; 27 | 28 | import ch.unil.genescore.gene.Gene; 29 | import ch.unil.genescore.main.Main; 30 | import ch.unil.genescore.main.Settings; 31 | import ch.unil.genescore.main.Utils; 32 | 33 | public class GeneWithItsSnps { 34 | 35 | private Gene gene_; 36 | private ArrayList snpList_; 37 | 38 | public GeneWithItsSnps(Gene gene,ArrayList snpList){ 39 | gene_=gene; 40 | snpList_=snpList; 41 | checkAndExecuteCodingRemoval(); 42 | } 43 | 44 | public ArrayList getSnpList(){ 45 | return snpList_; 46 | 47 | } 48 | public Gene getGene(){ 49 | return gene_; 50 | } 51 | public String getGeneId(){ 52 | return gene_.getId(); 53 | } 54 | 55 | private void checkAndExecuteCodingRemoval(){ 56 | if(Settings.removeCodingSnpsOfOtherGenes_){ 57 | removeCodingSnpsOfOtherGenes(); 58 | } 59 | } 60 | 61 | public int getNrOfSnps(){ 62 | return(snpList_.size()); 63 | } 64 | /** Remove coding snps of other genes in that window */ 65 | private void removeCodingSnpsOfOtherGenes() { 66 | 67 | Iterator iter = snpList_.iterator(); 68 | while (iter.hasNext()) { 69 | Snp snp = iter.next(); 70 | if (snp.isCodingForOtherGene(gene_.getId())) 71 | iter.remove(); 72 | } 73 | } 74 | 75 | public boolean checkNrOfSnps(GeneResultsSnpsOutOfBounds GeneResultsZeroOrAboveLimit_){ 76 | if (getNrOfSnps() == 0 || (Settings.maxSnpsPerGene_ > 0 && getNrOfSnps() > Settings.maxSnpsPerGene_)) { 77 | 78 | GeneResultsZeroOrAboveLimit_.addToMap(this); 79 | 80 | if (Settings.verbose_) { 81 | Main.print("\t" + Utils.padRight("0h 0min 0s 0ms", 22)); 82 | if (getNrOfSnps() == 0) 83 | Main.print("Gene has no SNPs\n"); 84 | else 85 | Main.print("Gene exceeds max number of SNPs defined in settings file\n"); 86 | 87 | } 88 | return false; 89 | } 90 | return true; 91 | } 92 | 93 | public void printGeneNameAndNrOfSnps(){ 94 | String symb = Utils.padRight(((gene_.symbol_ == null) ? "NA" : gene_.symbol_), 16); 95 | Main.print(Utils.padRight(gene_.id_, 18) + symb); 96 | Main.print(String.format("%6d", snpList_.size())); 97 | } 98 | } 99 | 100 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/DistributionMethods.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import org.apache.commons.math3.distribution.ChiSquaredDistribution; 25 | import org.apache.commons.math3.distribution.NormalDistribution; 26 | 27 | public class DistributionMethods { 28 | 29 | 30 | private static ChiSquaredDistribution chiSquared1df_ = new ChiSquaredDistribution(1); 31 | private static NormalDistribution normalDist_ = new NormalDistribution(); 32 | 33 | 34 | public static double chiSquared1dfCumulativeProbabilityUpperTail(double q){ 35 | double p; 36 | if (q > 50){ 37 | double q2 = Math.sqrt(q); 38 | p=normalCumulativeProbabilityUpperTailApprox(q2)*2; 39 | } 40 | else{ 41 | p=1-chiSquared1df_.cumulativeProbability(q); 42 | } 43 | return(p); 44 | } 45 | 46 | public static double chiSquared1dfInverseCumulativeProbabilityUpperTail(double p){ 47 | 48 | double p2=p/2; 49 | double q; 50 | if (p2 < 1E-14){ 51 | double upper=normalInversionUpperTailApprox(p2); 52 | q=Math.pow(upper,2); 53 | 54 | } 55 | else{ 56 | q=chiSquared1df_.inverseCumulativeProbability(1-p); 57 | } 58 | return(q); 59 | 60 | 61 | } 62 | 63 | 64 | public static double normalCumulativeProbability(double q){ 65 | double p; 66 | if (q < -8){ 67 | double upper=normalCumulativeProbabilityUpperTailApprox(q); 68 | p=upper; 69 | 70 | } 71 | else{ 72 | p=normalDist_.cumulativeProbability(q); 73 | } 74 | return(p); 75 | } 76 | 77 | public static double normalCumulativeProbabilityUpperTailApprox(double q){ 78 | 79 | 80 | q=Math.abs(q); 81 | double aa=-(q*q)/2-Math.log(q)-0.5*Math.log(2*Math.PI); 82 | return(Math.exp(aa)); 83 | } 84 | 85 | public static double normalInverseCumulativeProbability(double p){ 86 | double q; 87 | if (p < 10E-14){ 88 | double upper=normalInversionUpperTailApprox(p); 89 | q=(-1)*upper; 90 | 91 | } 92 | else{ 93 | q=normalDist_.inverseCumulativeProbability(p); 94 | } 95 | return(q); 96 | } 97 | 98 | 99 | public static double normalInversionUpperTailApprox(double p){ 100 | // approximates tail integral of normal distribution function:: only use for very low values; below 10^-14 101 | double lp = Math.log(p); 102 | double diff=1; 103 | double a1=1; 104 | double a=1; 105 | while(diff>0.001){ 106 | 107 | 108 | a=Math.sqrt((-lp-Math.log(Math.sqrt(2*Math.PI))-Math.log(a1))*2); 109 | diff=Math.abs(a-a1); 110 | a1=a; 111 | 112 | } 113 | return(a); 114 | } 115 | 116 | } 117 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/pathway/test/RankSumTestTest.java: -------------------------------------------------------------------------------- 1 | package ch.unil.genescore.pathway.test; 2 | /******************************************************************************* 3 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | *******************************************************************************/ 23 | import static org.junit.Assert.assertEquals; 24 | 25 | import java.util.ArrayList; 26 | import java.util.Collection; 27 | 28 | import javastat.inference.nonparametric.RankSumTest; 29 | import jsc.independentsamples.MannWhitneyTest; 30 | import jsc.tests.H1; 31 | 32 | import org.apache.commons.math3.stat.inference.MannWhitneyUTest; 33 | import org.junit.AfterClass; 34 | import org.junit.BeforeClass; 35 | import org.junit.Test; 36 | 37 | import ch.unil.genescore.gene.Gene; 38 | import ch.unil.genescore.main.Settings; 39 | import ch.unil.genescore.pathway.GeneScoreList; 40 | import ch.unil.genescore.pathway.GeneSet; 41 | import ch.unil.genescore.pathway.GeneSetLibrary; 42 | import ch.unil.genescore.pathway.PathwayMain; 43 | 44 | 45 | /** 46 | * Unit tests for GenomicElement. 47 | */ 48 | public class RankSumTestTest { 49 | 50 | 51 | // ============================================================================ 52 | // SETUP 53 | 54 | @BeforeClass 55 | public static void testSetup() { 56 | Settings.loadSettings(); 57 | } 58 | 59 | @AfterClass 60 | public static void testCleanup() { } 61 | 62 | 63 | // ============================================================================ 64 | // TESTS 65 | 66 | /** Test PathwayMain.run() */ 67 | @Test 68 | public void testRankSumTest(){ 69 | double [] testdata1 = {0.8, 0.83, 1.89, 1.04, 1.45, 1.38, 1.91, 1.64, 0.73, 1.46}; 70 | double [] testdata2 = {1.15, 0.88, 0.9, 0.74, 1.21}; 71 | 72 | 73 | 74 | // Null constructor 75 | RankSumTest testclass2 = new RankSumTest(); 76 | double pValue = testclass2.pValue("equal", testdata1, testdata2); 77 | assertEquals(pValue, 0.2544, 0.001);//copied from R 78 | pValue = testclass2.pValue("greater", testdata1, testdata2); 79 | assertEquals(pValue, 1-0.1272, 0.001);//copied from R 80 | 81 | 82 | 83 | MannWhitneyUTest test = new MannWhitneyUTest(); 84 | 85 | double pval = test.mannWhitneyUTest(testdata1,testdata2); 86 | System.out.println(""); 87 | MannWhitneyTest test3 = new MannWhitneyTest(testdata1,testdata2); 88 | double pval3=test3.approxSP(); 89 | double pval4=test3.exactSP(); 90 | assertEquals(pval3, 0.2446, 0.001); 91 | assertEquals(pval4, 0.2544, 0.001); 92 | 93 | MannWhitneyTest test4 = new MannWhitneyTest(testdata1,testdata2, H1.GREATER_THAN); 94 | double pval5=test4.approxSP(); 95 | double pval6=test4.exactSP(); 96 | assertEquals(pval5, 0.1227, 0.001); 97 | assertEquals(pval6, 0.1272, 0.001); 98 | 99 | 100 | System.out.println(""); 101 | 102 | 103 | 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/main/FileExport.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.main; 23 | 24 | import java.io.BufferedWriter; 25 | import java.io.FileOutputStream; 26 | import java.io.FileWriter; 27 | import java.io.IOException; 28 | import java.io.OutputStreamWriter; 29 | import java.util.zip.GZIPOutputStream; 30 | 31 | 32 | /** 33 | * Write a text file 34 | */ 35 | public class FileExport { 36 | 37 | /** The buffered file writer */ 38 | BufferedWriter writer_ = null; 39 | 40 | 41 | // ============================================================================ 42 | // PUBLIC METHODS 43 | 44 | 45 | public FileExport(){}; 46 | public FileExport(String filename, boolean gzip) { 47 | 48 | setWriter(filename, gzip); 49 | } 50 | 51 | /** Constructor for uncompressed file */ 52 | public FileExport(String filename) { 53 | 54 | this(filename, false); 55 | } 56 | 57 | public void setWriter(String filename){ 58 | setWriter(filename, false); 59 | } 60 | 61 | public void setWriter(String filename, boolean gzip){ 62 | 63 | try { 64 | if (gzip) 65 | filename += ".gz"; 66 | System.out.println("Writing file: " + filename); 67 | 68 | if (gzip) { 69 | FileOutputStream output = new FileOutputStream(filename); 70 | writer_ = new BufferedWriter(new OutputStreamWriter(new GZIPOutputStream(output), "UTF-8")); 71 | } else { 72 | FileWriter fstream = new FileWriter(filename); 73 | writer_ = new BufferedWriter(fstream); 74 | } 75 | 76 | } catch (Exception e) { 77 | Main.error(e); 78 | } 79 | } 80 | 81 | 82 | // ---------------------------------------------------------------------------- 83 | 84 | /** Write a line to the file */ 85 | public void println(String str) { 86 | 87 | print(str + "\n"); 88 | } 89 | 90 | 91 | // ---------------------------------------------------------------------------- 92 | 93 | /** Write the given string to the file */ 94 | public void print(String str) { 95 | 96 | try { 97 | writer_.write(str); 98 | } catch (IOException e) { 99 | Main.error(e); 100 | } 101 | } 102 | 103 | 104 | // ---------------------------------------------------------------------------- 105 | 106 | /** Be polite and close the file writer */ 107 | public void flush() { 108 | 109 | try { 110 | writer_.flush(); 111 | } catch (IOException e) { 112 | Main.error(e); 113 | } 114 | } 115 | 116 | 117 | // ---------------------------------------------------------------------------- 118 | 119 | /** Be polite and close the file writer */ 120 | public void close() { 121 | 122 | try { 123 | writer_.close(); 124 | } catch (IOException e) { 125 | Main.error(e); 126 | } 127 | } 128 | 129 | } 130 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/SnpSerializedPositionStream.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import java.io.DataInputStream; 25 | import java.io.IOException; 26 | 27 | import ch.unil.genescore.main.Main; 28 | 29 | public class SnpSerializedPositionStream { 30 | 31 | private int lineCounter_ = 0; 32 | /** Next line */ 33 | private Snp nextLine_ = null; 34 | /** current line */ 35 | private Snp currentLine_ = null; 36 | 37 | DataInputStream inStream_ = null; 38 | String binaryFileVersionID_= null; 39 | String filename_ = null; 40 | boolean streamOpen_ = true; 41 | 42 | 43 | public SnpSerializedPositionStream(String filename,String binaryFileVersionID){ 44 | System.out.println("reading snp positions from file:" + filename); 45 | DataInputStream inStream = null; 46 | try { 47 | inStream = StreamMethods.openDataInputStream(filename, binaryFileVersionID); 48 | } 49 | catch(Exception e){ 50 | Main.error(e, "Error loading SNP position (try deleting the binary reference population files)"); 51 | } 52 | inStream_ = inStream; 53 | streamOpen_= true; 54 | filename_ = filename; 55 | binaryFileVersionID_ = binaryFileVersionID; 56 | readLine(); 57 | } 58 | public boolean lineAvailable(){ 59 | return(currentLine_!=null); 60 | } 61 | 62 | private Snp createSnpFromStream(){ 63 | 64 | if (!streamOpen_) 65 | throw new RuntimeException("Don't call on closed stream"); 66 | 67 | String snpId = null; 68 | 69 | try { 70 | snpId = inStream_.readUTF(); 71 | } catch (IOException e) { 72 | // TODO Auto-generated catch block 73 | e.printStackTrace(); 74 | System.exit(-1); 75 | } 76 | // End of file is marked with the verion ID 77 | if (snpId.equals(binaryFileVersionID_)){ 78 | streamOpen_=false; 79 | 80 | try { 81 | inStream_.close(); 82 | } catch (IOException e) { 83 | Main.error(e); 84 | } 85 | return null; 86 | } 87 | Snp currentSnp = new Snp(snpId); 88 | 89 | try { 90 | currentSnp.readPosAndMinorAllele(inStream_); 91 | } catch (IOException e) { 92 | Main.error(e, "Error loading SNP position (try deleting the binary reference population files)"); 93 | } 94 | catch (DataInconsistencyException e) { 95 | Main.error(e); 96 | } 97 | return(currentSnp); 98 | } 99 | 100 | public Snp readLine() { 101 | Snp outLine = currentLine_; 102 | if (lineCounter_==0){ 103 | currentLine_ = createSnpFromStream(); 104 | outLine=currentLine_; 105 | } 106 | else{ 107 | currentLine_=nextLine_; 108 | } 109 | lineCounter_++; 110 | if (streamOpen_) 111 | nextLine_ = createSnpFromStream(); 112 | return outLine; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/MaxSimulAndAnalyticVegas.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import java.util.ArrayList; 25 | 26 | import no.uib.cipr.matrix.DenseMatrix; 27 | import no.uib.cipr.matrix.UpperSymmDenseMatrix; 28 | import ch.unil.genescore.main.Settings; 29 | import ch.unil.genescore.vegas.VegasSimulationNew;; 30 | 31 | public class MaxSimulAndAnalyticVegas extends AnalyticVegas { 32 | 33 | AnalyticVegas myAnalyticVegas_ = null; 34 | 35 | 36 | 37 | public MaxSimulAndAnalyticVegas(){ 38 | 39 | } 40 | 41 | public MaxSimulAndAnalyticVegas(ArrayList snpScores, DenseMatrix ld) { 42 | super(snpScores, ld); 43 | 44 | } 45 | 46 | public MaxSimulAndAnalyticVegas(ArrayList snpScores, DenseMatrix ld, double[] weights) { 47 | super(snpScores, ld); 48 | weights_ = weights; 49 | } 50 | 51 | @Override 52 | public boolean computeScore(){ 53 | // if (weights_==null){ 54 | // setDefaultWeights(); 55 | //} 56 | VegasSimulationNew simulVegas = new VegasSimulationNew(snpScores_,covariance_,(1E5)); 57 | simulVegas.computeScore(); 58 | if (simulVegas.checkSimulFail()){ 59 | simulVegas= null; 60 | MaxVegas maxVegas= new MaxVegas(snpScores_,covariance_, weights_); 61 | maxVegas.computeScore(); 62 | if(maxVegas.checkTooManySnps()){ 63 | 64 | simulVegas = new VegasSimulationNew(snpScores_,covariance_,(1E7)); 65 | simulVegas.computeScore(); 66 | //myAnalyticVegas_ = simulVegas; 67 | } 68 | else { 69 | myAnalyticVegas_ = maxVegas; 70 | } 71 | } 72 | else{ 73 | myAnalyticVegas_ = simulVegas; 74 | } 75 | copyResultsOver(); 76 | //MaxVegasWithoutPruning analyticVegas= new MaxVegasWithoutPruning(snpScores_,covariance_, weights_); 77 | return true; 78 | } 79 | private void copyResultsOver(){ 80 | /**copies results from lower-level myAnalyticVegas_ to this*/ 81 | geneScore_=myAnalyticVegas_.geneScore_; 82 | status_=myAnalyticVegas_.status_; 83 | tolerance_=myAnalyticVegas_.tolerance_; 84 | positiveDefiniteCorrection_=myAnalyticVegas_.positiveDefiniteCorrection_; 85 | numSnps_=myAnalyticVegas_.numSnps_; 86 | 87 | } 88 | 89 | private void setDefaultWeights(){ 90 | double[] weights = new double[snpScores_.size()]; 91 | for (int i=0; i snpScores_; 32 | protected ArrayList snpList_; 33 | 34 | public GeneData(ArrayList snpList){ 35 | snpScores_ = new ArrayList(); 36 | snpList_=snpList; 37 | } 38 | 39 | 40 | protected void setSnpScores(){ 41 | snpScores_ = new ArrayList(); 42 | if (Settings.withZScore_){ 43 | for (Snp snp : snpList_){ 44 | snpScores_.add(snp.getZscore()); 45 | } 46 | } 47 | else{ 48 | for (Snp snp : snpList_){ 49 | 50 | snpScores_.add(snp.getChi2Stat()); 51 | } 52 | } 53 | } 54 | 55 | public DenseMatrix getCorr(){return cov_;} 56 | public ArrayList getScores(){return snpScores_;} 57 | 58 | public void writeCovMatToFile(String fileName){ 59 | 60 | //ArrayList valStrings = new ArrayList(); 61 | 62 | //for (int i=0 ; i < snpList_.size() ; i++){ 63 | // String currentString = ""; 64 | // currentString += String.valueOf(snpScores_); 65 | // valStrings.add(currentString); 66 | // } 67 | String header="scores"; 68 | // WritingMethods.writeSnpPosWithValToFile(snpList_, valStrings,header, fileName,Settings.writeGenewiseSnpFiles_); 69 | //String corfileName = "corMat_" + fileName; 70 | WritingMethods.writeMTJ(cov_, fileName,Settings.writeGenewiseSnpFiles_); 71 | } 72 | 73 | 74 | public void writeGeneSnpsToFile(String fileName){ 75 | 76 | ArrayList valStrings = new ArrayList(); 77 | 78 | for (int i=0 ; i < snpScores_.size() ; i++){ 79 | String currentString = ""; 80 | currentString = String.valueOf(snpScores_.get(i)); 81 | valStrings.add(currentString); 82 | } 83 | String header="scores"; 84 | WritingMethods.writeSnpPosWithValToFile(snpList_, valStrings,header, fileName,Settings.writeGenewiseSnpFiles_); 85 | } 86 | 87 | public void processData(){ 88 | cov_ = LinkageDisequilibrium.computeCorrelationMatrixMTJ(snpList_); 89 | setSnpScores(); 90 | } 91 | public double[] returnWeights(boolean dummy){ 92 | int n= snpScores_.size(); 93 | double[] weights = new double[n]; 94 | for (int i=0; i mySnps = new ArrayList(); 80 | mySnps.add(new snpNeverCoding("id1")); 81 | mySnps.add(new snpAllwaysCoding("id2")); 82 | mySnps.add(new snpNeverCoding("id3")); 83 | GeneWithItsSnps myG = new GeneWithItsSnps(new Gene("gId"),mySnps); 84 | assertTrue(myG.getGene().getId().equals("gId")); 85 | 86 | assertTrue(myG.getNrOfSnps()==2); 87 | assertTrue(myG.getSnpList().get(0).id_.equals("id1")); 88 | assertTrue(myG.getSnpList().get(1).id_.equals("id3")); 89 | 90 | } 91 | 92 | @Test 93 | public void constructorTest2(){ 94 | 95 | 96 | Settings.removeCodingSnpsOfOtherGenes_=false; 97 | ArrayList mySnps = new ArrayList(); 98 | mySnps.add(new snpNeverCoding("id1")); 99 | mySnps.add(new snpAllwaysCoding("id2")); 100 | mySnps.add(new snpNeverCoding("id3")); 101 | GeneWithItsSnps myG = new GeneWithItsSnps(new Gene("gId"),mySnps); 102 | assertTrue(myG.getGene().getId().equals("gId")); 103 | 104 | assertTrue(myG.getNrOfSnps()==3); 105 | assertTrue(myG.getSnpList().get(0).id_.equals("id1")); 106 | assertTrue(myG.getSnpList().get(1).id_.equals("id2")); 107 | assertTrue(myG.getSnpList().get(2).id_.equals("id3")); 108 | 109 | } 110 | 111 | 112 | 113 | } 114 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/SnpPositionStream.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import java.io.DataInputStream; 25 | import java.io.IOException; 26 | 27 | import ch.unil.genescore.main.Main; 28 | 29 | public class SnpPositionStream 30 | extends OverlappedGenomicElementFileStream 31 | implements OverlappedGenomicElementStream { 32 | 33 | DataInputStream inStream_ = null; 34 | String binaryFileVersionID_= null; 35 | String filename_ = null; 36 | 37 | 38 | 39 | public SnpPositionStream(String filename,String binaryFileVersionID){ 40 | System.out.println("reading snp positions from file:" + filename); 41 | DataInputStream inStream = null; 42 | try { 43 | inStream = StreamMethods.openDataInputStream(filename, binaryFileVersionID); 44 | } 45 | catch(Exception e){ 46 | Main.error(e, "Error loading SNP position (try deleting the binary reference population files)"); 47 | } 48 | inStream_ = inStream; 49 | streamOpen_= true; 50 | filename_ = filename; 51 | binaryFileVersionID_ = binaryFileVersionID; 52 | } 53 | protected void loadElement() { 54 | 55 | Snp currentSnp = null; 56 | if (!streamOpen_) 57 | throw new RuntimeException("never use on closed stream"); 58 | 59 | String snpId = null; 60 | 61 | try { 62 | snpId = inStream_.readUTF(); 63 | } catch (IOException e) { 64 | // TODO Auto-generated catch block 65 | e.printStackTrace(); 66 | } 67 | // End of file is marked with the verion ID 68 | if (snpId.equals(binaryFileVersionID_)){ 69 | streamOpen_=false; 70 | currentElement_ = null; 71 | try { 72 | inStream_.close(); 73 | } catch (IOException e) { 74 | // TODO Auto-generated catch block 75 | e.printStackTrace(); 76 | } 77 | return; 78 | } 79 | currentSnp = new Snp(snpId); 80 | currentElement_ = new OverlappedGenomicElement(currentSnp); 81 | 82 | try { 83 | currentSnp.readPosAndAllele(inStream_); 84 | } catch (IOException e) { 85 | // TODO Auto-generated catch block 86 | e.printStackTrace(); 87 | } 88 | catch (DataInconsistencyException e) { 89 | // TODO Auto-generated catch block 90 | e.printStackTrace(); 91 | } 92 | } 93 | 94 | public Snp loadSnp(){ 95 | 96 | loadElement(); 97 | return ((Snp) currentElement_.getMainElement()); 98 | } 99 | 100 | public boolean streamOpen(){ return streamOpen_;} 101 | public void reOpenStream(){ 102 | if(streamOpen()){throw new RuntimeException("never call reopen on open stream");} 103 | DataInputStream inStream = null; 104 | try { 105 | inStream = StreamMethods.openDataInputStream(filename_, binaryFileVersionID_); 106 | } 107 | catch(Exception e){ 108 | Main.error(e, "Error loading SNP position (try deleting the binary reference population files)"); 109 | } 110 | inStream_ = inStream; 111 | streamOpen_= true; 112 | streamInitialized_ = false; 113 | 114 | 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/pathway/MetaGene.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.pathway; 23 | 24 | import java.util.ArrayList; 25 | import java.util.Iterator; 26 | import java.util.TreeSet; 27 | 28 | import ch.unil.genescore.gene.Gene; 29 | import ch.unil.genescore.gene.Genome; 30 | import ch.unil.genescore.vegas.Snp; 31 | 32 | 33 | /** 34 | * 35 | */ 36 | public class MetaGene extends Gene { 37 | 38 | /** The genes that are merged in this meta-gene */ 39 | private TreeSet genes_ = null; 40 | 41 | 42 | // ============================================================================ 43 | // PUBLIC METHODS 44 | 45 | /** Constructor */ 46 | public MetaGene(TreeSet genes) { 47 | 48 | super(null); 49 | initialize(genes); 50 | } 51 | 52 | 53 | // ---------------------------------------------------------------------------- 54 | 55 | /** Get the snps that are in the gene windows merged in this meta-gene */ 56 | @Override 57 | public ArrayList findSnps(Genome snps) { 58 | 59 | // Use a set to not include the same snp multiple times 60 | TreeSet geneSnps = new TreeSet(); 61 | for (Gene gene : genes_) 62 | geneSnps.addAll(gene.findSnps(snps)); 63 | 64 | return new ArrayList(geneSnps); 65 | } 66 | 67 | 68 | // ============================================================================ 69 | // PRIVATE METHODS 70 | 71 | /** Initialize this meta-gene with the given genes */ 72 | private void initialize(TreeSet genes) { 73 | 74 | if (genes == null || genes.size() < 2) 75 | throw new RuntimeException("Cannot create meta-gene with less than two genes"); 76 | 77 | genes_ = genes; 78 | 79 | Iterator iter = genes.iterator(); 80 | Gene first = iter.next(); 81 | 82 | id_ = "meta_" + first.id_; 83 | symbol_ = "meta_" + first.symbol_; 84 | chr_ = first.chr_; 85 | start_ = first.start_; 86 | end_ = first.end_; 87 | // Note, posStrand_ is not defined for meta-genes 88 | 89 | while (iter.hasNext()) { 90 | Gene gene = iter.next(); 91 | 92 | // Check ordering 93 | if (!gene.chr_.equals(chr_)) 94 | throw new RuntimeException("Genes of a meta-gene must be on same chromosome"); 95 | if (gene.start_ < start_) 96 | throw new RuntimeException("Genes of a meta-gene must be ordered by start position"); 97 | 98 | // Update end 99 | if (gene.end_ > end_) 100 | end_ = gene.end_; 101 | 102 | // Update id and symb 103 | id_ += "_" + gene.id_; 104 | symbol_ += "_" + gene.symbol_; 105 | 106 | } 107 | } 108 | 109 | 110 | // ============================================================================ 111 | // GETTERS AND SETTERS 112 | 113 | public TreeSet getGenes() { return genes_; } 114 | 115 | @Override 116 | public ArrayList getSymbolList(){ 117 | ArrayList myList = new ArrayList(); 118 | for (Gene gene : genes_){ 119 | myList.add(gene.symbol_); 120 | } 121 | return(myList); 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/pathway/Damper.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.pathway; 23 | 24 | import java.util.Collection; 25 | import java.util.Deque; 26 | import java.util.LinkedList; 27 | import java.util.ListIterator; 28 | import java.util.Queue; 29 | import java.util.Stack; 30 | import java.util.TreeSet; 31 | 32 | import ch.unil.genescore.gene.Gene; 33 | import ch.unil.genescore.main.Settings; 34 | 35 | public class Damper { 36 | /** Damper: takes in Collection of genes and lowers chi-square scores of genes that have neighbour with higher chi-sq-scores 37 | * nearby. Neighbourhood is defined as having boundaries no further away from each other than 'dist_'. 38 | * deflation of values is proceeds as: newVal=oldVal*(oldVal/maxVal)^(deflationRate_). 39 | * 40 | */ 41 | 42 | TreeSet mySet_ = null; 43 | Deque myGeneDeque_=null; 44 | //Deque myMaxDeque_=null; 45 | LinkedList myMaxDeque_=null; 46 | int dist_; 47 | double deflationRate_; 48 | public Damper(Collection genes, int dist, double deflationRate){ 49 | 50 | mySet_=new TreeSet(genes); 51 | dist_ = dist; 52 | deflationRate_=deflationRate; 53 | 54 | } 55 | 56 | 57 | public void dampSet(){ 58 | 59 | myGeneDeque_ = new LinkedList(); 60 | myMaxDeque_ = new LinkedList(); 61 | for (Gene el : mySet_){ 62 | while(!dequeFits(el)){ 63 | Gene poppedGene=null; 64 | Double poppedMax=null; 65 | poppedGene = myGeneDeque_.removeFirst(); 66 | poppedMax = myMaxDeque_.removeFirst(); 67 | double deflationFactor=Math.pow(poppedGene.getChi2Stat()/poppedMax,deflationRate_); 68 | poppedGene.setChi2Stat(poppedGene.getChi2Stat()*deflationFactor); 69 | } 70 | if (dequeFits(el)){ 71 | 72 | double dequeMax=0; 73 | ListIterator iter = myMaxDeque_.listIterator(); 74 | while (iter.hasNext()){ 75 | 76 | Double d = iter.next(); 77 | 78 | if (d < el.getChi2Stat()) 79 | iter.set(el.getChi2Stat()); 80 | if (d>dequeMax) 81 | dequeMax=d; 82 | 83 | } 84 | 85 | 86 | Double topOf=Math.max(dequeMax,el.getChi2Stat()); 87 | myGeneDeque_.addLast(el); 88 | myMaxDeque_.addLast(topOf); 89 | 90 | } 91 | } 92 | while (!myGeneDeque_.isEmpty()){ 93 | Gene poppedGene=null; 94 | Double poppedMax=null; 95 | poppedGene = myGeneDeque_.removeFirst(); 96 | poppedMax = myMaxDeque_.removeFirst(); 97 | double deflationFactor=Math.pow(poppedGene.getChi2Stat()/poppedMax,deflationRate_); 98 | poppedGene.setChi2Stat(poppedGene.getChi2Stat()*deflationFactor); 99 | } 100 | } 101 | 102 | private boolean dequeFits(Gene g) { 103 | if (myGeneDeque_.isEmpty()) 104 | return true; 105 | 106 | //boolean b =(myGeneDeque_.getFirst().chr_.equals(g.chr_) && myGeneDeque_.getFirst().end_ > g.start_- dist_); 107 | boolean b =(myGeneDeque_.getFirst().chr_.equals(g.chr_) && myGeneDeque_.getFirst().end_ > g.start_- dist_); 108 | return b; 109 | 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/test/FileParserTest.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas.test; 23 | 24 | import static org.junit.Assert.assertEquals; 25 | import static org.junit.Assert.assertTrue; 26 | 27 | import java.io.BufferedReader; 28 | import java.io.InputStreamReader; 29 | import java.io.Reader; 30 | import java.io.StringReader; 31 | import java.util.ArrayList; 32 | 33 | import org.junit.AfterClass; 34 | import org.junit.BeforeClass; 35 | import org.junit.Test; 36 | 37 | import ch.unil.genescore.main.FileParser; 38 | import ch.unil.genescore.main.Settings; 39 | 40 | public class FileParserTest { 41 | // ============================================================================ 42 | // SETUP 43 | 44 | @BeforeClass 45 | public static void testSetup() { 46 | Settings.loadSettings(); 47 | } 48 | 49 | @AfterClass 50 | public static void testCleanup() { } 51 | 52 | 53 | // ============================================================================ 54 | // TESTS 55 | class BufferedReaderStub extends BufferedReader{ 56 | 57 | private ArrayList myOut = new ArrayList(); 58 | int counter=0; 59 | public BufferedReaderStub() { 60 | super(new StringReader("blub.txt")); 61 | 62 | } 63 | public BufferedReaderStub(Reader rd) { 64 | super(rd); 65 | } 66 | public void setMyOut(ArrayList out){ 67 | myOut=out; 68 | } 69 | @Override 70 | public String readLine(){ 71 | if (counter < myOut.size()){ 72 | String outStr= myOut.get(counter); 73 | counter++; 74 | return outStr; 75 | } 76 | else{ 77 | return null; 78 | } 79 | } 80 | } 81 | 82 | 83 | 84 | 85 | @Test 86 | public void fileParserTest(){ 87 | ArrayList myStrs= new ArrayList(); 88 | String str1="first\tfirst2"; 89 | myStrs.add(str1); 90 | String str2="second\tsecond2"; 91 | myStrs.add(str2); 92 | String str3="third\tthird2"; 93 | myStrs.add(str3); 94 | BufferedReaderStub myStub = new BufferedReaderStub(); 95 | myStub.setMyOut(myStrs); 96 | FileParser fp= new FileParser(myStub); 97 | 98 | assertTrue(fp.readLineAsString().equals(str1)); 99 | assertTrue(fp.lineAvailable()); 100 | assertTrue(fp.readLineAsString().equals(str2)); 101 | assertTrue(fp.lineAvailable()); 102 | assertTrue(fp.readLineAsString().equals(str3)); 103 | assertTrue(!fp.lineAvailable()); 104 | assertTrue(fp.readLineAsString()==null); 105 | assertTrue(!fp.lineAvailable()); 106 | 107 | myStub = new BufferedReaderStub(); 108 | myStub.setMyOut(myStrs); 109 | fp= new FileParser(myStub); 110 | String[] line=fp.readLine(); 111 | assertTrue(line[0].equals("first")); 112 | assertTrue(fp.lineAvailable()); 113 | line=fp.readLine(); 114 | assertTrue(line[0].equals("second")); 115 | assertTrue(fp.lineAvailable()); 116 | line=fp.readLine(); 117 | assertTrue(line[0].equals("third")); 118 | assertTrue(!fp.lineAvailable()); 119 | assertTrue(fp.readLine()==null); 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/gene/GeneIdMapping.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.gene; 23 | 24 | import java.util.HashMap; 25 | import java.util.HashSet; 26 | 27 | import ch.unil.genescore.main.FileParser; 28 | import ch.unil.genescore.main.Main; 29 | import ch.unil.genescore.main.Settings; 30 | 31 | 32 | /** 33 | * 34 | */ 35 | public class GeneIdMapping { 36 | 37 | /** The unique instance of the mapping (Singleton design pattern) */ 38 | static private GeneIdMapping instance_ = null; 39 | 40 | /** Mapping ensembl to entrez */ 41 | private HashMap> ensembl2entrez_ = null; 42 | 43 | 44 | // ============================================================================ 45 | // PUBLIC METHODS 46 | 47 | /** Get the unique instance */ 48 | public static GeneIdMapping getInstance() { 49 | 50 | if (instance_ == null) 51 | instance_ = new GeneIdMapping(); 52 | 53 | return instance_; 54 | } 55 | 56 | 57 | // ---------------------------------------------------------------------------- 58 | 59 | /** Map ensembl to entrez ids */ 60 | public HashSet ensembl2entrez(String ensemblId) { 61 | 62 | return ensembl2entrez_.get(ensemblId); 63 | } 64 | 65 | 66 | // ============================================================================ 67 | // PRIVATE METHODS 68 | 69 | /** Private constructor, loads mapping */ 70 | private GeneIdMapping() { 71 | 72 | load(Settings.geneIdMappingFile_); 73 | } 74 | 75 | 76 | // ---------------------------------------------------------------------------- 77 | 78 | /** Load the mapping */ 79 | private void load(String filename) { 80 | 81 | ensembl2entrez_ = new HashMap>(); 82 | FileParser parser = new FileParser(filename); 83 | 84 | while(true) { 85 | // Read next line 86 | String[] nextLine = parser.readLine(); 87 | if (nextLine == null) 88 | break; 89 | 90 | // Check number of columns 91 | if (nextLine.length != 3) 92 | Main.error("Expected three columns (ensembl id, entrez id, gene symbol)"); 93 | 94 | // Parse ensembl id 95 | String ensg = nextLine[0]; 96 | if (!(ensg.length() > 4 && ensg.substring(0, 4).equals("ENSG"))) 97 | Main.error("Invalid ENSEMBL gene ID (expected 'ENSG...'): " + ensg); 98 | ensg = GeneAnnotationGencode.removeEnsemblVersion(ensg); 99 | 100 | // Parse entrez id 101 | String entrez = nextLine[1]; 102 | if (entrez.length() > 0) { 103 | try { 104 | Integer.valueOf(entrez); 105 | } catch (NumberFormatException e) { 106 | Main.error("Invalid Entrez gene ID (expected an integer number): " + entrez); 107 | } 108 | } 109 | 110 | // Parse gene symbol 111 | //String symbol = nextLine[2]; 112 | 113 | HashSet entrezSet = ensembl2entrez_.get(ensg); 114 | if (entrezSet == null) { 115 | entrezSet = new HashSet(1); 116 | ensembl2entrez_.put(ensg, entrezSet); 117 | } 118 | entrezSet.add(entrez); 119 | } 120 | } 121 | 122 | 123 | 124 | // ============================================================================ 125 | // GETTERS AND SETTERS 126 | 127 | 128 | } 129 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/EmpiricalPvalue.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import ch.unil.genescore.main.Main; 25 | import ch.unil.genescore.main.Settings; 26 | 27 | 28 | /** 29 | * Summarize p-values of multiple snps at a given locus 30 | */ 31 | public class EmpiricalPvalue { 32 | 33 | /** The observed test statistic */ 34 | private double testStatisticReal_ = -1; 35 | /** The number of samples that are greater or equal than the test statistic */ 36 | private int numSamplesGreater_ = -1; 37 | /** The total number of samples done (how many times addSample() was called since the last reset()) */ 38 | private int numSamples_ = -1; 39 | 40 | 41 | // ============================================================================ 42 | // PUBLIC METHODS 43 | 44 | /** Constructor */ 45 | public EmpiricalPvalue(double testStatisticObserved) { 46 | 47 | testStatisticReal_ = testStatisticObserved; 48 | } 49 | 50 | 51 | // ---------------------------------------------------------------------------- 52 | 53 | /** Add the next sample */ 54 | public void addSample(double x) { 55 | 56 | // Doesn't make a difference if we use > or >= because we're comparing random doubles 57 | if (x > testStatisticReal_) 58 | numSamplesGreater_++; 59 | } 60 | 61 | 62 | // ---------------------------------------------------------------------------- 63 | 64 | /** Reset the sample counts */ 65 | public void start() { 66 | 67 | numSamplesGreater_ = 0; 68 | numSamples_ = 0; 69 | } 70 | 71 | 72 | // ---------------------------------------------------------------------------- 73 | 74 | /** Give the number of samples that were done since the last reset(), returns true if stopping criteria are met */ 75 | public boolean stop(int numSamples) { 76 | 77 | numSamples_ = numSamples; 78 | return numSamplesGreater_ > Settings.numSamplesGreaterCutoff_; 79 | //return false; 80 | } 81 | 82 | 83 | // ---------------------------------------------------------------------------- 84 | 85 | /** Get the current p-value (assumes that addSample() has been called numSample times) */ 86 | public double getPval() { 87 | 88 | // Check that there are more samples greater than the observed statistic than the cutoff, or the max number of samples was reached 89 | assert numSamplesGreater_ > Settings.numSamplesGreaterCutoff_ || numSamples_ == Settings.adaptiveNumSamples_.get(Settings.adaptiveNumSamples_.size()-1); 90 | 91 | // Note, the correct (unbiased) estimate of a p-value from monte carlo sampling is not r/n, but (r+1)/(n+1) 92 | if (numSamples_ <= 0) 93 | Main.error("Number of samples <= 0, stop() may not have been called"); 94 | 95 | return (numSamplesGreater_ + 1.0) / (numSamples_ + 1.0); 96 | } 97 | 98 | 99 | // ============================================================================ 100 | // PRIVATE METHODS 101 | 102 | 103 | // ============================================================================ 104 | // GETTERS AND SETTERS 105 | 106 | public double getTestStatisticReal() { return testStatisticReal_; } 107 | public int getNumSamplesGreater() { return numSamplesGreater_; } 108 | public int getNumSamples() { return numSamples_; } 109 | 110 | } 111 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/LoadedGenotypes.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import java.io.IOException; 25 | import java.util.LinkedList; 26 | 27 | import ch.unil.genescore.gene.GenomicElement; 28 | import ch.unil.genescore.main.Main; 29 | import ch.unil.genescore.main.Settings; 30 | 31 | public class LoadedGenotypes { 32 | 33 | /** A stack with the SNPs for which genotypes are currently loaded, newly loaded snps are put on top */ 34 | private LinkedList snpsWithGenotypes_ = null; 35 | 36 | /** The current chromosome */ 37 | private String loadedChr_ = "not_initialized"; 38 | /** The start and end coordinates of the region for which genotypes are currently loaded */ 39 | private int loadedStart_ = -1; 40 | /** The start and end coordinates of the region for which genotypes are currently loaded */ 41 | private int loadedEnd_ = -1; 42 | /** Flag set when end of file is reached */ 43 | protected boolean endOfFile_ = false; 44 | private ReferencePopulation refpop_ =null; 45 | public LoadedGenotypes(){ 46 | snpsWithGenotypes_ = new LinkedList(); 47 | } 48 | 49 | public LoadedGenotypes(ReferencePopulation refpop){ 50 | snpsWithGenotypes_ = new LinkedList(); 51 | refpop_ = refpop; 52 | } 53 | 54 | 55 | 56 | /** 57 | * Load genotypes for SNPs in the window around the given gene, delete genotypes of SNPs 58 | * that are not needed anymore. 59 | */ 60 | public void updateLoadedGenotypes(GenomicElement gene) { 61 | 62 | // If the gene is not on the current chromosome, delete all loaded genotypes, reinitialize and open binary file 63 | if (!gene.chr_.equals(loadedChr_)) 64 | refpop_.initialize(gene.chr_); 65 | else if (endOfFile_) 66 | return; 67 | 68 | // Boundaries of region for which genotypes should be loaded (note, with meta-genes we don't really know what's up and down, that's why we use max()) 69 | int d = Math.max(Settings.geneWindowDownstream_, Settings.geneWindowUpstream_); 70 | int laxityFactor=3000000; 71 | int newStart = gene.start_ - d - laxityFactor; 72 | int newEnd = gene.end_ + d + laxityFactor;; 73 | 74 | // Remove snps before newStart from the front of the list and delete their genotypes 75 | while (getSnpsWithGenotypes().size() > 0 && getSnpsWithGenotypes().getFirst().start_ < newStart) 76 | getSnpsWithGenotypes().poll().setGenotypes(null); 77 | 78 | loadedStart_ = newStart; 79 | while (loadedEnd_ < newEnd && !endOfFile_) 80 | refpop_.loadNextGenotype(); // updates loadedEnd_ 81 | 82 | // The first snp of the list should be the first gwas snp > loadedStart 83 | assert getSnpsWithGenotypes().size() == 0 || getSnpsWithGenotypes().getFirst().start_ >= loadedStart_; 84 | // The last snp of the list should be the first gwas snp > loadedEnd (we load one too many), unless the end of the file has been reached 85 | assert getSnpsWithGenotypes().size() == 0 || endOfFile_ || getSnpsWithGenotypes().getLast().start_ >= loadedEnd_; 86 | } 87 | 88 | 89 | 90 | 91 | public LinkedList getSnpsWithGenotypes() { 92 | return snpsWithGenotypes_; 93 | } 94 | 95 | public void setSnpsWithGenotypes(LinkedList snpsWithGenotypes) { 96 | this.snpsWithGenotypes_ = snpsWithGenotypes; 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/ReferencePopulationFakeSignal.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import java.io.DataInputStream; 25 | import java.io.IOException; 26 | import java.util.ArrayList; 27 | import java.util.Random; 28 | 29 | import ch.unil.genescore.main.Main; 30 | import ch.unil.genescore.main.Settings; 31 | 32 | 33 | public class ReferencePopulationFakeSignal { 34 | // public class ReferencePopulationFakeSignal { 35 | 36 | private ReferencePopulation refpop_ = new ReferencePopulation(); 37 | 38 | Snp currentSnp_ = null; 39 | Random rnd = new Random(Settings.randomSeed_); 40 | ArrayList snpNames_= new ArrayList(); 41 | double[] signal_ = null; 42 | 43 | 44 | private void initializeChr(String chr) { 45 | 46 | refpop_.closeDataInputStream(); 47 | 48 | // Open input binary file 49 | String filename = refpop_.getGenotypeFileHandler().getFilePrefix() + chr + ".gnt.ser.gz"; 50 | refpop_.openDataInputStream(filename); 51 | 52 | // Read genotype vector length and phased flag 53 | try { 54 | int length = refpop_.getInStream().readInt(); 55 | boolean phased = refpop_.getInStream().readBoolean(); 56 | 57 | Snp.setGenotypeLength(length); 58 | Snp.setGenotypeIsPhased(phased); 59 | if (signal_ == null){ 60 | signal_ = new double[length]; 61 | } 62 | 63 | } catch (IOException e) { 64 | Main.error(e, "Error reading genotype length"); 65 | } 66 | if (Snp.getGenotypeIsPhased()==true){ 67 | 68 | throw new RuntimeException("problem in method generating fake phenotype with signal: saved genotype is phased but has not been treated yet"); 69 | } 70 | 71 | } 72 | public void runFakeSignal(){ 73 | 74 | for (String chr : refpop_.chromosomes_){ 75 | runChr(chr); 76 | } 77 | 78 | } 79 | 80 | private void runChr(String chr){ 81 | 82 | initializeChr(chr); 83 | while(!refpop_.isEndOfFile()){ 84 | loadSnpGenotype(); 85 | if(!refpop_.isEndOfFile()) 86 | processSnp(); 87 | } 88 | } 89 | 90 | /** Load the next snp from the input stream */ 91 | private void loadSnpGenotype() { 92 | 93 | try { 94 | String snpId = refpop_.getInStream().readUTF(); 95 | // End of file is marked with the version ID 96 | if (snpId.equals(refpop_.getGenotypeFileHandler().getBinaryFileVersionID())) { 97 | refpop_.setEndOfFile(true); 98 | return; 99 | } 100 | currentSnp_ = new Snp(snpId); 101 | currentSnp_.readGenotype(refpop_.getInStream()); 102 | } catch (Exception e) { 103 | Main.error(e, "Error loading genotypes"); 104 | } 105 | } 106 | 107 | /** Load the next snp from the input stream */ 108 | private void processSnp() { 109 | double maf; 110 | double rndNr; 111 | double beta; 112 | double sd; 113 | byte[] genotypes = null; 114 | currentSnp_.computeAlleleStats(); 115 | maf = currentSnp_.getMaf(); 116 | sd = currentSnp_.getAlleleSd(); 117 | if (maf > Settings.useMafCutoff_ || sd==0){ 118 | rndNr = rnd.nextDouble(); 119 | if (rndNr < Settings.chanceOfSignal_){ 120 | beta = 10;//rnd.nextGaussian()*10; 121 | genotypes = currentSnp_.getGenotypes(); 122 | assert(genotypes.length==signal_.length); 123 | for (int i=0; i snpScores, DenseMatrix ld, double maxAllowedSimulRuns) { 46 | super(snpScores, ld); 47 | maxAllowedSimulRuns_=maxAllowedSimulRuns; 48 | 49 | } 50 | 51 | public VegasSimulationNew(ArrayList snpScores, UpperSymmDenseMatrix ld, double maxAllowedSimulRuns) { 52 | super(snpScores, ld); 53 | maxAllowedSimulRuns_=maxAllowedSimulRuns; 54 | 55 | } 56 | @Override 57 | protected double computeTestStatisticRealSubclass() { 58 | maxStat_=0; 59 | if (Settings.withZScore_){ 60 | for (int i=0; i maxAllowedSimulRuns_){ 107 | tooManySimuls_=true; 108 | count=0; 109 | break; 110 | } 111 | // System.out.println("nr of Simuls " + simulRuns_); 112 | for (int i=0; i maxStat_){ 117 | count++; 118 | } 119 | } 120 | } 121 | geneScore_ = count/simulRuns_; 122 | return true; 123 | } 124 | 125 | private void simulateNormals(DenseVector normVect) { 126 | for (int i=0; i loadAnnotation() { 49 | // TODO Auto-generated method stub 50 | genes_ = new LinkedHashMap(); 51 | HashSet inconsistentEntries = new HashSet(); 52 | HashSet inconsistentStrand = new HashSet(); 53 | System.out.println("Loading annotation: " + annotationFile_); 54 | FileParser parser = new FileParser(annotationFile_); 55 | 56 | // Skip the header lines (start with #) 57 | String[] nextLine = parser.readLine(); 58 | while (nextLine[0].startsWith("#")) 59 | nextLine = parser.readLine(); 60 | 61 | while (nextLine != null) { 62 | // Check number of columns 63 | if (nextLine.length != 6) 64 | parser.error("Expected 6 columns"); 65 | 66 | // Chromosome 67 | String chr = nextLine[0]; 68 | // Continue if this chromosome should not be loaded based on settings 69 | if (skipChromosome(chr)) { 70 | nextLine = parser.readLine(); 71 | continue; 72 | } 73 | 74 | // Gene id and name 75 | String geneId = nextLine[3]; 76 | String geneName = nextLine[3]; 77 | 78 | // If a gene set to be loaded was specified and this gene is NOT in this set, continue 79 | if (!checkGenesToBeLoaded(geneId, geneName)) { 80 | nextLine = parser.readLine(); 81 | continue; 82 | } 83 | 84 | 85 | // Strand, start and end 86 | boolean posStrand = GenomicElement.isPosStrand(nextLine[5]); 87 | int start = Integer.parseInt(nextLine[1]); 88 | int end = Integer.parseInt(nextLine[2]); 89 | 90 | Gene nextGene = genes_.get(geneId); 91 | // Create new gene 92 | if (nextGene == null) { 93 | nextGene = new Gene(geneId, geneName); 94 | nextGene.setPosition(chr, start, end, posStrand); 95 | genes_.put(geneId, nextGene); 96 | 97 | // Update existing gene 98 | } else { 99 | // Check that symbol, chr and position are consistent 100 | if (nextGene.chr_.equals(chr) && nextGene.symbol_.equals(geneName) && 101 | (Math.abs(nextGene.start_ - start) < 1e6 || Math.abs(nextGene.end_ - end) < 1e6)) { 102 | 103 | // If only the strand is not consistent, we'll just give a warning 104 | if (nextGene.posStrand_ != posStrand) { 105 | // We believe the longer gene 106 | if (end - start > nextGene.end_ - nextGene.start_) 107 | nextGene.posStrand_ = posStrand; 108 | inconsistentStrand.add(geneId); 109 | } 110 | 111 | // Update start and end 112 | if (start < nextGene.start_) 113 | nextGene.start_ = start; 114 | if (end > nextGene.end_) 115 | nextGene.end_ = end; 116 | 117 | } else { 118 | inconsistentEntries.add(geneId); 119 | genes_.remove(geneId); 120 | } 121 | } 122 | nextLine = parser.readLine(); 123 | } 124 | parser.close(); 125 | 126 | // Sort the genes 127 | Genome genome = new Genome(genes_.values()); 128 | genes_ = (LinkedHashMap) genome.asLinkedHashMap(); 129 | 130 | return genes_; 131 | 132 | 133 | } 134 | 135 | } 136 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/test/OverlappedCollectionStreamTest.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas.test; 23 | 24 | import java.util.ArrayList; 25 | import java.util.Collections; 26 | 27 | import ch.unil.genescore.gene.Gene; 28 | import ch.unil.genescore.vegas.OverlappedGenomicElement; 29 | import static org.junit.Assert.*; 30 | 31 | 32 | import org.junit.AfterClass; 33 | import org.junit.BeforeClass; 34 | import org.junit.Rule; 35 | import org.junit.Test; 36 | import org.junit.rules.ExpectedException; 37 | 38 | import ch.unil.genescore.vegas.OverlappedCollectionStream; 39 | 40 | 41 | 42 | /** 43 | * Unit tests for GenewiseSnpWeightsTest 44 | */ 45 | public class OverlappedCollectionStreamTest { 46 | 47 | 48 | static double delta; 49 | // ============================================================================ 50 | // SETUP 51 | 52 | @BeforeClass 53 | public static void testSetup() { 54 | delta=1E-14; 55 | } 56 | 57 | @AfterClass 58 | public static void testCleanup() { } 59 | 60 | @Rule 61 | public ExpectedException thrown= ExpectedException.none(); 62 | 63 | // ============================================================================ 64 | // TESTS 65 | 66 | /** Test PathwayMain.run() */ 67 | @Test 68 | public void RunCollectionStream1Test() { 69 | // test standard application 70 | String chr1="chr19"; 71 | String chr2="chr22"; 72 | Gene g1 = new Gene("g1"); 73 | Gene g2 = new Gene("g2"); 74 | Gene g3 = new Gene("g3"); 75 | g1.setPosition(chr1, 1000, 2000, true); 76 | g2.setPosition(chr1, 1900, 3000, true); 77 | g3.setPosition(chr2, 1000, 2000, true); 78 | 79 | ArrayList gArray = new ArrayList(3); 80 | gArray.add(new OverlappedGenomicElement(g1)); 81 | gArray.add(new OverlappedGenomicElement(g2)); 82 | gArray.add(new OverlappedGenomicElement(g3)); 83 | Collections.sort(gArray); 84 | OverlappedCollectionStream str = new OverlappedCollectionStream(gArray); 85 | OverlappedGenomicElement el1 = str.getNextAsOverlappedGenomicElement(); 86 | OverlappedGenomicElement el2 = str.getNextAsOverlappedGenomicElement(); 87 | OverlappedGenomicElement el3 = str.getNextAsOverlappedGenomicElement(); 88 | 89 | assertTrue(el1.getMainElement()==g1); 90 | assertTrue(el2.getMainElement()==g2); 91 | assertTrue(el3.getMainElement()==g3); 92 | } 93 | @Test 94 | public void RunCollectionStreamTest2() { 95 | //Test closed stream exception 96 | String chr1="chr19"; 97 | Gene g1 = new Gene("g1"); 98 | g1.setPosition(chr1, 1000, 2000, true); 99 | ArrayList gArray = new ArrayList(3); 100 | gArray.add(new OverlappedGenomicElement(g1)); 101 | Collections.sort(gArray); 102 | OverlappedCollectionStream str = new OverlappedCollectionStream(gArray); 103 | OverlappedGenomicElement el1 = str.getNextAsOverlappedGenomicElement(); 104 | thrown.expect(RuntimeException.class); 105 | OverlappedGenomicElement el2 = str.getNextAsOverlappedGenomicElement(); 106 | } 107 | @Test 108 | public void RunCollectionStream3Test() { 109 | // test unsorted exception 110 | String chr1="chr2"; 111 | String chr2="chr12"; 112 | Gene g1 = new Gene("g1"); 113 | Gene g2 = new Gene("g2"); 114 | Gene g3 = new Gene("g3"); 115 | g1.setPosition(chr1, 1000, 2000, true); 116 | g2.setPosition(chr1, 1900, 3000, true); 117 | g3.setPosition(chr2, 1000, 2000, true); 118 | 119 | ArrayList gArray = new ArrayList(3); 120 | 121 | gArray.add(new OverlappedGenomicElement(g1)); 122 | gArray.add(new OverlappedGenomicElement(g2)); 123 | gArray.add(new OverlappedGenomicElement(g3)); 124 | 125 | OverlappedCollectionStream str = new OverlappedCollectionStream(gArray); 126 | thrown.expect(RuntimeException.class); 127 | OverlappedGenomicElement el1 = str.getNextAsOverlappedGenomicElement(); 128 | OverlappedGenomicElement el2 = str.getNextAsOverlappedGenomicElement(); 129 | OverlappedGenomicElement el3 = str.getNextAsOverlappedGenomicElement(); 130 | } 131 | } -------------------------------------------------------------------------------- /src/ch/unil/genescore/gene/Chromosome.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.gene; 23 | 24 | import java.util.ArrayList; 25 | import java.util.Collection; 26 | import java.util.Map.Entry; 27 | import java.util.TreeMap; 28 | 29 | 30 | /** 31 | * A chromosome with its genomic elements 32 | */ 33 | public class Chromosome { 34 | 35 | /** 36 | * The genomic elements of this chromosome, sorted by starting position. 37 | * Note: Using TreeMap implies that we cannot have two distinct elements with the same start_. 38 | * Using TreeSet would resolve this, but make some operations a bit less elegant. 39 | * I guess for SNPs and genes, we wouldn't have two different ones with the same start anyway. 40 | */ 41 | private TreeMap elements_ = null; 42 | 43 | 44 | // ============================================================================ 45 | // PUBLIC METHODS 46 | 47 | /** Constructor */ 48 | public Chromosome() { 49 | 50 | elements_ = new TreeMap(); 51 | } 52 | 53 | 54 | // ---------------------------------------------------------------------------- 55 | 56 | /** Return true if the chr is chr1-22 or chrX,Y,M (also returns true for chr23,... too lazy to fix) */ 57 | public static boolean isValidId(String chr) { 58 | 59 | return chr.matches("^chr(([1-9][0-9]?)|([XYM]))$"); 60 | } 61 | 62 | 63 | // ---------------------------------------------------------------------------- 64 | 65 | /** Convert chromosome ID to int (1-22; 23, 24, 25 for chrX, chrY and chrM, respectively) */ 66 | public static int asInt(String chr) { 67 | 68 | if (chr.equals("chrX")) 69 | return 23; 70 | if (chr.equals("chrY")) 71 | return 24; 72 | if (chr.equals("chrM")) 73 | return 25; 74 | 75 | int chrNum = Integer.parseInt(chr.substring(3)); 76 | if (chrNum < 1 || chrNum > 22) 77 | throw new RuntimeException("Unknown chromosome ID: " + chr); 78 | else 79 | return chrNum; 80 | } 81 | 82 | 83 | // ---------------------------------------------------------------------------- 84 | 85 | /** Add the given element to this chromosome */ 86 | public void addElement(GenomicElement element) { 87 | 88 | elements_.put(element.start_, element); 89 | } 90 | 91 | 92 | // ---------------------------------------------------------------------------- 93 | 94 | /** Get the elements in the given window */ 95 | public ArrayList getElementsIn(int start, int end) { 96 | 97 | return new ArrayList(elements_.subMap(start, true, end, true).values()); 98 | } 99 | 100 | 101 | // ---------------------------------------------------------------------------- 102 | 103 | /** Get all elements */ 104 | public Collection getElements() { 105 | 106 | return elements_.values(); 107 | } 108 | 109 | 110 | // ---------------------------------------------------------------------------- 111 | 112 | /** Get the nearest neighboring element (based on the start_ positions) */ 113 | public GenomicElement getNearestElement(int pos) { 114 | 115 | Entry prev = elements_.floorEntry(pos); 116 | Entry next = elements_.ceilingEntry(pos); 117 | 118 | // The case where either one or both are null 119 | if (prev == null && next == null) 120 | return null; 121 | if (prev == null) 122 | return next.getValue(); 123 | if (next == null) 124 | return prev.getValue(); 125 | 126 | // Distance to the two neighbors 127 | int deltaPrev = pos - prev.getKey(); 128 | int deltaNext = next.getKey() - pos; 129 | assert deltaPrev >= 0 && deltaNext >= 0; 130 | 131 | // Return the closer 132 | if (deltaPrev <= deltaNext) 133 | return prev.getValue(); 134 | else 135 | return next.getValue(); 136 | } 137 | 138 | 139 | // ============================================================================ 140 | // PRIVATE METHODS 141 | 142 | 143 | // ============================================================================ 144 | // GETTERS AND SETTERS 145 | 146 | public TreeMap getElementTreeMap() { return elements_; } 147 | 148 | public int getNumElements() { return elements_.size(); } 149 | } 150 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/pathway/EffTestCalculator.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.pathway; 23 | 24 | import java.util.ArrayList; 25 | import java.util.Collection; 26 | import java.util.HashMap; 27 | import java.util.Map.Entry; 28 | import java.util.Set; 29 | 30 | import org.apache.commons.math3.distribution.ChiSquaredDistribution; 31 | 32 | import ch.unil.genescore.gene.Gene; 33 | 34 | /** 35 | * Calculates the effective number of Tests for the given pathway library. 36 | * Atm only implements chi2-statistic 37 | */ 38 | public class EffTestCalculator { 39 | /**genes_: genes that contain random scores. these scores are sampled. 40 | * */ 41 | private HashMap genes_ = null; 42 | private HashMap indexMap_ = new HashMap(); 43 | //null; 44 | private double[] geneVals_ = null; 45 | /**geneSets_: geneSets_ that are used to get ids: 46 | * these are the genes that have been calculated: Do not change internally. 47 | * */ 48 | private ArrayList geneSets_ = null; 49 | private double[] cutoffs_ = null; 50 | private double[] cutoffsTheor_ = null; 51 | private static ChiSquaredDistribution chiSquared1df_ = new ChiSquaredDistribution(1); 52 | 53 | 54 | public void addGenes(Collection genes){ 55 | for (Gene gene : genes){ 56 | Gene fake = new Gene(gene.id_); 57 | fake.setChi2Stat(chiSquared1df_.sample()); 58 | genes_.put(gene.id_,fake); 59 | } 60 | } 61 | public void addMetaGenes(HashMap metaGenes){ 62 | for (Entry e : metaGenes.entrySet()){ 63 | Gene g = e.getValue(); 64 | Gene fake = new Gene(g.id_); 65 | fake.setChi2Stat(chiSquared1df_.sample()); 66 | genes_.put(g.id_,fake); 67 | } 68 | } 69 | 70 | public void initializeGeneVals(){ 71 | geneVals_ = new double[indexMap_.size()]; 72 | } 73 | 74 | 75 | public void addGeneIds(Collection genes){ 76 | for (Gene gene : genes){ 77 | indexMap_.put(gene.id_,indexMap_.size()); 78 | } 79 | } 80 | public void addMetaGeneIds(HashMap metaGenes){ 81 | for (Entry e : metaGenes.entrySet()){ 82 | Gene gene = e.getValue(); 83 | indexMap_.put(gene.id_,indexMap_.size()); 84 | } 85 | } 86 | protected void calcGeneVals(){ 87 | 88 | for (int i=0 ; i< geneVals_.length; i++){ 89 | geneVals_[i]=chiSquared1df_.sample(); 90 | } 91 | } 92 | 93 | 94 | 95 | 96 | private double getMinScores(double[] score){ 97 | double min=1; 98 | for (double d : score){ 99 | if (d ids){ 113 | int[] indSet= new int[ids.size()]; 114 | for (int i=0 ; i< ids.size();i++){ 115 | String id = ids.get(i); 116 | if(!indexMap_.containsKey(id)){ 117 | throw new RuntimeException("Not all ids present"); 118 | } 119 | indSet[i]=indexMap_.get(id); 120 | } 121 | return(indSet); 122 | } 123 | 124 | double getScoreSum(int[] indSet){ 125 | double scoreSum=0; 126 | for (int i=0; i gIds = geneSets_.get(i).getGeneIds(); 137 | int[] indSet = getIndexSet(gIds); 138 | double scoreSum = getScoreSum(indSet); 139 | chiSquaredNdf = new ChiSquaredDistribution(indSet.length); 140 | setScores[i] = 1-chiSquaredNdf.cumulativeProbability(scoreSum); 141 | } 142 | return(setScores); 143 | } 144 | 145 | public void recalcFakeScores(){ 146 | Set> gg = genes_.entrySet(); 147 | for (Entry g: gg){ 148 | g.getValue().setChi2Stat(chiSquared1df_.sample()); 149 | } 150 | } 151 | 152 | public void setGeneSets(ArrayList geneSets){ 153 | geneSets_= geneSets; 154 | } 155 | 156 | 157 | 158 | 159 | } 160 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/test/DistributionMethodsTest.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas.test; 23 | 24 | import static org.junit.Assert.assertEquals; 25 | 26 | import org.apache.commons.math3.distribution.ChiSquaredDistribution; 27 | import org.apache.commons.math3.distribution.NormalDistribution; 28 | import org.junit.AfterClass; 29 | import org.junit.BeforeClass; 30 | import org.junit.Test; 31 | 32 | import ch.unil.genescore.main.Settings; 33 | import ch.unil.genescore.vegas.DistributionMethods; 34 | 35 | 36 | 37 | 38 | 39 | public class DistributionMethodsTest { 40 | 41 | private static ChiSquaredDistribution chiSquared1df_ = new ChiSquaredDistribution(1); 42 | private static NormalDistribution normalDist_ = new NormalDistribution(); 43 | 44 | // ============================================================================ 45 | // SETUP 46 | 47 | @BeforeClass 48 | public static void testSetup() { 49 | } 50 | 51 | @AfterClass 52 | public static void testCleanup() { } 53 | 54 | @Test 55 | public void testChiSquared1dfCumulativeProbabilityUpperTail(){ 56 | 57 | double val=DistributionMethods.chiSquared1dfCumulativeProbabilityUpperTail(49); 58 | System.out.println(val); 59 | assertEquals(2.559619e-12,val, 0.01e-12); 60 | val=DistributionMethods.chiSquared1dfCumulativeProbabilityUpperTail(60); 61 | System.out.println(val); 62 | assertEquals(9.436896e-15,val, 0.3e-15); 63 | val=DistributionMethods.chiSquared1dfCumulativeProbabilityUpperTail(70); 64 | System.out.println(val); 65 | assertEquals(5.930446e-17,val, 0.3e-17); 66 | val=DistributionMethods.chiSquared1dfCumulativeProbabilityUpperTail(1373.873); 67 | System.out.println(val); 68 | assertEquals(1e-300,val, 0.01e-300); 69 | } 70 | 71 | @Test 72 | public void testChiSquared1dfInverseCumulativeProbabilityUpperTail(){ 73 | 74 | double val=DistributionMethods.chiSquared1dfInverseCumulativeProbabilityUpperTail(1e-10); 75 | System.out.println(val); 76 | assertEquals(41.82146,val, 0.1); 77 | val=DistributionMethods.chiSquared1dfInverseCumulativeProbabilityUpperTail(2e-14); 78 | System.out.println(val); 79 | assertEquals(58.53368,val, 0.1); 80 | val=DistributionMethods.chiSquared1dfInverseCumulativeProbabilityUpperTail(1e-14); 81 | System.out.println(val); 82 | assertEquals(59.8976,val, 0.1); 83 | val=DistributionMethods.chiSquared1dfInverseCumulativeProbabilityUpperTail(1e-18); 84 | assertEquals(78.05916,val, 0.1); 85 | val=DistributionMethods.chiSquared1dfInverseCumulativeProbabilityUpperTail(1e-200); 86 | System.out.println(val); 87 | assertEquals(913.7627,val, 0.1); 88 | val=DistributionMethods.chiSquared1dfInverseCumulativeProbabilityUpperTail(1e-200); 89 | System.out.println(val); 90 | assertEquals(913.7627,val, 0.1); 91 | val=DistributionMethods.chiSquared1dfInverseCumulativeProbabilityUpperTail(1e-300); 92 | System.out.println(val); 93 | assertEquals(1373.873,val, 0.1); 94 | } 95 | 96 | @Test 97 | public void testNormalCumulativeProbability(){ 98 | 99 | double val=DistributionMethods.normalCumulativeProbability(3); 100 | assertEquals( 0.9986501,val, 0.000001); 101 | val=DistributionMethods.normalCumulativeProbability(-8); 102 | assertEquals(6.220961e-16,val,0.2e-16); 103 | val=DistributionMethods.normalCumulativeProbability(-8.1); 104 | assertEquals(2.747959e-16,val,0.2e-16); 105 | val=DistributionMethods.normalCumulativeProbability(-20); 106 | assertEquals(2.753624e-89,val,0.2e-89); 107 | } 108 | 109 | @Test 110 | public void TestNormalInverseCumulativeProbability(){ 111 | 112 | double val=DistributionMethods.normalInverseCumulativeProbability(1e-5); 113 | assertEquals( -4.2648910,val, 0.0001); 114 | val=DistributionMethods.normalInverseCumulativeProbability(0.9999); 115 | assertEquals( 3.719016,val, 0.0001); 116 | val=DistributionMethods.normalInverseCumulativeProbability(1e-14); 117 | assertEquals(-7.650628,val, 0.01); 118 | val=DistributionMethods.normalInverseCumulativeProbability(0.5e-14); 119 | assertEquals(-7.739256,val, 0.01); 120 | val=DistributionMethods.normalInverseCumulativeProbability(0.5e-18); 121 | assertEquals(-8.83511,val, 0.01); 122 | val=DistributionMethods.normalInverseCumulativeProbability(0.5e-300); 123 | assertEquals(-37.06579,val, 0.01); 124 | 125 | } 126 | 127 | } 128 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/test/OverlappedGenomicElementTest.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | 23 | package ch.unil.genescore.vegas.test; 24 | 25 | import static org.junit.Assert.*; 26 | 27 | import java.util.LinkedList; 28 | 29 | import org.junit.AfterClass; 30 | import org.junit.BeforeClass; 31 | import org.junit.Test; 32 | 33 | import ch.unil.genescore.gene.Gene; 34 | import ch.unil.genescore.gene.GenomicElement; 35 | import ch.unil.genescore.vegas.OverlappedGenomicElement; 36 | 37 | public class OverlappedGenomicElementTest { 38 | // ============================================================================ 39 | // SETUP 40 | 41 | @BeforeClass 42 | public static void testSetup() { 43 | } 44 | 45 | @AfterClass 46 | public static void testCleanup() { } 47 | 48 | 49 | // ============================================================================ 50 | // TESTS 51 | 52 | /** Test test statistic */ 53 | @Test 54 | public void compareToTest(){ 55 | // test whether overlap chromosome works as expected. 56 | Gene g1 = new Gene("g1"); 57 | Gene g2 = new Gene("g2"); 58 | 59 | OverlappedGenomicElement el1 = new OverlappedGenomicElement(g1); 60 | OverlappedGenomicElement el2 = new OverlappedGenomicElement(g2); 61 | 62 | g1.setPosition("chr22", 12, 500, true); 63 | g2.setPosition("chr22", 20, 500, true); 64 | assertTrue(el1.compareTo(el2)<0); 65 | g1.setPosition("chr19", 20, 500, true); 66 | g2.setPosition("chr2", 20, 500, true); 67 | assertTrue(el1.compareTo(el2)<0); 68 | g1.setPosition("chr1", 20, 500, true); 69 | g2.setPosition("chr2", 1, 1200, true); 70 | assertTrue(el1.compareTo(el2)<0); 71 | g1.setPosition("chr2", 20, 500, true); 72 | g2.setPosition("chr2", 20, 1200, true); 73 | assertTrue(el1.compareTo(el2)<0); 74 | } 75 | 76 | // OverlappedGenomicElement el = new OverlappedGenomicElement(g1); 77 | // OverlappedGenomicElement el2 = new OverlappedGenomicElement(g2); 78 | 79 | // assertTrue(el.isPastElement(el2)); 80 | 81 | 82 | 83 | @Test 84 | public void runTest(){ 85 | 86 | Gene g1 = new Gene("g1"); 87 | OverlappedGenomicElement el = new OverlappedGenomicElement(g1); 88 | assertTrue(el.getMainElement().id_.equals("g1")); 89 | assertTrue(el.getAllOverlappedElements().size()==0); 90 | assertTrue(el.getAllOverlappedElements()!=null); 91 | LinkedList elList = el.getRecursiveOverlappedElementAtLevelN(0); 92 | assertTrue(elList.size()==0); 93 | assertTrue(elList!=null); 94 | 95 | 96 | OverlappedGenomicElement elOv1 = new OverlappedGenomicElement(new GenomicElement("el1")); 97 | OverlappedGenomicElement elOv2 = new OverlappedGenomicElement(new GenomicElement("el2")); 98 | el.addToList(elOv1); 99 | el.addToList(elOv2); 100 | 101 | assertEquals(el.getRecursiveOverlappedElementAtLevelN(0).getFirst(),el.getAllOverlappedElements().getFirst()); 102 | assertEquals(el.getRecursiveOverlappedElementAtLevelN(0).getLast(),el.getAllOverlappedElements().getLast()); 103 | assertEquals(el.getRecursiveOverlappedElementAtLevelN(0),el.getAllOverlappedElements()); 104 | 105 | assertTrue(el.getRecursiveOverlappedElementAtLevelN(1).isEmpty()); 106 | OverlappedGenomicElement ov3 = new OverlappedGenomicElement(new GenomicElement("el3")); 107 | elOv1.addToList(ov3); 108 | assertTrue(el.getRecursiveOverlappedElementAtLevelN(1).size()==1); 109 | assertTrue(el.getRecursiveOverlappedElementAtLevelN(1).getFirst()==ov3.getMainElement()); 110 | 111 | OverlappedGenomicElement ov4 = new OverlappedGenomicElement(new GenomicElement("el4")); 112 | elOv2.addToList(ov4); 113 | 114 | assertTrue(el.getRecursiveOverlappedElementAtLevelN(1).size()==2); 115 | assertTrue(el.getRecursiveOverlappedElementAtLevelN(1).getLast()==ov4.getMainElement()); 116 | 117 | OverlappedGenomicElement ov5 = new OverlappedGenomicElement(new GenomicElement("el5")); 118 | ov4.addToList(ov5); 119 | 120 | assertTrue(el.getRecursiveOverlappedElementAtLevelN(1).size()==2); 121 | assertTrue(el.getRecursiveOverlappedElementAtLevelN(1).getLast()==ov4.getMainElement()); 122 | assertTrue(el.getRecursiveOverlappedElementAtLevelN(2).size()==1); 123 | assertTrue(el.getRecursiveOverlappedElementAtLevelN(2).getLast()==ov5.getMainElement()); 124 | assertTrue(el.getRecursiveOverlappedElementAtLevelN(3).isEmpty()); 125 | } 126 | 127 | } 128 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/gene/GeneAnnotationUcsc.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.gene; 23 | 24 | import java.util.HashSet; 25 | import java.util.LinkedHashMap; 26 | 27 | import ch.unil.genescore.main.FileParser; 28 | import ch.unil.genescore.main.Main; 29 | import ch.unil.genescore.main.Settings; 30 | 31 | 32 | /** 33 | * UCSC genome browser annotation with Entrez IDs 34 | * => There are inconsistent entries in the file (same entrez ids, different strand / location) 35 | */ 36 | public class GeneAnnotationUcsc extends GeneAnnotation { 37 | 38 | 39 | // ============================================================================ 40 | // PUBLIC METHODS 41 | 42 | /** Constructor */ 43 | public GeneAnnotationUcsc() { 44 | 45 | super(Settings.ucscAnnotationFile_); 46 | System.out.println("Load annotation: " + Settings.ucscAnnotationFile_); 47 | } 48 | 49 | 50 | // ---------------------------------------------------------------------------- 51 | 52 | /** 53 | * Load gene coordinates for the given gene set (load all genes if the set is empty) 54 | * => There are inconsistent entries in the file (same entrez ids, different strand / location) 55 | * => But they affect less than 100 genes 56 | */ 57 | @SuppressWarnings({ "unchecked", "rawtypes" }) 58 | public LinkedHashMap loadAnnotation() { 59 | 60 | genes_ = new LinkedHashMap(); 61 | HashSet inconsistentEntries = new HashSet(); 62 | HashSet inconsistentStrand = new HashSet(); 63 | System.out.println("Loading annotation: " + annotationFile_); 64 | FileParser parser = new FileParser(annotationFile_); 65 | 66 | // Skip the header lines (start with #) 67 | String[] nextLine = parser.readLine(); 68 | while (nextLine[0].startsWith("#")) 69 | nextLine = parser.readLine(); 70 | 71 | while (nextLine != null) { 72 | // Check number of columns 73 | if (nextLine.length != 8) 74 | parser.error("Expected 8 columns"); 75 | 76 | // Chromosome 77 | String chr = nextLine[1]; 78 | // Continue if this chromosome should not be loaded based on settings 79 | if (skipChromosome(chr)) { 80 | nextLine = parser.readLine(); 81 | continue; 82 | } 83 | 84 | // Gene id and name 85 | String geneId = nextLine[7]; 86 | String geneName = nextLine[5]; 87 | 88 | // If a gene set to be loaded was specified and this gene is NOT in this set, continue 89 | if (!checkGenesToBeLoaded(geneId, geneName)) { 90 | nextLine = parser.readLine(); 91 | continue; 92 | } 93 | 94 | // Skip entries without entrez id or that were listed before with inconsistent info 95 | if (inconsistentEntries.contains(geneId) || geneId.equals("n/a")) { 96 | nextLine = parser.readLine(); 97 | continue; 98 | } 99 | 100 | // Strand, start and end 101 | boolean posStrand = GenomicElement.isPosStrand(nextLine[2]); 102 | int start = Integer.parseInt(nextLine[3]); 103 | int end = Integer.parseInt(nextLine[4]); 104 | 105 | Gene nextGene = genes_.get(geneId); 106 | // Create new gene 107 | if (nextGene == null) { 108 | nextGene = new Gene(geneId, geneName); 109 | nextGene.setPosition(chr, start, end, posStrand); 110 | genes_.put(geneId, nextGene); 111 | 112 | // Update existing gene 113 | } else { 114 | // Check that symbol, chr and position are consistent 115 | if (nextGene.chr_.equals(chr) && nextGene.symbol_.equals(geneName) && 116 | (Math.abs(nextGene.start_ - start) < 1e6 || Math.abs(nextGene.end_ - end) < 1e6)) { 117 | 118 | // If only the strand is not consistent, we'll just give a warning 119 | if (nextGene.posStrand_ != posStrand) { 120 | // We believe the longer gene 121 | if (end - start > nextGene.end_ - nextGene.start_) 122 | nextGene.posStrand_ = posStrand; 123 | inconsistentStrand.add(geneId); 124 | } 125 | 126 | // Update start and end 127 | if (start < nextGene.start_) 128 | nextGene.start_ = start; 129 | if (end > nextGene.end_) 130 | nextGene.end_ = end; 131 | 132 | } else { 133 | inconsistentEntries.add(geneId); 134 | genes_.remove(geneId); 135 | } 136 | } 137 | 138 | // Read next line 139 | nextLine = parser.readLine(); 140 | } 141 | parser.close(); 142 | 143 | // Currently this happens only for 48 and 51 genes respectively... 144 | if (inconsistentEntries.size() > 100) 145 | Main.warning(inconsistentEntries.size() + " genes with multiple entries at different genomic positions were skipped"); 146 | if (inconsistentStrand.size() > 100) 147 | Main.warning(inconsistentStrand.size() + " genes with inconsistent strands"); 148 | 149 | // Sort the genes 150 | Genome genome = new Genome(genes_.values()); 151 | genes_ = (LinkedHashMap) genome.asLinkedHashMap(); 152 | 153 | return genes_; 154 | } 155 | 156 | } 157 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/gene/GeneAnnotationGencode.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.gene; 23 | 24 | import java.util.LinkedHashMap; 25 | 26 | import ch.unil.genescore.main.FileParser; 27 | import ch.unil.genescore.main.Main; 28 | import ch.unil.genescore.main.Settings; 29 | 30 | 31 | /** 32 | * Gencode gene annotation. 33 | * Gene types are (attribute 'gene_type'): 34 | * antisense 35 | * snRNA 36 | * miRNA 37 | * snoRNA 38 | * polymorphic_pseudogene 39 | * lincRNA 40 | * protein_coding 41 | * 3prime_overlapping_ncrna 42 | * misc_RNA 43 | * rRNA 44 | * sense_intronic 45 | * processed_transcript 46 | * pseudogene 47 | * sense_overlapping 48 | */ 49 | public class GeneAnnotationGencode extends GeneAnnotation { 50 | 51 | 52 | // ============================================================================ 53 | // PUBLIC METHODS 54 | 55 | /** Constructor */ 56 | public GeneAnnotationGencode() { 57 | 58 | super(Settings.gencodeAnnotationFile_); 59 | } 60 | 61 | 62 | // ---------------------------------------------------------------------------- 63 | 64 | /** Load gene coordinates for the given gene set (load all genes if the set is empty) */ 65 | public LinkedHashMap loadAnnotation() { 66 | 67 | genes_ = new LinkedHashMap(); 68 | // To print gene types 69 | //HashSet geneType = new HashSet(); 70 | 71 | // Open the file 72 | FileParser parser = new FileParser(annotationFile_); 73 | //GeneIdMapping mapping = GeneIdMapping.getInstance(); 74 | 75 | // Skip the first 5 lines (start with #) 76 | String[] nextLine = parser.readLine(); 77 | while (nextLine[0].startsWith("#")) 78 | nextLine = parser.readLine(); 79 | 80 | while (nextLine != null) { 81 | // Check number of columns 82 | if (nextLine.length != 9) 83 | parser.error("Expected 9 columns"); 84 | 85 | // Check that this is a gene 86 | if (!nextLine[2].equals("gene")) 87 | parser.error("Third column expected to be 'gene'"); 88 | 89 | // Chromosome 90 | String chr = nextLine[0]; 91 | // Continue if this chromosome should not be loaded based on settings 92 | if (skipChromosome(chr)) { 93 | nextLine = parser.readLine(); 94 | continue; 95 | } 96 | 97 | if (loadOnlyProteinCoding_) { 98 | // Check that it's a protein coding gene 99 | String gene_type = getGencodeKeyValue(nextLine[8], "gene_type"); 100 | //geneType.add(gene_type); 101 | 102 | if (!gene_type.equalsIgnoreCase("protein_coding")) { 103 | nextLine = parser.readLine(); 104 | continue; 105 | } 106 | 107 | } 108 | // Gene id 109 | String geneId = removeEnsemblVersion(getGencodeKeyValue(nextLine[8], "gene_id").toUpperCase()); 110 | String geneName = getGencodeKeyValue(nextLine[8], "gene_name").toUpperCase(); 111 | 112 | // If a gene set to be loaded was specified and this gene is NOT in this set, continue 113 | if (!checkGenesToBeLoaded(geneId, geneName)) { 114 | nextLine = parser.readLine(); 115 | continue; 116 | } 117 | 118 | // Check that the id is unique 119 | if (genes_.containsKey(geneId)) 120 | parser.error("Duplicate gene id: " + geneId); 121 | 122 | // Create the gene 123 | Gene nextGene = new Gene(geneId, geneName); 124 | genes_.put(geneId, nextGene); 125 | 126 | // Position 127 | int start = Integer.parseInt(nextLine[3]); 128 | int end = Integer.parseInt(nextLine[4]); 129 | boolean posStrand = GenomicElement.isPosStrand(nextLine[6]); 130 | nextGene.setPosition(chr, start, end, posStrand); 131 | 132 | // Read next line 133 | nextLine = parser.readLine(); 134 | } 135 | parser.close(); 136 | 137 | // Print gene types 138 | //for (String type : geneType) 139 | //Ngsea.println(type); 140 | 141 | return genes_; 142 | } 143 | 144 | 145 | // ---------------------------------------------------------------------------- 146 | 147 | /** If this is an ensembl id, remove the version number */ 148 | public static String removeEnsemblVersion(String id) { 149 | 150 | if (id.length() > 4 && id.substring(0, 4).equals("ENSG")) { 151 | int dot = id.lastIndexOf("."); 152 | if (dot != -1) 153 | id = id.substring(0, dot); 154 | } 155 | return id; 156 | } 157 | 158 | 159 | // ============================================================================ 160 | // PRIVATE METHODS 161 | 162 | /** Get the value of the given key, throw exception if not found */ 163 | private String getGencodeKeyValue(String keyValueList, String key) { 164 | 165 | int start = keyValueList.indexOf(key + " \""); 166 | if (start == -1) 167 | Main.error("Key not found: '" + key + "\""); 168 | 169 | start = start + key.length() + 2; 170 | int end = keyValueList.indexOf("\"", start); 171 | 172 | String value = keyValueList.substring(start, end); 173 | return value; 174 | } 175 | 176 | } 177 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/OverlappedGenomicElement.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import java.util.Iterator; 25 | import java.util.LinkedList; 26 | 27 | import ch.unil.genescore.gene.Chromosome; 28 | import ch.unil.genescore.gene.GenomicElement; 29 | /**this is a recursive DataStructure*/ 30 | public class OverlappedGenomicElement implements Comparable { 31 | 32 | GenomicElement mainElement_ = null; 33 | LinkedList overlappedElements_ = null; 34 | 35 | public OverlappedGenomicElement(GenomicElement mainElement){ 36 | mainElement_ = mainElement; 37 | overlappedElements_ = new LinkedList(); 38 | } 39 | 40 | public void addToList(OverlappedGenomicElement element){ 41 | overlappedElements_.add(element); 42 | } 43 | public GenomicElement getMainElement(){ 44 | return mainElement_; 45 | } 46 | public LinkedList getAllOverlappedElements(){ 47 | LinkedList elementList = new LinkedList(); 48 | for (OverlappedGenomicElement overlappedElement : overlappedElements_){ 49 | elementList.add(overlappedElement.getMainElement()); 50 | } 51 | return elementList; 52 | } 53 | public LinkedList addAllOverlappedElements(LinkedList priorList){ 54 | LinkedList elementList = new LinkedList(); 55 | for (OverlappedGenomicElement overlappedElement : overlappedElements_){ 56 | elementList.add(overlappedElement); 57 | } 58 | priorList.addAll(elementList); 59 | return elementList; 60 | } 61 | public void filterMembers(String id){ 62 | Iterator iter = overlappedElements_.iterator(); 63 | OverlappedGenomicElement current = null; 64 | String currentId = null; 65 | while(iter.hasNext()){ 66 | current=iter.next(); 67 | currentId = current.mainElement_.id_; 68 | if (!currentId.equals(id)){ 69 | iter.remove(); 70 | } 71 | } 72 | } 73 | 74 | public LinkedList getRecursiveOverlappedElementAtLevelN(int n){ 75 | LinkedList outList= new LinkedList(); 76 | if (n>0){ 77 | for (OverlappedGenomicElement overlappedElement : overlappedElements_){ 78 | outList.addAll(overlappedElement.getRecursiveOverlappedElementAtLevelN(n-1)); 79 | } 80 | } 81 | if (n==0){ 82 | outList=getAllOverlappedElements(); 83 | } 84 | return outList; 85 | } 86 | //TODO: potentially interface properly 87 | // now just double def in both classes. 88 | /** Check whether main element completely overlaps second main element 89 | */ 90 | public boolean completelyOverlapsElement(OverlappedGenomicElement other, int start_mod, int end_mod) { 91 | return mainElement_.completelyOverlapsElement(other.getMainElement(), start_mod, end_mod); 92 | } 93 | /** Check whether main element partially overlaps second main element 94 | */ 95 | public boolean partiallyOverlapsElement(OverlappedGenomicElement other) { 96 | return mainElement_.partiallyOverlapsElement(other.getMainElement()); 97 | } 98 | 99 | /** Check whether main element partially overlaps second main element 100 | * * conceptually move first element borders by start_mod and end_mod first.start+start_mod, first.end+end_mod 101 | */ 102 | public boolean extensionPartiallyOverlapsElement(OverlappedGenomicElement other, int start_mod, int end_mod) { 103 | return mainElement_.extensionPartiallyOverlapsElement(other.getMainElement(), start_mod, end_mod); 104 | } 105 | 106 | /** Check whether element is completely downStream of second 107 | * Compare first by chromosome 108 | * then check wether first.start + start_mod is higher than sec.end 109 | * conceptually first move first.start by start_mod*/ 110 | public boolean isPastElement(OverlappedGenomicElement other, int start_mod) { 111 | return mainElement_.isPastElement(other.getMainElement(), start_mod); 112 | } 113 | /** Compare first by chromosome, second by start position, third by id */ 114 | @Override 115 | public int compareTo(OverlappedGenomicElement other) { 116 | return mainElement_.compareTo(other.getMainElement()); 117 | } 118 | 119 | /** 120 | * Override hashCode to be consistent with equals() and compareTo(). 121 | * Automatically generated using Eclipse 122 | */ 123 | @Override 124 | public int hashCode() { 125 | return mainElement_.hashCode(); 126 | } 127 | /** 128 | * Override equals comparing by chromosome, start and id (consistent with compareTo() and hashCode()). 129 | * Automatically generated using Eclipse. 130 | */ 131 | @Override 132 | public boolean equals(Object obj) { 133 | return mainElement_.equals(obj); 134 | 135 | } 136 | } 137 | 138 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/WritingMethods.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas; 23 | 24 | import java.io.File; 25 | import java.io.IOException; 26 | import java.util.ArrayList; 27 | 28 | import no.uib.cipr.matrix.DenseMatrix; 29 | import no.uib.cipr.matrix.Matrix; 30 | import ch.unil.genescore.main.FileExport; 31 | import ch.unil.genescore.main.Main; 32 | import ch.unil.genescore.main.Settings; 33 | 34 | public class WritingMethods { 35 | 36 | public static void writeMTJ(Matrix mat, String fileName, String additonalDirectory) { 37 | 38 | String filename = Settings.outputDirectory_ + "/" + additonalDirectory + "/" + fileName; 39 | System.out.println(filename); 40 | File file = new File(filename); 41 | try { 42 | file.createNewFile(); 43 | } catch (IOException e) { 44 | e.printStackTrace(); 45 | } 46 | FileExport writer = new FileExport(filename); 47 | 48 | for (int i = 0; i < mat.numRows(); i++){ 49 | String line = ""; 50 | for (int j = 0; j < mat.numColumns(); j++){ 51 | if (j == 0){ 52 | line += mat.get(i,j); 53 | } 54 | else { 55 | line += "\t" + mat.get(i,j); 56 | } 57 | } 58 | writer.println(line); 59 | } 60 | writer.close(); 61 | Main.println(""); 62 | } 63 | 64 | public static void writeGenotypeToFile(ArrayList snpList, String fileName, String additonalDirectory) { 65 | 66 | int numRows = snpList.get(0).getGenotypes().length; 67 | int numColumns = snpList.size(); 68 | DenseMatrix printMat = new DenseMatrix(numRows, numColumns); 69 | double val=0; 70 | for (int j=0; j snpList, String fileName, String additonalDirectory) { 80 | 81 | 82 | int numSnps = snpList.size(); 83 | DenseMatrix printMat = new DenseMatrix(numSnps,1); 84 | double val=0; 85 | for (int i=0 ; i< numSnps ; i++){ 86 | val=snpList.get(i).getZscore(); 87 | printMat.set(i,0,val); 88 | } 89 | writeMTJ( printMat, fileName, additonalDirectory); 90 | } 91 | 92 | public static void writeSnpPosWithValToFile(ArrayList snpList, ArrayList valString,String header, String fileName, String additonalDirectory) { 93 | 94 | int numSnps= snpList.size(); 95 | if (numSnps!=valString.size()){ 96 | throw new RuntimeException("can't write; not same length"); 97 | } 98 | String filename = Settings.outputDirectory_ + "/" + additonalDirectory + "/" + fileName; 99 | System.out.println(filename); 100 | File file = new File(filename); 101 | try { 102 | file.createNewFile(); 103 | } catch (IOException e) { 104 | e.printStackTrace(); 105 | } 106 | 107 | FileExport writer = new FileExport(filename); 108 | String headerline= "chr\tstart\tend\tid\t" + header; 109 | writer.println(headerline); 110 | for (int i=0 ; i < numSnps ; i++){ 111 | String line = ""; 112 | Snp curSnp = snpList.get(i); 113 | String curVal = valString.get(i); 114 | 115 | line += curSnp.chr_; 116 | line += "\t" + curSnp.start_; 117 | line += "\t" + curSnp.end_; 118 | line += "\t" + curSnp.id_; 119 | 120 | line += "\t" + curVal; 121 | writer.println(line); 122 | } 123 | writer.close(); 124 | } 125 | 126 | // ---------------------------------------------------------------------------- 127 | 128 | /** Write correlation matrix of a gene to file */ 129 | public static void writeLD(double[][] cor, String geneName, String additonalDirectory) { 130 | 131 | String filename = Settings.outputDirectory_ + "/" + additonalDirectory + "/" + "run_" + geneName + ".ld"; 132 | System.out.println(filename); 133 | File file = new File(filename); 134 | try { 135 | file.createNewFile(); 136 | } catch (IOException e) { 137 | e.printStackTrace(); 138 | } 139 | FileExport writer = new FileExport(filename); 140 | 141 | for (int i = 0; i < cor.length; i++){ 142 | String line = ""; 143 | for (int j = 0; j < cor[i].length; j++){ 144 | if (j == 0){ 145 | line += cor[i][j]; 146 | } 147 | else { 148 | line += "\t" + cor[i][j]; 149 | } 150 | } 151 | writer.println(line); 152 | } 153 | writer.close(); 154 | Main.println(""); 155 | } 156 | 157 | public static void writeLdMTJ(DenseMatrix cor, String geneName, String additonalDirectory) { 158 | 159 | String filename = Settings.outputDirectory_ + "/" + additonalDirectory + "/" + "run_" + geneName + ".ld"; 160 | System.out.println(filename); 161 | File file = new File(filename); 162 | try { 163 | file.createNewFile(); 164 | } catch (IOException e) { 165 | e.printStackTrace(); 166 | } 167 | FileExport writer = new FileExport(filename); 168 | 169 | for (int i = 0; i < cor.numRows(); i++){ 170 | String line = ""; 171 | for (int j = 0; j < cor.numColumns(); j++){ 172 | if (j == 0){ 173 | line += cor.get(i,j); 174 | } 175 | else { 176 | line += "\t" + cor.get(i,j); 177 | } 178 | } 179 | writer.println(line); 180 | } 181 | writer.close(); 182 | Main.println(""); 183 | } 184 | 185 | } 186 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/gene/Gene.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.gene; 23 | 24 | import java.util.ArrayList; 25 | import java.util.Collection; 26 | import java.util.Collections; 27 | 28 | 29 | 30 | import java.util.HashSet; 31 | 32 | import ch.unil.genescore.main.Settings; 33 | import ch.unil.genescore.vegas.Snp; 34 | import ch.unil.genescore.vegas.DistributionMethods; 35 | 36 | 37 | /** 38 | * A gene and it's score(s) / p-value(s) 39 | */ 40 | public class Gene extends GenomicElement { 41 | 42 | /** The gene name / symbol (use GenomicElement.id_ for the ensembl/entrez ID) */ 43 | public String symbol_ = null; 44 | 45 | /** 46 | * The association scores / p-values. 47 | * Keep this as a vector, we could potentially do pathway analysis over multiple gene scores, 48 | * e.g. obtained using different snp weighting strategies. 49 | */ 50 | private double[] score_ = null; 51 | 52 | /** score that has to been rank-normalized */ 53 | private double normalizedScore_ = 1; 54 | 55 | /** Score mapped to chi2 stat */ 56 | private double chi2Stat_ = -1; 57 | 58 | /** sampling weight; only used for pathway methods.*/ 59 | private double samplingWeight_ = 0; 60 | 61 | 62 | // ============================================================================ 63 | // STATIC METHODS 64 | 65 | /** Create a list of genes sorted by position */ 66 | static public ArrayList sortByPosition(Collection genes) { 67 | 68 | ArrayList genesSorted = new ArrayList(genes); 69 | Collections.sort(genesSorted); 70 | return genesSorted; 71 | } 72 | 73 | 74 | // ============================================================================ 75 | // PUBLIC METHODS 76 | 77 | /** Constructor */ 78 | public Gene(String id) { 79 | 80 | super(id); 81 | } 82 | 83 | 84 | /** Constructor */ 85 | public Gene(String id, String symbol) { 86 | 87 | super(id); 88 | symbol_ = symbol; 89 | } 90 | 91 | 92 | // ---------------------------------------------------------------------------- 93 | 94 | /** Get the TSS (start if on + strand, end if on - strand) */ 95 | public int getTss() { 96 | 97 | if (posStrand_) 98 | return start_; 99 | else 100 | return end_; 101 | } 102 | 103 | 104 | // ---------------------------------------------------------------------------- 105 | 106 | /** Get the the symbol or "NA" if the symbol is NULL */ 107 | public String getSymbolOrNA() { 108 | 109 | return (symbol_ == null) ? "NA" : symbol_; 110 | } 111 | 112 | 113 | // ---------------------------------------------------------------------------- 114 | 115 | /** String representation of the gene */ 116 | public String toString() { 117 | 118 | return chr_ + "\t" + start_ + "\t" + end_ + "\t" + getPosStrandStr() + "\t" + id_ + "\t" + getSymbolOrNA(); 119 | } 120 | 121 | 122 | // ---------------------------------------------------------------------------- 123 | /** Get the snps that are in the window around the given gene */ 124 | @SuppressWarnings({ "rawtypes", "unchecked" }) 125 | public ArrayList findSnps(Genome snps) { 126 | 127 | // Define the window around the gene (depends on orientation) 128 | int start; 129 | int end; 130 | if (posStrand_) { 131 | start = start_ - Settings.geneWindowUpstream_; 132 | end = end_ + Settings.geneWindowDownstream_; 133 | } else { 134 | start = start_ - Settings.geneWindowDownstream_; 135 | end = end_ + Settings.geneWindowUpstream_; 136 | } 137 | 138 | // Get the set of snps assigned to this gene as an array 139 | ArrayList geneSnps = (ArrayList) snps.getElementsIn(chr_, start, end); 140 | return geneSnps; 141 | } 142 | 143 | //@SuppressWarnings({ "rawtypes", "unchecked" }) 144 | public HashSet findSnpsHash(Genome snps) { 145 | 146 | HashSet hashset = new HashSet(); 147 | ArrayList geneSnps = findSnps(snps); 148 | for(Snp snp : geneSnps) 149 | hashset.add(snp); 150 | return(hashset); 151 | } 152 | 153 | 154 | public void copyScores(Gene inputGene){ 155 | setScore(inputGene.getScore()); 156 | setChi2Stat(inputGene.getChi2Stat()); 157 | 158 | } 159 | 160 | public void calcChi2StatFromScore() { 161 | chi2Stat_ = DistributionMethods.chiSquared1dfInverseCumulativeProbabilityUpperTail(score_[0]); 162 | } 163 | // ============================================================================ 164 | // GETTERS AND SETTERS 165 | 166 | public void setScore(double[] x) { score_ = x; } 167 | public void setScore(double x) { 168 | double[] xar = new double[1]; 169 | xar[0]=x; 170 | score_ = xar; } 171 | public double[] getScore() { return score_; } 172 | public double getScore(int i) { return score_[i]; } 173 | public double getNormalizedScore() { return normalizedScore_; } 174 | public void setNormalizedScore(double normalizedScore) { normalizedScore_=normalizedScore;} 175 | 176 | 177 | public void setChi2Stat(double x) { 178 | chi2Stat_ = x; } 179 | public double getChi2Stat() { return chi2Stat_; } 180 | public void setsamplingWeight(double x) { 181 | samplingWeight_ = x; } 182 | public double getSamplingWeight(){return samplingWeight_;} 183 | public void updateSamplingWeight(double x){samplingWeight_=samplingWeight_ + x;} 184 | 185 | public ArrayList getSymbolList(){ 186 | ArrayList myList = new ArrayList(); 187 | myList.add(symbol_); 188 | return(myList); 189 | } 190 | } 191 | -------------------------------------------------------------------------------- /src/ch/unil/genescore/vegas/test/AnalyticVegasTest.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2015 David Lamparter, Daniel Marbach 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | *******************************************************************************/ 22 | package ch.unil.genescore.vegas.test; 23 | 24 | import static org.junit.Assert.*; 25 | 26 | import java.util.ArrayList; 27 | 28 | import no.uib.cipr.matrix.DenseMatrix; 29 | import no.uib.cipr.matrix.UpperSymmDenseMatrix; 30 | 31 | import org.junit.AfterClass; 32 | import org.junit.BeforeClass; 33 | import org.junit.Test; 34 | 35 | import org.apache.commons.math3.distribution.ChiSquaredDistribution; 36 | 37 | import ch.unil.genescore.main.Settings; 38 | import ch.unil.genescore.vegas.AnalyticVegas; 39 | import ch.unil.genescore.vegas.Snp; 40 | 41 | 42 | public class AnalyticVegasTest { 43 | 44 | 45 | // ============================================================================ 46 | // SETUP 47 | 48 | @BeforeClass 49 | public static void testSetup() { 50 | Settings.loadSettings(); 51 | } 52 | 53 | @AfterClass 54 | public static void testCleanup() { } 55 | 56 | 57 | // ============================================================================ 58 | // TESTS 59 | 60 | /** Test test statistic */ 61 | @Test 62 | public void test() { 63 | double delta = 0.00001; 64 | Snp fakeSnp1 = new Snp("fakeId1", 0, 0.3); 65 | Snp fakeSnp2 = new Snp("fakeId2", 0, 0.8); 66 | Snp fakeSnp3 = new Snp("fakeId3", 0, 1.4); 67 | Settings.withZScore_=true; 68 | ArrayList geneSnps = new ArrayList(); 69 | geneSnps.add(fakeSnp1);geneSnps.add(fakeSnp2);geneSnps.add(fakeSnp3); 70 | DenseMatrix ld= new DenseMatrix(3,3); 71 | //DenseMatrix crossLd= new DenseMatrix(3,2); 72 | 73 | ArrayList snpScores = new ArrayList(3); 74 | snpScores.add(fakeSnp1.getZscore()); 75 | snpScores.add(fakeSnp2.getZscore()); 76 | snpScores.add(fakeSnp3.getZscore()); 77 | //ld and crossLd calculated as follows: 78 | // make 0.9-toeplitz mat of size 5. 79 | // 3,4,5 for ld-mat 80 | // 1,2 for crossLd-mat 81 | ld.set(0,0,1);ld.set(1,1,1);ld.set(2,2,1); 82 | ld.set(0,1,0.9);ld.set(1,2,0.9); 83 | ld.set(1,0,0.9);ld.set(2,1,0.9); 84 | ld.set(0,2,0.81); 85 | ld.set(2,0,0.81); 86 | double[] weights = {2,2,2}; 87 | AnalyticVegas myAnalyticObj= null; 88 | UpperSymmDenseMatrix myMatToDecompose = null; 89 | double[] myEigenvals = null; 90 | double[] emptyWeights = {1,1,1}; 91 | myAnalyticObj= new AnalyticVegas(snpScores, ld, emptyWeights); 92 | myAnalyticObj.computeScore(); 93 | myEigenvals = myAnalyticObj.computeLambda(); 94 | assertEquals(myEigenvals[0],2.74067, delta); 95 | assertEquals(myEigenvals[1],0.1900, delta); 96 | assertEquals(myEigenvals[2],0.06932, delta); 97 | 98 | myAnalyticObj= new AnalyticVegas(snpScores, ld, weights); 99 | myAnalyticObj.computeScore(); 100 | myEigenvals = myAnalyticObj.computeLambda(); 101 | assertEquals(myEigenvals[0], 5.481348, delta); 102 | assertEquals(myEigenvals[1],0.380000, delta); 103 | assertEquals(myEigenvals[2],0.138652, delta); 104 | 105 | double[] weights2 = {1,2,0.5}; 106 | 107 | myAnalyticObj= new AnalyticVegas(snpScores, ld, weights2); 108 | myAnalyticObj.computeScore(); 109 | myEigenvals = myAnalyticObj.computeLambda(); 110 | assertEquals(myEigenvals[0],3.27694674, delta); 111 | assertEquals(myEigenvals[1],0.1492338, delta); 112 | assertEquals(myEigenvals[2],0.07381938, delta); 113 | 114 | } 115 | 116 | public void computeTestStatisticRealTest() { 117 | 118 | DenseMatrix ld= new DenseMatrix(3,3); 119 | ld.set(0,0,1);ld.set(1,1,1);ld.set(2,2,1); 120 | ld.set(0,1,0.9);ld.set(1,2,0.9); 121 | ld.set(1,0,0.9);ld.set(2,1,0.9); 122 | ld.set(0,2,0.81); 123 | ld.set(2,0,0.81); 124 | 125 | double delta = 0.00001; 126 | Snp fakeSnp1 = new Snp("fakeId1", 0.7641772, 0); 127 | Snp fakeSnp2 = new Snp("fakeId2", 0.4237108, 0); 128 | Snp fakeSnp3 = new Snp("fakeId3", 0.1615133, 0); 129 | Settings.withZScore_=false; 130 | ArrayList geneSnps = new ArrayList(); 131 | geneSnps.add(fakeSnp1);geneSnps.add(fakeSnp2);geneSnps.add(fakeSnp3); 132 | // 133 | ChiSquaredDistribution chiSquareDist1 = new ChiSquaredDistribution(1); 134 | ArrayList chiSquaredVals= new ArrayList(); 135 | chiSquaredVals.add(chiSquareDist1.inverseCumulativeProbability(1-fakeSnp1.getPval())); 136 | chiSquaredVals.add(chiSquareDist1.inverseCumulativeProbability(1-fakeSnp2.getPval())); 137 | chiSquaredVals.add(chiSquareDist1.inverseCumulativeProbability(1-fakeSnp3.getPval())); 138 | //cumulativeProbability(chiStat); 139 | double[] emptyWeights = {1,1,1}; 140 | AnalyticVegas myAnalyticObj= new AnalyticVegas(chiSquaredVals, ld, emptyWeights); 141 | myAnalyticObj.computeScore(); 142 | 143 | 144 | myAnalyticObj.computeTestStatisticReal(); 145 | double firstRes = myAnalyticObj.getTestStatisticReal(); 146 | 147 | 148 | Snp fakeSnp1_1 = new Snp("fakeId1", 0, 0.3); 149 | Snp fakeSnp2_1 = new Snp("fakeId2", 0, 0.8); 150 | Snp fakeSnp3_1 = new Snp("fakeId3", 0, 1.4); 151 | Settings.withZScore_=false; 152 | ArrayList zscores= new ArrayList(); 153 | zscores.add(0.3); 154 | zscores.add(0.8); 155 | zscores.add(1.4); 156 | 157 | //geneSnps_1.add(fakeSnp1_1);geneSnps_1.add(fakeSnp2_1);geneSnps_1.add(fakeSnp3_1); 158 | AnalyticVegas myAnalyticObj_1= new AnalyticVegas(zscores, ld, emptyWeights); 159 | myAnalyticObj.computeScore(); 160 | myAnalyticObj_1.computeTestStatisticReal(); 161 | double secRes = myAnalyticObj_1.getTestStatisticReal(); 162 | assertEquals(firstRes,secRes, 1E-4); 163 | } 164 | } --------------------------------------------------------------------------------