├── libplinkio ├── libs │ ├── cmockery │ │ ├── NEWS │ │ ├── packages │ │ │ ├── deb │ │ │ │ ├── compat │ │ │ │ ├── libcmockery0.dirs │ │ │ │ ├── libcmockery0.install │ │ │ │ ├── libcmockery-dev.dirs │ │ │ │ ├── docs │ │ │ │ ├── libcmockery-dev.install │ │ │ │ ├── changelog │ │ │ │ ├── README │ │ │ │ ├── control │ │ │ │ ├── copyright │ │ │ │ └── rules │ │ │ ├── rpm │ │ │ │ └── rpm.spec │ │ │ ├── rpm.sh │ │ │ └── deb.sh │ │ ├── AUTHORS │ │ ├── CMakeLists.txt │ │ ├── README │ │ ├── ChangeLog │ │ ├── m4 │ │ │ ├── ltversion.m4 │ │ │ ├── namespaces.m4 │ │ │ ├── stl_namespace.m4 │ │ │ ├── google_namespace.m4 │ │ │ └── ltsugar.m4 │ │ ├── src │ │ │ ├── example │ │ │ │ ├── product_database.c │ │ │ │ ├── run_tests.c │ │ │ │ ├── assert_macro.c │ │ │ │ ├── database.h │ │ │ │ ├── allocate_module_test.c │ │ │ │ ├── allocate_module.c │ │ │ │ ├── customer_database.c │ │ │ │ ├── assert_macro_test.c │ │ │ │ ├── key_value.c │ │ │ │ ├── customer_database_test.c │ │ │ │ ├── product_database_test.c │ │ │ │ └── key_value_test.c │ │ │ ├── config.h │ │ │ └── config.h.in │ │ ├── COPYING │ │ ├── configure.ac │ │ ├── mkinstalldirs │ │ ├── compile │ │ └── Makefile.am │ ├── CMakeLists.txt │ └── libcsv │ │ ├── Makefile.am │ │ ├── CMakeLists.txt │ │ └── inc │ │ └── csv.h ├── py-plinkio │ ├── plinkio │ │ └── __init__.py │ ├── .clang_complete │ ├── common.h │ ├── Makefile.am │ └── snparray.c ├── tests │ ├── data │ │ ├── wgas.bed │ │ └── wgas.fam │ ├── transpose.c │ ├── mock.h │ ├── Makefile.am │ ├── CMakeLists.txt │ ├── mock.c │ ├── plinkio_test.c │ ├── plinkio_test.py │ ├── bim_test.c │ └── fam_test.c ├── .gitignore ├── MANIFEST.in ├── Makefile.am ├── README.rst ├── src │ ├── Makefile.am │ ├── CMakeLists.txt │ ├── plinkio │ │ ├── status.h │ │ ├── fam_parse.h │ │ ├── bim_parse.h │ │ ├── file.h │ │ ├── snp_lookup.h │ │ ├── bim.h │ │ ├── fam.h │ │ ├── bed.h │ │ └── bed_header.h │ ├── file.c │ ├── bim.c │ └── fam.c ├── m4 │ ├── ltversion.m4 │ └── ltsugar.m4 ├── docs │ └── mainpage.dox ├── CMakeLists.txt ├── configure.ac ├── COPYING ├── compile └── setup.py ├── src ├── .vscode │ └── settings.json └── CMakeLists.txt ├── .gitignore ├── setup.sh ├── matlab ├── make_empty_frame.m ├── exclude_snps.txt ├── merged_frame_reader.m ├── test_2017_02_15.m ├── make_gwaslogp.m ├── PlinkRead_bim.m ├── plot_qq.m ├── make_genofreq.m ├── remove_dups_100K_rep10.sh ├── make_phenotype.m ├── PlinkRead_binary2.m ├── make_truepheno.m ├── generate_sym01.m ├── run_polygenic_truepheno_EUR_100K_80M_chunks.m ├── tutorial.m ├── generate_sym03.m ├── reframe.m ├── make_truebeta_gmm.m ├── make_gwas.m ├── generate_sym02.m ├── keep_and_extract.py ├── chunked_frame_reader.m ├── run.m ├── generate_sym05_nold.m └── generate_sym04.m ├── Dockerfile └── CMakeLists.txt /libplinkio/libs/cmockery/NEWS: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | } -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/packages/deb/compat: -------------------------------------------------------------------------------- 1 | 4 2 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/AUTHORS: -------------------------------------------------------------------------------- 1 | opensource@google.com 2 | 3 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/packages/deb/libcmockery0.dirs: -------------------------------------------------------------------------------- 1 | usr/lib 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore .mat files (for now) 2 | *.mat 3 | slurm*out 4 | 5 | -------------------------------------------------------------------------------- /libplinkio/py-plinkio/plinkio/__init__.py: -------------------------------------------------------------------------------- 1 | __all__ = [ "plinkfile" ] 2 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/packages/deb/libcmockery0.install: -------------------------------------------------------------------------------- 1 | usr/lib/lib*.so.* 2 | debian/tmp/usr/lib/lib*.so.* 3 | -------------------------------------------------------------------------------- /libplinkio/tests/data/wgas.bed: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/precimed/simu/HEAD/libplinkio/tests/data/wgas.bed -------------------------------------------------------------------------------- /libplinkio/.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.log 3 | *.swp 4 | autom4te.cache 5 | config.status 6 | build 7 | *.swo 8 | html/ 9 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/packages/deb/libcmockery-dev.dirs: -------------------------------------------------------------------------------- 1 | usr/lib 2 | usr/include 3 | usr/include/google 4 | 5 | -------------------------------------------------------------------------------- /libplinkio/py-plinkio/.clang_complete: -------------------------------------------------------------------------------- 1 | -I/usr/include/python2.7/ -I../build/include/ -I../build/include/plinkio 2 | -------------------------------------------------------------------------------- /setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | mkdir build && cd build 4 | cmake .. -DCMAKE_INSTALL_PREFIX:PATH=.. 5 | make && make install 6 | -------------------------------------------------------------------------------- /libplinkio/MANIFEST.in: -------------------------------------------------------------------------------- 1 | include src/plinkio/*.h 2 | include libs/libcsv/inc/*.h 3 | include py-plinkio/*.h 4 | include README.rst 5 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/packages/deb/docs: -------------------------------------------------------------------------------- 1 | AUTHORS 2 | COPYING 3 | ChangeLog 4 | INSTALL 5 | NEWS 6 | README 7 | doc/index.html 8 | -------------------------------------------------------------------------------- /libplinkio/libs/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory( libcsv ) 2 | 3 | if( NOT DISABLE_TESTS ) 4 | add_subdirectory( cmockery ) 5 | endif( ) 6 | -------------------------------------------------------------------------------- /libplinkio/libs/libcsv/Makefile.am: -------------------------------------------------------------------------------- 1 | noinst_LTLIBRARIES = libcsv.la 2 | libcsv_la_SOURCES = inc/csv.h src/libcsv.c 3 | libcsv_la_CFLAGS = -I$(srcdir)/inc 4 | 5 | -------------------------------------------------------------------------------- /libplinkio/Makefile.am: -------------------------------------------------------------------------------- 1 | ACLOCAL_AMFLAGS = -I m4 2 | 3 | EXTRA_DIST = README.md 4 | 5 | if WITH_TESTS 6 | TEST_DIRS = libs/cmockery/ tests 7 | endif 8 | 9 | SUBDIRS = libs/libcsv/ src $(TEST_DIRS) py-plinkio 10 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include_directories( "src/google/" ) 2 | 3 | add_library( libcmockery "src/cmockery.c" ) 4 | 5 | target_link_libraries( libcmockery ) 6 | SET_TARGET_PROPERTIES( libcmockery PROPERTIES OUTPUT_NAME cmockery ) 7 | -------------------------------------------------------------------------------- /matlab/make_empty_frame.m: -------------------------------------------------------------------------------- 1 | function frame = make_empty_frame(config, framename) 2 | frame = struct(); 3 | frame.snps = config.frames.(framename).snps; 4 | frame.subj = config.frames.(framename).subj; 5 | frame.name = framename; 6 | end -------------------------------------------------------------------------------- /libplinkio/libs/libcsv/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include_directories( "inc/" ) 2 | 3 | add_library( libcsv STATIC src/libcsv.c ) 4 | 5 | SET_TARGET_PROPERTIES( libcsv PROPERTIES POSITION_INDEPENDENT_CODE ON ) 6 | SET_TARGET_PROPERTIES( libcsv PROPERTIES OUTPUT_NAME csv ) 7 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/packages/deb/libcmockery-dev.install: -------------------------------------------------------------------------------- 1 | usr/include/google/* 2 | usr/lib/lib*.so 3 | usr/lib/lib*.a 4 | usr/lib/lib*.la 5 | debian/tmp/usr/include/google/* 6 | debian/tmp/usr/lib/lib*.so 7 | debian/tmp/usr/lib/lib*.a 8 | debian/tmp/usr/lib/lib*.la 9 | 10 | -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include_directories( ${PROJECT_SOURCE_DIR}/libplinkio/src ) 2 | 3 | set(SRC_LIST 4 | source.cc 5 | ) 6 | 7 | add_executable(simu ${SRC_LIST}) 8 | target_link_libraries(simu libplinkio-static libcsv ${Boost_LIBRARIES}) 9 | 10 | install( TARGETS simu DESTINATION bin ) 11 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/packages/deb/changelog: -------------------------------------------------------------------------------- 1 | cmockery (0.1-2) unstable; urgency=low 2 | * cmockery: version 0.12 3 | Made it possible to specify additional compiler, lib tool and link 4 | flags on Windows. 5 | Added Windows makefile to the tar ball. 6 | 7 | -- Google Inc. Mon, 28 Aug 2008 17:43:20 -0700 8 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/packages/deb/README: -------------------------------------------------------------------------------- 1 | The list of files here isn't complete. For a step-by-step guide on 2 | how to set this package up correctly, check out 3 | http://www.debian.org/doc/maint-guide/ 4 | 5 | Most of the files that are in this directory are boilerplate. 6 | However, you may need to change the list of binary-arch dependencies 7 | in 'rules'. 8 | -------------------------------------------------------------------------------- /libplinkio/py-plinkio/common.h: -------------------------------------------------------------------------------- 1 | #ifndef _COMMON_H_ 2 | #define _COMMON_H_ 3 | 4 | #include 5 | 6 | /** 7 | * Common integer conversion for python 3 and 2.x. 8 | */ 9 | #if PY_MAJOR_VERSION < 3 10 | #define PyLong_FromLong(x) ( (PyObject *) PyInt_FromLong( (long) ( x ) ) ) 11 | #define PyLong_AsLong(x) ( (long) PyInt_AsLong( ( x ) ) ) 12 | #endif 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /libplinkio/README.rst: -------------------------------------------------------------------------------- 1 | ==== 2 | plinkio 3 | ==== 4 | 5 | Plinkio is a python module for reading and creating Plink genotype files. It 6 | is written as a C-extension to efficiently parse and store these potentially 7 | large files. 8 | 9 | The lastest version can be installed by: 10 | 11 | pip install plinkio 12 | 13 | Documentation is available at: `plinkio ` 14 | -------------------------------------------------------------------------------- /libplinkio/tests/transpose.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * Simple test program for transpose. 5 | */ 6 | int 7 | main(int argc, char *argv[]) 8 | { 9 | if( argc != 3 ) 10 | { 11 | printf( "Usage: transpose plink_prefix plink_transposed_prefix\n" ); 12 | exit( 1 ); 13 | } 14 | 15 | pio_transpose( argv[ 1 ], argv[ 2 ] ); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /libplinkio/src/Makefile.am: -------------------------------------------------------------------------------- 1 | if GCC 2 | AM_CFLAGS = -Wall -strict -pendantic 3 | endif 4 | 5 | lib_LTLIBRARIES = libplinkio.la 6 | libplinkio_la_HEADERS = $(top_srcdir)/src/plinkio/*.h 7 | libplinkio_la_SOURCES = fam.c fam_parse.c bim.c bim_parse.c bed.c plinkio.c file.c bed_header.c 8 | libplinkio_ladir = $(includedir)/plinkio 9 | libplinkio_la_CFLAGS = -I$(top_srcdir)/src/ -I$(top_srcdir)/libs/libcsv/inc 10 | libplinkio_la_LIBADD = ../libs/libcsv/libcsv.la 11 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/README: -------------------------------------------------------------------------------- 1 | For information about how to use the cmockery unit testing framework see 2 | doc/index.html. 3 | 4 | COMPILING 5 | --------- 6 | To compile the cmockery library and example applications run ./configure 7 | followed by make. On Windows from the command line run vsvars.bat then cd into 8 | the windows subdirectory of this project and run nmake. 9 | 10 | This code has been tested on Linux (Ubuntu) and Windows using VC++7 and VC++8. 11 | -------------------------------------------------------------------------------- /matlab/exclude_snps.txt: -------------------------------------------------------------------------------- 1 | rs112741103 2 | rs1677780 3 | rs1671787 4 | rs71501039 5 | rs141927528 6 | rs142701510 7 | rs145926341 8 | rs542232278 9 | rs1610794 10 | rs371891811 11 | rs62011115 12 | rs28763720 13 | rs115010285 14 | rs59201531 15 | rs145970356 16 | rs370528220 17 | rs146021604 18 | rs71218065 19 | rs2558060 20 | rs111827956 21 | rs13374233 22 | rs6658405 23 | rs202107783 24 | rs200269882 25 | rs201680403 26 | rs200141404 27 | rs201551115 28 | rs11261873 29 | rs201093356 30 | -------------------------------------------------------------------------------- /matlab/merged_frame_reader.m: -------------------------------------------------------------------------------- 1 | function genomat = merged_frame_reader(subjvec, snpvec, nsubj, bfile) 2 | % Read genotypes from a single file 3 | % subjvec --- indices of subjects to read 4 | % snpvec --- indices of SNPs to read 5 | % nsubj --- total number of subjects present in the file 6 | % bfile --- path to plink BED file (without extension) 7 | 8 | genomat = PlinkRead_binary2(nsubj, snpvec, bfile); % int8, nsubj * nsnp 9 | genomat = genomat(subjvec, :); 10 | end 11 | -------------------------------------------------------------------------------- /matlab/test_2017_02_15.m: -------------------------------------------------------------------------------- 1 | if ~exist('config', 'var'), find_config; end; % => config 2 | 3 | frame = make_empty_frame (config, 'EUR_10K_1188K'); 4 | frame = make_truebeta_gmm(frame, config, 'pivec', 0.0001, 'sig1vec', 1.0, 'sig2vec', 1.0, 'rhovec', 0.5); 5 | frame = make_truepheno (frame, config); 6 | frame = make_phenotype (frame, config, 'h2', 0.8); 7 | frame = make_gwas (frame, config); 8 | frame = make_gwaslogp (frame, config); 9 | 10 | frame2 = reframe(frame, config, 'EUR_10K_1190K'); 11 | -------------------------------------------------------------------------------- /matlab/make_gwaslogp.m: -------------------------------------------------------------------------------- 1 | function frame = make_gwaslogp(frame, config, varargin) 2 | % MAKE_LOGPVEC generates standard columns such as logpvec and zvec 3 | 4 | p = inputParser; 5 | parse(p, varargin{:}); opts = p.Results; 6 | 7 | gwaslogp = -log10(frame.gwaspval); 8 | gwaslogp(isinf(gwaslogp)) = -log10(realmin); 9 | gwaszscore = -norminv(10.^-gwaslogp/2).*sign(frame.gwasbeta); 10 | 11 | frame.logpvec = gwaslogp; 12 | frame.zvec = gwaszscore; 13 | frame.opts.make_gwaslogp = opts; 14 | end -------------------------------------------------------------------------------- /libplinkio/py-plinkio/Makefile.am: -------------------------------------------------------------------------------- 1 | AM_CFLAGS = $(PYTHON_INCLUDE) -I$(srcdir)/../src/plinkio -I$(srcdir)/../src/ 2 | 3 | pkgpythondir = $(pythondir)/plinkio 4 | pkgpyexecdir = $(pyexecdir)/plinkio 5 | 6 | pkgpython_SCRIPTS = plinkio/__init__.py plinkio/plinkfile.py 7 | pkgpython_LTLIBRARIES = cplinkio.la 8 | 9 | cplinkio_la_SOURCES = cplinkio.c snparray.c snparray.h common.h 10 | cplinkio_la_LIBADD = ../src/libplinkio.la 11 | cplinkio_la_LDFLAGS = -avoid-version -module 12 | 13 | EXTRA_DIST = plinkio/__init__.py plinkio/plinkfile.py 14 | -------------------------------------------------------------------------------- /matlab/PlinkRead_bim.m: -------------------------------------------------------------------------------- 1 | function bim = PlinkRead_bim(fileprefix, header, format) 2 | 3 | if ~exist('format', 'var'), format = '%s %s %f %d %s %s'; end; 4 | if ~exist('header', 'var'), header = false; end; 5 | 6 | bimprefix = [fileprefix,'.bim']; 7 | fprintf('Read in plink bim from %s \r\n', bimprefix); 8 | bimid = fopen(bimprefix,'r'); 9 | if header, fgets(bimid); end; 10 | bimdata = textscan(bimid, format); 11 | fclose(bimid); 12 | bim.chrvec = bimdata{1}; 13 | bim.snplist = bimdata{2}; 14 | bim.cMvec = bimdata{3}; 15 | bim.bpvec = bimdata{4}; 16 | bim.A1vec = bimdata{5}; 17 | bim.A2vec = bimdata{6}; 18 | 19 | end 20 | 21 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/ChangeLog: -------------------------------------------------------------------------------- 1 | Mon Sep 15 17:21:22 2008 Google Inc. 2 | * cmockery: version 0.12 3 | * Made it possible to specify additional compiler, lib tool and link 4 | flags on Windows. 5 | * Added Windows makefile to the tar ball. 6 | 7 | Fri Aug 29 10:50:46 2008 Google Inc. 8 | 9 | * cmockery: version 0.11 10 | * Made it possible to specify executable, library and object output 11 | directories. 12 | 13 | Tue Aug 26 10:18:02 2008 Google Inc. 14 | 15 | * cmockery: initial release: 16 | A lightweight library to simplify and generalize the process of 17 | writing unit tests for C applications. 18 | -------------------------------------------------------------------------------- /matlab/plot_qq.m: -------------------------------------------------------------------------------- 1 | function plot_qq(logpvec, varargin) 2 | % PLOT_QQ generates standard QQ plot 3 | 4 | p = inputParser; 5 | addOptional(p, 'qqlim', 7); 6 | addOptional(p, 'maxpoints', 500000); 7 | parse(p, varargin{:}); opts = p.Results; 8 | 9 | logpvec = logpvec(~isnan(logpvec)); 10 | n=length(logpvec); 11 | y = sort(logpvec); 12 | x=-log10(1-(1:n)/n)'; 13 | 14 | mp = opts.maxpoints; % limit number of display points to avoid matlab plotting issues 15 | if length(x) > mp, x=x(end-mp:end); y=y(end-mp:end); end; 16 | 17 | hold on 18 | plot(x, y, 'b'); 19 | plot([0 opts.qqlim], [0 opts.qqlim], 'k--'); 20 | axis([0 opts.qqlim 0 opts.qqlim]); 21 | end 22 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/packages/deb/control: -------------------------------------------------------------------------------- 1 | Source: cmockery 2 | Section: libdevel 3 | Priority: optional 4 | Maintainer: Google Inc, 5 | Build-Depends: debhelper (>= 4.0.0), binutils 6 | Standards-Version: 3.6.1 7 | 8 | Package: libcmockery-dev 9 | Section: libdevel 10 | Architecture: any 11 | Depends: libcmockery0 (= ${Source-Version}) 12 | Description: The cmockery package contains a lightweight library to simplify 13 | and generalize the process of writing unit tests for C applications. 14 | 15 | Package: libcmockery0 16 | Section: libs 17 | Architecture: any 18 | Description: The cmockery package contains static and debug libraries and 19 | header files for the development of test applications using %name. 20 | 21 | -------------------------------------------------------------------------------- /libplinkio/m4/ltversion.m4: -------------------------------------------------------------------------------- 1 | # ltversion.m4 -- version numbers -*- Autoconf -*- 2 | # 3 | # Copyright (C) 2004 Free Software Foundation, Inc. 4 | # Written by Scott James Remnant, 2004 5 | # 6 | # This file is free software; the Free Software Foundation gives 7 | # unlimited permission to copy and/or distribute it, with or without 8 | # modifications, as long as this notice is preserved. 9 | 10 | # @configure_input@ 11 | 12 | # serial 3293 ltversion.m4 13 | # This file is part of GNU Libtool 14 | 15 | m4_define([LT_PACKAGE_VERSION], [2.4]) 16 | m4_define([LT_PACKAGE_REVISION], [1.3293]) 17 | 18 | AC_DEFUN([LTVERSION_VERSION], 19 | [macro_version='2.4' 20 | macro_revision='1.3293' 21 | _LT_DECL(, macro_version, 0, [Which release of libtool.m4 was used?]) 22 | _LT_DECL(, macro_revision, 0) 23 | ]) 24 | -------------------------------------------------------------------------------- /libplinkio/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include_directories( "." ) 2 | include_directories( ${LIBCSV_INCLUDE_DIR} ) 3 | 4 | file( GLOB_RECURSE SRC_LIST "*.c" "." ) 5 | file( GLOB_RECURSE HEADERS "*.h" "plinkio/" ) 6 | 7 | if( NOT DISABLE_SHARED_LIBRARY ) 8 | add_library( libplinkio SHARED ${SRC_LIST} ) 9 | target_link_libraries( libplinkio libcsv ) 10 | SET_TARGET_PROPERTIES( libplinkio PROPERTIES OUTPUT_NAME plinkio ) 11 | install( TARGETS libplinkio DESTINATION lib ) 12 | install( FILES ${HEADERS} DESTINATION include/plinkio ) 13 | endif( ) 14 | 15 | if( NOT DISABLE_STATIC_LIBRARY ) 16 | add_library( libplinkio-static STATIC ${SRC_LIST} ) 17 | target_link_libraries( libplinkio-static libcsv ) 18 | SET_TARGET_PROPERTIES( libplinkio-static PROPERTIES OUTPUT_NAME plinkio ) 19 | endif( ) 20 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/m4/ltversion.m4: -------------------------------------------------------------------------------- 1 | # ltversion.m4 -- version numbers -*- Autoconf -*- 2 | # 3 | # Copyright (C) 2004 Free Software Foundation, Inc. 4 | # Written by Scott James Remnant, 2004 5 | # 6 | # This file is free software; the Free Software Foundation gives 7 | # unlimited permission to copy and/or distribute it, with or without 8 | # modifications, as long as this notice is preserved. 9 | 10 | # @configure_input@ 11 | 12 | # serial 3293 ltversion.m4 13 | # This file is part of GNU Libtool 14 | 15 | m4_define([LT_PACKAGE_VERSION], [2.4]) 16 | m4_define([LT_PACKAGE_REVISION], [1.3293]) 17 | 18 | AC_DEFUN([LTVERSION_VERSION], 19 | [macro_version='2.4' 20 | macro_revision='1.3293' 21 | _LT_DECL(, macro_version, 0, [Which release of libtool.m4 was used?]) 22 | _LT_DECL(, macro_revision, 0) 23 | ]) 24 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/m4/namespaces.m4: -------------------------------------------------------------------------------- 1 | # Checks whether the compiler implements namespaces 2 | AC_DEFUN([AC_CXX_NAMESPACES], 3 | [AC_CACHE_CHECK(whether the compiler implements namespaces, 4 | ac_cv_cxx_namespaces, 5 | [AC_LANG_SAVE 6 | AC_LANG_CPLUSPLUS 7 | AC_TRY_COMPILE([namespace Outer { 8 | namespace Inner { int i = 0; }}], 9 | [using namespace Outer::Inner; return i;], 10 | ac_cv_cxx_namespaces=yes, 11 | ac_cv_cxx_namespaces=no) 12 | AC_LANG_RESTORE]) 13 | if test "$ac_cv_cxx_namespaces" = yes; then 14 | AC_DEFINE(HAVE_NAMESPACES, 1, [define if the compiler implements namespaces]) 15 | fi]) 16 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:20.04 2 | 3 | LABEL org.opencontainers.image.source=https://github.com/precimed/simu 4 | LABEL org.opencontainers.image.description="Simu-Linux" 5 | LABEL org.opencontainers.image.licenses=GPL-3.0 6 | 7 | ENV TZ=Europe 8 | ENV DEBIAN_FRONTEND noninteractive 9 | 10 | WORKDIR /tmp 11 | 12 | RUN apt-get update && apt-get install -y ca-certificates && \ 13 | update-ca-certificates && \ 14 | apt-get install -y git build-essential libboost-all-dev cmake && \ 15 | rm -rf /var/lib/apt/lists/* 16 | 17 | RUN git clone --recurse-submodules https://github.com/precimed/simu && \ 18 | cd simu && \ 19 | mkdir build && \ 20 | cd build && \ 21 | cmake .. && \ 22 | make && \ 23 | make install && \ 24 | cd /tmp && \ 25 | rm -rf simu 26 | 27 | WORKDIR / 28 | ENTRYPOINT ["/usr/local/bin/simu"] -------------------------------------------------------------------------------- /libplinkio/tests/mock.h: -------------------------------------------------------------------------------- 1 | #ifndef __MOCK_H__ 2 | #define __MOCK_H__ 3 | 4 | #include 5 | #include 6 | 7 | /** 8 | * Initializes the mock suite. Just call this function before you 9 | * are using functions that depend on fread etc. 10 | */ 11 | void mock_init(const char *string); 12 | 13 | /** 14 | * Mocks fopen, always returns stdin. 15 | */ 16 | FILE *mock_fopen(const char *path, const char *mode); 17 | 18 | /** 19 | * Mocks fclose, always returns 0. 20 | */ 21 | int mock_fclose(FILE *fp); 22 | 23 | /** 24 | * Mocks feof, returns eof when we are at the end of the string. 25 | */ 26 | int mock_feof(FILE *stream); 27 | 28 | /** 29 | * Mock fread, reads from a string instead of a file. 30 | */ 31 | size_t mock_fread(void *p, size_t size, size_t nmemb, FILE *stream); 32 | 33 | #endif /* End of __MOCK_H__ */ 34 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/src/example/product_database.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include 17 | 18 | // Connect to the database containing customer information. 19 | DatabaseConnection* connect_to_product_database() { 20 | return connect_to_database("products.abcd.org", 322); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /libplinkio/tests/Makefile.am: -------------------------------------------------------------------------------- 1 | AM_CFLAGS = -I$(srcdir)/../libs/cmockery/src/google -I$(srcdir)/../src/plinkio -I$(srcdir)/../src/ -I$(srcdir)/../libs/libcsv/inc -DUNIT_TESTING=1 2 | 3 | if GCC 4 | AM_CFLAGS += -Wall 5 | endif 6 | 7 | TESTS = fam_test bim_test bed_test 8 | noinst_PROGRAMS = $(TESTS) plinkio_test transpose 9 | 10 | fam_test_SOURCES = fam_test.c mock.c mock.h 11 | fam_test_LDADD = ../libs/cmockery/libcmockery.la ../libs/libcsv/libcsv.la 12 | 13 | bim_test_SOURCES = bim_test.c mock.c mock.h 14 | bim_test_LDADD = ../libs/cmockery/libcmockery.la ../libs/libcsv/libcsv.la 15 | 16 | bed_test_SOURCES = bed_test.c 17 | bed_test_LDADD = ../libs/cmockery/libcmockery.la 18 | 19 | plinkio_test_SOURCES = plinkio_test.c ../libs/libcsv/libcsv.la 20 | plinkio_test_LDADD = ../src/libplinkio.la 21 | plinkio_test_LDFLAGS = -static 22 | 23 | transpose_SOURCES = transpose.c 24 | transpose_LDADD = ../src/libplinkio.la -lm 25 | transpose_LDFLAGS = -static 26 | -------------------------------------------------------------------------------- /libplinkio/docs/mainpage.dox: -------------------------------------------------------------------------------- 1 | /** 2 | @brief Documentation file for Mainpage, and defgroups. 3 | @author Mattias Frånberg 4 | @file 5 | */ 6 | /** @defgroup Libplinkio */ 7 | /** 8 | @mainpage Libplinkio - Parsing plink genotype files 9 | This is a small C and Python library for reading Plink genotype files. 10 | 11 | Currently it can: 12 | - Read and parse BED, BIM and FAM files. 13 | - Transpose BED files. 14 | 15 | Libplinkio will reach 1.0 when it can: 16 | - Read PED files (i.e. non-binary bed-files). 17 | - Write BED, BIM and FAM files. 18 | 19 | Project rationales: 20 | - Use C to make it as simple as possible to add bindings 21 | for other languages. 22 | - Focus only on IO-functionality. 23 | - Few external dependencies to make it easy to use. 24 | 25 | The most relevant functions can be found in plinkio.h. You then have the pio_sample_t struct and pio_locus_t struct that allows you to access meta information about individuals and snps. 26 | 27 | */ 28 | -------------------------------------------------------------------------------- /libplinkio/src/plinkio/status.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012-2013, Mattias Frånberg 3 | * All rights reserved. 4 | * 5 | * This file is distributed under the Modified BSD License. See the COPYING file 6 | * for details. 7 | */ 8 | 9 | #ifndef __STATUS_H__ 10 | #define __STATUS_H__ 11 | 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | 16 | enum pio_status_e 17 | { 18 | /** 19 | * Function successful. 20 | */ 21 | PIO_OK, 22 | 23 | /** 24 | * File reached EOF. 25 | */ 26 | PIO_END, 27 | 28 | /** 29 | * Generic error. 30 | */ 31 | PIO_ERROR, 32 | 33 | /** 34 | * FAM IO error. 35 | */ 36 | P_FAM_IO_ERROR, 37 | 38 | /** 39 | * BIM IO error. 40 | */ 41 | P_BIM_IO_ERROR, 42 | 43 | /** 44 | * Bed IO error. 45 | */ 46 | P_BED_IO_ERROR 47 | }; 48 | 49 | typedef enum pio_status_e pio_status_t; 50 | 51 | #ifdef __cplusplus 52 | } 53 | #endif 54 | 55 | #endif /* End of __STATUS_H__ */ 56 | -------------------------------------------------------------------------------- /libplinkio/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include_directories( ${LIBPLINKIO_INCLUDE_DIR} ) 2 | include_directories( ${LIBPLINKIO_SOURCE_DIR} ) 3 | include_directories( ${LIBCMOCKERY_INCLUDE_DIR} ) 4 | include_directories( ${LIBCSV_INCLUDE_DIR} ) 5 | include_directories( "." ) 6 | 7 | add_definitions( -DUNIT_TESTING=1 ) 8 | 9 | add_executable( bed_test "bed_test.c" ) 10 | target_link_libraries( bed_test libcmockery libcsv ) 11 | add_test( bed_test bed_test ) 12 | 13 | add_executable( bim_test "bim_test.c" "mock.c" ) 14 | target_link_libraries( bim_test libcmockery libcsv ) 15 | add_test( bim_test bim_test ) 16 | 17 | add_executable( fam_test "fam_test.c" "mock.c" ) 18 | target_link_libraries( fam_test libcmockery libcsv ) 19 | add_test( fam_test fam_test ) 20 | 21 | add_executable( plinkio_test "plinkio_test.c" ) 22 | if( NOT DISABLE_STATIC_LIBRARY ) 23 | target_link_libraries( plinkio_test libcsv libplinkio-static ) 24 | else () 25 | target_link_libraries( plinkio_test libcsv libplinkio ) 26 | endif () 27 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/m4/stl_namespace.m4: -------------------------------------------------------------------------------- 1 | # We check what namespace stl code like vector expects to be executed in 2 | 3 | AC_DEFUN([AC_CXX_STL_NAMESPACE], 4 | [AC_CACHE_CHECK( 5 | what namespace STL code is in, 6 | ac_cv_cxx_stl_namespace, 7 | [AC_REQUIRE([AC_CXX_NAMESPACES]) 8 | AC_LANG_SAVE 9 | AC_LANG_CPLUSPLUS 10 | AC_TRY_COMPILE([#include ], 11 | [vector t; return 0;], 12 | ac_cv_cxx_stl_namespace=none) 13 | AC_TRY_COMPILE([#include ], 14 | [std::vector t; return 0;], 15 | ac_cv_cxx_stl_namespace=std) 16 | AC_LANG_RESTORE]) 17 | if test "$ac_cv_cxx_stl_namespace" = none; then 18 | AC_DEFINE(STL_NAMESPACE,, 19 | [the namespace where STL code like vector<> is defined]) 20 | fi 21 | if test "$ac_cv_cxx_stl_namespace" = std; then 22 | AC_DEFINE(STL_NAMESPACE,std, 23 | [the namespace where STL code like vector<> is defined]) 24 | fi 25 | ]) 26 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/src/example/run_tests.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | // A test case that does nothing and succeeds. 22 | void null_test_success(void **state) { 23 | } 24 | 25 | int main(int argc, char* argv[]) { 26 | const UnitTest tests[] = { 27 | unit_test(null_test_success), 28 | }; 29 | return run_tests(tests); 30 | } 31 | -------------------------------------------------------------------------------- /libplinkio/src/plinkio/fam_parse.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012-2013, Mattias Frånberg 3 | * All rights reserved. 4 | * 5 | * This file is distributed under the Modified BSD License. See the COPYING file 6 | * for details. 7 | */ 8 | 9 | #ifndef __FAM_PARSE_H__ 10 | #define __FAM_PARSE_H__ 11 | 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | 16 | #include 17 | 18 | #include 19 | #include 20 | 21 | /** 22 | * Parses the samples and points the given sample array to a 23 | * the memory that contains them, and writes back the number 24 | * of samples. 25 | * 26 | * @param fam_fp Fam file. 27 | * @param sample Parsed samples will be stored here. 28 | * 29 | * @return PIO_OK if the samples could be parsed, PIO_ERROR otherwise. 30 | */ 31 | pio_status_t parse_samples(FILE *fam_fp, UT_array *sample); 32 | 33 | /** 34 | * Writes a sample to the .fam file. 35 | * 36 | * @param fam_fp Fam file. 37 | * @param sample The sample to write. 38 | */ 39 | pio_status_t write_sample(FILE *fam_fp, struct pio_sample_t *sample); 40 | 41 | #ifdef __cplusplus 42 | } 43 | #endif 44 | 45 | #endif /* End of __FAM_PARSE_H__ */ 46 | -------------------------------------------------------------------------------- /libplinkio/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( libplinkio ) 2 | cmake_minimum_required( VERSION 2.8.9 ) 3 | 4 | set( LIBPLINKIO_VERSION_MAJOR 0 ) 5 | set( LIBPLINKIO_VERSION_MINOR 3 ) 6 | set( LIBPLINKIO_VERSION_PATCH 0 ) 7 | 8 | set( CPACK_SOURCE_GENERATOR "TGZ" ) 9 | set( CPACK_SOURCE_PACKAGE_FILE_NAME 10 | "${CMAKE_PROJECT_NAME}-${LIBPLINKIO_VERSION_MAJOR}.${LIBPLINKIO_VERSION_MINOR}.${LIBPLINKIO_VERSION_PATCH}" ) 11 | set( CPACK_SOURCE_IGNORE_FILES "build*;/.git/;log$;gitignore$;/autom4te.cache/;~$;${CPACK_SOURCE_IGNORE_FILES}" ) 12 | include( CPack ) 13 | 14 | set( LIBCSV_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/libs/libcsv/inc/" ) 15 | set( LIBCMOCKERY_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/libs/cmockery/src/google/" ) 16 | set( LIBPLINKIO_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/src/plinkio/" ) 17 | set( LIBPLINKIO_SOURCE_DIR "${PROJECT_SOURCE_DIR}/src/" ) 18 | 19 | option( DISABLE_TESTS "Disables building tests." OFF ) 20 | option( DISABLE_SHARED_LIBS "Disable building shared library." OFF ) 21 | option( DISABLE_STATIC_LIBS "Disable building static library." OFF ) 22 | 23 | add_subdirectory( src ) 24 | add_subdirectory( libs ) 25 | 26 | if( NOT DISABLE_TESTS ) 27 | enable_testing( ) 28 | add_subdirectory( tests ) 29 | endif( ) 30 | -------------------------------------------------------------------------------- /libplinkio/src/plinkio/bim_parse.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012-2013, Mattias Frånberg 3 | * All rights reserved. 4 | * 5 | * This file is distributed under the Modified BSD License. See the COPYING file 6 | * for details. 7 | */ 8 | 9 | #ifndef __BIM_PARSE_H__ 10 | #define __BIM_PARSE_H__ 11 | 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | 16 | #include 17 | 18 | /** 19 | * Parses the loci and points the given locus array to a 20 | * the memory that contains them, and writes back the number 21 | * of loci. 22 | * 23 | * @param bim_fp Bim file. 24 | * @param locus The parsed loci will be stored here. 25 | * 26 | * @return PIO_OK if the loci could be parsed, PIO_ERROR otherwise. 27 | */ 28 | pio_status_t parse_loci(FILE *bim_fp, UT_array *locus); 29 | 30 | /** 31 | * Writes a single locus to the .bim file. 32 | * 33 | * @param bim_fp The .bim file to write to. 34 | * @param locus The locus to write. 35 | * 36 | * @return PIO_OK if the locus was successfully written, 37 | * PIO_ERROR otherwise. 38 | */ 39 | pio_status_t write_locus(FILE *bim_fp, struct pio_locus_t *locus); 40 | 41 | #ifdef __cplusplus 42 | } 43 | #endif 44 | 45 | #endif /* End of __BIM_PARSE_H__ */ 46 | -------------------------------------------------------------------------------- /matlab/make_genofreq.m: -------------------------------------------------------------------------------- 1 | function frame = make_genofreq(frame, config, varargin) 2 | % MAKE_GENOFREQ calculate frequencies of AA, Aa, and aa genotypes. 3 | 4 | p = inputParser; 5 | addOptional(p, 'snpstep', 100); % how many SNPs to scan per one iteration 6 | parse(p, varargin{:}); opts = p.Results; 7 | 8 | if frame.subj ~= config.frames.(frame.name).subj, error('Number of subjects mismatch'); end; 9 | if frame.snps ~= config.frames.(frame.name).snps, error('Number of SNPs mismatch'); end; 10 | 11 | nsnp = frame.snps; 12 | nsubj = frame.subj; 13 | reader = config.frames.(frame.name).reader; 14 | snpstep = opts.snpstep; 15 | 16 | genofreq = zeros(nsnp, 3); 17 | beginningOfTime=now; 18 | for i=1:snpstep:nsnp 19 | e = min(i+snpstep, nsnp); 20 | X = reader(1:nsubj, i:e); 21 | genofreq(i:e, 1) = sum(X == 0); 22 | genofreq(i:e, 2) = sum(X == 1); 23 | genofreq(i:e, 3) = sum(X == 2); 24 | fprintf(1,'make_genofreq: %.1f%% done, Now:%s eta:%s\n', 100 * (i+snpstep) / nsnp, datestr(now,'dddd HH:MM:SS'),datestr(beginningOfTime+(now-beginningOfTime)/(i+snpstep)*nsnp)); 25 | end 26 | 27 | frame.genofreq = genofreq; 28 | frame.opts.make_genofreq = opts; 29 | end 30 | -------------------------------------------------------------------------------- /libplinkio/tests/mock.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "mock.h" 4 | 5 | /** 6 | * Returns the smallest value of x and y 7 | * compared with the < operator. 8 | */ 9 | #define MIN(x,y) ( ( (x) < (y) ) ? (x) : (y) ) 10 | 11 | /** 12 | * File string. 13 | */ 14 | static const char *g_file_str; 15 | 16 | /** 17 | * Start index of next mock_fread call. 18 | */ 19 | static int g_file_pos = 0; 20 | 21 | void 22 | mock_init(const char *string) 23 | { 24 | g_file_str = string; 25 | g_file_pos = 0; 26 | } 27 | 28 | FILE * 29 | mock_fopen(const char *path, const char *mode) 30 | { 31 | return stdin; 32 | } 33 | 34 | int 35 | mock_fclose(FILE *fp) 36 | { 37 | return 0; 38 | } 39 | 40 | int 41 | mock_feof(FILE *stream) 42 | { 43 | return g_file_pos >= strlen( g_file_str ); 44 | } 45 | 46 | size_t 47 | mock_fread(void *p, size_t size, size_t nmemb, FILE *stream) 48 | { 49 | size_t length_left = strlen( g_file_str ) - g_file_pos; 50 | size_t bytes_to_copy = MIN( size * nmemb, length_left ); 51 | g_file_pos += bytes_to_copy; 52 | 53 | if( bytes_to_copy > 0 ) 54 | { 55 | strncpy( p, g_file_str, bytes_to_copy ); 56 | return bytes_to_copy; 57 | } 58 | else 59 | { 60 | return 0; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /libplinkio/src/plinkio/file.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012-2013, Mattias Frånberg 3 | * All rights reserved. 4 | * 5 | * This file is distributed under the Modified BSD License. See the COPYING file 6 | * for details. 7 | */ 8 | 9 | #ifndef __FILE_H__ 10 | #define __FILE_H__ 11 | 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | 16 | /** 17 | * Defines return values from file operations. 18 | */ 19 | enum file_status { 20 | /** 21 | * Means that an operation was successfully performed. 22 | */ 23 | FILE_OK, 24 | 25 | /** 26 | * Means that an error occurred. 27 | */ 28 | FILE_ERROR 29 | }; 30 | 31 | typedef enum file_status file_status_t; 32 | 33 | /** 34 | * Copies a file to another. 35 | * 36 | * @param from_path The path to copy from. 37 | * @param to_path The destination path. 38 | * 39 | * @return FILE_OK if the file was copied, FILE_ERROR otherwise. 40 | */ 41 | file_status_t file_copy(const char *from_path, const char *to_path); 42 | 43 | /** 44 | * Removes the file of the given path 45 | * 46 | * @param path Path to the file to remove. 47 | * 48 | * @return FILE_OK if the file was removed, FILE_ERROR otherwise. 49 | */ 50 | file_status_t file_remove(const char *path); 51 | 52 | #ifdef __cplusplus 53 | } 54 | #endif 55 | 56 | #endif /* __FILE_H__ */ 57 | -------------------------------------------------------------------------------- /libplinkio/src/plinkio/snp_lookup.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012-2013, Mattias Frånberg 3 | * All rights reserved. 4 | * 5 | * This file is distributed under the Modified BSD License. See the COPYING file 6 | * for details. 7 | */ 8 | 9 | #ifndef __SNP_LOOKUP_H__ 10 | #define __SNP_LOOKUP_H__ 11 | 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | 16 | #if HAVE_ENDIAN_H 17 | #include 18 | #elif HAVE_MACHINE_ENDIAN_H 19 | #include 20 | #elif HAVE_SYS_ENDIAN_H 21 | #include 22 | #endif 23 | 24 | /** 25 | * Maps an unpacked snp to its corresponding bits. 26 | */ 27 | unsigned char snp_to_bits[] = { 0, 2, 3, 1 }; 28 | 29 | /** 30 | * This files contains a lookup table that maps 31 | * SNPs packed in a single byte into an array of 32 | * four bytes. 33 | */ 34 | union snp_lookup_t 35 | { 36 | /** 37 | * Accessible as an array. 38 | */ 39 | unsigned char snp_array[4]; 40 | 41 | /** 42 | * Accessible as a block of bytes. 43 | */ 44 | int32_t snp_block; 45 | }; 46 | 47 | #if __BYTE_ORDER == __LITTLE_ENDIAN 48 | #include 49 | #else 50 | #include 51 | #endif /* End test endianess */ 52 | 53 | #ifdef __cplusplus 54 | } 55 | #endif 56 | 57 | #endif /* End of __SNP_LOOKUP_H__ */ 58 | -------------------------------------------------------------------------------- /matlab/remove_dups_100K_rep10.sh: -------------------------------------------------------------------------------- 1 | export FILES="EURqc_chr8_chunk125_plink_100K EURqc_chr8_chunk5_plink_100K EURqc_chr15_chunk86_plink_100K EURqc_chr9_chunk68_plink_100K EURqc_chr8_chunk217_plink_100K EURqc_chr2_chunk136_plink_100K EURqc_chr2_chunk15_plink_100K EURqc_chr2_chunk14_plink_100K EURqc_chr1_chunk20_plink_100K EURqc_chr8_chunk126_plink_100K EURqc_chr9_chunk69_plink_100K EURqc_chr8_chunk6_plink_100K EURqc_chr9_chunk70_plink_100K" 2 | export SNPS=exclude_snps.txt 3 | module load plink 4 | for FILE in $FILES; do 5 | for REPI in `seq 1 10`; do 6 | #mv /work/users/oleksanf/HAPGEN/"$FILE"_rep"$REPI".bim /work/users/oleksanf/HAPGEN/"$FILE"_rep"$REPI".dups.bim 7 | #mv /work/users/oleksanf/HAPGEN/"$FILE"_rep"$REPI".bed /work/users/oleksanf/HAPGEN/"$FILE"_rep"$REPI".dups.bed 8 | #mv /work/users/oleksanf/HAPGEN/"$FILE"_rep"$REPI".fam /work/users/oleksanf/HAPGEN/"$FILE"_rep"$REPI".dups.fam 9 | #mv /work/users/oleksanf/HAPGEN/"$FILE"_rep"$REPI".nosex /work/users/oleksanf/HAPGEN/"$FILE"_rep"$REPI".dups.nosex 10 | #mv /work/users/oleksanf/HAPGEN/"$FILE"_rep"$REPI".log /work/users/oleksanf/HAPGEN/"$FILE"_rep"$REPI".dups.log 11 | plink --bfile /work/users/oleksanf/HAPGEN/"$FILE"_rep"$REPI".dups --make-bed --out /work/users/oleksanf/HAPGEN/"$FILE"_rep"$REPI" --exclude $SNPS 12 | done 13 | done 14 | 15 | -------------------------------------------------------------------------------- /libplinkio/tests/plinkio_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | /** 8 | * A small test program that reads the given plink file 9 | * and asserts that the number of rows is correct, depending 10 | * on how the genotypes are stored. 11 | */ 12 | int 13 | main(int argc, char *argv[]) 14 | { 15 | int num_rows; 16 | snp_t *snp_buffer; 17 | struct pio_file_t plink_file; 18 | 19 | if( argc != 2 ) 20 | { 21 | printf("Usage: plinkio_test plink_path_prefix\n"); 22 | return EXIT_FAILURE; 23 | } 24 | 25 | if( pio_open( &plink_file, argv[ 1 ] ) != PIO_OK ) 26 | { 27 | printf( "Error: Could not open %s\n", argv[ 1 ] ); 28 | return EXIT_FAILURE; 29 | } 30 | 31 | num_rows = 0; 32 | snp_buffer = (snp_t *) malloc( pio_row_size( &plink_file ) ); 33 | while( pio_next_row( &plink_file, snp_buffer ) == PIO_OK ) 34 | { 35 | num_rows++; 36 | } 37 | 38 | if( pio_one_locus_per_row( &plink_file ) ) 39 | { 40 | assert( num_rows == bim_num_loci( &plink_file.bim_file ) ); 41 | } 42 | else 43 | { 44 | assert( num_rows == fam_num_samples( &plink_file.fam_file ) ); 45 | } 46 | 47 | pio_close( &plink_file ); 48 | free( snp_buffer ); 49 | 50 | return EXIT_SUCCESS; 51 | } 52 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/src/example/assert_macro.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include 17 | 18 | static const char* status_code_strings[] = { 19 | "Address not found", 20 | "Connection dropped", 21 | "Connection timed out", 22 | }; 23 | 24 | const char* get_status_code_string(const unsigned int status_code) { 25 | return status_code_strings[status_code]; 26 | }; 27 | 28 | unsigned int string_to_status_code(const char* const status_code_string) { 29 | unsigned int i; 30 | for (i = 0; i < sizeof(status_code_string) / sizeof(status_code_string[0]); 31 | i++) { 32 | if (strcmp(status_code_strings[i], status_code_string) == 0) { 33 | return i; 34 | } 35 | } 36 | return ~0U; 37 | } 38 | -------------------------------------------------------------------------------- /matlab/make_phenotype.m: -------------------------------------------------------------------------------- 1 | function frame = make_phenotype(frame, config, varargin) 2 | % MAKE_PHENOTYPE adjust truepheno to a given level of heritability 3 | 4 | p = inputParser; 5 | addOptional(p, 'h2', 0.8); % how many SNPs to scan per one iteration 6 | parse(p, varargin{:}); opts = p.Results; 7 | 8 | nsubj = frame.subj; 9 | h2 = opts.h2; 10 | ntraits = size(frame.truepheno, 2); 11 | 12 | for trait = 1:ntraits 13 | truePhenotype = frame.truepheno(:, trait); 14 | noisevec = randn(nsubj, 1); 15 | if h2 == 0 || var(truePhenotype) == 0 16 | observedPhenotype = noisevec / sqrt(var(noisevec)); 17 | % This is an interesting case --- why there are some true casual SNPs, and yet heritability is 0? 18 | % This imply that environment has far larger effect that those SNPs. 19 | % Mathematically in this case the factor (1-h2)/h2 would go to infinity, 20 | % So we just pretend that observed phenotype has no relation with true phenotype (thus with beta vec). 21 | else 22 | observedPhenotype = truePhenotype + noisevec*sqrt((1-h2)/h2*var(truePhenotype)/var(noisevec)); 23 | %=> var(truePhenotype) / var(observedPhenotype) == h2 24 | end 25 | frame.phenotype(:, trait) = observedPhenotype; 26 | end 27 | 28 | frame.opts.make_phenotype = opts; 29 | end -------------------------------------------------------------------------------- /libplinkio/src/file.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012-2013, Mattias Frånberg 3 | * All rights reserved. 4 | * 5 | * This file is distributed under the Modified BSD License. See the COPYING file 6 | * for details. 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | 15 | /** 16 | * Total size for a buffer containing the copy command 17 | * that will be issued to the shell. 18 | */ 19 | #define FILE_COPY_BUFFER_SIZE 4096 20 | 21 | file_status_t 22 | file_copy(const char *from_path, const char *to_path) 23 | { 24 | char *copy_command = malloc( sizeof( char ) * ( strlen( from_path ) + strlen( to_path ) + 5 ) ); 25 | int status = 0; 26 | 27 | sprintf( copy_command, "cp %s %s", from_path, to_path ); 28 | status = system( copy_command ); 29 | free( copy_command ); 30 | 31 | if( status != -1 ) 32 | { 33 | return FILE_OK; 34 | } 35 | else 36 | { 37 | return FILE_ERROR; 38 | } 39 | 40 | } 41 | 42 | file_status_t 43 | file_remove(const char *path) 44 | { 45 | char *rm_command = malloc( sizeof( char ) * ( strlen( path ) + 4 ) ); 46 | int status = 0; 47 | 48 | sprintf( rm_command, "rm %s", path ); 49 | status = system( rm_command ); 50 | free( rm_command ); 51 | 52 | if( status != -1 ) 53 | { 54 | return FILE_OK; 55 | } 56 | else 57 | { 58 | return FILE_ERROR; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/src/example/database.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | typedef struct DatabaseConnection DatabaseConnection; 17 | 18 | /* Function that takes an SQL query string and sets results to an array of 19 | * pointers with the result of the query. The value returned specifies the 20 | * number of items in the returned array of results. The returned array of 21 | * results are statically allocated and should not be deallocated using free() 22 | */ 23 | typedef unsigned int (*QueryDatabase)( 24 | DatabaseConnection* const connection, const char * const query_string, 25 | void *** const results); 26 | 27 | // Connection to a database. 28 | struct DatabaseConnection { 29 | const char *url; 30 | unsigned int port; 31 | QueryDatabase query_database; 32 | }; 33 | 34 | // Connect to a database. 35 | DatabaseConnection* connect_to_database(const char * const url, 36 | const unsigned int port); 37 | 38 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/COPYING: -------------------------------------------------------------------------------- 1 | Copyright (c) 2006, Google Inc. 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 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | * Redistributions in binary form must reproduce the above 11 | copyright notice, this list of conditions and the following disclaimer 12 | in the documentation and/or other materials provided with the 13 | distribution. 14 | * Neither the name of Google Inc. nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | -------------------------------------------------------------------------------- /matlab/PlinkRead_binary2.m: -------------------------------------------------------------------------------- 1 | function genomat = PlinkRead_binary2(nsubj,snps,fileprefix) 2 | persistent geno_values 3 | 4 | % Written by Chun 2015 5 | if ~issorted(snps), error('PlinkRead_binary2 expect sorted list of snps'); end; 6 | nsnp = length(snps); 7 | 8 | % bit shift to generate the genovalue matrix 9 | bedprefix = [fileprefix,'.bed']; 10 | 11 | if isempty(geno_values) 12 | geno_values = zeros(256,4,'int8'); 13 | geno_code = [-1,1,0,2]; 14 | shiftind = [0,2,4,6]; 15 | indvec=zeros(1,4); 16 | 17 | for i = 1:256; 18 | ishift = int16(i-1); 19 | for j = 1:4; 20 | indvec(j) = bitand(bitsra(ishift,shiftind(j)),3) ; 21 | end 22 | indvec(indvec == 0) = 4; 23 | geno_values(i,:) = geno_code(indvec); 24 | end 25 | end 26 | 27 | % Read in the binary file 28 | bedid = fopen(bedprefix); 29 | genobin = uint16(fread(bedid, 3)); 30 | 31 | % Check magic number 32 | if genobin(1) ~= 108; 33 | error('- Not a valid Plink BED file \r\n'); 34 | elseif genobin(2) ~= 27; 35 | error('- Not a valid Plink BED file \r\n'); 36 | elseif genobin(3) ~= 1; 37 | error('- Not in SNP-major format \r\n'); 38 | end 39 | 40 | n_bytes = ceil(nsubj/4); 41 | genomat = zeros(nsubj,nsnp,'int8'); 42 | for i = 1:nsnp; 43 | fseek(bedid, 3 + (snps(i) - 1) * n_bytes, 'bof'); 44 | genobin = uint16(fread(bedid, n_bytes)); 45 | if length(genobin) ~= n_bytes, error('-- Invalid number of entries from the bed \r\n'); end 46 | tmp_values = geno_values(genobin + 1, :)'; 47 | tmp_values = tmp_values(:); 48 | genomat(:,i) = tmp_values(1:nsubj); 49 | end 50 | fclose(bedid); 51 | 52 | end -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/src/example/allocate_module_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | extern void leak_memory(); 22 | extern void buffer_overflow(); 23 | extern void buffer_underflow(); 24 | 25 | // Test case that fails as leak_memory() leaks a dynamically allocated block. 26 | void leak_memory_test(void **state) { 27 | leak_memory(); 28 | } 29 | 30 | // Test case that fails as buffer_overflow() corrupts an allocated block. 31 | void buffer_overflow_test(void **state) { 32 | buffer_overflow(); 33 | } 34 | 35 | // Test case that fails as buffer_underflow() corrupts an allocated block. 36 | void buffer_underflow_test(void **state) { 37 | buffer_underflow(); 38 | } 39 | 40 | int main(int argc, char* argv[]) { 41 | const UnitTest tests[] = { 42 | unit_test(leak_memory_test), 43 | unit_test(buffer_overflow_test), 44 | unit_test(buffer_underflow_test), 45 | }; 46 | return run_tests(tests); 47 | } 48 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/src/example/allocate_module.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include 17 | 18 | #ifdef HAVE_MALLOC_H 19 | #include 20 | #endif 21 | 22 | #if UNIT_TESTING 23 | extern void* _test_malloc(const size_t size, const char* file, const int line); 24 | extern void* _test_calloc(const size_t number_of_elements, const size_t size, 25 | const char* file, const int line); 26 | extern void _test_free(void* const ptr, const char* file, const int line); 27 | 28 | #define malloc(size) _test_malloc(size, __FILE__, __LINE__) 29 | #define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__) 30 | #define free(ptr) _test_free(ptr, __FILE__, __LINE__) 31 | #endif // UNIT_TESTING 32 | 33 | void leak_memory() { 34 | int * const temporary = (int*)malloc(sizeof(int)); 35 | *temporary = 0; 36 | } 37 | 38 | void buffer_overflow() { 39 | char * const memory = (char*)malloc(sizeof(int)); 40 | memory[sizeof(int)] = '!'; 41 | free(memory); 42 | } 43 | 44 | void buffer_underflow() { 45 | char * const memory = (char*)malloc(sizeof(int)); 46 | memory[-1] = '!'; 47 | free(memory); 48 | } 49 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/src/example/customer_database.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include 17 | #include 18 | #include 19 | #ifdef _WIN32 20 | #define snprintf _snprintf 21 | #endif // _WIN32 22 | 23 | // Connect to the database containing customer information. 24 | DatabaseConnection* connect_to_customer_database() { 25 | return connect_to_database("customers.abcd.org", 321); 26 | } 27 | 28 | /* Find the ID of a customer by his/her name returning a value > 0 if 29 | * successful, 0 otherwise. */ 30 | unsigned int get_customer_id_by_name( 31 | DatabaseConnection * const connection, 32 | const char * const customer_name) { 33 | char query_string[256]; 34 | int number_of_results; 35 | void **results; 36 | snprintf(query_string, sizeof(query_string), 37 | "SELECT ID FROM CUSTOMERS WHERE NAME = %s", customer_name); 38 | number_of_results = connection->query_database(connection, query_string, 39 | &results); 40 | if (number_of_results != 1) { 41 | return -1; 42 | } 43 | return (unsigned int)results[0]; 44 | } 45 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/packages/deb/copyright: -------------------------------------------------------------------------------- 1 | This package was debianized by Google Inc. on 2 | 28 August 2008. 3 | 4 | It was downloaded from http://code.google.com/ 5 | 6 | Upstream Author: opensource@google.com 7 | 8 | Copyright (c) 2006, Google Inc. 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are 13 | met: 14 | 15 | * Redistributions of source code must retain the above copyright 16 | notice, this list of conditions and the following disclaimer. 17 | * Redistributions in binary form must reproduce the above 18 | copyright notice, this list of conditions and the following disclaimer 19 | in the documentation and/or other materials provided with the 20 | distribution. 21 | * Neither the name of Google Inc. nor the names of its 22 | contributors may be used to endorse or promote products derived from 23 | this software without specific prior written permission. 24 | 25 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 28 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 29 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 31 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 35 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/src/example/assert_macro_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | extern const char* get_status_code_string(const unsigned int status_code); 22 | extern unsigned int string_to_status_code( 23 | const char* const status_code_string); 24 | 25 | /* This test will fail since the string returned by get_status_code_string(0) 26 | * doesn't match "Connection timed out". */ 27 | void get_status_code_string_test(void **state) { 28 | assert_string_equal(get_status_code_string(0), "Address not found"); 29 | assert_string_equal(get_status_code_string(1), "Connection timed out"); 30 | } 31 | 32 | // This test will fail since the status code of "Connection timed out" isn't 1 33 | void string_to_status_code_test(void **state) { 34 | assert_int_equal(string_to_status_code("Address not found"), 0); 35 | assert_int_equal(string_to_status_code("Connection timed out"), 1); 36 | } 37 | 38 | int main(int argc, char *argv[]) { 39 | const UnitTest tests[] = { 40 | unit_test(get_status_code_string_test), 41 | unit_test(string_to_status_code_test), 42 | }; 43 | return run_tests(tests); 44 | } 45 | -------------------------------------------------------------------------------- /matlab/make_truepheno.m: -------------------------------------------------------------------------------- 1 | function frame = make_truepheno(frame, config, varargin) 2 | % MAKE_TRUEPHENO generates true phenotypes (without adding environmental noise) 3 | 4 | p = inputParser; 5 | addOptional(p, 'snpstep', 100); % how many SNPs to scan per one iteration 6 | addOptional(p, 'stdnorm', false); % normalize genotypes as in LD score regression (=> rare SNPs has larger effect size) 7 | parse(p, varargin{:}); opts = p.Results; 8 | 9 | if frame.subj ~= config.frames.(frame.name).subj, error('Number of subjects mismatch'); end; 10 | if frame.snps ~= config.frames.(frame.name).snps, error('Number of SNPs mismatch'); end; 11 | 12 | nsubj = frame.subj; 13 | snpstep = opts.snpstep; 14 | ntraits = size(frame.truebeta, 2); 15 | reader = config.frames.(frame.name).reader; 16 | 17 | frame.truepheno = nan(nsubj, ntraits); 18 | beginningOfTime = now; 19 | for trait = 1:ntraits 20 | betavec = frame.truebeta(:, trait); 21 | betavec_idx = find(betavec ~= 0); 22 | truePhenotype = zeros(nsubj, 1); 23 | 24 | for i=1:snpstep:length(betavec_idx) 25 | e = min(i+snpstep, length(betavec_idx)); 26 | genotypes = reader(1:nsubj, betavec_idx(i:e)); 27 | X = double(genotypes); X = (X-repmat(mean(X,1),nsubj,1)); 28 | if opts.stdnorm, X = X ./ repmat(std(X), [nsubj, 1]); end; 29 | truePhenotype = truePhenotype + X * betavec(betavec_idx(i:e)); 30 | fprintf(1,'make_truepheno (trait %i of %i): %.1f%% done, Now:%s eta:%s\n', trait, ntraits, 100 * (i+snpstep) / length(betavec_idx), datestr(now,'dddd HH:MM:SS'),datestr(beginningOfTime+(now-beginningOfTime)/(i+snpstep)*length(betavec_idx))); 31 | end 32 | 33 | frame.truepheno(:, trait) = truePhenotype; 34 | end 35 | 36 | frame.opts.make_truepheno = opts; 37 | end -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/src/example/key_value.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include 17 | #include 18 | #include 19 | 20 | typedef struct KeyValue { 21 | unsigned int key; 22 | const char* value; 23 | } KeyValue; 24 | 25 | static KeyValue *key_values = NULL; 26 | static unsigned int number_of_key_values = 0; 27 | 28 | void set_key_values(KeyValue * const new_key_values, 29 | const unsigned int new_number_of_key_values) { 30 | key_values = new_key_values; 31 | number_of_key_values = new_number_of_key_values; 32 | } 33 | 34 | // Compare two key members of KeyValue structures. 35 | int key_value_compare_keys(const void *a, const void *b) { 36 | return (int)((KeyValue*)a)->key - (int)((KeyValue*)b)->key; 37 | } 38 | 39 | // Search an array of key value pairs for the item with the specified value. 40 | KeyValue* find_item_by_value(const char * const value) { 41 | unsigned int i; 42 | for (i = 0; i < number_of_key_values; i++) { 43 | if (strcmp(key_values[i].value, value) == 0) { 44 | return &key_values[i]; 45 | } 46 | } 47 | return NULL; 48 | } 49 | 50 | // Sort an array of key value pairs by key. 51 | void sort_items_by_key() { 52 | qsort(key_values, number_of_key_values, sizeof(*key_values), 53 | key_value_compare_keys); 54 | } 55 | -------------------------------------------------------------------------------- /matlab/generate_sym01.m: -------------------------------------------------------------------------------- 1 | index %index = 1; % to be defined by caller 2 | 3 | rng('shuffle') 4 | 5 | if ~exist('config', 'var'), find_config; end; 6 | fieldnames(config.frames) 7 | 8 | load(fullfile(norstore_path, 'SYNGP/misc/gencode_annomat_binary.mat')); 9 | 10 | differential_enrichment = { '_AllSNPs', '_Introns', '_Exons' }; 11 | 12 | hvec_opts = [0.1, 0.3, 0.5, 0.7, 0.9]; 13 | pivec_opts = [1e-6, 3e-6, 1e-5, 3e-5, 1e-4, 3e-4, 1e-3, 3e-3, 1e-2, 3e-2, 1e-1, 3e-1]; 14 | 15 | index = mod(index - 1, length(pivec_opts)) + 1; 16 | if index <= 0 || index > length(pivec_opts), error('invalid index'); end; 17 | pivec = pivec_opts(index); 18 | 19 | for di = 1:3 20 | 21 | if di == 1, mask = []; end; 22 | if di == 2, mask = GENCODE_intron; end; 23 | if di == 3, mask = GENCODE_exon; end; 24 | 25 | frame = make_empty_frame (config, 'EUR_100K_80M_chunks'); 26 | frame = make_truebeta_gmm(frame, config, 'pivec', pivec, 'sig1vec', 1.0, 'sig2vec', 1.0, 'rhovec', 0.0, 'mask', mask); 27 | frame.truebeta = frame.truebeta(:, 1); 28 | frame.mixture = frame.mixture(:, 1); 29 | frame = make_truepheno (frame, config, 'snpstep', 100); 30 | 31 | [~,filename,~] = fileparts(tempname); 32 | save(sprintf('truepheno_%s_pi%.0e.mat', filename, pivec), 'frame', '-v7.3'); 33 | 34 | %load(fullfile(norstore_path, 'SYNGP/EUR_100K_80M_pheno/polygenic_20170218_index5.mat')) 35 | frame0 = frame; 36 | for hindex = 1:length(hvec_opts) 37 | frame = reframe (frame0, config, 'EUR_100K_1188K_merged'); 38 | frame = make_phenotype(frame, config, 'h2', hvec_opts(hindex)); 39 | frame = make_gwas (frame, config); 40 | frame = make_gwaslogp (frame, config); 41 | frame = reframe (frame, config, 'EUR_100K_1190K_ref'); 42 | frame.opts.now = datestr(now); 43 | save(sprintf('gwaslogp_%s_pi=%.0e_h2=%.2f%s.mat', filename, pivec, hvec_opts(hindex), differential_enrichment{di}), 'frame', '-v7.3'); 44 | end 45 | 46 | % frame = make_genofreq(frame, config); save('genofreq_EUR_100K_80M_chunks.mat', 'frame', '-v7.3'); 47 | 48 | end 49 | -------------------------------------------------------------------------------- /matlab/run_polygenic_truepheno_EUR_100K_80M_chunks.m: -------------------------------------------------------------------------------- 1 | index %index = 1; % to be defined by caller 2 | framename %framename = 'EUR_100K_80M_chunks'; % to be defined by caller 3 | config = struct(); 4 | 5 | t = 'H:\NORSTORE'; if exist(t, 'dir'), norstore_path = t; end; 6 | t = '/work/users/oleksanf/NORSTORE'; if exist(t, 'dir'), norstore_path = t; end; 7 | t = 'E:\EUR_100K_80M'; if exist(t, 'dir'), path80m = t; end; 8 | t = '/work/users/oleksanf/MMIL/cfan_SimuPop/EUR'; if exist(t, 'dir'), path80m = t; end; 9 | snp_detail_path = fullfile(norstore_path, 'SYNGP/misc/snp_detail.mat'); 10 | 11 | % 80M chunked frame - 80378054 SNPs, 3920 non-empty chunks, 3931 chunks (11 weird empty chunks) 12 | config.frames.EUR_100K_80M_chunks.subj = 100000; 13 | config.frames.EUR_100K_80M_chunks.snps = 80378054; 14 | config.frames.EUR_100K_80M_chunks.reader = @(subjvec, snpvec)chunked_frame_reader(subjvec, snpvec, 100000, snp_detail_path, path80m); 15 | 16 | % 80M chunked frame, 500 subjects 17 | config.frames.EUR_500_80M_chunks.subj = 500; 18 | config.frames.EUR_500_80M_chunks.snps = 80378054; 19 | config.frames.EUR_500_80M_chunks.reader = @(subjvec, snpvec)chunked_frame_reader(subjvec, snpvec, 500, snp_detail_path, fullfile(norstore_path, 'SYNGP/EUR_500_80M')); 20 | 21 | %framename = 'EUR_500_80M_chunks'; 22 | %framename = 'EUR_100K_80M_chunks'; 23 | 24 | pivec_opts = [1e-5, 3e-5, 1e-4, 3e-4, 1e-3, 3e-3, 1e-2]; 25 | if index <= 0 || index > length(pivec_opts), error('invalid index'); end; 26 | pivec = pivec_opts(index); 27 | 28 | frame = make_empty_frame (config, framename); 29 | %frame = make_genofreq (frame, config, 'snpstep', 10000); save(sprintf('genofreq_%s.mat', framename), 'frame', '-v7.3'); 30 | frame = make_truebeta_gmm(frame, config, 'pivec', pivec, 'sig1vec', 1.0, 'sig2vec', 1.0, 'rhovec', 0.0); 31 | frame = make_truepheno (frame, config, 'snpstep', 100); 32 | 33 | [~,filename,~] = fileparts(tempname); 34 | save(sprintf('result_polygenic_%i_%s_%s.mat', index, framename, filename), 'frame', '-v7.3'); 35 | 36 | 37 | -------------------------------------------------------------------------------- /matlab/tutorial.m: -------------------------------------------------------------------------------- 1 | % Data folder with synthetic genotypes (can be downloaded from dropbox) 2 | % https://www.dropbox.com/sh/b2it1oax6l0nc9b/AACT6ADa6bcr2gUV8tRwuvdka?dl=0 3 | datafolder = 'H:\Dropbox\analysis\2017_09_September_28_Norbis'; 4 | 5 | % System code - bootstrap simulations... 6 | if ~exist('config', 'var') 7 | fprintf('Setup genotypes... '); 8 | config.frames.EUR_10K_1188K_merged.subj = 10000; 9 | config.frames.EUR_10K_1188K_merged.snps = 1188973; 10 | config.frames.EUR_10K_1188K_merged.reader = @(subjvec, snpvec)merged_frame_reader(subjvec, snpvec, 10000, fullfile(datafolder, 'EUR_10K_1M_merged/all')); 11 | config.frames.EUR_10K_1188K_merged.bim = PlinkRead_bim(fullfile(datafolder, 'EUR_10K_1M_merged/all')); 12 | end 13 | frame = make_empty_frame (config, 'EUR_10K_1188K_merged'); 14 | 15 | % Simulation - Step1. Generate additive effect sizes for 2 traits using 16 | % 3-component mixture: first components has 5e-4 causal variants, 17 | % affecting both traits, with 0.9 correlation of effect sizes. 18 | % Second component is the same but correlation is set to -0.9. 19 | frame = make_truebeta_gmm(frame, config, 'pivec', [5e-4 5e-4], 'sig1vec', [1 1], 'sig2vec', [1 1], 'rhovec', [0.9 -0.9]); 20 | % now frame.truebeta gives a matrix of size [nsnp, 2] with true (causal) effect 21 | % sizes in two simulated traits for each SNP 22 | 23 | % Calculate genetic component of the phenotypes 24 | frame = make_truepheno (frame, config, 'snpstep', 100); 25 | 26 | % Calculate actual phenotypes, setting heritability to h2=0.5 27 | frame = make_phenotype (frame, config, 'h2', 0.5); 28 | % now frame.phenotype gives a matrix of size [nsubj, 2] 29 | % with quantitative phenotype for each of the two traits 30 | 31 | % Perform GWAS using non-overlapping subjects (5K subjects for each trait) 32 | frame = make_gwas (frame, config, 'subj_idx', {1:5000, 5001:10000}); 33 | % now frame.gwaspval and frame.gwasbeta contains estimated loci effect 34 | % sizes and association p-value for SNP. 35 | 36 | save('results', 'frame') 37 | % The order of SNPs in the frame is the same as in the input .bim file. 38 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(simu) 4 | 5 | set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules) 6 | 7 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin) 8 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib) 9 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib) 10 | 11 | set(CMAKE_BUILD_TYPE "Release") 12 | message("-- Build type: ${CMAKE_BUILD_TYPE}") 13 | 14 | set(Boost_ADDITIONAL_VERSIONS "1.57" "1.57.0" "1.56" "1.56.0" "1.55" "1.55.0" "1.54" "1.54.0" "1.53" "1.53.0" "1.52" "1.52.0" "1.51" "1.51.0" "1.50" "1.50.0" "1.49" "1.49.0" "1.48" "1.48.0" "1.47" "1.47.0" "1.46" "1.46.0" "1.45" "1.45.0" "1.44" "1.44.0" "1.42" "1.42.0" "1.41.0" "1.41" "1.40.0" "1.40") 15 | 16 | # find boost 17 | find_package(Boost REQUIRED) 18 | if (NOT Boost_FOUND) 19 | message(SEND_ERROR "Failed to find boost libraries.") 20 | endif (NOT Boost_FOUND) 21 | 22 | set(Boost_USE_MULTITHREADED ON) 23 | set(Boost_USE_STATIC_RUNTIME_LIBS ON) 24 | set(Boost_USE_STATIC ON) 25 | set(Boost_USE_STATIC_LIBS ON) 26 | set(SIMU_BOOST_COMPONENTS program_options filesystem system) 27 | find_package(Boost COMPONENTS REQUIRED ${SIMU_BOOST_COMPONENTS}) 28 | if (NOT Boost_FOUND) 29 | message(SEND_ERROR "Failed to find required boost libraries.") 30 | return() 31 | else (NOT Boost_FOUND) 32 | include_directories(${Boost_INCLUDE_DIRS}) 33 | endif (NOT Boost_FOUND) 34 | 35 | include(CheckCXXCompilerFlag) 36 | CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11) 37 | CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X) 38 | if (COMPILER_SUPPORTS_CXX11) 39 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 40 | elseif (COMPILER_SUPPORTS_CXX0X) 41 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") 42 | else (COMPILER_SUPPORTS_CXX11) 43 | message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.") 44 | endif (COMPILER_SUPPORTS_CXX11) 45 | 46 | SET(DISABLE_TESTS ON CACHE BOOL "Disables building tests.") 47 | SET(DISABLE_SHARED_LIBRARY OFF CACHE BOOL "Disables building shared libraries.") 48 | 49 | add_subdirectory( libplinkio ) 50 | add_subdirectory( src ) 51 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/m4/google_namespace.m4: -------------------------------------------------------------------------------- 1 | # Allow users to override the namespace we define our application's classes in 2 | # Arg $1 is the default namespace to use if --enable-namespace isn't present. 3 | 4 | # In general, $1 should be 'google', so we put all our exported symbols in a 5 | # unique namespace that is not likely to conflict with anyone else. However, 6 | # when it makes sense -- for instance, when publishing stl-like code -- you 7 | # may want to go with a different default, like 'std'. 8 | 9 | # We guarantee the invariant that GOOGLE_NAMESPACE starts with ::, 10 | # unless it's the empty string. Thus, it's always safe to do 11 | # GOOGLE_NAMESPACE::foo and be sure you're getting the foo that's 12 | # actually in the google namespace, and not some other namespace that 13 | # the namespace rules might kick in. 14 | 15 | AC_DEFUN([AC_DEFINE_GOOGLE_NAMESPACE], 16 | [google_namespace_default=[$1] 17 | AC_ARG_ENABLE(namespace, [ --enable-namespace=FOO to define these Google 18 | classes in the FOO namespace. --disable-namespace 19 | to define them in the global namespace. Default 20 | is to define them in namespace $1.], 21 | [case "$enableval" in 22 | yes) google_namespace="$google_namespace_default" ;; 23 | no) google_namespace="" ;; 24 | *) google_namespace="$enableval" ;; 25 | esac], 26 | [google_namespace="$google_namespace_default"]) 27 | if test -n "$google_namespace"; then 28 | ac_google_namespace="::$google_namespace" 29 | ac_google_start_namespace="namespace $google_namespace {" 30 | ac_google_end_namespace="}" 31 | else 32 | ac_google_namespace="" 33 | ac_google_start_namespace="" 34 | ac_google_end_namespace="" 35 | fi 36 | AC_DEFINE_UNQUOTED(GOOGLE_NAMESPACE, $ac_google_namespace, 37 | Namespace for Google classes) 38 | AC_DEFINE_UNQUOTED(_START_GOOGLE_NAMESPACE_, $ac_google_start_namespace, 39 | Puts following code inside the Google namespace) 40 | AC_DEFINE_UNQUOTED(_END_GOOGLE_NAMESPACE_, $ac_google_end_namespace, 41 | Stops putting the code inside the Google namespace) 42 | ]) 43 | -------------------------------------------------------------------------------- /libplinkio/configure.ac: -------------------------------------------------------------------------------- 1 | # -*- Autoconf -*- 2 | # Process this file with autoconf to produce a configure script. 3 | 4 | AC_PREREQ(2.61) 5 | AC_INIT([libplinkio], [0.3.1], [mattias.franberg@googlemail.com]) 6 | AM_INIT_AUTOMAKE([foreign -Wall -Werror]) 7 | AC_CONFIG_SRCDIR([src/fam.c]) 8 | AC_CONFIG_MACRO_DIR([m4]) 9 | 10 | # Checks for programs. 11 | AC_PROG_CC 12 | AM_PROG_CC_C_O 13 | AM_CONDITIONAL(GCC, test "$GCC" = yes) 14 | 15 | # Checks for libraries. 16 | LT_INIT 17 | 18 | # Python 19 | AM_PATH_PYTHON([2.7]) 20 | AC_ARG_VAR([PYTHON_INCLUDE], [Include flags for python, bypassing python-config]) 21 | AC_ARG_VAR([PYTHON_CONFIG], [Path to python-config]) 22 | AS_IF([test -z "$PYTHON_INCLUDE"], [ 23 | AS_IF([test -z "$PYTHON_CONFIG"], [ 24 | AC_PATH_PROGS([PYTHON_CONFIG], 25 | [python$PYTHON_VERSION-config python-config], 26 | [no], 27 | [`dirname $PYTHON`]) 28 | AS_IF([test "$PYTHON_CONFIG" = no], [AC_MSG_ERROR([cannot find python-config for $PYTHON.])]) 29 | ]) 30 | AC_MSG_CHECKING([python include flags]) 31 | PYTHON_INCLUDE=`$PYTHON_CONFIG --includes` 32 | AC_MSG_RESULT([$PYTHON_INCLUDE]) 33 | ]) 34 | 35 | 36 | # Checks for header files. 37 | AC_HEADER_STDC 38 | AC_CHECK_HEADERS([stddef.h stdlib.h string.h endian.h machine/endian.h sys/endian.h]) 39 | 40 | # Checks for typedefs, structures, and compiler characteristics. 41 | AC_C_CONST 42 | AC_TYPE_SIZE_T 43 | 44 | # Checks for library functions. 45 | AC_FUNC_MALLOC 46 | AC_FUNC_REALLOC 47 | AC_CHECK_FUNCS([bzero memmove memset strdup strtol strtod]) 48 | 49 | # Optionally enable tests 50 | AC_ARG_ENABLE(tests, 51 | [AC_HELP_STRING([--enable-tests], [Build unit tests (default is yes)])], 52 | [case "${enableval}" in 53 | yes | no ) WITH_TESTS="${enableval}" ;; 54 | *) AC_MSG_ERROR(bad value ${enableval} for --enable-tests) ;; 55 | esac], 56 | [WITH_TESTS="yes"]) 57 | AM_CONDITIONAL(WITH_TESTS, test x$WITH_TESTS = xyes) 58 | 59 | if test "$WITH_TESTS" = "yes"; then 60 | AC_CONFIG_SUBDIRS([libs/cmockery]) 61 | fi 62 | 63 | AC_CONFIG_FILES([Makefile src/Makefile libs/libcsv/Makefile tests/Makefile py-plinkio/Makefile]) 64 | 65 | AC_OUTPUT 66 | 67 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/packages/rpm/rpm.spec: -------------------------------------------------------------------------------- 1 | ## This is a boilerplate file for Google opensource projects. 2 | ## To make it useful, replace <> with actual text for your project. 3 | ## Also, look at comments with "## double hashes" to see if any are worth 4 | ## uncommenting or modifying. 5 | 6 | %define RELEASE 1 7 | %define rel %{?CUSTOM_RELEASE} %{!?CUSTOM_RELEASE:%RELEASE} 8 | %define prefix /usr 9 | 10 | Name: %NAME 11 | Summary: Lightweight C unit testing framework. 12 | Version: %VERSION 13 | Release: %rel 14 | Group: Development/Libraries 15 | URL: http://code.google.com/p/cmockery 16 | License: Apache 17 | Vendor: Google 18 | Packager: Google Inc. 19 | Source: http://%{NAME}.googlecode.com/files/%{NAME}-%{VERSION}.tar.gz 20 | Distribution: Redhat 7 and above. 21 | Buildroot: %{_tmppath}/%{name}-root 22 | Prefix: %prefix 23 | 24 | %description 25 | The %name package contains a lightweight library to simplify and generalize the 26 | process of writing unit tests for C applications. 27 | 28 | %package devel 29 | Summary: Lightweight C unit testing framework. 30 | Group: Development/Libraries 31 | Requires: %{NAME} = %{VERSION} 32 | 33 | %description devel 34 | The %name package contains static and debug libraries and header files for the 35 | development of test applications using %name. 36 | 37 | %changelog 38 | * Mon Aug 25 2008 39 | - First draft 40 | 41 | %prep 42 | %setup 43 | 44 | %build 45 | ./configure 46 | make prefix=%prefix 47 | 48 | %install 49 | rm -rf $RPM_BUILD_ROOT 50 | make prefix=$RPM_BUILD_ROOT%{prefix} install 51 | 52 | %clean 53 | rm -rf $RPM_BUILD_ROOT 54 | 55 | %files 56 | %defattr(-,root,root) 57 | 58 | ## Mark all installed files within /usr/share/doc/{package name} as 59 | ## documentation. This depends on the following two lines appearing in 60 | ## Makefile.am: 61 | ## docdir = $(prefix)/share/doc/$(PACKAGE)-$(VERSION) 62 | ## dist_doc_DATA = AUTHORS COPYING ChangeLog INSTALL NEWS README 63 | %docdir %{prefix}/share/doc/%{NAME}-%{VERSION} 64 | %{prefix}/share/doc/%{NAME}-%{VERSION}/* 65 | 66 | %{prefix}/lib/libcmockery.so.0 67 | %{prefix}/lib/libcmockery.so.0.0.0 68 | 69 | 70 | %files devel 71 | %defattr(-,root,root) 72 | 73 | %{prefix}/include/google 74 | %{prefix}/lib/libcmockery.a 75 | %{prefix}/lib/libcmockery.la 76 | %{prefix}/lib/libcmockery.so 77 | -------------------------------------------------------------------------------- /matlab/generate_sym03.m: -------------------------------------------------------------------------------- 1 | %pi_index %pi_index = 1 .. 12; % to be defined by caller 2 | %pg_index %pg_index = 1 .. 6; 3 | 4 | rng('shuffle') 5 | 6 | if ~exist('config', 'var'), find_config; end; 7 | fieldnames(config.frames) 8 | 9 | hvec_opts = [0.9]; %[0.1, 0.3, 0.5, 0.7, 0.9]; 10 | pivec_opts = [1e-6, 3e-6, 1e-5, 3e-5, 1e-4, 3e-4, 1e-3, 3e-3, 1e-2, 3e-2, 1e-1, 3e-1]; 11 | gencorr_opts = [NaN, 0, 0.5, 0.5, 0.75, 1.0]; 12 | 13 | pi_index = mod(pi_index - 1, length(pivec_opts)) + 1; 14 | if pi_index <= 0 || pi_index > length(pivec_opts), error('invalid pi_index'); end; 15 | pival = pivec_opts(pi_index); 16 | 17 | pg_index = mod(pg_index - 1, length(gencorr_opts)) + 1; 18 | if pg_index <= 0 || pg_index > length(gencorr_opts), error('invalid pg_index'); end; 19 | gencorr = gencorr_opts(pg_index); 20 | 21 | if isnan(gencorr), pivec = [pival, pival]; sig1vec = [1 0]; sig2vec = [0 1]; rhovec = [0 0]; % indep 22 | else pivec = pival; sig1vec = [1]; sig2vec = [1]; rhovec = gencorr; % pleio 23 | end 24 | 25 | frame = make_empty_frame (config, 'EUR_100K_80M_chunks'); 26 | frame = make_truebeta_gmm(frame, config, 'pivec', pivec, 'sig1vec', sig1vec, 'sig2vec', sig2vec, 'rhovec', rhovec); 27 | frame = make_truepheno (frame, config, 'snpstep', 100); 28 | 29 | [~,filename,~] = fileparts(tempname); 30 | save(sprintf('truepheno_%s_pi=%.0e_gencorr=%.2f.mat', filename, pival, gencorr), 'frame', '-v7.3'); 31 | 32 | nsubj = frame.subj; 33 | s500 = floor(nsubj/2); s501 = s500+1; s1000 = nsubj; 34 | s333 = floor(nsubj/3); s334 = s333+1; s667 = 2*s333+1; 35 | no_overlap = {1:s500, s501:s1000}; % no overlap 36 | partial_overlap = {1:s667, s334:s1000}; % partial overlap 37 | full_overlap = {1:s1000, 1:s1000}; % full overlap 38 | overlap_indices = {no_overlap, partial_overlap, full_overlap}; 39 | 40 | frame0 = frame; 41 | for hindex = 1:length(hvec_opts) 42 | frame1 = reframe (frame0, config, 'EUR_100K_8801K_merged'); 43 | frame1 = make_phenotype(frame1, config, 'h2', hvec_opts(hindex)); 44 | frame = make_gwas (frame1, config, 'subj_idx', no_overlap); 45 | frame = make_gwaslogp (frame, config); 46 | frame = reframe (frame, config, 'EUR_100K_9279K_ref'); 47 | frame.opts.now = datestr(now); 48 | save(sprintf('gwaslogp_%s_pi=%.0e_gencorr=%.2f_h2=%.2f.mat', filename, pival, gencorr, hvec_opts(hindex)), 'frame', '-v7.3'); 49 | end 50 | 51 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/configure.ac: -------------------------------------------------------------------------------- 1 | ## This is a boilerplate file for Google opensource projects. 2 | ## To make it useful, replace <> with actual text for your project. 3 | ## Also, look at comments with "## double hashes" to see if any are worth 4 | ## uncommenting or modifying. 5 | 6 | ## Process this file with autoconf to produce configure. 7 | ## In general, the safest way to proceed is to run ./autogen.sh 8 | 9 | # make sure we're interpreted by some minimal autoconf 10 | AC_PREREQ(2.57) 11 | 12 | AC_INIT(cmockery, 0.1.2, opensource@google.com) 13 | # The argument here is just something that should be in the current directory 14 | # (for sanity checking) 15 | AC_CONFIG_SRCDIR(README) 16 | AM_INIT_AUTOMAKE 17 | AM_CONFIG_HEADER(src/config.h) 18 | AC_CONFIG_MACRO_DIR([m4]) 19 | 20 | # Checks for programs. 21 | AC_PROG_CC 22 | AC_PROG_CPP 23 | AC_PROG_CXX 24 | AM_PROG_CC_C_O 25 | AM_CONDITIONAL(GCC, test "$GCC" = yes) # let the Makefile know if we're gcc 26 | 27 | # Uncomment this if you'll be exporting libraries (.so's) 28 | AC_PROG_LIBTOOL 29 | AC_SUBST(LIBTOOL_DEPS) 30 | 31 | # Check whether some low-level functions/files are available 32 | AC_HEADER_STDC 33 | 34 | # Here are some examples of how to check for the existence of a fn or file 35 | ##AC_CHECK_FUNCS(memmove) 36 | ##AC_CHECK_HEADERS(sys/resource.h) 37 | AC_CHECK_FUNCS(setjmp) 38 | AC_CHECK_FUNCS(longjmp) 39 | AC_CHECK_FUNCS(strcmp) 40 | AC_CHECK_FUNCS(strcpy) 41 | AC_CHECK_FUNCS(memcpy) 42 | AC_CHECK_FUNCS(memset) 43 | AC_CHECK_FUNCS(malloc) 44 | AC_CHECK_FUNCS(calloc) 45 | AC_CHECK_FUNCS(free) 46 | AC_CHECK_FUNCS(exit) 47 | AC_CHECK_FUNCS(signal) 48 | AC_CHECK_FUNCS(printf) 49 | AC_CHECK_FUNCS(fprintf) 50 | AC_CHECK_FUNCS(snprintf) 51 | AC_CHECK_FUNCS(vsnprintf) 52 | AC_CHECK_HEADERS(assert.h) 53 | AC_CHECK_HEADERS(malloc.h) 54 | AC_CHECK_HEADERS(setjmp.h) 55 | AC_CHECK_HEADERS(stdarg.h) 56 | AC_CHECK_HEADERS(stddef.h) 57 | AC_CHECK_HEADERS(stdio.h) 58 | AC_CHECK_HEADERS(stdlib.h) 59 | AC_CHECK_HEADERS(string.h) 60 | AC_CHECK_HEADERS(signal.h) 61 | 62 | # Find out what namespace 'normal' STL code lives in, and also what namespace 63 | # the user wants our classes to be defined in 64 | AC_CXX_STL_NAMESPACE 65 | AC_DEFINE_GOOGLE_NAMESPACE(google) 66 | 67 | ## Check out ../autoconf/ for other macros you can call to do useful stuff 68 | 69 | # Write generated configuration file 70 | AC_CONFIG_FILES([Makefile]) 71 | AC_OUTPUT 72 | -------------------------------------------------------------------------------- /libplinkio/COPYING: -------------------------------------------------------------------------------- 1 | The libplinkio software library is distributed under the following terms: 2 | 3 | Copyright (c) 2012-2013, Matias Frånberg 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright 13 | notice, this list of conditions and the following disclaimer in the 14 | documentation and/or other materials provided with the distribution. 15 | 16 | * Neither the name of Mattias Frånberg nor the 17 | names of its contributors may be used to endorse or promote products 18 | derived from this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY 24 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | =============================================================================== 32 | 33 | The libplinkio software contains code written by third parties. Such software 34 | will either have its own individual LICENSE/COPYING file in the directory in 35 | which it appears, or have the license specified in the source files. This file 36 | will describe the copyrights, license, and restrictions which apply to that code. 37 | 38 | The following pieces of software have additional or alternate copyrights, 39 | licenses, and/or restrictions: 40 | 41 | Directory/File License 42 | libs/csv GNU Lesser General Public 43 | libs/cmockery Apache License, Version 2.0 44 | src/plinkio/utarray.h Revised BSD License 45 | 46 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/packages/rpm.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | 3 | # Run this from the 'packages' directory, just under rootdir 4 | 5 | # We can only build rpm packages, if the rpm build tools are installed 6 | if [ \! -x /usr/bin/rpmbuild ] 7 | then 8 | echo "Cannot find /usr/bin/rpmbuild. Not building an rpm." 1>&2 9 | exit 0 10 | fi 11 | 12 | # Check the commandline flags 13 | PACKAGE="$1" 14 | VERSION="$2" 15 | fullname="${PACKAGE}-${VERSION}" 16 | archive=../$fullname.tar.gz 17 | 18 | if [ -z "$1" -o -z "$2" ] 19 | then 20 | echo "Usage: $0 " 1>&2 21 | exit 0 22 | fi 23 | 24 | # Double-check we're in the packages directory, just under rootdir 25 | if [ \! -r ../Makefile -a \! -r ../INSTALL ] 26 | then 27 | echo "Must run $0 in the 'packages' directory, under the root directory." 1>&2 28 | echo "Also, you must run \"make dist\" before running this script." 1>&2 29 | exit 0 30 | fi 31 | 32 | if [ \! -r "$archive" ] 33 | then 34 | echo "Cannot find $archive. Run \"make dist\" first." 1>&2 35 | exit 0 36 | fi 37 | 38 | # Create the directory where the input lives, and where the output should live 39 | RPM_SOURCE_DIR="/tmp/rpmsource-$fullname" 40 | RPM_BUILD_DIR="/tmp/rpmbuild-$fullname" 41 | 42 | trap 'rm -rf $RPM_SOURCE_DIR $RPM_BUILD_DIR; exit $?' EXIT SIGHUP SIGINT SIGTERM 43 | 44 | rm -rf "$RPM_SOURCE_DIR" "$RPM_BUILD_DIR" 45 | mkdir "$RPM_SOURCE_DIR" 46 | mkdir "$RPM_BUILD_DIR" 47 | 48 | cp "$archive" "$RPM_SOURCE_DIR" 49 | 50 | rpmbuild -bb rpm/rpm.spec \ 51 | --define "NAME $PACKAGE" \ 52 | --define "VERSION $VERSION" \ 53 | --define "_sourcedir $RPM_SOURCE_DIR" \ 54 | --define "_builddir $RPM_BUILD_DIR" \ 55 | --define "_rpmdir $RPM_SOURCE_DIR" 56 | 57 | # We put the output in a directory based on what system we've built for 58 | destdir=rpm-unknown 59 | if [ -r /etc/issue ] 60 | then 61 | grep "Red Hat.*release 7" /etc/issue >/dev/null 2>&1 && destdir=rh7 62 | grep "Red Hat.*release 8" /etc/issue >/dev/null 2>&1 && destdir=rh8 63 | grep "Red Hat.*release 9" /etc/issue >/dev/null 2>&1 && destdir=rh9 64 | if grep Fedora /etc/issue >/dev/null; then 65 | destdir=fc`grep Fedora /etc/issue | cut -d' ' -f 4`; 66 | fi 67 | fi 68 | 69 | rm -rf "$destdir" 70 | mkdir -p "$destdir" 71 | # We want to get not only the main package but devel etc, hence the middle * 72 | mv "$RPM_SOURCE_DIR"/*/"${PACKAGE}"-*"${VERSION}"*.rpm "$destdir" 73 | 74 | echo 75 | echo "The rpm package file(s) are located in $PWD/$destdir" 76 | -------------------------------------------------------------------------------- /libplinkio/tests/data/wgas.fam: -------------------------------------------------------------------------------- 1 | CH18526 NA18526 0 0 2 1 2 | CH18524 NA18524 0 0 1 1 3 | CH18529 NA18529 0 0 2 1 4 | CH18558 NA18558 0 0 1 1 5 | CH18532 NA18532 0 0 2 1 6 | CH18561 NA18561 0 0 1 1 7 | CH18562 NA18562 0 0 1 1 8 | CH18537 NA18537 0 0 2 2 9 | CH18603 NA18603 0 0 1 2 10 | CH18540 NA18540 0 0 2 1 11 | CH18605 NA18605 0 0 1 1 12 | CH18542 NA18542 0 0 2 1 13 | CH18545 NA18545 0 0 2 1 14 | CH18572 NA18572 0 0 1 2 15 | CH18547 NA18547 0 0 2 2 16 | CH18609 NA18609 0 0 1 1 17 | CH18550 NA18550 0 0 2 1 18 | CH18608 NA18608 0 0 1 1 19 | CH18552 NA18552 0 0 2 1 20 | CH18611 NA18611 0 0 1 1 21 | CH18555 NA18555 0 0 2 1 22 | CH18564 NA18564 0 0 2 2 23 | CH18566 NA18566 0 0 2 1 24 | CH18563 NA18563 0 0 1 1 25 | CH18570 NA18570 0 0 2 1 26 | CH18612 NA18612 0 0 1 2 27 | CH18571 NA18571 0 0 2 1 28 | CH18620 NA18620 0 0 1 1 29 | CH18621 NA18621 0 0 1 1 30 | CH18594 NA18594 0 0 2 1 31 | CH18622 NA18622 0 0 1 2 32 | CH18573 NA18573 0 0 2 2 33 | CH18623 NA18623 0 0 1 1 34 | CH18576 NA18576 0 0 2 1 35 | CH18577 NA18577 0 0 2 1 36 | CH18624 NA18624 0 0 1 1 37 | CH18579 NA18579 0 0 2 1 38 | CH18632 NA18632 0 0 1 2 39 | CH18582 NA18582 0 0 2 1 40 | CH18633 NA18633 0 0 1 1 41 | CH18635 NA18635 0 0 1 2 42 | CH18592 NA18592 0 0 2 1 43 | CH18636 NA18636 0 0 1 1 44 | CH18593 NA18593 0 0 2 2 45 | CH18637 NA18637 0 0 1 1 46 | JA18942 NA18942 0 0 2 2 47 | JA18940 NA18940 0 0 1 2 48 | JA18951 NA18951 0 0 2 2 49 | JA18943 NA18943 0 0 1 2 50 | JA18947 NA18947 0 0 2 2 51 | JA18944 NA18944 0 0 1 2 52 | JA18945 NA18945 0 0 1 2 53 | JA18949 NA18949 0 0 2 2 54 | JA18948 NA18948 0 0 1 2 55 | JA18952 NA18952 0 0 1 2 56 | JA18956 NA18956 0 0 2 2 57 | JA18964 NA18964 0 0 2 2 58 | JA18953 NA18953 0 0 1 1 59 | JA18968 NA18968 0 0 2 2 60 | JA18959 NA18959 0 0 1 2 61 | JA18969 NA18969 0 0 2 1 62 | JA18960 NA18960 0 0 1 2 63 | JA18961 NA18961 0 0 1 2 64 | JA18972 NA18972 0 0 2 2 65 | JA18965 NA18965 0 0 1 2 66 | JA18973 NA18973 0 0 2 2 67 | JA18966 NA18966 0 0 1 2 68 | JA18975 NA18975 0 0 2 2 69 | JA18967 NA18967 0 0 1 2 70 | JA18976 NA18976 0 0 2 1 71 | JA18978 NA18978 0 0 2 2 72 | JA18970 NA18970 0 0 1 1 73 | JA18980 NA18980 0 0 2 2 74 | JA18995 NA18995 0 0 1 1 75 | JA18981 NA18981 0 0 2 2 76 | JA18971 NA18971 0 0 1 2 77 | JA18974 NA18974 0 0 1 1 78 | JA18987 NA18987 0 0 2 2 79 | JA18990 NA18990 0 0 1 1 80 | JA18991 NA18991 0 0 2 2 81 | JA18994 NA18994 0 0 1 2 82 | JA18992 NA18992 0 0 2 2 83 | JA18997 NA18997 0 0 2 2 84 | JA18998 NA18998 0 0 2 2 85 | JA19000 NA19000 0 0 1 2 86 | JA19005 NA19005 0 0 1 2 87 | JA18999 NA18999 0 0 2 2 88 | JA19007 NA19007 0 0 1 2 89 | JA19003 NA19003 0 0 2 2 90 | JA19012 NA19012 0 0 1 2 91 | -------------------------------------------------------------------------------- /libplinkio/tests/plinkio_test.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from plinkio import plinkfile 4 | 5 | ## 6 | # Tests that opening, reading and closing 7 | # of plink files works from python. 8 | # 9 | class TestPlinkIo(unittest.TestCase): 10 | ## 11 | # Test open. 12 | # 13 | def test_open(self): 14 | pf = plinkfile.open( "./data/wgas" ) 15 | self.assertNotEqual( pf, None ); 16 | 17 | ## 18 | # Make sure that the path is correct. 19 | # 20 | def test_get_path(self): 21 | path = "./data/wgas" 22 | pf = plinkfile.open( path ) 23 | self.assertEqual( path, pf.get_path( ) ) 24 | 25 | ## 26 | # Make sure that the number of parsed samples 27 | # is correct. 28 | # 29 | def test_get_samples(self): 30 | pf = plinkfile.open( "./data/wgas" ) 31 | self.assertEqual( len( pf.get_samples( ) ), 90 ); 32 | 33 | ## 34 | # Make sure that the number parsed loci is 35 | # correct. 36 | # 37 | def test_get_loci(self): 38 | pf = plinkfile.open( "./data/wgas" ) 39 | self.assertEqual( len( pf.get_loci( ) ), 228694 ); 40 | 41 | ## 42 | # Make sure open throws IOError when 43 | # an invalid path is specified. 44 | # 45 | def test_fail_open(self): 46 | with self.assertRaises( IOError ): 47 | plinkfile.open( "/" ) 48 | 49 | ## 50 | # Test iteration. 51 | # 52 | def test_iter(self): 53 | pf = plinkfile.open( "./data/wgas" ) 54 | 55 | num_rows = 0 56 | for row in pf: 57 | num_rows += 1 58 | 59 | self.assertEqual( num_rows, 228694 ) 60 | 61 | ## 62 | # Tests the snp array. 63 | # 64 | class TestSnpArray(unittest.TestCase): 65 | ## 66 | # Common setup. 67 | # 68 | def setUp(self): 69 | pf = plinkfile.open( "./data/wgas" ) 70 | self.row = next( pf ) 71 | pf.close( ) 72 | 73 | ## 74 | # Test access operator. 75 | # 76 | def test_access(self): 77 | self.assertEqual( self.row[ 0 ], 2 ) 78 | self.assertEqual( self.row[ 2 ], 1 ) 79 | 80 | ## 81 | # Test contains. 82 | # 83 | def test_contains(self): 84 | self.assertFalse( 0 in self.row ) 85 | self.assertTrue( 1 in self.row ) 86 | self.assertTrue( 2 in self.row ) 87 | self.assertTrue( 3 in self.row ) 88 | 89 | ## 90 | # Test allele count on first row. 91 | # 92 | def test_count(self): 93 | self.assertEqual( self.row.allele_counts( ), [ 0, 22, 67, 1 ] ) 94 | 95 | if __name__ == '__main__': 96 | unittest.main( ) 97 | -------------------------------------------------------------------------------- /matlab/reframe.m: -------------------------------------------------------------------------------- 1 | function frame = reframe(frame, config, framename) 2 | % REFRAME changes frame and align matrices to the new set of SNPs. 3 | 4 | fprintf('reframe %s to %s...\n', frame.name, framename); 5 | oldcfg = config.frames.(frame.name); 6 | newcfg = config.frames.(framename); 7 | fn = fieldnames(frame); 8 | 9 | % Use information from bim files to rearrange SNPs 10 | if isfield(oldcfg, 'bim') && isfield(newcfg, 'bim') && (oldcfg.subj == newcfg.subj) 11 | % Changing the set of subjects is not allowed 12 | if length(oldcfg.bim.snplist) ~= frame.snps, error('Number of SNPs mismatch (in the old frame)'); end; 13 | if length(newcfg.bim.snplist) ~= newcfg.snps, error('Number of SNPs mismatch (in the new frame)'); end; 14 | [is_in_new_frame, index_to_new_frame] = ismember(oldcfg.bim.snplist, newcfg.bim.snplist); 15 | if ~all(is_in_new_frame), warning('Unable to map %i SNPs to the new frame; %i SNPs mapped successfully', sum(~is_in_new_frame), sum(is_in_new_frame)); end; 16 | for i=1:length(fn) 17 | if size(frame.(fn{i}), 1) ~= frame.snps, continue; end; 18 | frame.(fn{i}) = align_to_ref(frame.(fn{i}), length(newcfg.bim.snplist), index_to_new_frame); 19 | end 20 | end 21 | 22 | % Taking subset of subjects 23 | if (oldcfg.subj >= newcfg.subj) 24 | for i=1:length(fn) 25 | if size(frame.(fn{i}), 1) ~= frame.subj, continue; end; 26 | matrix = frame.(fn{i}); 27 | matrix = matrix(1:newcfg.subj, :); 28 | frame.(fn{i}) = matrix; 29 | end 30 | end 31 | 32 | % Removing fields that will not match new frame 33 | to_be_removed = {}; 34 | for i=1:length(fn) 35 | if size(frame.(fn{i}), 1) == frame.subj && oldcfg.subj ~= newcfg.subj, frame.legacy.(fn{i}) = frame.(fn{i}); frame = rmfield(frame, fn{i}); to_be_removed{end+1} = fn{i}; end; 36 | if size(frame.(fn{i}), 1) == frame.snps && oldcfg.snps ~= newcfg.snps, frame.legacy.(fn{i}) = frame.(fn{i}); frame = rmfield(frame, fn{i}); to_be_removed{end+1} = fn{i}; end; 37 | end 38 | if ~isempty(to_be_removed), warning('fields %s are moved to legacy\n', sprintf('%s ', to_be_removed{:})); end; 39 | 40 | frame.snps = newcfg.snps; 41 | frame.subj = newcfg.subj; 42 | frame.name = framename; 43 | frame.opts.reframe_time = datestr(now); 44 | end 45 | 46 | function aligned_vec = align_to_ref(vec, nsnp_ref, index_to_ref) 47 | numtraits = size(vec, 2); 48 | aligned_vec = nan(nsnp_ref, numtraits); 49 | aligned_vec(index_to_ref(index_to_ref ~= 0), :) = vec(index_to_ref ~= 0, :); 50 | end 51 | -------------------------------------------------------------------------------- /matlab/make_truebeta_gmm.m: -------------------------------------------------------------------------------- 1 | function frame = make_truebeta_gmm(frame, config, varargin) 2 | % MAKE_TRUEBETA_GMM generates two vectors of true effect sizes from gaussian mixture model 3 | % Example: 4 | % frame.snps = 1000; make_truebeta_gmm(frame, 'pivec', [0.01], 'sig1vec', [1.0], 'sig2vec', [1.0], 'rhovec', [0.5]) 5 | 6 | p = inputParser; 7 | addOptional(p, 'pivec', []); % proportion of snps in each component (except null) 8 | addOptional(p, 'sig1vec', []); % variance (first component) 9 | addOptional(p, 'sig2vec', []); % variance (second component) 10 | addOptional(p, 'rhovec', []); % correlation (between two components) 11 | addOptional(p, 'mask', []); % restrict heritability to certain SNPs 12 | % mask can be either logical array of size "snps x 1" 13 | % or an array of indices between 1 and snps. 14 | parse(p, varargin{:}); opts = p.Results; 15 | 16 | mask = opts.mask; 17 | pivec = opts.pivec; 18 | sig1vec = opts.sig1vec; 19 | sig2vec = opts.sig2vec; 20 | rhovec = opts.rhovec; 21 | 22 | nsnp = frame.snps; 23 | if isempty(mask), mask = 1:nsnp; end; 24 | if islogical(mask), mask = find(mask); end; 25 | 26 | if nsnp <= 0, error('nsnp must be positive'); end; 27 | if sum(floor(pivec * nsnp)) > nsnp, error('sum of pivec must be less than 1'); end; 28 | if sum(floor(pivec * nsnp)) > length(mask), error('sum of pivec is to large for given mask'); end; 29 | if length(unique([length(sig1vec), length(sig2vec), length(rhovec), length(pivec)])) > 1, 30 | error('pivec, sig1vec, sig2vec, rhovec must have the same length'); 31 | end; 32 | 33 | betamat = zeros(nsnp, 2); 34 | mix = zeros(nsnp, 1); 35 | num_mixtures = length(pivec); 36 | 37 | idx = mask(randperm(length(mask))); idx_start = 1; 38 | for i=1:num_mixtures 39 | mu = [0 0]; 40 | sigma = [sig1vec(i), sqrt(sig1vec(i) * sig2vec(i)) * rhovec(i); 41 | sqrt(sig1vec(i) * sig2vec(i)) * rhovec(i), sig2vec(i)]; 42 | num_causals = floor(nsnp * pivec(i)); 43 | %idx = randsample(1:nsnp, num_causals); betamat(idx, :) = betamat(idx, :) + mvnrnd(mu, sigma, num_causals); 44 | 45 | idx_end = idx_start + num_causals - 1; 46 | betamat(idx(idx_start:idx_end), :) = mvnrnd(mu, sigma, num_causals); 47 | mix(idx(idx_start:idx_end), :) = i; 48 | idx_start = idx_start + num_causals; 49 | end 50 | 51 | frame.opts.make_truebeta = opts; 52 | frame.truebeta = betamat; 53 | frame.mixture = repmat(mix, [1 2]); 54 | end 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/packages/deb.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | # This takes one commandline argument, the name of the package. If no 4 | # name is given, then we'll end up just using the name associated with 5 | # an arbitrary .tar.gz file in the rootdir. That's fine: there's probably 6 | # only one. 7 | # 8 | # Run this from the 'packages' directory, just under rootdir 9 | 10 | ## Set LIB to lib if exporting a library, empty-string else 11 | LIB= 12 | #LIB=lib 13 | 14 | PACKAGE="$1" 15 | 16 | # We can only build Debian packages, if the Debian build tools are installed 17 | if [ \! -x /usr/bin/debuild ]; then 18 | echo "Cannot find /usr/bin/debuild. Not building Debian packages." 1>&2 19 | exit 0 20 | fi 21 | 22 | # Double-check we're in the packages directory, just under rootdir 23 | if [ \! -r ../Makefile -a \! -r ../INSTALL ]; then 24 | echo "Must run $0 in the 'packages' directory, under the root directory." 1>&2 25 | echo "Also, you must run \"make dist\" before running this script." 1>&2 26 | exit 0 27 | fi 28 | 29 | # Find the top directory for this package 30 | topdir="${PWD%/*}" 31 | 32 | # Find the tar archive built by "make dist" 33 | archive="$(basename "$(ls -1 ${topdir}/$PACKAGE*.tar.gz | tail -n 1)" .tar.gz)" 34 | if [ -z "${archive}" ]; then 35 | echo "Cannot find ../$PACKAGE*.tar.gz. Run \"make dist\" first." 1>&2 36 | exit 0 37 | fi 38 | 39 | # Create a pristine directory for building the Debian package files 40 | trap 'rm -rf '`pwd`/tmp'; exit $?' EXIT SIGHUP SIGINT SIGTERM 41 | 42 | rm -rf tmp 43 | mkdir -p tmp 44 | cd tmp 45 | 46 | # Debian has very specific requirements about the naming of build 47 | # directories, and tar archives. It also wants to write all generated 48 | # packages to the parent of the source directory. We accommodate these 49 | # requirements by building directly from the tar file. 50 | ln -s "${topdir}/${archive}.tar.gz" "${LIB}${archive}.orig.tar.gz" 51 | tar zfx "${LIB}${archive}.orig.tar.gz" 52 | [ -n "${LIB}" ] && mv "${archive}" "${LIB}${archive}" 53 | cd "${LIB}${archive}" 54 | # This is one of those 'specific requirements': where the deb control files live 55 | ln -s "packages/deb" "debian" 56 | 57 | # Now, we can call Debian's standard build tool 58 | debuild -uc -us 59 | cd ../.. # get back to the original top-level dir 60 | 61 | # We'll put the result in a subdirectory that's named after the OS version 62 | # we've made this .deb file for. 63 | destdir="debian-$(cat /etc/debian_version 2>/dev/null || echo UNKNOWN)" 64 | 65 | rm -rf "$destdir" 66 | mkdir -p "$destdir" 67 | mv $(find tmp -mindepth 1 -maxdepth 1 -type f) "$destdir" 68 | 69 | echo 70 | echo "The Debian package files are located in $PWD/$destdir" 71 | -------------------------------------------------------------------------------- /libplinkio/tests/bim_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | #include 12 | #include 13 | #include 14 | #include "mock.h" 15 | 16 | /** 17 | * Tests the parsing of a position. 18 | */ 19 | void 20 | test_parse_position(void **state) 21 | { 22 | const char *TEST_STRING1 = "123456"; 23 | const char *TEST_STRING2 = "-1"; 24 | pio_status_t status; 25 | 26 | assert_int_equal( parse_bp_position( TEST_STRING1, strlen( TEST_STRING1 ), &status ), 123456LL ); 27 | assert_int_equal( status, PIO_OK ); 28 | 29 | assert_int_equal( parse_bp_position( TEST_STRING2, strlen( TEST_STRING2 ), &status ), -1LL ); 30 | assert_int_equal( status, PIO_OK ); 31 | } 32 | 33 | /** 34 | * Tests the parsing of a position. 35 | */ 36 | void 37 | test_parse_chr(void **state) 38 | { 39 | const char *TEST_STRING = "16"; 40 | pio_status_t status; 41 | 42 | assert_int_equal( parse_chr( TEST_STRING, strlen( TEST_STRING ), &status ), 16 ); 43 | assert_int_equal( status, PIO_OK ); 44 | } 45 | 46 | /** 47 | * Tests the parsing of multiple loci. Since parse_loci uses 48 | * IO functions, we need to have mocked versions for these. 49 | */ 50 | void 51 | test_parse_multiple_loci(void **state) 52 | { 53 | struct pio_locus_t locus; 54 | struct pio_bim_file_t bim_file; 55 | 56 | mock_init( "1 rs1 0 1234567 A C\n1 rs2 0.23 7654321 - ACCG" ); 57 | assert_int_equal( bim_open( &bim_file, "" ), PIO_OK ); 58 | assert_int_equal( bim_num_loci( &bim_file ), 2 ); 59 | 60 | locus = *bim_get_locus( &bim_file, 0 ); 61 | assert_int_equal( locus.chromosome, 1 ); 62 | assert_string_equal( locus.name, "rs1" ); 63 | assert_true( fabs( locus.position - 0.0 ) <= 1e-6 ); 64 | assert_int_equal( locus.bp_position, 1234567 ); 65 | assert_string_equal( locus.allele1, "A" ); 66 | assert_string_equal( locus.allele2, "C" ); 67 | 68 | locus = *bim_get_locus( &bim_file, 1 ); 69 | assert_int_equal( locus.chromosome, 1 ); 70 | assert_string_equal( locus.name, "rs2" ); 71 | assert_true( fabs( locus.position - 0.23 ) <= 1e-6 ); 72 | assert_int_equal( locus.bp_position, 7654321 ); 73 | assert_string_equal( locus.allele1, "-" ); 74 | assert_string_equal( locus.allele2, "ACCG" ); 75 | 76 | bim_close( &bim_file ); 77 | } 78 | 79 | int main(int argc, char* argv[]) 80 | { 81 | const UnitTest tests[] = { 82 | unit_test( test_parse_position ), 83 | unit_test( test_parse_chr ), 84 | unit_test( test_parse_multiple_loci ), 85 | }; 86 | 87 | return run_tests( tests ); 88 | } 89 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/src/example/customer_database_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | extern DatabaseConnection* connect_to_customer_database(); 23 | extern unsigned int get_customer_id_by_name( 24 | DatabaseConnection * const connection, const char * const customer_name); 25 | 26 | // Mock query database function. 27 | unsigned int mock_query_database( 28 | DatabaseConnection* const connection, const char * const query_string, 29 | void *** const results) { 30 | *results = (void**)mock(); 31 | return (unsigned int)mock(); 32 | } 33 | 34 | // Mock of the connect to database function. 35 | DatabaseConnection* connect_to_database(const char * const database_url, 36 | const unsigned int port) { 37 | return (DatabaseConnection*)mock(); 38 | } 39 | 40 | void test_connect_to_customer_database(void **state) { 41 | will_return(connect_to_database, 0x0DA7ABA53); 42 | assert_int_equal((int)connect_to_customer_database(), 0x0DA7ABA53); 43 | } 44 | 45 | /* This test fails as the mock function connect_to_database() will have no 46 | * value to return. */ 47 | void fail_connect_to_customer_database(void **state) { 48 | assert_true(connect_to_customer_database() == 49 | (DatabaseConnection*)0x0DA7ABA53); 50 | } 51 | 52 | void test_get_customer_id_by_name(void **state) { 53 | DatabaseConnection connection = { 54 | "somedatabase.somewhere.com", 12345678, mock_query_database 55 | }; 56 | // Return a single customer ID when mock_query_database() is called. 57 | int customer_ids = 543; 58 | will_return(mock_query_database, &customer_ids); 59 | will_return(mock_query_database, 1); 60 | assert_int_equal(get_customer_id_by_name(&connection, "john doe"), 543); 61 | } 62 | 63 | int main(int argc, char* argv[]) { 64 | const UnitTest tests[] = { 65 | unit_test(test_connect_to_customer_database), 66 | unit_test(test_get_customer_id_by_name), 67 | }; 68 | return run_tests(tests); 69 | } 70 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/src/example/product_database_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | extern DatabaseConnection* connect_to_product_database(); 23 | 24 | /* Mock connect to database function. 25 | * NOTE: This mock function is very general could be shared between tests 26 | * that use the imaginary database.h module. */ 27 | DatabaseConnection* connect_to_database(const char * const url, 28 | const unsigned int port) { 29 | check_expected(url); 30 | check_expected(port); 31 | return (DatabaseConnection*)mock(); 32 | } 33 | 34 | void test_connect_to_product_database(void **state) { 35 | expect_string(connect_to_database, url, "products.abcd.org"); 36 | expect_value(connect_to_database, port, 322); 37 | will_return(connect_to_database, 0xDA7ABA53); 38 | assert_int_equal((int)connect_to_product_database(), 0xDA7ABA53); 39 | } 40 | 41 | /* This test will fail since the expected URL is different to the URL that is 42 | * passed to connect_to_database() by connect_to_product_database(). */ 43 | void test_connect_to_product_database_bad_url(void **state) { 44 | expect_string(connect_to_database, url, "products.abcd.com"); 45 | expect_value(connect_to_database, port, 322); 46 | will_return(connect_to_database, 0xDA7ABA53); 47 | assert_int_equal((int)connect_to_product_database(), 0xDA7ABA53); 48 | } 49 | 50 | /* This test will fail since the mock connect_to_database() will attempt to 51 | * retrieve a value for the parameter port which isn't specified by this 52 | * test function. */ 53 | void test_connect_to_product_database_missing_parameter(void **state) { 54 | expect_string(connect_to_database, url, "products.abcd.org"); 55 | will_return(connect_to_database, 0xDA7ABA53); 56 | assert_int_equal((int)connect_to_product_database(), 0xDA7ABA53); 57 | } 58 | 59 | int main(int argc, char* argv[]) { 60 | const UnitTest tests[] = { 61 | unit_test(test_connect_to_product_database), 62 | unit_test(test_connect_to_product_database_bad_url), 63 | unit_test(test_connect_to_product_database_missing_parameter), 64 | }; 65 | return run_tests(tests); 66 | } 67 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/src/example/key_value_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | /* This is duplicated here from the module setup_teardown.c to reduce the 23 | * number of files used in this test. */ 24 | typedef struct KeyValue { 25 | unsigned int key; 26 | const char* value; 27 | } KeyValue; 28 | 29 | void set_key_values(KeyValue * const new_key_values, 30 | const unsigned int new_number_of_key_values); 31 | extern KeyValue* find_item_by_value(const char * const value); 32 | extern void sort_items_by_key(); 33 | 34 | static KeyValue key_values[] = { 35 | { 10, "this" }, 36 | { 52, "test" }, 37 | { 20, "a" }, 38 | { 13, "is" }, 39 | }; 40 | 41 | void create_key_values(void **state) { 42 | KeyValue * const items = (KeyValue*)test_malloc(sizeof(key_values)); 43 | memcpy(items, key_values, sizeof(key_values)); 44 | *state = (void*)items; 45 | set_key_values(items, sizeof(key_values) / sizeof(key_values[0])); 46 | } 47 | 48 | void destroy_key_values(void **state) { 49 | test_free(*state); 50 | set_key_values(NULL, 0); 51 | } 52 | 53 | void test_find_item_by_value(void **state) { 54 | unsigned int i; 55 | for (i = 0; i < sizeof(key_values) / sizeof(key_values[0]); i++) { 56 | KeyValue * const found = find_item_by_value(key_values[i].value); 57 | assert_true(found); 58 | assert_int_equal(found->key, key_values[i].key); 59 | assert_string_equal(found->value, key_values[i].value); 60 | } 61 | } 62 | 63 | void test_sort_items_by_key(void **state) { 64 | unsigned int i; 65 | KeyValue * const kv = *state; 66 | sort_items_by_key(); 67 | for (i = 1; i < sizeof(key_values) / sizeof(key_values[0]); i++) { 68 | assert_true(kv[i - 1].key < kv[i].key); 69 | } 70 | } 71 | 72 | int main(int argc, char* argv[]) { 73 | const UnitTest tests[] = { 74 | unit_test_setup_teardown(test_find_item_by_value, create_key_values, 75 | destroy_key_values), 76 | unit_test_setup_teardown(test_sort_items_by_key, create_key_values, 77 | destroy_key_values), 78 | }; 79 | return run_tests(tests); 80 | } 81 | -------------------------------------------------------------------------------- /matlab/make_gwas.m: -------------------------------------------------------------------------------- 1 | function frame = make_gwas(frame, config, varargin) 2 | % MAKE_GWAS generates gwas estimates of effect size and corresponding p-value 3 | 4 | p = inputParser; 5 | addOptional(p, 'snpstep', 100); % how many SNPs to scan per one iteration 6 | addOptional(p, 'subj_idx', {}); % which subjects to involve in GWAS 7 | % subj_idx can be 8 | % - empty 9 | % - an array with subjects to include in gwas (will be used for all traits) 10 | % - an cell array of length ntraits; each element is the list to use for each trait 11 | 12 | parse(p, varargin{:}); opts = p.Results; 13 | 14 | if frame.subj ~= config.frames.(frame.name).subj, error('Number of subjects mismatch'); end; 15 | if frame.snps ~= config.frames.(frame.name).snps, error('Number of SNPs mismatch'); end; 16 | 17 | nsnp = frame.snps; 18 | nsubj = frame.subj; 19 | snpstep = opts.snpstep; 20 | ntraits = size(frame.phenotype, 2); 21 | reader = config.frames.(frame.name).reader; 22 | 23 | opts = fix_and_validate(frame, config, opts); 24 | 25 | gwasbeta = zeros(nsnp, ntraits); 26 | gwaspval = zeros(nsnp, ntraits); 27 | beginningOfTime = now; 28 | for i=1:snpstep:nsnp 29 | e = min(i+snpstep, nsnp); 30 | X = reader(1:nsubj, i:e); X = double(X); X = (X-repmat(mean(X,1),nsubj,1)); 31 | % X = X ./ repmat(std(X), [nsubj, 1]); ???????? do we need somehting like this or not? 32 | for trait=1:ntraits 33 | observedPhenotype = frame.phenotype(opts.subj_idx{trait}, trait); 34 | [rhovec_tmp, pvec_tmp] = corr(X(opts.subj_idx{trait}, :), double(observedPhenotype)); 35 | if all(isnan(rhovec_tmp)) || all(isnan(pvec_tmp)), error('corr return nan\n'); end; 36 | gwasbeta(i:e, trait) = rhovec_tmp; 37 | gwaspval(i:e, trait) = pvec_tmp; 38 | end 39 | fprintf(1,'make_gwas: %.1f%% done, Now:%s eta:%s\n', 100 * (i+snpstep) / nsnp, datestr(now,'dddd HH:MM:SS'),datestr(beginningOfTime+(now-beginningOfTime)/(i+snpstep)*nsnp)); 40 | end 41 | 42 | frame.gwasbeta = gwasbeta; 43 | frame.gwaspval = gwaspval; 44 | frame.nvec = zeros(size(gwaspval)); for trait=1:ntraits, frame.gwassize(:, trait) = ones(frame.snps, 1) * length(opts.subj_idx{trait}); end 45 | frame.opts.make_gwas = opts; 46 | end 47 | 48 | 49 | 50 | function opts = fix_and_validate(frame, config, opts) 51 | nsubj = frame.subj; 52 | ntraits = size(frame.phenotype, 2); 53 | 54 | if isempty(opts.subj_idx), 55 | for trait=1:ntraits, opts.subj_idx{trait} = 1:nsubj; end; 56 | end; 57 | if ~iscell(opts.subj_idx), 58 | a=opts.subj_idx; opts.subj_idx=cell(1, ntraits); 59 | for trait=1:ntraits, opts.subj_idx{trait} = a; end; 60 | end 61 | if length(opts.subj_idx) ~= ntraits, error('Invalid argument: subj_idx'); end; 62 | for trait=1:ntraits 63 | if islogical(opts.subj_idx{trait}), opts.subj_idx{trait} = find(opts.subj_idx{trait}); end; 64 | if any(opts.subj_idx{trait} < 1 | opts.subj_idx{trait} > nsubj), error('Argument out of range: subj_idx'); end; 65 | end 66 | end -------------------------------------------------------------------------------- /matlab/generate_sym02.m: -------------------------------------------------------------------------------- 1 | pi_index %pi_index = 1 .. 12; % to be defined by caller 2 | pe_index %pe_index = 1 .. 4; 3 | 4 | rng('shuffle') 5 | 6 | if ~exist('config', 'var'), find_config; end; 7 | fieldnames(config.frames) 8 | 9 | pleiotropic_enrichment = { '_Independent', '_Correlation', '_SharedSnps', '_Opposite' }; 10 | overlap_options = { '_NoneOverlap', '_PartialOverlap', '_FullOverlap' }; 11 | 12 | hvec_opts = [0.1, 0.3, 0.5, 0.7, 0.9]; 13 | pivec_opts = [1e-6, 3e-6, 1e-5, 3e-5, 1e-4, 3e-4, 1e-3, 3e-3, 1e-2, 3e-2, 1e-1, 3e-1]; 14 | 15 | pi_index = mod(pi_index - 1, length(pivec_opts)) + 1; 16 | if pi_index <= 0 || pi_index > length(pivec_opts), error('invalid pi_index'); end; 17 | pival = pivec_opts(pi_index); 18 | 19 | pe_index = mod(pe_index - 1, length(pleiotropic_enrichment)) + 1; 20 | if pe_index <= 0 || pe_index > length(pleiotropic_enrichment), error('invalid pe_index'); end; 21 | 22 | if pe_index == 1, pivec = [pival/2 pival/2]; sig1vec = [5 0.0]; sig2vec = [0.0 5]; rhovec = [0 0]; end; % indep 23 | if pe_index == 2, pivec = [pival/3 pival/3 pival/3]; sig1vec = [5 0.0 5]; sig2vec = [0.0 5 5]; rhovec = [0 0 0.99]; end; % full 24 | if pe_index == 3, pivec = [pival/3 pival/3 pival/3]; sig1vec = [5 0.0 5]; sig2vec = [0.0 5 5]; rhovec = [0 0 0]; end; % indep (big) + pleio_no_correlation (small) 25 | if pe_index == 4, pivec = [pival/4 pival/4 pival/4 pival/4]; sig1vec = [5 0.0 5 5]; sig2vec = [0.0 5 5 5]; rhovec = [0 0 -0.99 0.99]; end; % indep (big) + pleio opposite (small) 26 | 27 | frame = make_empty_frame (config, 'EUR_100K_80M_chunks'); 28 | frame = make_truebeta_gmm(frame, config, 'pivec', pivec, 'sig1vec', sig1vec, 'sig2vec', sig2vec, 'rhovec', rhovec); 29 | frame = make_truepheno (frame, config, 'snpstep', 100); 30 | 31 | [~,filename,~] = fileparts(tempname); 32 | save(sprintf('truepheno_%s_pi%.0e%s.mat', filename, pival, pleiotropic_enrichment{pe_index}), 'frame', '-v7.3'); 33 | 34 | nsubj = frame.subj; 35 | s500 = floor(nsubj/2); s501 = s500+1; s1000 = nsubj; 36 | s333 = floor(nsubj/3); s334 = s333+1; s667 = 2*s333+1; 37 | no_overlap = {1:s500, s501:s1000}; % no overlap 38 | partial_overlap = {1:s667, s334:s1000}; % partial overlap 39 | full_overlap = {1:s1000, 1:s1000}; % full overlap 40 | overlap_indices = {no_overlap, partial_overlap, full_overlap}; 41 | 42 | frame0 = frame; 43 | for hindex = 1:length(hvec_opts) 44 | frame1 = reframe (frame0, config, 'EUR_100K_1188K_merged'); 45 | frame1 = make_phenotype(frame1, config, 'h2', hvec_opts(hindex)); 46 | for oindex = 1:length(overlap_options) 47 | % For each overlap option start from the same frame, e.g. frame1 48 | % Remember that at the end we reframe it to another reference 49 | frame = make_gwas (frame1, config, 'subj_idx', overlap_indices{oindex}); 50 | frame = make_gwaslogp (frame, config); 51 | frame = reframe (frame, config, 'EUR_100K_1190K_ref'); 52 | frame.opts.now = datestr(now); 53 | save(sprintf('gwaslogp_%s_pi=%.0e_h2=%.2f%s%s.mat', filename, pival, hvec_opts(hindex), pleiotropic_enrichment{pe_index}, overlap_options{oindex}), 'frame', '-v7.3'); 54 | end 55 | end 56 | 57 | -------------------------------------------------------------------------------- /matlab/keep_and_extract.py: -------------------------------------------------------------------------------- 1 | import os 2 | import glob 3 | import ntpath 4 | import subprocess 5 | #workingpath = 'E:/' 6 | workingpath = '/work/users/oleksanf/MMIL/cfan_SimuPop/EUR' 7 | plink = 'plink' 8 | 9 | # 10K individuals, 2M SNPs template 10 | keep(os.path.join(workingpath, 'EUR_10K_2M'), 10000, '/home/oleksandr/2558411_ref.bim') 11 | merge(os.path.join(workingpath, 'EUR_10K_2M'), os.path.join(workingpath, 'EUR_10K_2M_merged')) 12 | 13 | # 500 individuals, all SNPs 14 | keep(os.path.join(workingpath, 'EUR_500_80M'), 500) 15 | merge(os.path.join(workingpath, 'EUR_500_80M'), os.path.join(workingpath, 'EUR_500_80M_merged')) 16 | 17 | # 10K individuals, all SNPs 18 | keep(os.path.join(workingpath, 'EUR_10K_80M'), 10000) 19 | 20 | # 100K individuals, 1M SNPs template 21 | keep(os.path.join(workingpath, 'EUR_100K_1M'), None, '/space/syn03/1/data/oleksandr/w_hm3.snplist/1m.ref') 22 | merge(os.path.join(workingpath, 'EUR_100K_1M'), os.path.join(workingpath, 'EUR_100K_1M_merged')) 23 | 24 | 25 | # Filter out SNPs and/or individuals 26 | def keep(outputDir, numIndividuals=None, extract=None): 27 | if not os.path.exists(outputDir): 28 | os.makedirs(outputDir) 29 | if numIndividuals: 30 | with open('tmp.txt', 'w') as text_file: 31 | for i in range(numIndividuals): 32 | text_file.write('id1_{0} id2_{0}\n'.format(i)) 33 | #files = glob.glob(os.path.join('/space/syn03/1/data/cfan/SimuPop/EUR', '*.bed')) 34 | #files = glob.glob(os.path.join(r'E:\EUR_100K_80M', '*.bed')) 35 | files = glob.glob(os.path.join(r'/work/users/oleksanf/MMIL/cfan_SimuPop/EUR', '*.bed')) 36 | #~/plink/plink --bfile chr1_chunk123 --keep keep10k.txt --make-bed --out chr1_chunk123_10k --extract ~/2558411_ref.bim 37 | for i, file in enumerate(files): 38 | print('Processing {0} out of {1}...\n'.format(i+1, len(files))) 39 | filename, file_extension = os.path.splitext(file) 40 | outfilename = os.path.join(outputDir, ntpath.basename(filename)); 41 | command_extract = '--extract {0}'.format(extract) if extract else '' 42 | command_keep = '--keep tmp.txt' if numIndividuals else '' 43 | command = '{0} --memory 4096 --bfile {1} {2} {3} --make-bed --out {4}'.format( 44 | plink, filename, command_keep, command_extract, outfilename) 45 | print(command) 46 | subprocess.call(command.split()) 47 | if numIndividuals: os.remove('tmp.txt') 48 | 49 | # Merge together all bed files in a folder 50 | def merge(inputDir, outputDir): 51 | if not os.path.exists(outputDir): 52 | os.makedirs(outputDir) 53 | files = glob.glob(os.path.join(inputDir, '*.bed')) 54 | first, _ = os.path.splitext(files[0]) 55 | with open('mergelist.txt', 'w') as mergelist: 56 | for file in files[1:]: 57 | filename, file_extension = os.path.splitext(file) 58 | mergelist.write('{0}.bed {0}.bim {0}.fam\n'.format(filename)) 59 | command = '{0} --memory 8192 --bfile {1} --merge-list mergelist.txt --allow-no-sex --make-bed --out {2}'.format( 60 | plink, first, os.path.join(outputDir, 'all')) 61 | subprocess.call(command.split()) 62 | os.remove('mergelist.txt') 63 | 64 | # ToDo: make .mat file with mapping between 65 | # 1. Indices in the original 80M file 66 | # 2. Indices in the resulting file 67 | # 3. Indices in the reference file 68 | # Now, (2) is subset of (1) and (3) 69 | 70 | -------------------------------------------------------------------------------- /matlab/chunked_frame_reader.m: -------------------------------------------------------------------------------- 1 | function genomat = chunked_frame_reader(subjvec, snpvec, nsubj, snpdetailpath, genotypepath) 2 | % Read genotypes from a single file 3 | % subjvec --- indices of subjects to read 4 | % snpvec --- indices of SNPs to read 5 | % nsubj --- total number of subjects present in the file 6 | % bfile --- path to plink BED file (without extension) 7 | % 8 | persistent PRS_chunk_file % cell array [nchunks x 1], file name of each non-empty chunk 9 | persistent PRS_chunk_of_snp % vector, [nsnp x 1], which chunk SNP belongs to 10 | persistent PRS_chunk_index_of_snp % vector, [nsnp x 1], unity-based index of each SNP in its chunk 11 | persistent PRS_snpdetailpath PRS_genotypepath 12 | 13 | % force rebuild persistent variables if snpdetailpath or genotypepath 14 | % has changed compared to the previous call of this function 15 | if isempty(PRS_snpdetailpath) || ~strcmp(PRS_snpdetailpath, snpdetailpath), PRS_chunk_file = []; end; 16 | if isempty(PRS_genotypepath) || ~strcmp(PRS_genotypepath, genotypepath), PRS_chunk_file = []; end; 17 | 18 | if isempty(PRS_chunk_file) 19 | snp_detail = load(snpdetailpath); 20 | PRS_chunk_file = {}; 21 | PRS_chunk_of_snp = nan(sum(snp_detail.snp_size(:)), 1); 22 | PRS_chunk_index_of_snp = nan(sum(snp_detail.snp_size(:)), 1); 23 | snp_index = 0; 24 | fprintf('Verifying integrity of chunks... '); 25 | ok = true; 26 | for i=1:snp_detail.nchrom 27 | for j=1:snp_detail.nchunk(i) 28 | fileprefix=sprintf('%s/chr%d_chunk%d',genotypepath,i,j); 29 | bedprefix=[fileprefix,'.bed']; 30 | snps_per_chunk = snp_detail.snp_size(i, j); 31 | if snps_per_chunk > 0 32 | if ~exist(bedprefix,'file'), ok = false; fprintf('Expected %s file not found --- %i SNPs lost\n', bedprefix, snps_per_chunk); end 33 | dirinfo = dir(bedprefix); if dirinfo.bytes ~= 3 + ceil(nsubj/4) * snps_per_chunk, ok = false; fprintf('File %s has size %i, expected %i\n', bedprefix, dirinfo.bytes, 3 + ceil(nsubj/4) * snps_per_chunk); end 34 | PRS_chunk_file{end+1, 1} = fileprefix; 35 | PRS_chunk_of_snp((snp_index + 1) : (snp_index + snps_per_chunk)) = length(PRS_chunk_file); 36 | PRS_chunk_index_of_snp((snp_index + 1) : (snp_index + snps_per_chunk)) = 1:snps_per_chunk; 37 | snp_index = snp_index + snps_per_chunk; 38 | end 39 | end 40 | end 41 | if ~ok, error('Errors found. See messages above for more details.'); end; 42 | fprintf('OK. Found %i chunks with %i SNPs in total.', length(PRS_chunk_file), snp_index); 43 | PRS_snpdetailpath = snpdetailpath; PRS_genotypepath = genotypepath; 44 | end 45 | 46 | if islogical(snpvec), snpvec = find(snpvec); end; 47 | if ~issorted(snpvec), error('chunked_frame_reader expect sorted list of snps'); end; 48 | 49 | genomat = zeros(length(subjvec),length(snpvec),'int8'); 50 | chunks_to_scan = unique(PRS_chunk_of_snp(snpvec)); 51 | for chunk = chunks_to_scan' 52 | snpvec_for_this_chunk = snpvec(PRS_chunk_of_snp(snpvec) == chunk); 53 | tmpgeno = PlinkRead_binary2(nsubj, PRS_chunk_index_of_snp(snpvec_for_this_chunk), PRS_chunk_file{chunk}); 54 | tmpgeno = tmpgeno(subjvec, :); 55 | genomat(:, PRS_chunk_of_snp(snpvec) == chunk) = tmpgeno; 56 | end 57 | end 58 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/packages/deb/rules: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | # -*- makefile -*- 3 | # Sample debian/rules that uses debhelper. 4 | # This file was originally written by Joey Hess and Craig Small. 5 | # As a special exception, when this file is copied by dh-make into a 6 | # dh-make output file, you may use that output file without restriction. 7 | # This special exception was added by Craig Small in version 0.37 of dh-make. 8 | 9 | # Uncomment this to turn on verbose mode. 10 | #export DH_VERBOSE=1 11 | 12 | 13 | # These are used for cross-compiling and for saving the configure script 14 | # from having to guess our platform (since we know it already) 15 | DEB_HOST_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE) 16 | DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE) 17 | 18 | 19 | CFLAGS = -Wall -g 20 | 21 | ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS))) 22 | CFLAGS += -O0 23 | else 24 | CFLAGS += -O2 25 | endif 26 | ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS))) 27 | INSTALL_PROGRAM += -s 28 | endif 29 | 30 | # shared library versions, option 1 31 | #version=2.0.5 32 | #major=2 33 | # option 2, assuming the library is created as src/.libs/libfoo.so.2.0.5 or so 34 | version=`ls src/.libs/lib*.so.* | \ 35 | awk '{if (match($$0,/[0-9]+\.[0-9]+\.[0-9]+$$/)) print substr($$0,RSTART)}'` 36 | major=`ls src/.libs/lib*.so.* | \ 37 | awk '{if (match($$0,/\.so\.[0-9]+$$/)) print substr($$0,RSTART+4)}'` 38 | 39 | config.status: configure 40 | dh_testdir 41 | # Add here commands to configure the package. 42 | CFLAGS="$(CFLAGS)" ./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info 43 | 44 | 45 | build: build-stamp 46 | build-stamp: config.status 47 | dh_testdir 48 | 49 | # Add here commands to compile the package. 50 | $(MAKE) 51 | 52 | touch build-stamp 53 | 54 | clean: 55 | dh_testdir 56 | dh_testroot 57 | rm -f build-stamp 58 | 59 | # Add here commands to clean up after the build process. 60 | -$(MAKE) distclean 61 | ifneq "$(wildcard /usr/share/misc/config.sub)" "" 62 | cp -f /usr/share/misc/config.sub config.sub 63 | endif 64 | ifneq "$(wildcard /usr/share/misc/config.guess)" "" 65 | cp -f /usr/share/misc/config.guess config.guess 66 | endif 67 | 68 | 69 | dh_clean 70 | 71 | install: build 72 | dh_testdir 73 | dh_testroot 74 | dh_clean -k 75 | dh_installdirs 76 | 77 | # Add here commands to install the package into debian/tmp 78 | $(MAKE) install DESTDIR=$(CURDIR)/debian/tmp 79 | 80 | 81 | # Build architecture-independent files here. 82 | binary-indep: build install 83 | # We have nothing to do by default. 84 | 85 | # Build architecture-dependent files here. 86 | binary-arch: build install 87 | dh_testdir 88 | dh_testroot 89 | dh_installchangelogs ChangeLog 90 | dh_installdocs 91 | dh_installexamples 92 | dh_install --sourcedir=debian/tmp 93 | # dh_installmenu 94 | # dh_installdebconf 95 | # dh_installlogrotate 96 | # dh_installemacsen 97 | # dh_installpam 98 | # dh_installmime 99 | # dh_installinit 100 | # dh_installcron 101 | # dh_installinfo 102 | dh_installman 103 | dh_link 104 | dh_strip 105 | dh_compress 106 | dh_fixperms 107 | # dh_perl 108 | # dh_python 109 | dh_makeshlibs 110 | dh_installdeb 111 | dh_shlibdeps 112 | dh_gencontrol 113 | dh_md5sums 114 | dh_builddeb 115 | 116 | binary: binary-indep binary-arch 117 | .PHONY: build clean binary-indep binary-arch binary install 118 | -------------------------------------------------------------------------------- /libplinkio/src/plinkio/bim.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012-2013, Mattias Frånberg 3 | * All rights reserved. 4 | * 5 | * This file is distributed under the Modified BSD License. See the COPYING file 6 | * for details. 7 | */ 8 | 9 | #ifndef __BIM_H__ 10 | #define __BIM_H__ 11 | 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | 16 | #include 17 | #include 18 | 19 | /** 20 | * Data structure that contains the PLINK information about a locus (SNP). 21 | */ 22 | struct pio_locus_t 23 | { 24 | /** 25 | * An internal reference id, so that we can read them in order. 26 | */ 27 | size_t pio_id; 28 | 29 | /** 30 | * Chromosome number starting from 1. 31 | */ 32 | unsigned char chromosome; 33 | 34 | /** 35 | * Name of the SNP. 36 | */ 37 | char *name; 38 | 39 | /** 40 | * Genetic position of the SNP. 41 | */ 42 | float position; 43 | 44 | /** 45 | * Base pair position of the SNP. 46 | */ 47 | long long bp_position; 48 | 49 | /** 50 | * First allele. 51 | */ 52 | char *allele1; 53 | 54 | /** 55 | * Second allele. 56 | */ 57 | char *allele2; 58 | }; 59 | 60 | /** 61 | * Contains the information about a bim file. On opening the file is 62 | * traversed and read into memory, each locus will have a record 63 | * in the locus array. 64 | */ 65 | struct pio_bim_file_t 66 | { 67 | /** 68 | * File pointer of bim file 69 | */ 70 | FILE *fp; 71 | 72 | /** 73 | * List of all locus in the file. 74 | */ 75 | UT_array *locus; 76 | }; 77 | 78 | /** 79 | * Opens the bim file at the given path and reads all loci 80 | * into memory, and closes the file. 81 | * 82 | * @param bim_file Bim file. 83 | * @param path The location of the bim file. 84 | * 85 | * @return Returns PIO_OK if the file could be read, PIO_ERROR otherwise. 86 | */ 87 | pio_status_t bim_open(struct pio_bim_file_t *bim_file, const char *path); 88 | 89 | /** 90 | * Creates a new bim file at the given path. 91 | * 92 | * @param bim_file Bim file. 93 | * @param path The location of the bim file. 94 | * 95 | * @return PIO_OK if the file could be created, PIO_ERROR otherwise. 96 | */ 97 | pio_status_t bim_create(struct pio_bim_file_t *bim_file, const char *path); 98 | 99 | /** 100 | * Writes the given locus to the bim file. 101 | * 102 | * @param bim_file Bim file. 103 | * @param locus The locus to write to the bim file. 104 | * 105 | * @return PIO_OK if the locus could be written, PIO_ERROR otherwise. 106 | */ 107 | pio_status_t bim_write(struct pio_bim_file_t *bim_file, struct pio_locus_t *locus); 108 | 109 | /** 110 | * Returns the locus with the given pio_id. 111 | * 112 | * @param bim_file The bim file to get the locus from. 113 | * @param pio_id The pio id of the locus. 114 | * 115 | * @return the locus with the given pio_id. 116 | */ 117 | struct pio_locus_t * bim_get_locus(struct pio_bim_file_t *bim_file, size_t pio_id); 118 | 119 | /** 120 | * Returns the number of loci that are stored in the given bim file. 121 | * 122 | * @param bim_file Bim file. 123 | * 124 | * @return the number of loci that are stored in the bim file. 125 | */ 126 | size_t bim_num_loci(struct pio_bim_file_t *bim_file); 127 | 128 | /** 129 | * Removes the read loci from memory. 130 | * 131 | * @param bim_file Bim file. 132 | */ 133 | void bim_close(struct pio_bim_file_t *bim_file); 134 | 135 | #ifdef __cplusplus 136 | } 137 | #endif 138 | 139 | #endif /* End of __BIM_H__ */ 140 | -------------------------------------------------------------------------------- /matlab/run.m: -------------------------------------------------------------------------------- 1 | % Simulate a pair of synthetic quantitative traits 2 | % Supports the following features: 3 | % pi - polygenicity of both traits; default: 1e-3 4 | % h2 - heritability of both traits; default: 0.5 5 | % pi12 - polygenic overlap between traits; default: pi 6 | % limitation: must not exceed pi. 7 | % rho12 - genetic correlation within polygenic overlap; default: 0 8 | % n12 - number of overlaping subjects across GWASs 9 | % limitation: must not exceed nsubj (frame parameter) 10 | % repeat - how many traits to make with the same parameters 11 | % each time different SNPs will be causal, and different noise 12 | % applied to the phenotype 13 | % 14 | % Options that control which set of SNPs to use: 15 | % (you may need up update paths in find_config.m). 16 | % 17 | % frame_pheno - frame to use when generate phenotype, default: EUR_100K_80M_chunks 18 | % frame_gwas - frame to use when perform gwas, default: EUR_100K_1188K_merged 19 | % frame_final - frame to use when save the result, default: EUR_100K_1190K_ref 20 | % 21 | % Features available in massim but not supported by this script: 22 | % differential_enrichment 23 | 24 | rng('shuffle') 25 | 26 | if ~exist('pi', 'var'), pi = 1e-3; end 27 | if ~exist('h2', 'var'), h2 = 0.5; end 28 | if ~exist('pi12', 'var'), pi12 = pi; end 29 | if ~exist('rho12', 'var'), rho12 = 0; end 30 | if ~exist('n12', 'var'), n12 = 0; end 31 | if ~exist('repeat', 'var'), repeat = 1; end 32 | if ~exist('frame_pheno', 'var'), frame_pheno = 'EUR_100K_80M_chunks'; end 33 | if ~exist('frame_gwas', 'var'), frame_gwas = 'EUR_100K_1188K_merged'; end 34 | if ~exist('frame_final', 'var'), frame_final = 'EUR_100K_1190K_ref'; end 35 | 36 | if pi > 1 || pi < 0 || isnan(pi), error('pi out of range'); end; 37 | if pi12 > 1 || pi12 < 0 || isnan(pi12) || pi12 > pi, error('pi12 out of range'); end; 38 | if 2*pi - pi12 > 1, error('parameters out of range: pi + pi - pi12 > 1'); end; 39 | pi1 = pi - pi12; % now pi1 represents the weight of the component specific to each trait (excluding overlap) 40 | 41 | if h2 < 0 || h2 > 1 || isnan(h2), error('h2 out of range'); end; 42 | if rho12 < -1 || rho12 > 1 || isnan(rho12), error('rho12 out of range'); end; 43 | 44 | if ~exist('config', 'var'), find_config; end; 45 | fieldnames(config.frames) 46 | disp(config.frames.(frame_pheno)) 47 | disp(config.frames.(frame_gwas)) 48 | disp(config.frames.(frame_final)) 49 | 50 | if n12 < 0 || n12 > config.frames.(frame_pheno).subj, error('n12 out of range'); end; 51 | 52 | pivec = [];sig1vec = [];sig2vec = [];rhovec = []; 53 | if pi1>0, pivec = [pivec pi1 pi1]; sig1vec = [sig1vec 1 0]; sig2vec = [sig2vec 0 1]; rhovec = [rhovec 0 0]; end; 54 | if pi12>0, pivec = [pivec pi12]; sig1vec = [sig1vec 1]; sig2vec = [sig2vec 1]; rhovec = [rhovec rho12]; end; 55 | 56 | for irepeat = 1:repeat 57 | frame = make_empty_frame (config, frame_pheno); 58 | frame = make_truebeta_gmm(frame, config, 'pivec', pivec, 'sig1vec', sig1vec, 'sig2vec', sig2vec, 'rhovec', rhovec); 59 | frame = make_truepheno (frame, config, 'snpstep', 100); 60 | 61 | nsubj = frame.subj; 62 | subj_idx = {1:fix((nsubj+n12)/2), 1+fix((nsubj-n12)/2):nsubj}; 63 | 64 | frame = reframe (frame, config, frame_gwas); 65 | frame = make_phenotype(frame, config, 'h2', h2); 66 | frame = make_gwas (frame, config, 'subj_idx', subj_idx); 67 | frame = make_gwaslogp (frame, config); 68 | frame = reframe (frame, config, frame_final); 69 | frame.opts.now = datestr(now); 70 | 71 | [~,filename,~] = fileparts(tempname); 72 | save(sprintf('gwaslogp_h2=%.2f_pi=%.0e_pi12=%.0e_rho12=%.2f_n12=%i_%s.mat', h2, pi, pi12, rho12, fix(n12), filename), 'frame', '-v7.3'); 73 | end -------------------------------------------------------------------------------- /libplinkio/libs/libcsv/inc/csv.h: -------------------------------------------------------------------------------- 1 | #ifndef LIBCSV_H__ 2 | #define LIBCSV_H__ 3 | #include 4 | #include 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | #define CSV_MAJOR 3 11 | #define CSV_MINOR 0 12 | #define CSV_RELEASE 0 13 | 14 | /* Error Codes */ 15 | #define CSV_SUCCESS 0 16 | #define CSV_EPARSE 1 /* Parse error in strict mode */ 17 | #define CSV_ENOMEM 2 /* Out of memory while increasing buffer size */ 18 | #define CSV_ETOOBIG 3 /* Buffer larger than SIZE_MAX needed */ 19 | #define CSV_EINVALID 4 /* Invalid code,should never be received from csv_error*/ 20 | 21 | 22 | /* parser options */ 23 | #define CSV_STRICT 1 /* enable strict mode */ 24 | #define CSV_REPALL_NL 2 /* report all unquoted carriage returns and linefeeds */ 25 | #define CSV_STRICT_FINI 4 /* causes csv_fini to return CSV_EPARSE if last 26 | field is quoted and doesn't containg ending 27 | quote */ 28 | #define CSV_APPEND_NULL 8 /* Ensure that all fields are null-ternimated */ 29 | 30 | 31 | /* Character values */ 32 | #define CSV_TAB 0x09 33 | #define CSV_SPACE 0x20 34 | #define CSV_CR 0x0d 35 | #define CSV_LF 0x0a 36 | #define CSV_COMMA 0x2c 37 | #define CSV_QUOTE 0x22 38 | 39 | struct csv_parser { 40 | int pstate; /* Parser state */ 41 | int quoted; /* Is the current field a quoted field? */ 42 | size_t spaces; /* Number of continious spaces after quote or in a non-quoted field */ 43 | unsigned char * entry_buf; /* Entry buffer */ 44 | size_t entry_pos; /* Current position in entry_buf (and current size of entry) */ 45 | size_t entry_size; /* Size of entry buffer */ 46 | int status; /* Operation status */ 47 | unsigned char options; 48 | unsigned char quote_char; 49 | unsigned char delim_char; 50 | int (*is_delim)(unsigned char); 51 | int (*is_space)(unsigned char); 52 | int (*is_term)(unsigned char); 53 | size_t blk_size; 54 | void *(*malloc_func)(size_t); 55 | void *(*realloc_func)(void *, size_t); 56 | void (*free_func)(void *); 57 | }; 58 | 59 | /* Function Prototypes */ 60 | int csv_init(struct csv_parser *p, unsigned char options); 61 | int csv_fini(struct csv_parser *p, void (*cb1)(void *, size_t, void *), void (*cb2)(int, void *), void *data); 62 | void csv_free(struct csv_parser *p); 63 | int csv_error(struct csv_parser *p); 64 | const char * csv_strerror(int error); 65 | size_t csv_parse(struct csv_parser *p, const void *s, size_t len, void (*cb1)(void *, size_t, void *), void (*cb2)(int, void *), void *data); 66 | size_t csv_write(void *dest, size_t dest_size, const void *src, size_t src_size); 67 | int csv_fwrite(FILE *fp, const void *src, size_t src_size); 68 | size_t csv_write2(void *dest, size_t dest_size, const void *src, size_t src_size, unsigned char quote); 69 | int csv_fwrite2(FILE *fp, const void *src, size_t src_size, unsigned char quote); 70 | int csv_get_opts(struct csv_parser *p); 71 | int csv_set_opts(struct csv_parser *p, unsigned char options); 72 | void csv_set_delim(struct csv_parser *p, unsigned char c); 73 | void csv_set_quote(struct csv_parser *p, unsigned char c); 74 | unsigned char csv_get_delim(struct csv_parser *p); 75 | unsigned char csv_get_quote(struct csv_parser *p); 76 | void csv_set_space_func(struct csv_parser *p, int (*f)(unsigned char)); 77 | void csv_set_term_func(struct csv_parser *p, int (*f)(unsigned char)); 78 | void csv_set_delim_func(struct csv_parser *p, int (*f)(unsigned char)); 79 | void csv_set_realloc_func(struct csv_parser *p, void *(*)(void *, size_t)); 80 | void csv_set_free_func(struct csv_parser *p, void (*)(void *)); 81 | void csv_set_blk_size(struct csv_parser *p, size_t); 82 | size_t csv_get_buffer_size(struct csv_parser *p); 83 | 84 | #ifdef __cplusplus 85 | } 86 | #endif 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /matlab/generate_sym05_nold.m: -------------------------------------------------------------------------------- 1 | % input: 2 | % pi1u, pi2u - total polygenicity of each trait 3 | % pi12 - polygenic overlap (pi12 <= pi1, pi12 <= pi2) 4 | % h2 - heritability 5 | % gencorr - genetic correlation in the pleiotropic component 6 | % subjoverlap - 'zero', 'partial', 'full' 7 | % 8 | % example: 9 | % pi1u=1e-5; pi2u=1e-4; pi12=1e-6; h2=0.5; gencorr=0.6; subjoverlap='zero'; 10 | % filename_id={'QRDKXL'; 'ASCPL'}; 11 | 12 | pi1 = pi1u - pi12; 13 | pi2 = pi2u - pi12; 14 | if (pi1 < 0 || pi2 < 0 || pi12 < 0 || (pi1+pi2+pi12 > 1)) 15 | error('error in pi1u, pi2u, or pi12'); 16 | end 17 | 18 | if (h2 < 0 || h2 > 1), error('error in h2'); end; 19 | if (gencorr < -1 || gencorr > 1), error('error in gencorr'); end; 20 | 21 | if ~exist('config', 'var') 22 | t = 'H:\NORSTORE\SYNGP'; if exist(t, 'dir'), norstore_path = t; end; 23 | t = '/usit/abel/u1/oleksanf'; if exist(t, 'dir'), norstore_path = t; end; 24 | t = 'E:\noLD_100K_1M_merged'; if exist(t, 'dir'), noLD_100K_1M_merged_path = t; end; 25 | t = '/work/users/oleksanf/noLD_100K_1M_merged'; if exist(t, 'dir'), noLD_100K_1M_merged_path = t; end; 26 | 27 | % Matlab 1M template 28 | config.frames.noLD_100K_1M_merged.subj = 100000; 29 | config.frames.noLD_100K_1M_merged.snps = 1188973; 30 | config.frames.noLD_100K_1M_merged.reader = @(subjvec, snpvec)merged_frame_reader(subjvec, snpvec, 100000, fullfile(noLD_100K_1M_merged_path, 'all')); 31 | config.frames.noLD_100K_1M_merged.bim = PlinkRead_bim(fullfile(noLD_100K_1M_merged_path, 'all')); 32 | config.frames.noLD_100K_1M_merged.bim = rmfield(config.frames.noLD_100K_1M_merged.bim, {'chrvec', 'cMvec', 'bpvec', 'A1vec', 'A2vec'}); 33 | 34 | % Matlab 1M reference template (w_hm3) 35 | config.frames.noLD_100K_1190K_ref.subj = 100000; 36 | config.frames.noLD_100K_1190K_ref.snps = 1190321; 37 | config.frames.noLD_100K_1190K_ref.bim = PlinkRead_bim(fullfile(norstore_path, '1m_ref'), true, '%s %s %f %d %s %s %s %s'); 38 | end 39 | fieldnames(config.frames) 40 | 41 | for repeat_index = 1:length(filename_id) 42 | rng('shuffle') 43 | 44 | frame = make_empty_frame (config, 'noLD_100K_1M_merged'); 45 | 46 | nsubj = frame.subj; 47 | s500 = floor(nsubj/2); s501 = s500+1; s1000 = nsubj; 48 | s333 = floor(nsubj/3); s334 = s333+1; s667 = 2*s333+1; 49 | overlap=[]; 50 | overlap.zero = {1:s500, s501:s1000}; % zero overlap 51 | overlap.partial = {1:s667, s334:s1000}; % partial overlap 52 | overlap.full = {1:s1000, 1:s1000}; % full overlap 53 | 54 | overlap = overlap.(subjoverlap); 55 | 56 | pivec = []; sig1vec = []; sig2vec = []; rhovec = []; 57 | if (pi1 > 0) pivec = [pivec pi1]; sig1vec = [sig1vec 1]; sig2vec = [sig2vec 0]; rhovec = [rhovec 0]; end; 58 | if (pi2 > 0) pivec = [pivec pi2]; sig1vec = [sig1vec 0]; sig2vec = [sig2vec 1]; rhovec = [rhovec 0]; end; 59 | if (pi12 > 0) pivec = [pivec pi12]; sig1vec = [sig1vec 1]; sig2vec = [sig2vec 1]; rhovec = [rhovec gencorr]; end; 60 | 61 | frame = make_truebeta_gmm(frame, config, 'pivec', pivec, 'sig1vec', sig1vec, 'sig2vec', sig2vec, 'rhovec', rhovec); 62 | frame = make_truepheno (frame, config, 'snpstep', 100); 63 | 64 | frame = make_phenotype(frame, config, 'h2', h2); 65 | frame = make_gwas (frame, config, 'subj_idx', overlap); 66 | frame = make_gwaslogp (frame, config); 67 | 68 | templates = {'noLD_100K_1190K_ref'}; 69 | template_short_name = {'1MnoLD'}; 70 | for it = 1:length(templates) 71 | frame1 = reframe(frame, config, templates{it}); 72 | filename = sprintf('template=%s_pi1u=%.0e_pi2u=%.0e_pi12=%.0e_gencorr=%.2f_h2=%.2f_subjoverlap=%s_id=%s.mat', template_short_name{it}, pi1u, pi2u, pi12, gencorr, h2, subjoverlap, filename_id{repeat_index}); 73 | save(filename, '-struct', 'frame1', 'truebeta', 'mixture', 'truepheno', 'phenotype', 'gwasbeta', 'gwaspval', 'nvec', 'logpvec', 'zvec', '-v7.3'); 74 | end 75 | 76 | end -------------------------------------------------------------------------------- /libplinkio/py-plinkio/snparray.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "snparray.h" 4 | #include "common.h" 5 | 6 | snp_array_t * 7 | snparray_from_array(PyTypeObject *prototype, snp_t *array, size_t length) 8 | { 9 | snp_array_t *snp_array = (snp_array_t *) prototype->tp_alloc( prototype, 0 ); 10 | if( snp_array != NULL ) 11 | { 12 | snp_array->array = (snp_t *) malloc( sizeof( snp_t ) * length ); 13 | memcpy( snp_array->array, array, sizeof( snp_t ) * length ); 14 | snp_array->length = length; 15 | } 16 | 17 | return snp_array; 18 | } 19 | 20 | void 21 | snparray_dealloc(snp_array_t *self) 22 | { 23 | if( self != NULL ) 24 | { 25 | free( self->array ); 26 | self->length = 0; 27 | Py_TYPE( self )->tp_free( ( PyObject * ) self ); 28 | } 29 | } 30 | 31 | PyObject * 32 | snparray_str(PyObject *self) 33 | { 34 | snp_array_t *snp_array = (snp_array_t *) self; 35 | int i; 36 | size_t string_length = 3 * snp_array->length + 3; 37 | char *as_string = (char *) malloc( string_length ); 38 | char *string_p = as_string; 39 | PyObject *py_string; 40 | 41 | *string_p++ = '['; 42 | 43 | for(i = 0; i < snp_array->length; i++) 44 | { 45 | if( snp_array->array[ i ] <= 3 ) 46 | { 47 | *string_p++ = '0' + snp_array->array[ i ]; 48 | } 49 | else 50 | { 51 | *string_p++ = 'E'; 52 | } 53 | 54 | *string_p++ = ','; 55 | *string_p++ = ' '; 56 | } 57 | 58 | // We should remove the last ", ". 59 | string_p -= 2; 60 | *string_p++ = ']'; 61 | *string_p++ = '\0'; 62 | 63 | py_string = PyUnicode_FromString( as_string ); 64 | free( as_string ); 65 | 66 | return py_string; 67 | } 68 | 69 | PyObject * 70 | snparray_allele_counts(PyObject *self, PyObject *none) 71 | { 72 | snp_array_t *snp_array = (snp_array_t *) self; 73 | long counts[4] = { 0 }; 74 | int i; 75 | PyObject *count_list; 76 | 77 | for(i = 0; i < snp_array->length; i++) 78 | { 79 | if( snp_array->array[ i ] <= 3 ) 80 | { 81 | counts[ snp_array->array[ i ] ]++; 82 | } 83 | } 84 | 85 | count_list = PyList_New( 4 ); 86 | if( count_list == NULL ) 87 | { 88 | PyErr_SetString( PyExc_MemoryError, "Could not allocate count list." ); 89 | return NULL; 90 | } 91 | 92 | for(i = 0; i < 4; i++) 93 | { 94 | PyObject *count = PyLong_FromLong( counts[ i ] ); 95 | PyList_SET_ITEM( count_list, i, count ); 96 | } 97 | 98 | return count_list; 99 | } 100 | 101 | Py_ssize_t 102 | snparray_length(PyObject *self) 103 | { 104 | snp_array_t *snp_array = (snp_array_t *) self; 105 | return snp_array->length; 106 | } 107 | 108 | PyObject * 109 | snparray_getitem(PyObject *self, Py_ssize_t index) 110 | { 111 | snp_array_t *snp_array = (snp_array_t *) self; 112 | 113 | if( index >= snp_array->length ) 114 | { 115 | PyErr_SetString( PyExc_IndexError, "snparray index out of range" ); 116 | return NULL; 117 | } 118 | 119 | return PyLong_FromLong( (long) snp_array->array[ index ] ); 120 | } 121 | 122 | int 123 | snparray_contains(PyObject *self, PyObject *value) 124 | { 125 | snp_array_t *snp_array = (snp_array_t *) self; 126 | int i; 127 | 128 | long value_as_long = PyLong_AsLong( value ); 129 | snp_t value_to_look_for; 130 | if( value_as_long == -1 ) 131 | { 132 | return 0; 133 | } 134 | 135 | value_to_look_for = (snp_t) value_as_long; 136 | for(i = 0; i < snp_array->length; i++) 137 | { 138 | if( snp_array->array[ i ] == value_to_look_for ) 139 | { 140 | return 1; 141 | } 142 | } 143 | 144 | return 0; 145 | } 146 | -------------------------------------------------------------------------------- /libplinkio/src/bim.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012-2013, Mattias Frånberg 3 | * All rights reserved. 4 | * 5 | * This file is distributed under the Modified BSD License. See the COPYING file 6 | * for details. 7 | */ 8 | 9 | #include 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | /** 17 | * Creates mock versions of IO functions to allow unit testing. 18 | */ 19 | #ifdef UNIT_TESTING 20 | extern FILE *mock_fopen(const char *path, const char *mode); 21 | int mock_fclose(FILE *fp); 22 | 23 | #define fopen mock_fopen 24 | #define fclose mock_fclose 25 | #endif 26 | 27 | /** 28 | * Locus destructor. Ensures that the allocated 29 | * strings are freed properly. 30 | * 31 | * @param element Pointer to a locus. 32 | */ 33 | void 34 | utarray_locus_dtor(void *element) 35 | { 36 | struct pio_locus_t *locus = (struct pio_locus_t *) element; 37 | 38 | if( locus->name != NULL ) 39 | { 40 | free( locus->name ); 41 | } 42 | if( locus->allele1 != NULL ) 43 | { 44 | free( locus->allele1 ); 45 | } 46 | if( locus->allele2 != NULL ) 47 | { 48 | free( locus->allele2 ); 49 | } 50 | } 51 | 52 | /** 53 | * Properties of the locus array for dtarray. 54 | */ 55 | UT_icd LOCUS_ICD = { sizeof( struct pio_locus_t ), NULL, NULL, utarray_locus_dtor }; 56 | 57 | pio_status_t 58 | bim_open(struct pio_bim_file_t *bim_file, const char *path) 59 | { 60 | int status; 61 | FILE *bim_fp; 62 | bzero( bim_file, sizeof( *bim_file ) ); 63 | bim_fp = fopen( path, "r" ); 64 | if( bim_fp == NULL ) 65 | { 66 | return PIO_ERROR; 67 | } 68 | 69 | bim_file->fp = bim_fp; 70 | utarray_new( bim_file->locus, &LOCUS_ICD ); 71 | status = parse_loci( bim_file->fp, bim_file->locus ); 72 | 73 | fclose( bim_fp ); 74 | bim_file->fp = NULL; 75 | 76 | return status; 77 | } 78 | 79 | pio_status_t 80 | bim_create(struct pio_bim_file_t *bim_file, const char *path) 81 | { 82 | FILE *bim_fp; 83 | bzero( bim_file, sizeof( *bim_file ) ); 84 | bim_fp = fopen( path, "w" ); 85 | if( bim_fp == NULL ) 86 | { 87 | return PIO_ERROR; 88 | } 89 | 90 | bim_file->fp = bim_fp; 91 | utarray_new( bim_file->locus, &LOCUS_ICD ); 92 | 93 | return PIO_OK; 94 | } 95 | 96 | pio_status_t 97 | bim_write(struct pio_bim_file_t *bim_file, struct pio_locus_t *locus) 98 | { 99 | struct pio_locus_t locus_copy; 100 | if( write_locus( bim_file->fp, locus ) == PIO_OK ) 101 | { 102 | locus_copy.pio_id = bim_num_loci( bim_file ); 103 | locus_copy.chromosome = locus->chromosome; 104 | locus_copy.name = strdup( locus->name ); 105 | locus_copy.position = locus->position; 106 | locus_copy.bp_position = locus->bp_position; 107 | locus_copy.allele1 = strdup( locus->allele1 ); 108 | locus_copy.allele2 = strdup( locus->allele2 ); 109 | 110 | utarray_push_back( bim_file->locus, &locus_copy ); 111 | return PIO_OK; 112 | } 113 | else 114 | { 115 | return PIO_ERROR; 116 | } 117 | } 118 | 119 | struct pio_locus_t * 120 | bim_get_locus(struct pio_bim_file_t *bim_file, size_t pio_id) 121 | { 122 | return (struct pio_locus_t *) utarray_eltptr( bim_file->locus, pio_id ); 123 | } 124 | 125 | size_t 126 | bim_num_loci(struct pio_bim_file_t *bim_file) 127 | { 128 | return utarray_len( bim_file->locus ); 129 | } 130 | 131 | void 132 | bim_close(struct pio_bim_file_t *bim_file) 133 | { 134 | if( bim_file->locus == NULL ) 135 | { 136 | return; 137 | } 138 | if( bim_file->fp != NULL ) 139 | { 140 | fclose( bim_file->fp ); 141 | } 142 | 143 | utarray_free( bim_file->locus ); 144 | bim_file->locus = NULL; 145 | bim_file->fp = NULL; 146 | } 147 | -------------------------------------------------------------------------------- /libplinkio/src/plinkio/fam.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012-2013, Mattias Frånberg 3 | * All rights reserved. 4 | * 5 | * This file is distributed under the Modified BSD License. See the COPYING file 6 | * for details. 7 | */ 8 | 9 | #ifndef __FAM_H__ 10 | #define __FAM_H__ 11 | 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | 16 | #include 17 | 18 | #include 19 | #include 20 | 21 | /** 22 | * Sex of a sample. 23 | */ 24 | enum sex_t 25 | { 26 | PIO_MALE, 27 | PIO_FEMALE, 28 | PIO_UNKNOWN 29 | }; 30 | 31 | /** 32 | * Affection status. 33 | */ 34 | enum affection_t 35 | { 36 | PIO_CONTROL = 0, 37 | PIO_CASE = 1, 38 | PIO_MISSING, 39 | PIO_CONTINUOUS 40 | }; 41 | 42 | /** 43 | * Data structure that contains the PLINK information about a sample (individual). 44 | */ 45 | struct pio_sample_t 46 | { 47 | /** 48 | * An internal reference id, so that we can read them in order. 49 | */ 50 | size_t pio_id; 51 | 52 | /** 53 | * Family identifier. 54 | */ 55 | char *fid; 56 | 57 | /** 58 | * Plink individual identifier. 59 | */ 60 | char *iid; 61 | 62 | /** 63 | * Plink individual identifier of father, 0 if none. 64 | */ 65 | char *father_iid; 66 | 67 | /** 68 | * Plink individual identifier of mother, 0 if none. 69 | */ 70 | char *mother_iid; 71 | 72 | /** 73 | * The sex of the individual. 74 | */ 75 | enum sex_t sex; 76 | 77 | /** 78 | * Affection of the individuals, case, control or unkown. Control 79 | * is always 0 and case always 1. 80 | */ 81 | enum affection_t affection; 82 | 83 | /** 84 | * A continuous phenotype of the individual. 85 | */ 86 | float phenotype; 87 | }; 88 | 89 | /** 90 | * Contains the information about a fam file. On opening the file it is 91 | * traversed and read into memory, each sample will have a record 92 | * in the sample array. 93 | */ 94 | struct pio_fam_file_t 95 | { 96 | /** 97 | * Pointer to an opened and parse file. 98 | */ 99 | FILE *fp; 100 | 101 | /** 102 | * List of additional information for each sample. 103 | */ 104 | UT_array *sample; 105 | }; 106 | 107 | /** 108 | * Opens the fam file at the given path and reads all individuals 109 | * into memory, and closes the file. 110 | * 111 | * @param fam_file Fam file. 112 | * @param path The location of the fam file. 113 | * 114 | * @return Returns PIO_OK if the file could be read, PIO_ERROR otherwise. 115 | */ 116 | pio_status_t fam_open(struct pio_fam_file_t *fam_file, const char *path); 117 | 118 | /** 119 | * Creates a fam file at the given path, and writes all 120 | * individuals to the file. 121 | * 122 | * @param fam_file Fam file. 123 | * @param path The location of the fam file. 124 | * @param samples List of samples. 125 | * @param num_samples Number of samples in the list. 126 | * 127 | * @return PIO_OK if the file could be created and written, PIO_ERROR otherwise. 128 | */ 129 | pio_status_t fam_create(struct pio_fam_file_t *fam_file, const char *path, struct pio_sample_t *samples, size_t num_samples); 130 | 131 | /** 132 | * Returns the sample with the given pio_id. 133 | * 134 | * @param fam_file The fam file to get the sample from. 135 | * @param pio_id The pio id of the sample. 136 | * 137 | * @return the sample with the given pio_id. 138 | */ 139 | struct pio_sample_t * fam_get_sample(struct pio_fam_file_t *fam_file, size_t pio_id); 140 | 141 | /** 142 | * Returns the number of samples that are stored in the given fam file. 143 | * 144 | * @param fam_file Fam file. 145 | * 146 | * @return the number of samples that are stored in the fam file. 147 | */ 148 | size_t fam_num_samples(struct pio_fam_file_t *fam_file); 149 | 150 | /** 151 | * Removes the read samples from memory. 152 | * 153 | * @param fam_file Fam file. 154 | */ 155 | void fam_close(struct pio_fam_file_t *fam_file); 156 | 157 | #ifdef __cplusplus 158 | } 159 | #endif 160 | 161 | #endif /* End of __FAM_H__ */ 162 | -------------------------------------------------------------------------------- /libplinkio/src/fam.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012-2013, Mattias Frånberg 3 | * All rights reserved. 4 | * 5 | * This file is distributed under the Modified BSD License. See the COPYING file 6 | * for details. 7 | */ 8 | 9 | #include 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | /** 17 | * Creates mock versions of IO functions to allow unit testing. 18 | */ 19 | #ifdef UNIT_TESTING 20 | extern FILE *mock_fopen(const char *path, const char *mode); 21 | int mock_fclose(FILE *fp); 22 | 23 | #define fopen mock_fopen 24 | #define fclose mock_fclose 25 | #endif 26 | 27 | /** 28 | * Sample destructor. Ensures that the allocated 29 | * strings are freed properly. 30 | * 31 | * @param element Pointer to a sample. 32 | */ 33 | static void 34 | utarray_sample_dtor(void *element) 35 | { 36 | struct pio_sample_t *sample = (struct pio_sample_t *) element; 37 | 38 | if( sample->fid != NULL ) 39 | { 40 | free( sample->fid ); 41 | } 42 | if( sample->iid != NULL ) 43 | { 44 | free( sample->iid ); 45 | } 46 | if( sample->father_iid != NULL ) 47 | { 48 | free( sample->father_iid ); 49 | } 50 | if( sample->mother_iid != NULL ) 51 | { 52 | free( sample->mother_iid ); 53 | } 54 | } 55 | 56 | /** 57 | * Properties of the sample array for dtarray. 58 | */ 59 | UT_icd SAMPLE_ICD = { 60 | sizeof( struct pio_sample_t ), 61 | NULL, 62 | NULL, 63 | utarray_sample_dtor 64 | }; 65 | 66 | pio_status_t 67 | fam_open(struct pio_fam_file_t *fam_file, const char *path) 68 | { 69 | pio_status_t status; 70 | FILE *fam_fp; 71 | 72 | bzero( fam_file, sizeof( *fam_file ) ); 73 | fam_fp = fopen( path, "r" ); 74 | if( fam_fp == NULL ) 75 | { 76 | return PIO_ERROR; 77 | } 78 | 79 | fam_file->fp = fam_fp; 80 | utarray_new( fam_file->sample, &SAMPLE_ICD ); 81 | status = parse_samples( fam_file->fp, fam_file->sample ); 82 | 83 | fclose( fam_fp ); 84 | fam_file->fp = NULL; 85 | 86 | return status; 87 | } 88 | 89 | pio_status_t 90 | fam_create(struct pio_fam_file_t *fam_file, const char *path, struct pio_sample_t *samples, size_t num_samples) 91 | { 92 | int i; 93 | FILE *fam_fp; 94 | struct pio_sample_t sample_copy; 95 | 96 | bzero( fam_file, sizeof( *fam_file ) ); 97 | fam_fp = fopen( path, "w" ); 98 | if( fam_fp == NULL ) 99 | { 100 | return PIO_ERROR; 101 | } 102 | 103 | fam_file->fp = fam_fp; 104 | 105 | utarray_new( fam_file->sample, &SAMPLE_ICD ); 106 | for(i = 0; i < num_samples; i++) 107 | { 108 | if( write_sample( fam_fp, &samples[ i ] ) != PIO_OK ) 109 | { 110 | return PIO_ERROR; 111 | } 112 | 113 | sample_copy.pio_id = i; 114 | sample_copy.fid = strdup( samples[ i ].fid ); 115 | sample_copy.iid = strdup( samples[ i ].iid ); 116 | sample_copy.mother_iid = strdup( samples[ i ].mother_iid ); 117 | sample_copy.father_iid = strdup( samples[ i ].father_iid ); 118 | sample_copy.sex = samples[ i ].sex; 119 | sample_copy.affection = samples[ i ].affection; 120 | sample_copy.phenotype = samples[ i ].phenotype; 121 | 122 | utarray_push_back( fam_file->sample, &sample_copy ); 123 | } 124 | 125 | return PIO_OK; 126 | } 127 | 128 | struct pio_sample_t * 129 | fam_get_sample(struct pio_fam_file_t *fam_file, size_t pio_id) 130 | { 131 | return (struct pio_sample_t *) utarray_eltptr( fam_file->sample, pio_id ); 132 | } 133 | 134 | size_t 135 | fam_num_samples(struct pio_fam_file_t *fam_file) 136 | { 137 | return utarray_len( fam_file->sample ); 138 | } 139 | 140 | void 141 | fam_close(struct pio_fam_file_t *fam_file) 142 | { 143 | if( fam_file->sample == NULL ) 144 | { 145 | return; 146 | } 147 | if( fam_file->fp != NULL ) 148 | { 149 | fclose( fam_file->fp ); 150 | } 151 | 152 | utarray_free( fam_file->sample ); 153 | 154 | fam_file->sample = NULL; 155 | fam_file->fp = NULL; 156 | } 157 | -------------------------------------------------------------------------------- /matlab/generate_sym04.m: -------------------------------------------------------------------------------- 1 | % input: 2 | % pi1u, pi2u - total polygenicity of each trait 3 | % pi12 - polygenic overlap (pi12 <= pi1, pi12 <= pi2) 4 | % h2 - heritability 5 | % gencorr - genetic correlation in the pleiotropic component 6 | % subjoverlap - 'zero', 'partial', 'full' 7 | % 8 | % example: 9 | % pi1u=1e-5; pi2u=1e-4; pi12=1e-6; h2=0.5; gencorr=0.6; subjoverlap='zero'; 10 | % filename_id={'QRDKXL'; 'ASCPL'}; 11 | 12 | pi1 = pi1u - pi12; 13 | pi2 = pi2u - pi12; 14 | if (pi1 < 0 || pi2 < 0 || pi12 < 0 || (pi1+pi2+pi12 > 1)) 15 | error('error in pi1u, pi2u, or pi12'); 16 | end 17 | 18 | if (h2 < 0 || h2 > 1), error('error in h2'); end; 19 | if (gencorr < -1 || gencorr > 1), error('error in gencorr'); end; 20 | 21 | if ~exist('config', 'var') 22 | t = 'H:\NORSTORE\SYNGP'; if exist(t, 'dir'), norstore_path = t; end; 23 | t = '/usit/abel/u1/oleksanf'; if exist(t, 'dir'), norstore_path = t; end; 24 | t = 'E:\EUR_100K_9M_merged'; if exist(t, 'dir'), EUR_100K_9M_merged_path = t; end; 25 | t = '/work/users/oleksanf/EUR_100K_9M_merged'; if exist(t, 'dir'), EUR_100K_9M_merged_path = t; end; 26 | 27 | % A subset of 80M and 9M template 28 | config.frames.EUR_100K_8801K_merged.subj = 100000; 29 | config.frames.EUR_100K_8801K_merged.snps = 8801249; 30 | config.frames.EUR_100K_8801K_merged.reader = @(subjvec, snpvec)merged_frame_reader(subjvec, snpvec, 100000, fullfile(EUR_100K_9M_merged_path, 'all')); 31 | config.frames.EUR_100K_8801K_merged.bim = PlinkRead_bim(fullfile(EUR_100K_9M_merged_path, 'all')); 32 | config.frames.EUR_100K_8801K_merged.bim = rmfield(config.frames.EUR_100K_8801K_merged.bim, {'chrvec', 'cMvec', 'bpvec', 'A1vec', 'A2vec'}); 33 | 34 | % Matlab 9M template 35 | config.frames.EUR_100K_9279K_ref.subj = 100000; 36 | config.frames.EUR_100K_9279K_ref.snps = 9279485; 37 | config.frames.EUR_100K_9279K_ref.bim = PlinkRead_bim(fullfile(norstore_path, '9279485_ref'), true, '%s %s %f %d %s %s %s %s'); 38 | config.frames.EUR_100K_9279K_ref.bim = rmfield(config.frames.EUR_100K_9279K_ref.bim, {'chrvec', 'cMvec', 'bpvec', 'A1vec', 'A2vec'}); 39 | 40 | % Matlab 1M reference template (w_hm3) 41 | config.frames.EUR_100K_1190K_ref.subj = 100000; 42 | config.frames.EUR_100K_1190K_ref.snps = 1190321; 43 | config.frames.EUR_100K_1190K_ref.bim = PlinkRead_bim(fullfile(norstore_path, '1m_ref'), true, '%s %s %f %d %s %s %s %s'); 44 | end 45 | fieldnames(config.frames) 46 | 47 | for repeat_index = 1:length(filename_id) 48 | rng('shuffle') 49 | 50 | frame = make_empty_frame (config, 'EUR_100K_8801K_merged'); 51 | 52 | nsubj = frame.subj; 53 | s500 = floor(nsubj/2); s501 = s500+1; s1000 = nsubj; 54 | s333 = floor(nsubj/3); s334 = s333+1; s667 = 2*s333+1; 55 | overlap=[]; 56 | overlap.zero = {1:s500, s501:s1000}; % zero overlap 57 | overlap.partial = {1:s667, s334:s1000}; % partial overlap 58 | overlap.full = {1:s1000, 1:s1000}; % full overlap 59 | 60 | overlap = overlap.(subjoverlap); 61 | 62 | pivec = []; sig1vec = []; sig2vec = []; rhovec = []; 63 | if (pi1 > 0) pivec = [pivec pi1]; sig1vec = [sig1vec 1]; sig2vec = [sig2vec 0]; rhovec = [rhovec 0]; end; 64 | if (pi2 > 0) pivec = [pivec pi2]; sig1vec = [sig1vec 0]; sig2vec = [sig2vec 1]; rhovec = [rhovec 0]; end; 65 | if (pi12 > 0) pivec = [pivec pi12]; sig1vec = [sig1vec 1]; sig2vec = [sig2vec 1]; rhovec = [rhovec gencorr]; end; 66 | 67 | frame = make_truebeta_gmm(frame, config, 'pivec', pivec, 'sig1vec', sig1vec, 'sig2vec', sig2vec, 'rhovec', rhovec); 68 | frame = make_truepheno (frame, config, 'snpstep', 100); 69 | 70 | frame = make_phenotype(frame, config, 'h2', h2); 71 | frame = make_gwas (frame, config, 'subj_idx', overlap); 72 | frame = make_gwaslogp (frame, config); 73 | 74 | templates = {'EUR_100K_9279K_ref', 'EUR_100K_1190K_ref'}; 75 | template_short_name = {'9M', '1M'}; 76 | for it = 1:length(templates) 77 | frame1 = reframe(frame, config, templates{it}); 78 | filename = sprintf('template=%s_pi1u=%.0e_pi2u=%.0e_pi12=%.0e_gencorr=%.2f_h2=%.2f_subjoverlap=%s_id=%s.mat', template_short_name{it}, pi1u, pi2u, pi12, gencorr, h2, subjoverlap, filename_id{repeat_index}); 79 | save(filename, '-struct', 'frame1', 'truebeta', 'mixture', 'truepheno', 'phenotype', 'gwasbeta', 'gwaspval', 'nvec', 'logpvec', 'zvec', '-v7.3'); 80 | end 81 | 82 | end -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/mkinstalldirs: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | # mkinstalldirs --- make directory hierarchy 3 | 4 | scriptversion=2005-06-29.22 5 | 6 | # Original author: Noah Friedman 7 | # Created: 1993-05-16 8 | # Public domain. 9 | # 10 | # This file is maintained in Automake, please report 11 | # bugs to or send patches to 12 | # . 13 | 14 | errstatus=0 15 | dirmode= 16 | 17 | usage="\ 18 | Usage: mkinstalldirs [-h] [--help] [--version] [-m MODE] DIR ... 19 | 20 | Create each directory DIR (with mode MODE, if specified), including all 21 | leading file name components. 22 | 23 | Report bugs to ." 24 | 25 | # process command line arguments 26 | while test $# -gt 0 ; do 27 | case $1 in 28 | -h | --help | --h*) # -h for help 29 | echo "$usage" 30 | exit $? 31 | ;; 32 | -m) # -m PERM arg 33 | shift 34 | test $# -eq 0 && { echo "$usage" 1>&2; exit 1; } 35 | dirmode=$1 36 | shift 37 | ;; 38 | --version) 39 | echo "$0 $scriptversion" 40 | exit $? 41 | ;; 42 | --) # stop option processing 43 | shift 44 | break 45 | ;; 46 | -*) # unknown option 47 | echo "$usage" 1>&2 48 | exit 1 49 | ;; 50 | *) # first non-opt arg 51 | break 52 | ;; 53 | esac 54 | done 55 | 56 | for file 57 | do 58 | if test -d "$file"; then 59 | shift 60 | else 61 | break 62 | fi 63 | done 64 | 65 | case $# in 66 | 0) exit 0 ;; 67 | esac 68 | 69 | # Solaris 8's mkdir -p isn't thread-safe. If you mkdir -p a/b and 70 | # mkdir -p a/c at the same time, both will detect that a is missing, 71 | # one will create a, then the other will try to create a and die with 72 | # a "File exists" error. This is a problem when calling mkinstalldirs 73 | # from a parallel make. We use --version in the probe to restrict 74 | # ourselves to GNU mkdir, which is thread-safe. 75 | case $dirmode in 76 | '') 77 | if mkdir -p --version . >/dev/null 2>&1 && test ! -d ./--version; then 78 | echo "mkdir -p -- $*" 79 | exec mkdir -p -- "$@" 80 | else 81 | # On NextStep and OpenStep, the `mkdir' command does not 82 | # recognize any option. It will interpret all options as 83 | # directories to create, and then abort because `.' already 84 | # exists. 85 | test -d ./-p && rmdir ./-p 86 | test -d ./--version && rmdir ./--version 87 | fi 88 | ;; 89 | *) 90 | if mkdir -m "$dirmode" -p --version . >/dev/null 2>&1 && 91 | test ! -d ./--version; then 92 | echo "mkdir -m $dirmode -p -- $*" 93 | exec mkdir -m "$dirmode" -p -- "$@" 94 | else 95 | # Clean up after NextStep and OpenStep mkdir. 96 | for d in ./-m ./-p ./--version "./$dirmode"; 97 | do 98 | test -d $d && rmdir $d 99 | done 100 | fi 101 | ;; 102 | esac 103 | 104 | for file 105 | do 106 | case $file in 107 | /*) pathcomp=/ ;; 108 | *) pathcomp= ;; 109 | esac 110 | oIFS=$IFS 111 | IFS=/ 112 | set fnord $file 113 | shift 114 | IFS=$oIFS 115 | 116 | for d 117 | do 118 | test "x$d" = x && continue 119 | 120 | pathcomp=$pathcomp$d 121 | case $pathcomp in 122 | -*) pathcomp=./$pathcomp ;; 123 | esac 124 | 125 | if test ! -d "$pathcomp"; then 126 | echo "mkdir $pathcomp" 127 | 128 | mkdir "$pathcomp" || lasterr=$? 129 | 130 | if test ! -d "$pathcomp"; then 131 | errstatus=$lasterr 132 | else 133 | if test ! -z "$dirmode"; then 134 | echo "chmod $dirmode $pathcomp" 135 | lasterr= 136 | chmod "$dirmode" "$pathcomp" || lasterr=$? 137 | 138 | if test ! -z "$lasterr"; then 139 | errstatus=$lasterr 140 | fi 141 | fi 142 | fi 143 | fi 144 | 145 | pathcomp=$pathcomp/ 146 | done 147 | done 148 | 149 | exit $errstatus 150 | 151 | # Local Variables: 152 | # mode: shell-script 153 | # sh-indentation: 2 154 | # eval: (add-hook 'write-file-hooks 'time-stamp) 155 | # time-stamp-start: "scriptversion=" 156 | # time-stamp-format: "%:y-%02m-%02d.%02H" 157 | # time-stamp-end: "$" 158 | # End: 159 | -------------------------------------------------------------------------------- /libplinkio/compile: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | # Wrapper for compilers which do not understand `-c -o'. 3 | 4 | scriptversion=2005-05-14.22 5 | 6 | # Copyright (C) 1999, 2000, 2003, 2004, 2005 Free Software Foundation, Inc. 7 | # Written by Tom Tromey . 8 | # 9 | # This program is free software; you can redistribute it and/or modify 10 | # it under the terms of the GNU General Public License as published by 11 | # the Free Software Foundation; either version 2, or (at your option) 12 | # any later version. 13 | # 14 | # This program is distributed in the hope that it will be useful, 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | # GNU General Public License for more details. 18 | # 19 | # You should have received a copy of the GNU General Public License 20 | # along with this program; if not, write to the Free Software 21 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 22 | 23 | # As a special exception to the GNU General Public License, if you 24 | # distribute this file as part of a program that contains a 25 | # configuration script generated by Autoconf, you may include it under 26 | # the same distribution terms that you use for the rest of that program. 27 | 28 | # This file is maintained in Automake, please report 29 | # bugs to or send patches to 30 | # . 31 | 32 | case $1 in 33 | '') 34 | echo "$0: No command. Try \`$0 --help' for more information." 1>&2 35 | exit 1; 36 | ;; 37 | -h | --h*) 38 | cat <<\EOF 39 | Usage: compile [--help] [--version] PROGRAM [ARGS] 40 | 41 | Wrapper for compilers which do not understand `-c -o'. 42 | Remove `-o dest.o' from ARGS, run PROGRAM with the remaining 43 | arguments, and rename the output as expected. 44 | 45 | If you are trying to build a whole package this is not the 46 | right script to run: please start by reading the file `INSTALL'. 47 | 48 | Report bugs to . 49 | EOF 50 | exit $? 51 | ;; 52 | -v | --v*) 53 | echo "compile $scriptversion" 54 | exit $? 55 | ;; 56 | esac 57 | 58 | ofile= 59 | cfile= 60 | eat= 61 | 62 | for arg 63 | do 64 | if test -n "$eat"; then 65 | eat= 66 | else 67 | case $1 in 68 | -o) 69 | # configure might choose to run compile as `compile cc -o foo foo.c'. 70 | # So we strip `-o arg' only if arg is an object. 71 | eat=1 72 | case $2 in 73 | *.o | *.obj) 74 | ofile=$2 75 | ;; 76 | *) 77 | set x "$@" -o "$2" 78 | shift 79 | ;; 80 | esac 81 | ;; 82 | *.c) 83 | cfile=$1 84 | set x "$@" "$1" 85 | shift 86 | ;; 87 | *) 88 | set x "$@" "$1" 89 | shift 90 | ;; 91 | esac 92 | fi 93 | shift 94 | done 95 | 96 | if test -z "$ofile" || test -z "$cfile"; then 97 | # If no `-o' option was seen then we might have been invoked from a 98 | # pattern rule where we don't need one. That is ok -- this is a 99 | # normal compilation that the losing compiler can handle. If no 100 | # `.c' file was seen then we are probably linking. That is also 101 | # ok. 102 | exec "$@" 103 | fi 104 | 105 | # Name of file we expect compiler to create. 106 | cofile=`echo "$cfile" | sed -e 's|^.*/||' -e 's/\.c$/.o/'` 107 | 108 | # Create the lock directory. 109 | # Note: use `[/.-]' here to ensure that we don't use the same name 110 | # that we are using for the .o file. Also, base the name on the expected 111 | # object file name, since that is what matters with a parallel build. 112 | lockdir=`echo "$cofile" | sed -e 's|[/.-]|_|g'`.d 113 | while true; do 114 | if mkdir "$lockdir" >/dev/null 2>&1; then 115 | break 116 | fi 117 | sleep 1 118 | done 119 | # FIXME: race condition here if user kills between mkdir and trap. 120 | trap "rmdir '$lockdir'; exit 1" 1 2 15 121 | 122 | # Run the compile. 123 | "$@" 124 | ret=$? 125 | 126 | if test -f "$cofile"; then 127 | mv "$cofile" "$ofile" 128 | elif test -f "${cofile}bj"; then 129 | mv "${cofile}bj" "$ofile" 130 | fi 131 | 132 | rmdir "$lockdir" 133 | exit $ret 134 | 135 | # Local Variables: 136 | # mode: shell-script 137 | # sh-indentation: 2 138 | # eval: (add-hook 'write-file-hooks 'time-stamp) 139 | # time-stamp-start: "scriptversion=" 140 | # time-stamp-format: "%:y-%02m-%02d.%02H" 141 | # time-stamp-end: "$" 142 | # End: 143 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/compile: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | # Wrapper for compilers which do not understand `-c -o'. 3 | 4 | scriptversion=2005-05-14.22 5 | 6 | # Copyright (C) 1999, 2000, 2003, 2004, 2005 Free Software Foundation, Inc. 7 | # Written by Tom Tromey . 8 | # 9 | # This program is free software; you can redistribute it and/or modify 10 | # it under the terms of the GNU General Public License as published by 11 | # the Free Software Foundation; either version 2, or (at your option) 12 | # any later version. 13 | # 14 | # This program is distributed in the hope that it will be useful, 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | # GNU General Public License for more details. 18 | # 19 | # You should have received a copy of the GNU General Public License 20 | # along with this program; if not, write to the Free Software 21 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 22 | 23 | # As a special exception to the GNU General Public License, if you 24 | # distribute this file as part of a program that contains a 25 | # configuration script generated by Autoconf, you may include it under 26 | # the same distribution terms that you use for the rest of that program. 27 | 28 | # This file is maintained in Automake, please report 29 | # bugs to or send patches to 30 | # . 31 | 32 | case $1 in 33 | '') 34 | echo "$0: No command. Try \`$0 --help' for more information." 1>&2 35 | exit 1; 36 | ;; 37 | -h | --h*) 38 | cat <<\EOF 39 | Usage: compile [--help] [--version] PROGRAM [ARGS] 40 | 41 | Wrapper for compilers which do not understand `-c -o'. 42 | Remove `-o dest.o' from ARGS, run PROGRAM with the remaining 43 | arguments, and rename the output as expected. 44 | 45 | If you are trying to build a whole package this is not the 46 | right script to run: please start by reading the file `INSTALL'. 47 | 48 | Report bugs to . 49 | EOF 50 | exit $? 51 | ;; 52 | -v | --v*) 53 | echo "compile $scriptversion" 54 | exit $? 55 | ;; 56 | esac 57 | 58 | ofile= 59 | cfile= 60 | eat= 61 | 62 | for arg 63 | do 64 | if test -n "$eat"; then 65 | eat= 66 | else 67 | case $1 in 68 | -o) 69 | # configure might choose to run compile as `compile cc -o foo foo.c'. 70 | # So we strip `-o arg' only if arg is an object. 71 | eat=1 72 | case $2 in 73 | *.o | *.obj) 74 | ofile=$2 75 | ;; 76 | *) 77 | set x "$@" -o "$2" 78 | shift 79 | ;; 80 | esac 81 | ;; 82 | *.c) 83 | cfile=$1 84 | set x "$@" "$1" 85 | shift 86 | ;; 87 | *) 88 | set x "$@" "$1" 89 | shift 90 | ;; 91 | esac 92 | fi 93 | shift 94 | done 95 | 96 | if test -z "$ofile" || test -z "$cfile"; then 97 | # If no `-o' option was seen then we might have been invoked from a 98 | # pattern rule where we don't need one. That is ok -- this is a 99 | # normal compilation that the losing compiler can handle. If no 100 | # `.c' file was seen then we are probably linking. That is also 101 | # ok. 102 | exec "$@" 103 | fi 104 | 105 | # Name of file we expect compiler to create. 106 | cofile=`echo "$cfile" | sed -e 's|^.*/||' -e 's/\.c$/.o/'` 107 | 108 | # Create the lock directory. 109 | # Note: use `[/.-]' here to ensure that we don't use the same name 110 | # that we are using for the .o file. Also, base the name on the expected 111 | # object file name, since that is what matters with a parallel build. 112 | lockdir=`echo "$cofile" | sed -e 's|[/.-]|_|g'`.d 113 | while true; do 114 | if mkdir "$lockdir" >/dev/null 2>&1; then 115 | break 116 | fi 117 | sleep 1 118 | done 119 | # FIXME: race condition here if user kills between mkdir and trap. 120 | trap "rmdir '$lockdir'; exit 1" 1 2 15 121 | 122 | # Run the compile. 123 | "$@" 124 | ret=$? 125 | 126 | if test -f "$cofile"; then 127 | mv "$cofile" "$ofile" 128 | elif test -f "${cofile}bj"; then 129 | mv "${cofile}bj" "$ofile" 130 | fi 131 | 132 | rmdir "$lockdir" 133 | exit $ret 134 | 135 | # Local Variables: 136 | # mode: shell-script 137 | # sh-indentation: 2 138 | # eval: (add-hook 'write-file-hooks 'time-stamp) 139 | # time-stamp-start: "scriptversion=" 140 | # time-stamp-format: "%:y-%02m-%02d.%02H" 141 | # time-stamp-end: "$" 142 | # End: 143 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/src/config.h: -------------------------------------------------------------------------------- 1 | /* src/config.h. Generated by configure. */ 2 | /* src/config.h.in. Generated from configure.ac by autoheader. */ 3 | 4 | /* Namespace for Google classes */ 5 | #define GOOGLE_NAMESPACE ::google 6 | 7 | /* Define to 1 if you have the header file. */ 8 | #define HAVE_ASSERT_H 1 9 | 10 | /* Define to 1 if you have the `calloc' function. */ 11 | #define HAVE_CALLOC 1 12 | 13 | /* Define to 1 if you have the header file. */ 14 | #define HAVE_DLFCN_H 1 15 | 16 | /* Define to 1 if you have the `exit' function. */ 17 | #define HAVE_EXIT 1 18 | 19 | /* Define to 1 if you have the `fprintf' function. */ 20 | #define HAVE_FPRINTF 1 21 | 22 | /* Define to 1 if you have the `free' function. */ 23 | #define HAVE_FREE 1 24 | 25 | /* Define to 1 if you have the header file. */ 26 | #define HAVE_INTTYPES_H 1 27 | 28 | /* Define to 1 if you have the `longjmp' function. */ 29 | #define HAVE_LONGJMP 1 30 | 31 | /* Define to 1 if you have the `malloc' function. */ 32 | #define HAVE_MALLOC 1 33 | 34 | /* Define to 1 if you have the header file. */ 35 | #define HAVE_MALLOC_H 1 36 | 37 | /* Define to 1 if you have the `memcpy' function. */ 38 | #define HAVE_MEMCPY 1 39 | 40 | /* Define to 1 if you have the header file. */ 41 | #define HAVE_MEMORY_H 1 42 | 43 | /* Define to 1 if you have the `memset' function. */ 44 | #define HAVE_MEMSET 1 45 | 46 | /* define if the compiler implements namespaces */ 47 | #define HAVE_NAMESPACES 1 48 | 49 | /* Define to 1 if you have the `printf' function. */ 50 | #define HAVE_PRINTF 1 51 | 52 | /* Define to 1 if you have the `setjmp' function. */ 53 | #define HAVE_SETJMP 1 54 | 55 | /* Define to 1 if you have the header file. */ 56 | #define HAVE_SETJMP_H 1 57 | 58 | /* Define to 1 if you have the `signal' function. */ 59 | #define HAVE_SIGNAL 1 60 | 61 | /* Define to 1 if you have the header file. */ 62 | #define HAVE_SIGNAL_H 1 63 | 64 | /* Define to 1 if you have the `snprintf' function. */ 65 | #define HAVE_SNPRINTF 1 66 | 67 | /* Define to 1 if you have the header file. */ 68 | #define HAVE_STDARG_H 1 69 | 70 | /* Define to 1 if you have the header file. */ 71 | #define HAVE_STDDEF_H 1 72 | 73 | /* Define to 1 if you have the header file. */ 74 | #define HAVE_STDINT_H 1 75 | 76 | /* Define to 1 if you have the header file. */ 77 | #define HAVE_STDIO_H 1 78 | 79 | /* Define to 1 if you have the header file. */ 80 | #define HAVE_STDLIB_H 1 81 | 82 | /* Define to 1 if you have the `strcmp' function. */ 83 | #define HAVE_STRCMP 1 84 | 85 | /* Define to 1 if you have the `strcpy' function. */ 86 | #define HAVE_STRCPY 1 87 | 88 | /* Define to 1 if you have the header file. */ 89 | #define HAVE_STRINGS_H 1 90 | 91 | /* Define to 1 if you have the header file. */ 92 | #define HAVE_STRING_H 1 93 | 94 | /* Define to 1 if you have the header file. */ 95 | #define HAVE_SYS_STAT_H 1 96 | 97 | /* Define to 1 if you have the header file. */ 98 | #define HAVE_SYS_TYPES_H 1 99 | 100 | /* Define to 1 if you have the header file. */ 101 | #define HAVE_UNISTD_H 1 102 | 103 | /* Define to 1 if you have the `vsnprintf' function. */ 104 | #define HAVE_VSNPRINTF 1 105 | 106 | /* Name of package */ 107 | #define PACKAGE "cmockery" 108 | 109 | /* Define to the address where bug reports for this package should be sent. */ 110 | #define PACKAGE_BUGREPORT "opensource@google.com" 111 | 112 | /* Define to the full name of this package. */ 113 | #define PACKAGE_NAME "cmockery" 114 | 115 | /* Define to the full name and version of this package. */ 116 | #define PACKAGE_STRING "cmockery 0.1.2" 117 | 118 | /* Define to the one symbol short name of this package. */ 119 | #define PACKAGE_TARNAME "cmockery" 120 | 121 | /* Define to the version of this package. */ 122 | #define PACKAGE_VERSION "0.1.2" 123 | 124 | /* Define to 1 if you have the ANSI C header files. */ 125 | #define STDC_HEADERS 1 126 | 127 | /* the namespace where STL code like vector<> is defined */ 128 | #define STL_NAMESPACE std 129 | 130 | /* Version number of package */ 131 | #define VERSION "0.1.2" 132 | 133 | /* Stops putting the code inside the Google namespace */ 134 | #define _END_GOOGLE_NAMESPACE_ } 135 | 136 | /* Puts following code inside the Google namespace */ 137 | #define _START_GOOGLE_NAMESPACE_ namespace google { 138 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/src/config.h.in: -------------------------------------------------------------------------------- 1 | /* src/config.h.in. Generated from configure.ac by autoheader. */ 2 | 3 | /* Namespace for Google classes */ 4 | #undef GOOGLE_NAMESPACE 5 | 6 | /* Define to 1 if you have the header file. */ 7 | #undef HAVE_ASSERT_H 8 | 9 | /* Define to 1 if you have the `calloc' function. */ 10 | #undef HAVE_CALLOC 11 | 12 | /* Define to 1 if you have the header file. */ 13 | #undef HAVE_DLFCN_H 14 | 15 | /* Define to 1 if you have the `exit' function. */ 16 | #undef HAVE_EXIT 17 | 18 | /* Define to 1 if you have the `fprintf' function. */ 19 | #undef HAVE_FPRINTF 20 | 21 | /* Define to 1 if you have the `free' function. */ 22 | #undef HAVE_FREE 23 | 24 | /* Define to 1 if you have the header file. */ 25 | #undef HAVE_INTTYPES_H 26 | 27 | /* Define to 1 if you have the `longjmp' function. */ 28 | #undef HAVE_LONGJMP 29 | 30 | /* Define to 1 if you have the `malloc' function. */ 31 | #undef HAVE_MALLOC 32 | 33 | /* Define to 1 if you have the header file. */ 34 | #undef HAVE_MALLOC_H 35 | 36 | /* Define to 1 if you have the `memcpy' function. */ 37 | #undef HAVE_MEMCPY 38 | 39 | /* Define to 1 if you have the header file. */ 40 | #undef HAVE_MEMORY_H 41 | 42 | /* Define to 1 if you have the `memset' function. */ 43 | #undef HAVE_MEMSET 44 | 45 | /* define if the compiler implements namespaces */ 46 | #undef HAVE_NAMESPACES 47 | 48 | /* Define to 1 if you have the `printf' function. */ 49 | #undef HAVE_PRINTF 50 | 51 | /* Define to 1 if you have the `setjmp' function. */ 52 | #undef HAVE_SETJMP 53 | 54 | /* Define to 1 if you have the header file. */ 55 | #undef HAVE_SETJMP_H 56 | 57 | /* Define to 1 if you have the `signal' function. */ 58 | #undef HAVE_SIGNAL 59 | 60 | /* Define to 1 if you have the header file. */ 61 | #undef HAVE_SIGNAL_H 62 | 63 | /* Define to 1 if you have the `snprintf' function. */ 64 | #undef HAVE_SNPRINTF 65 | 66 | /* Define to 1 if you have the header file. */ 67 | #undef HAVE_STDARG_H 68 | 69 | /* Define to 1 if you have the header file. */ 70 | #undef HAVE_STDDEF_H 71 | 72 | /* Define to 1 if you have the header file. */ 73 | #undef HAVE_STDINT_H 74 | 75 | /* Define to 1 if you have the header file. */ 76 | #undef HAVE_STDIO_H 77 | 78 | /* Define to 1 if you have the header file. */ 79 | #undef HAVE_STDLIB_H 80 | 81 | /* Define to 1 if you have the `strcmp' function. */ 82 | #undef HAVE_STRCMP 83 | 84 | /* Define to 1 if you have the `strcpy' function. */ 85 | #undef HAVE_STRCPY 86 | 87 | /* Define to 1 if you have the header file. */ 88 | #undef HAVE_STRINGS_H 89 | 90 | /* Define to 1 if you have the header file. */ 91 | #undef HAVE_STRING_H 92 | 93 | /* Define to 1 if you have the header file. */ 94 | #undef HAVE_SYS_STAT_H 95 | 96 | /* Define to 1 if you have the header file. */ 97 | #undef HAVE_SYS_TYPES_H 98 | 99 | /* Define to 1 if you have the header file. */ 100 | #undef HAVE_UNISTD_H 101 | 102 | /* Define to 1 if you have the `vsnprintf' function. */ 103 | #undef HAVE_VSNPRINTF 104 | 105 | /* Define to the sub-directory in which libtool stores uninstalled libraries. 106 | */ 107 | #undef LT_OBJDIR 108 | 109 | /* Define to 1 if your C compiler doesn't accept -c and -o together. */ 110 | #undef NO_MINUS_C_MINUS_O 111 | 112 | /* Name of package */ 113 | #undef PACKAGE 114 | 115 | /* Define to the address where bug reports for this package should be sent. */ 116 | #undef PACKAGE_BUGREPORT 117 | 118 | /* Define to the full name of this package. */ 119 | #undef PACKAGE_NAME 120 | 121 | /* Define to the full name and version of this package. */ 122 | #undef PACKAGE_STRING 123 | 124 | /* Define to the one symbol short name of this package. */ 125 | #undef PACKAGE_TARNAME 126 | 127 | /* Define to the home page for this package. */ 128 | #undef PACKAGE_URL 129 | 130 | /* Define to the version of this package. */ 131 | #undef PACKAGE_VERSION 132 | 133 | /* Define to 1 if you have the ANSI C header files. */ 134 | #undef STDC_HEADERS 135 | 136 | /* the namespace where STL code like vector<> is defined */ 137 | #undef STL_NAMESPACE 138 | 139 | /* Version number of package */ 140 | #undef VERSION 141 | 142 | /* Stops putting the code inside the Google namespace */ 143 | #undef _END_GOOGLE_NAMESPACE_ 144 | 145 | /* Puts following code inside the Google namespace */ 146 | #undef _START_GOOGLE_NAMESPACE_ 147 | -------------------------------------------------------------------------------- /libplinkio/tests/fam_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include "mock.h" 16 | 17 | /** 18 | * Tests that iids are correctly parsed. 19 | */ 20 | void 21 | test_parse_iid(void **state) 22 | { 23 | const char *TEST_STRING = "F1"; 24 | pio_status_t status; 25 | char *iid = parse_iid( TEST_STRING, strlen( TEST_STRING ), &status ); 26 | 27 | assert_int_equal( status, PIO_OK ); 28 | assert_string_equal( iid, TEST_STRING ); 29 | free( iid ); 30 | } 31 | 32 | /** 33 | * Tests that sex is correctly parsed. 34 | */ 35 | void 36 | test_parse_sex(void **state) 37 | { 38 | const char *TEST_STRING_MALE = "1"; 39 | const char *TEST_STRING_FEMALE = "2"; 40 | const char *TEST_STRING_UNKNOWN = "0"; 41 | pio_status_t status; 42 | enum sex_t sex; 43 | 44 | sex = parse_sex( TEST_STRING_MALE, strlen( TEST_STRING_MALE ), &status ); 45 | assert_int_equal( status, PIO_OK ); 46 | assert_int_equal( sex, PIO_MALE ); 47 | 48 | sex = parse_sex( TEST_STRING_FEMALE, strlen( TEST_STRING_FEMALE ), &status ); 49 | assert_int_equal( status, PIO_OK ); 50 | assert_int_equal( sex, PIO_FEMALE ); 51 | 52 | sex = parse_sex( TEST_STRING_UNKNOWN, strlen( TEST_STRING_UNKNOWN ), &status ); 53 | assert_int_equal( status, PIO_OK ); 54 | assert_int_equal( sex, PIO_UNKNOWN ); 55 | } 56 | 57 | /** 58 | * Tests that a phenotype is correctly parsed. 59 | */ 60 | void 61 | test_parse_phenotype(void **state) 62 | { 63 | const char *TEST_STRING_CONTROL = "1"; 64 | const char *TEST_STRING_CASE = "2"; 65 | const char *TEST_STRING_PHENOTYPE = "1.0"; 66 | struct pio_sample_t sample; 67 | pio_status_t status; 68 | 69 | parse_phenotype( TEST_STRING_CONTROL, strlen( TEST_STRING_CONTROL ), &sample, &status ); 70 | assert_int_equal( status, PIO_OK ); 71 | assert_int_equal( sample.affection, PIO_CONTROL ); 72 | 73 | parse_phenotype( TEST_STRING_CASE, strlen( TEST_STRING_CASE ), &sample, &status ); 74 | assert_int_equal( status, PIO_OK ); 75 | assert_int_equal( sample.affection, PIO_CASE ); 76 | 77 | parse_phenotype( TEST_STRING_PHENOTYPE, strlen( TEST_STRING_PHENOTYPE ), &sample, &status ); 78 | assert_int_equal( status, PIO_OK ); 79 | assert_int_equal( sample.affection, PIO_CONTINUOUS ); 80 | assert_true( fabs( sample.phenotype - 1.0 ) <= 1e-6 ); 81 | } 82 | 83 | /** 84 | * Tests the parsing of multiple samples. Since parse_samples uses 85 | * IO functions, we need to have mocked versions for these. 86 | */ 87 | void 88 | test_parse_multiple_samples(void **state) 89 | { 90 | struct pio_sample_t person; 91 | struct pio_fam_file_t fam_file; 92 | 93 | 94 | mock_init( "F1 P1 0 0 1 1\nF1\t P2 0 0 2 2" ); 95 | assert_int_equal( fam_open( &fam_file, "" ), PIO_OK ); 96 | assert_int_equal( fam_num_samples( &fam_file ), 2 ); 97 | 98 | person = *fam_get_sample( &fam_file, 0 ); 99 | assert_string_equal( person.fid, "F1" ); 100 | assert_string_equal( person.iid, "P1" ); 101 | assert_string_equal( person.father_iid, "0" ); 102 | assert_string_equal( person.mother_iid, "0" ); 103 | assert_int_equal( person.sex, PIO_MALE ); 104 | assert_int_equal( person.affection, PIO_CONTROL ); 105 | 106 | person = *fam_get_sample( &fam_file, 1 ); 107 | assert_string_equal( person.fid, "F1" ); 108 | assert_string_equal( person.iid, "P2" ); 109 | assert_string_equal( person.father_iid, "0" ); 110 | assert_string_equal( person.mother_iid, "0" ); 111 | assert_int_equal( person.sex, PIO_FEMALE ); 112 | assert_int_equal( person.affection, PIO_CASE ); 113 | 114 | fam_close( &fam_file ); 115 | } 116 | 117 | /** 118 | * Cmockerys initial implementation couldn't handle realloc, 119 | * this test make sure that it works as intended. 120 | */ 121 | void 122 | test_utarray(void **state) 123 | { 124 | UT_array *samples; 125 | struct pio_sample_t person1 = {0}; 126 | struct pio_sample_t person2 = {0}; 127 | utarray_new( samples, &SAMPLE_ICD ); 128 | 129 | utarray_push_back( samples, &person1 ); 130 | utarray_push_back( samples, &person2 ); 131 | 132 | utarray_free( samples ); 133 | } 134 | 135 | int main(int argc, char* argv[]) 136 | { 137 | const UnitTest tests[] = { 138 | unit_test( test_parse_iid ), 139 | unit_test( test_parse_sex ), 140 | unit_test( test_parse_phenotype ), 141 | unit_test( test_parse_multiple_samples ), 142 | unit_test( test_utarray ), 143 | }; 144 | 145 | return run_tests( tests ); 146 | } 147 | -------------------------------------------------------------------------------- /libplinkio/m4/ltsugar.m4: -------------------------------------------------------------------------------- 1 | # ltsugar.m4 -- libtool m4 base layer. -*-Autoconf-*- 2 | # 3 | # Copyright (C) 2004, 2005, 2007, 2008 Free Software Foundation, Inc. 4 | # Written by Gary V. Vaughan, 2004 5 | # 6 | # This file is free software; the Free Software Foundation gives 7 | # unlimited permission to copy and/or distribute it, with or without 8 | # modifications, as long as this notice is preserved. 9 | 10 | # serial 6 ltsugar.m4 11 | 12 | # This is to help aclocal find these macros, as it can't see m4_define. 13 | AC_DEFUN([LTSUGAR_VERSION], [m4_if([0.1])]) 14 | 15 | 16 | # lt_join(SEP, ARG1, [ARG2...]) 17 | # ----------------------------- 18 | # Produce ARG1SEPARG2...SEPARGn, omitting [] arguments and their 19 | # associated separator. 20 | # Needed until we can rely on m4_join from Autoconf 2.62, since all earlier 21 | # versions in m4sugar had bugs. 22 | m4_define([lt_join], 23 | [m4_if([$#], [1], [], 24 | [$#], [2], [[$2]], 25 | [m4_if([$2], [], [], [[$2]_])$0([$1], m4_shift(m4_shift($@)))])]) 26 | m4_define([_lt_join], 27 | [m4_if([$#$2], [2], [], 28 | [m4_if([$2], [], [], [[$1$2]])$0([$1], m4_shift(m4_shift($@)))])]) 29 | 30 | 31 | # lt_car(LIST) 32 | # lt_cdr(LIST) 33 | # ------------ 34 | # Manipulate m4 lists. 35 | # These macros are necessary as long as will still need to support 36 | # Autoconf-2.59 which quotes differently. 37 | m4_define([lt_car], [[$1]]) 38 | m4_define([lt_cdr], 39 | [m4_if([$#], 0, [m4_fatal([$0: cannot be called without arguments])], 40 | [$#], 1, [], 41 | [m4_dquote(m4_shift($@))])]) 42 | m4_define([lt_unquote], $1) 43 | 44 | 45 | # lt_append(MACRO-NAME, STRING, [SEPARATOR]) 46 | # ------------------------------------------ 47 | # Redefine MACRO-NAME to hold its former content plus `SEPARATOR'`STRING'. 48 | # Note that neither SEPARATOR nor STRING are expanded; they are appended 49 | # to MACRO-NAME as is (leaving the expansion for when MACRO-NAME is invoked). 50 | # No SEPARATOR is output if MACRO-NAME was previously undefined (different 51 | # than defined and empty). 52 | # 53 | # This macro is needed until we can rely on Autoconf 2.62, since earlier 54 | # versions of m4sugar mistakenly expanded SEPARATOR but not STRING. 55 | m4_define([lt_append], 56 | [m4_define([$1], 57 | m4_ifdef([$1], [m4_defn([$1])[$3]])[$2])]) 58 | 59 | 60 | 61 | # lt_combine(SEP, PREFIX-LIST, INFIX, SUFFIX1, [SUFFIX2...]) 62 | # ---------------------------------------------------------- 63 | # Produce a SEP delimited list of all paired combinations of elements of 64 | # PREFIX-LIST with SUFFIX1 through SUFFIXn. Each element of the list 65 | # has the form PREFIXmINFIXSUFFIXn. 66 | # Needed until we can rely on m4_combine added in Autoconf 2.62. 67 | m4_define([lt_combine], 68 | [m4_if(m4_eval([$# > 3]), [1], 69 | [m4_pushdef([_Lt_sep], [m4_define([_Lt_sep], m4_defn([lt_car]))])]]dnl 70 | [[m4_foreach([_Lt_prefix], [$2], 71 | [m4_foreach([_Lt_suffix], 72 | ]m4_dquote(m4_dquote(m4_shift(m4_shift(m4_shift($@)))))[, 73 | [_Lt_sep([$1])[]m4_defn([_Lt_prefix])[$3]m4_defn([_Lt_suffix])])])])]) 74 | 75 | 76 | # lt_if_append_uniq(MACRO-NAME, VARNAME, [SEPARATOR], [UNIQ], [NOT-UNIQ]) 77 | # ----------------------------------------------------------------------- 78 | # Iff MACRO-NAME does not yet contain VARNAME, then append it (delimited 79 | # by SEPARATOR if supplied) and expand UNIQ, else NOT-UNIQ. 80 | m4_define([lt_if_append_uniq], 81 | [m4_ifdef([$1], 82 | [m4_if(m4_index([$3]m4_defn([$1])[$3], [$3$2$3]), [-1], 83 | [lt_append([$1], [$2], [$3])$4], 84 | [$5])], 85 | [lt_append([$1], [$2], [$3])$4])]) 86 | 87 | 88 | # lt_dict_add(DICT, KEY, VALUE) 89 | # ----------------------------- 90 | m4_define([lt_dict_add], 91 | [m4_define([$1($2)], [$3])]) 92 | 93 | 94 | # lt_dict_add_subkey(DICT, KEY, SUBKEY, VALUE) 95 | # -------------------------------------------- 96 | m4_define([lt_dict_add_subkey], 97 | [m4_define([$1($2:$3)], [$4])]) 98 | 99 | 100 | # lt_dict_fetch(DICT, KEY, [SUBKEY]) 101 | # ---------------------------------- 102 | m4_define([lt_dict_fetch], 103 | [m4_ifval([$3], 104 | m4_ifdef([$1($2:$3)], [m4_defn([$1($2:$3)])]), 105 | m4_ifdef([$1($2)], [m4_defn([$1($2)])]))]) 106 | 107 | 108 | # lt_if_dict_fetch(DICT, KEY, [SUBKEY], VALUE, IF-TRUE, [IF-FALSE]) 109 | # ----------------------------------------------------------------- 110 | m4_define([lt_if_dict_fetch], 111 | [m4_if(lt_dict_fetch([$1], [$2], [$3]), [$4], 112 | [$5], 113 | [$6])]) 114 | 115 | 116 | # lt_dict_filter(DICT, [SUBKEY], VALUE, [SEPARATOR], KEY, [...]) 117 | # -------------------------------------------------------------- 118 | m4_define([lt_dict_filter], 119 | [m4_if([$5], [], [], 120 | [lt_join(m4_quote(m4_default([$4], [[, ]])), 121 | lt_unquote(m4_split(m4_normalize(m4_foreach(_Lt_key, lt_car([m4_shiftn(4, $@)]), 122 | [lt_if_dict_fetch([$1], _Lt_key, [$2], [$3], [_Lt_key ])])))))])[]dnl 123 | ]) 124 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/m4/ltsugar.m4: -------------------------------------------------------------------------------- 1 | # ltsugar.m4 -- libtool m4 base layer. -*-Autoconf-*- 2 | # 3 | # Copyright (C) 2004, 2005, 2007, 2008 Free Software Foundation, Inc. 4 | # Written by Gary V. Vaughan, 2004 5 | # 6 | # This file is free software; the Free Software Foundation gives 7 | # unlimited permission to copy and/or distribute it, with or without 8 | # modifications, as long as this notice is preserved. 9 | 10 | # serial 6 ltsugar.m4 11 | 12 | # This is to help aclocal find these macros, as it can't see m4_define. 13 | AC_DEFUN([LTSUGAR_VERSION], [m4_if([0.1])]) 14 | 15 | 16 | # lt_join(SEP, ARG1, [ARG2...]) 17 | # ----------------------------- 18 | # Produce ARG1SEPARG2...SEPARGn, omitting [] arguments and their 19 | # associated separator. 20 | # Needed until we can rely on m4_join from Autoconf 2.62, since all earlier 21 | # versions in m4sugar had bugs. 22 | m4_define([lt_join], 23 | [m4_if([$#], [1], [], 24 | [$#], [2], [[$2]], 25 | [m4_if([$2], [], [], [[$2]_])$0([$1], m4_shift(m4_shift($@)))])]) 26 | m4_define([_lt_join], 27 | [m4_if([$#$2], [2], [], 28 | [m4_if([$2], [], [], [[$1$2]])$0([$1], m4_shift(m4_shift($@)))])]) 29 | 30 | 31 | # lt_car(LIST) 32 | # lt_cdr(LIST) 33 | # ------------ 34 | # Manipulate m4 lists. 35 | # These macros are necessary as long as will still need to support 36 | # Autoconf-2.59 which quotes differently. 37 | m4_define([lt_car], [[$1]]) 38 | m4_define([lt_cdr], 39 | [m4_if([$#], 0, [m4_fatal([$0: cannot be called without arguments])], 40 | [$#], 1, [], 41 | [m4_dquote(m4_shift($@))])]) 42 | m4_define([lt_unquote], $1) 43 | 44 | 45 | # lt_append(MACRO-NAME, STRING, [SEPARATOR]) 46 | # ------------------------------------------ 47 | # Redefine MACRO-NAME to hold its former content plus `SEPARATOR'`STRING'. 48 | # Note that neither SEPARATOR nor STRING are expanded; they are appended 49 | # to MACRO-NAME as is (leaving the expansion for when MACRO-NAME is invoked). 50 | # No SEPARATOR is output if MACRO-NAME was previously undefined (different 51 | # than defined and empty). 52 | # 53 | # This macro is needed until we can rely on Autoconf 2.62, since earlier 54 | # versions of m4sugar mistakenly expanded SEPARATOR but not STRING. 55 | m4_define([lt_append], 56 | [m4_define([$1], 57 | m4_ifdef([$1], [m4_defn([$1])[$3]])[$2])]) 58 | 59 | 60 | 61 | # lt_combine(SEP, PREFIX-LIST, INFIX, SUFFIX1, [SUFFIX2...]) 62 | # ---------------------------------------------------------- 63 | # Produce a SEP delimited list of all paired combinations of elements of 64 | # PREFIX-LIST with SUFFIX1 through SUFFIXn. Each element of the list 65 | # has the form PREFIXmINFIXSUFFIXn. 66 | # Needed until we can rely on m4_combine added in Autoconf 2.62. 67 | m4_define([lt_combine], 68 | [m4_if(m4_eval([$# > 3]), [1], 69 | [m4_pushdef([_Lt_sep], [m4_define([_Lt_sep], m4_defn([lt_car]))])]]dnl 70 | [[m4_foreach([_Lt_prefix], [$2], 71 | [m4_foreach([_Lt_suffix], 72 | ]m4_dquote(m4_dquote(m4_shift(m4_shift(m4_shift($@)))))[, 73 | [_Lt_sep([$1])[]m4_defn([_Lt_prefix])[$3]m4_defn([_Lt_suffix])])])])]) 74 | 75 | 76 | # lt_if_append_uniq(MACRO-NAME, VARNAME, [SEPARATOR], [UNIQ], [NOT-UNIQ]) 77 | # ----------------------------------------------------------------------- 78 | # Iff MACRO-NAME does not yet contain VARNAME, then append it (delimited 79 | # by SEPARATOR if supplied) and expand UNIQ, else NOT-UNIQ. 80 | m4_define([lt_if_append_uniq], 81 | [m4_ifdef([$1], 82 | [m4_if(m4_index([$3]m4_defn([$1])[$3], [$3$2$3]), [-1], 83 | [lt_append([$1], [$2], [$3])$4], 84 | [$5])], 85 | [lt_append([$1], [$2], [$3])$4])]) 86 | 87 | 88 | # lt_dict_add(DICT, KEY, VALUE) 89 | # ----------------------------- 90 | m4_define([lt_dict_add], 91 | [m4_define([$1($2)], [$3])]) 92 | 93 | 94 | # lt_dict_add_subkey(DICT, KEY, SUBKEY, VALUE) 95 | # -------------------------------------------- 96 | m4_define([lt_dict_add_subkey], 97 | [m4_define([$1($2:$3)], [$4])]) 98 | 99 | 100 | # lt_dict_fetch(DICT, KEY, [SUBKEY]) 101 | # ---------------------------------- 102 | m4_define([lt_dict_fetch], 103 | [m4_ifval([$3], 104 | m4_ifdef([$1($2:$3)], [m4_defn([$1($2:$3)])]), 105 | m4_ifdef([$1($2)], [m4_defn([$1($2)])]))]) 106 | 107 | 108 | # lt_if_dict_fetch(DICT, KEY, [SUBKEY], VALUE, IF-TRUE, [IF-FALSE]) 109 | # ----------------------------------------------------------------- 110 | m4_define([lt_if_dict_fetch], 111 | [m4_if(lt_dict_fetch([$1], [$2], [$3]), [$4], 112 | [$5], 113 | [$6])]) 114 | 115 | 116 | # lt_dict_filter(DICT, [SUBKEY], VALUE, [SEPARATOR], KEY, [...]) 117 | # -------------------------------------------------------------- 118 | m4_define([lt_dict_filter], 119 | [m4_if([$5], [], [], 120 | [lt_join(m4_quote(m4_default([$4], [[, ]])), 121 | lt_unquote(m4_split(m4_normalize(m4_foreach(_Lt_key, lt_car([m4_shiftn(4, $@)]), 122 | [lt_if_dict_fetch([$1], _Lt_key, [$2], [$3], [_Lt_key ])])))))])[]dnl 123 | ]) 124 | -------------------------------------------------------------------------------- /libplinkio/setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages, Extension 2 | from codecs import open 3 | from os import path 4 | import os 5 | import glob 6 | 7 | here = path.abspath(path.dirname(__file__)) 8 | 9 | # Get the long description from the relevant file 10 | with open(path.join(here, 'README.rst'), encoding='utf-8') as f: 11 | long_description = f.read() 12 | 13 | libplinkio_src_dir = "src" 14 | libplinkio_include_dir = os.path.join( libplinkio_src_dir ) 15 | libplinkio_src_files = glob.glob( os.path.join( libplinkio_src_dir, "*.c" ) ) 16 | 17 | libcsv_root_dir = os.path.join( "libs", "libcsv" ) 18 | libcsv_src_dir = os.path.join( libcsv_root_dir, "src" ) 19 | libcsv_include_dir = os.path.join( libcsv_root_dir, "inc" ) 20 | libcsv_src_files = glob.glob( os.path.join( libcsv_src_dir, "*.c" ) ) 21 | 22 | pyplinkio_src_dir = "py-plinkio" 23 | pyplinkio_include_dir = pyplinkio_src_dir 24 | pyplinkio_src_files = glob.glob( os.path.join( pyplinkio_src_dir, "*.c" ) ) 25 | 26 | cplinkio = Extension( 27 | "plinkio.cplinkio", 28 | libplinkio_src_files + libcsv_src_files + pyplinkio_src_files, 29 | library_dirs=[], 30 | include_dirs=[libplinkio_include_dir, libcsv_include_dir, pyplinkio_include_dir], 31 | libraries=[], 32 | language="c", 33 | extra_compile_args=[], 34 | define_macros=[] 35 | ) 36 | 37 | setup( 38 | name='plinkio', 39 | 40 | # Versions should comply with PEP440. For a discussion on single-sourcing 41 | # the version across setup.py and the project code, see 42 | # https://packaging.python.org/en/latest/single_source_version.html 43 | version='0.9.5', 44 | 45 | description='A library for parsing plink genotype files', 46 | long_description=long_description, 47 | 48 | # The project's main homepage. 49 | url='https://github.com/fadern/libplinkio', 50 | 51 | # Author details 52 | author='Mattias Franberg', 53 | author_email='mattias.franberg@scilifelab.se', 54 | 55 | # Choose your license 56 | license='BSD', 57 | 58 | # See https://pypi.python.org/pypi?%3Aaction=list_classifiers 59 | classifiers=[ 60 | # How mature is this project? Common values are 61 | # 3 - Alpha 62 | # 4 - Beta 63 | # 5 - Production/Stable 64 | 'Development Status :: 4 - Beta', 65 | 66 | # Indicate who your project is intended for 67 | 'Intended Audience :: Science/Research', 68 | 'Topic :: Scientific/Engineering :: Bio-Informatics', 69 | 70 | # Pick your license as you wish (should match "license" above) 71 | 'License :: OSI Approved :: BSD License', 72 | 73 | # Specify the Python versions you support here. In particular, ensure 74 | # that you indicate whether you support Python 2, Python 3 or both. 75 | 'Programming Language :: Python :: 2', 76 | 'Programming Language :: Python :: 2.7', 77 | 'Programming Language :: Python :: 3', 78 | 'Programming Language :: Python :: 3.2', 79 | 'Programming Language :: Python :: 3.3', 80 | 'Programming Language :: Python :: 3.4', 81 | ], 82 | 83 | # What does your project relate to? 84 | keywords='plinkio bioinformatics genetics', 85 | 86 | # You can just specify the packages manually here if your project is 87 | # simple. Or you can use find_packages(). 88 | packages=find_packages( "py-plinkio" ), 89 | 90 | # List run-time dependencies here. These will be installed by pip when your 91 | # project is installed. For an analysis of "install_requires" vs pip's 92 | # requirements files see: 93 | # https://packaging.python.org/en/latest/requirements.html 94 | install_requires=[], 95 | 96 | # List additional groups of dependencies here (e.g. development dependencies). 97 | # You can install these using the following syntax, for example: 98 | # $ pip install -e .[dev,test] 99 | extras_require = { 100 | }, 101 | 102 | ext_modules = [ cplinkio ], 103 | 104 | # If there are data files included in your packages that need to be 105 | # installed, specify them here. If using Python 2.6 or less, then these 106 | # have to be included in MANIFEST.in as well. 107 | package_data={ 108 | }, 109 | 110 | package_dir={ 111 | '' : "py-plinkio" 112 | }, 113 | 114 | # Although 'package_data' is the preferred approach, in some case you may 115 | # need to place data files outside of your packages. 116 | # see http://docs.python.org/3.4/distutils/setupscript.html#installing-additional-files 117 | # In this case, 'data_file' will be installed into '/my_data' 118 | data_files=[], 119 | 120 | # To provide executable scripts, use entry points in preference to the 121 | # "scripts" keyword. Entry points provide cross-platform support and allow 122 | # pip to create the appropriate form of executable for the target platform. 123 | entry_points={ 124 | }, 125 | ) 126 | -------------------------------------------------------------------------------- /libplinkio/libs/cmockery/Makefile.am: -------------------------------------------------------------------------------- 1 | ## This is a boilerplate file for Google opensource projects. 2 | ## To make it useful, replace <> with actual text for your project. 3 | ## Also, look at comments with "## double hashes" to see if any are worth 4 | ## uncommenting or modifying. 5 | 6 | ## Process this file with automake to produce Makefile.in 7 | 8 | # This is so we can #include 9 | AM_CPPFLAGS = -I$(srcdir)/src 10 | 11 | # Make sure that when we re-make ./configure, we get the macros we need 12 | ACLOCAL_AMFLAGS = -I m4 13 | 14 | # These are good warnings to turn on by default 15 | if GCC 16 | AM_CXXFLAGS = -Wall -Wwrite-strings -Woverloaded-virtual -Wno-sign-compare 17 | endif 18 | 19 | googleincludedir = $(includedir)/google 20 | ## The .h files you want to install (that is, .h files that people 21 | ## who install this package can include in their own applications.) 22 | #googleinclude_HEADERS = src/google/cmockery.h 23 | 24 | docdir = $(prefix)/share/doc/$(PACKAGE)-$(VERSION) 25 | ## This is for HTML and other documentation you want to install. 26 | ## Add your documentation files (in doc/) in addition to these 27 | ## top-level boilerplate files. Also add a TODO file if you have one. 28 | #dist_doc_DATA = AUTHORS COPYING ChangeLog INSTALL NEWS README doc/index.html \ 29 | # windows/makefile 30 | 31 | ## The libraries (.so's) you want to install 32 | lib_LTLIBRARIES = 33 | 34 | ## unittests you want to run when people type 'make check'. 35 | ## TESTS is for binary unittests, check_SCRIPTS for script-based unittests. 36 | ## TESTS_ENVIRONMENT sets environment variables for when you run unittest, 37 | ## but it only seems to take effect for *binary* unittests (argh!) 38 | TESTS = 39 | TESTS_ENVIRONMENT = CMOCKERY_ROOTDIR = $(srcdir) 40 | check_SCRIPTS = 41 | # Every time you add a unittest to check_SCRIPTS, add it here too 42 | noinst_SCRIPTS = 43 | 44 | 45 | ## vvvv RULES TO MAKE THE LIBRARIES, BINARIES, AND UNITTESTS 46 | 47 | noinst_LTLIBRARIES = libcmockery.la 48 | libcmockery_la_SOURCES = src/config.h src/cmockery.c src/google/cmockery.h 49 | libcmockery_la_CFLAGS = -I$(srcdir)/src/google 50 | 51 | noinst_PROGRAMS = calculator 52 | calculator_SOURCES = src/example/calculator.c src/config.h 53 | calculator_CFLAGS = 54 | 55 | unit_test_CFLAGS = -I$(srcdir)/src/google -I$(srcdir)/src/example -DUNIT_TESTING=1 56 | 57 | noinst_PROGRAMS += calculator_test 58 | calculator_test_SOURCES = src/example/calculator_test.c \ 59 | $(calculator_SOURCES) 60 | calculator_test_CFLAGS = $(unit_test_CFLAGS) 61 | calculator_test_LDADD = libcmockery.la 62 | 63 | noinst_PROGRAMS += allocate_module_test 64 | allocate_module_test_SOURCES = src/example/allocate_module_test.c \ 65 | src/example/allocate_module.c 66 | allocate_module_test_CFLAGS = $(unit_test_CFLAGS) 67 | allocate_module_test_LDADD = libcmockery.la 68 | 69 | noinst_PROGRAMS += assert_macro_test 70 | assert_macro_test_SOURCES = src/example/assert_macro_test.c \ 71 | src/example/assert_macro.c 72 | assert_macro_test_CFLAGS = $(unit_test_CFLAGS) 73 | assert_macro_test_LDADD = libcmockery.la 74 | 75 | noinst_PROGRAMS += customer_database_test 76 | customer_database_test_SOURCES = src/example/customer_database_test.c \ 77 | src/example/customer_database.c \ 78 | src/example/database.h 79 | customer_database_test_CFLAGS = $(unit_test_CFLAGS) 80 | customer_database_test_LDADD = libcmockery.la 81 | 82 | noinst_PROGRAMS += key_value_test 83 | key_value_test_SOURCES = src/example/key_value_test.c \ 84 | src/example/key_value.c 85 | key_value_test_CFLAGS = $(unit_test_CFLAGS) 86 | key_value_test_LDADD = libcmockery.la 87 | 88 | noinst_PROGRAMS += product_database_test 89 | product_database_testdir = src/example 90 | product_database_test_SOURCES = src/example/product_database_test.c \ 91 | src/example/product_database.c \ 92 | src/example/database.h 93 | product_database_test_CFLAGS = $(unit_test_CFLAGS) 94 | product_database_test_LDADD = libcmockery.la 95 | 96 | noinst_PROGRAMS += run_tests 97 | run_tests_SOURCES = src/example/run_tests.c 98 | run_tests_CFLAGS = $(unit_test_CFLAGS) 99 | run_tests_LDADD = libcmockery.la 100 | 101 | ## ^^^^ END OF RULES TO MAKE THE LIBRARIES, BINARIES, AND UNITTESTS 102 | 103 | 104 | ## This should always include $(TESTS), but may also include other 105 | ## binaries that you compile but don't want automatically installed. 106 | noinst_PROGRAMS += $(TESTS) 107 | 108 | rpm: dist-gzip packages/rpm.sh packages/rpm/rpm.spec 109 | @cd packages && ./rpm.sh ${PACKAGE} ${VERSION} 110 | 111 | deb: dist-gzip packages/deb.sh packages/deb/* 112 | @cd packages && ./deb.sh ${PACKAGE} ${VERSION} 113 | 114 | ## If you're using libtool, add 'libtool' here. Also add this rule: 115 | libtool: $(LIBTOOL_DEPS) 116 | $(SHELL) ./config.status --recheck 117 | EXTRA_DIST = packages/rpm.sh packages/rpm/rpm.spec packages/deb.sh packages/deb \ 118 | libtool $(SCRIPTS) 119 | -------------------------------------------------------------------------------- /libplinkio/src/plinkio/bed.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012-2013, Mattias Frånberg 3 | * All rights reserved. 4 | * 5 | * This file is distributed under the Modified BSD License. See the COPYING file 6 | * for details. 7 | */ 8 | 9 | #ifndef __BED_H__ 10 | #define __BED_H__ 11 | 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | 16 | #include 17 | 18 | #include 19 | #include 20 | 21 | /** 22 | * Integral type used for storing a single SNP. 23 | */ 24 | typedef unsigned char snp_t; 25 | 26 | /** 27 | * Contains the information about a bed file. On opening the file 28 | * header is read and the information stored in this structure. 29 | */ 30 | struct pio_bed_file_t 31 | { 32 | /** 33 | * File pointer of bed file 34 | */ 35 | FILE *fp; 36 | 37 | /** 38 | * Header of the bed file. 39 | */ 40 | struct bed_header_t header; 41 | 42 | /** 43 | * Temporary buffer for each row. 44 | */ 45 | unsigned char *read_buffer; 46 | 47 | /** 48 | * Index of the current row. 49 | */ 50 | size_t cur_row; 51 | 52 | /** 53 | * Number of rows to skip. 54 | */ 55 | size_t skip_row; 56 | }; 57 | 58 | /** 59 | * Opens the bed file and reads the header, the data is 60 | * not read until explicitly asking for it. 61 | * 62 | * @param bed_file Bed file. 63 | * @param path Path to the bed file. 64 | * @param num_loci The number loci. 65 | * @param num_samples The number of samples. 66 | * 67 | * @return PIO_OK if the file could be opened, PIO_ERROR otherwise. 68 | */ 69 | pio_status_t bed_open(struct pio_bed_file_t *bed_file, const char *path, size_t num_loci, size_t num_samples); 70 | 71 | /** 72 | * Creates a bed file. 73 | * 74 | * @param bed_file Bed file. 75 | * @param path Path to the bed file. 76 | * @param num_samples The number of samples that the .bed file will include. 77 | * 78 | * @return PIO_OK if the file could be created, PIO_ERROR otherwise. 79 | */ 80 | pio_status_t bed_create(struct pio_bed_file_t *bed_file, const char *path, size_t num_samples); 81 | 82 | /** 83 | * Writes a single row of samples to the bed file, assuming that the size of 84 | * the buffer is at least as big as specified when created. 85 | * 86 | * @param bed_file Bed file. 87 | * @param buffer List of SNPs to write to file. 88 | */ 89 | pio_status_t bed_write_row(struct pio_bed_file_t *bed_file, snp_t *buffer); 90 | 91 | /** 92 | * Reads a single row from the given bed_file. Each element in the buffer 93 | * will contain a SNP. The SNP will be encoded as follows: 94 | * 0 - Homozygous major 95 | * 1 - Hetrozygous 96 | * 2 - Homozygous minor 97 | * 3 - Missing value 98 | * 99 | * @param bed_file Bed file. 100 | * @param buffer The buffer to read into. It is asssumed that it 101 | * is big enough, e.g.: 102 | * SNPS_IN_ROWS: Number of individuals 103 | * SNPS_IN_COLUMNS: Number of loci. 104 | * 105 | * @return PIO_OK if a row could be read, 106 | * PIO_END if there are no more rows, 107 | * PIO_ERROR otherwise. 108 | */ 109 | pio_status_t bed_read_row(struct pio_bed_file_t *bed_file, snp_t *buffer); 110 | 111 | /** 112 | * Skips a single row from the given bed_file. 113 | * 114 | * @param bed_file Bed file. 115 | * 116 | * @return PIO_OK if a row could be skipped, 117 | * PIO_END if there are no more rows, 118 | * PIO_ERROR otherwise. 119 | */ 120 | pio_status_t bed_skip_row(struct pio_bed_file_t *bed_file); 121 | 122 | /** 123 | * Returns the number of bytes required to store a row from 124 | * the given bed file. 125 | * 126 | * @param bed_file Bed file. 127 | * 128 | * @return The number of bytes required to store a row from 129 | * the given bed file. 130 | */ 131 | size_t bed_row_size(struct pio_bed_file_t *bed_file); 132 | 133 | /** 134 | * Returns the number of snps stored in a row for the 135 | * given bed file. 136 | * 137 | * @param bed_file Bed file. 138 | * 139 | * @return The number of snps stroed in a row. 140 | */ 141 | size_t bed_num_snps_per_row(struct pio_bed_file_t *bed_file); 142 | 143 | /** 144 | * Returns the SNP order for the given bed file. 145 | * 146 | * @param bed_file Bed file. 147 | * 148 | * @return the SNP order for the given bed file. 149 | */ 150 | enum SnpOrder bed_snp_order(struct pio_bed_file_t *bed_file); 151 | 152 | /** 153 | * Restarts the reading at the first row. 154 | * 155 | * @param bed_file Bed file. 156 | */ 157 | void bed_reset_row(struct pio_bed_file_t *bed_file); 158 | 159 | /** 160 | * Closes the bed file. 161 | * 162 | * @param bed_file Bed file. 163 | */ 164 | void bed_close(struct pio_bed_file_t *bed_file); 165 | 166 | /** 167 | * Transposes the given file to the given output file. 168 | * 169 | * @param original_path The file to transpose. 170 | * @param transposed_path The file where the transposed data 171 | * will be stored. 172 | * @param num_loci The number of loci. 173 | * @param num_samples The number of samples. 174 | * 175 | */ 176 | pio_status_t bed_transpose(const char *original_path, const char *transposed_path, size_t num_loci, size_t num_samples); 177 | 178 | #ifdef __cplusplus 179 | } 180 | #endif 181 | 182 | #endif /* End of __BED_H__ */ 183 | -------------------------------------------------------------------------------- /libplinkio/src/plinkio/bed_header.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012-2013, Mattias Frånberg 3 | * All rights reserved. 4 | * 5 | * This file is distributed under the Modified BSD License. See the COPYING file 6 | * for details. 7 | */ 8 | 9 | #ifndef __BED_HEADER_H__ 10 | #define __BED_HEADER_H__ 11 | 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | 16 | #include 17 | 18 | #define BED_HEADER_MAX_SIZE 3 19 | 20 | enum SnpOrder 21 | { 22 | /** 23 | * Means that when reading one row, you get one SNP for all 24 | * individuals (common). 25 | */ 26 | BED_ONE_LOCUS_PER_ROW, 27 | 28 | /** 29 | * Means that when reading one row, you get all SNPs for 30 | * one individual. 31 | */ 32 | BED_ONE_SAMPLE_PER_ROW, 33 | 34 | /** 35 | * Unknown order. 36 | */ 37 | BED_UNKNOWN_ORDER 38 | }; 39 | 40 | enum BedVersion 41 | { 42 | /** 43 | * Old version, reading of these files might not work. 44 | */ 45 | PIO_VERSION_PRE_099, 46 | 47 | /** 48 | * Version v0.99, no magic header but snp order in first byte. 49 | */ 50 | PIO_VERSION_099, 51 | 52 | /** 53 | * Version v1.00, 16 bit magic header. 54 | */ 55 | PIO_VERSION_100 56 | }; 57 | 58 | struct bed_header_t 59 | { 60 | /** 61 | * Order of the SNPs in the file. 62 | */ 63 | enum SnpOrder snp_order; 64 | 65 | /** 66 | * Version of the file. 67 | */ 68 | enum BedVersion version; 69 | 70 | /** 71 | * Number of loci. 72 | */ 73 | size_t num_loci; 74 | 75 | /** 76 | * Number of samples. 77 | */ 78 | size_t num_samples; 79 | }; 80 | 81 | /** 82 | * Initializes and creates a bed header. 83 | * 84 | * By default the latest version is used, and the snps will be stored 85 | * accoding to ONE_LOCUS_PER_ROW. 86 | * 87 | * @param num_loci Number of loci. 88 | * @param num_samples Number of samples. 89 | * 90 | * @return The created bed header. 91 | */ 92 | struct bed_header_t bed_header_init(size_t num_loci, size_t num_samples); 93 | 94 | /** 95 | * Initializes and creates a bed header from a 96 | * 97 | * @param num_loci Number of loci. 98 | * @param num_samples Number of samples. 99 | * @param header_bytes Contains the header stored as bytes, it is at least 100 | * BED_HEADER_MAX_SIZE long. 101 | * 102 | * @return The created bed header. 103 | */ 104 | struct bed_header_t bed_header_init2(size_t num_loci, size_t num_samples, const unsigned char *header_bytes); 105 | 106 | /** 107 | * Converts a packed header that is stored as an array of bytes into 108 | * a header structure. 109 | * 110 | * Note: The given header structure must be initialized. 111 | * 112 | * @param header The new header will be stored here. 113 | * @param header_bytes A packed header, assumed to be at least 114 | * BED_HEADER_MAX_SIZE bytes long. 115 | */ 116 | void bed_header_from_bytes(struct bed_header_t *header, const unsigned char *header_bytes); 117 | 118 | /** 119 | * Converts a header structure into a packed header. 120 | * 121 | * @param header Bed header. 122 | * @param header_bytes The packed header will be stored here, assumed to be at 123 | * least BED_HEADER_MAX_SIZE bytes long. 124 | * @param length Size of the packed header in bytes. 125 | * 126 | */ 127 | void bed_header_to_bytes(struct bed_header_t *header, unsigned char *header_bytes, int *length); 128 | 129 | /** 130 | * Returns the number of rows stored in the file. 131 | * 132 | * @param header Bed header. 133 | * 134 | * @return The number of rows stored in the file. 135 | */ 136 | size_t bed_header_num_rows(struct bed_header_t *header); 137 | 138 | /** 139 | * Returns the number of columns stored in the file. 140 | * 141 | * @param header Bed header. 142 | * 143 | * @return The number of columns stored in the file. 144 | */ 145 | size_t bed_header_num_cols(struct bed_header_t *header); 146 | 147 | /** 148 | * Returns the length of a row in bytes for a bed file 149 | * with the given header. 150 | * 151 | * @param header Bed header. 152 | * 153 | * @return The number of bytes required to store a row of SNPs. 154 | */ 155 | size_t bed_header_row_size(struct bed_header_t *header); 156 | 157 | /** 158 | * Returns the number of bytes occupied by the header, 159 | * i.e. the file offset to the data. 160 | * 161 | * @param header Bed header. 162 | * 163 | * @return the number of bytes occupied by the header. 164 | */ 165 | size_t bed_header_data_offset(struct bed_header_t *header); 166 | 167 | /** 168 | * Returns the size of all rows in bytes. 169 | * 170 | * @param header Bed header. 171 | * 172 | * @return the size of all rows in bytes. 173 | */ 174 | size_t bed_header_data_size(struct bed_header_t *header); 175 | 176 | /** 177 | * Returns the size of the file in bytes including the header. 178 | * 179 | * @param header Bed header. 180 | * 181 | * @return the size of the file in bytes including the header. 182 | */ 183 | size_t bed_header_file_size(struct bed_header_t *header); 184 | 185 | /** 186 | * Returns the SNP order for the header. 187 | * 188 | * @param header Bed header. 189 | * 190 | * @return the SNP order for the header. 191 | */ 192 | enum SnpOrder bed_header_snp_order(struct bed_header_t *header); 193 | 194 | /** 195 | * Transposes the given header. 196 | * 197 | * @param header Bed header. 198 | */ 199 | void bed_header_transpose(struct bed_header_t *header); 200 | 201 | #ifdef __cplusplus 202 | } 203 | #endif 204 | 205 | #endif 206 | --------------------------------------------------------------------------------