<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi Lennart,<div><br></div><div>nice little utility! good style, very readable code. </div><div><br></div><div>I am curious why you got into trouble to program this in C++ (e.g. this can be done using DatABEL functionality). Suppose I miss the context :)</div><div><br></div><div>best wishes,</div><div>Yurii<br><div>
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>-------------------------------------------------------</div><div>Yurii S. Aulchenko, PhD</div><div>Director, YuriiA consulting</div><div>Rotterdam, The Netherlands</div><div>yurii [dot] aulchenko [at] gmail [dot] com</div></div></div></span></div></span></div></span></span>
</div>
<br><div><div>On Jul 12, 2012, at 4:16 PM, <a href="mailto:noreply@r-forge.r-project.org">noreply@r-forge.r-project.org</a> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>Author: lckarssen<br>Date: 2012-07-12 14:16:00 +0200 (Thu, 12 Jul 2012)<br>New Revision: 929<br><br>Added:<br> pkg/ProbABEL/src/extract-snp.cpp<br>Modified:<br> pkg/ProbABEL/src/Makefile.am<br>Log:<br>ProbABEL: added the extract-snp binary that extracts SNP dosages from filevector files (from the command line). It can do this for all IDs or just one. <br><br>Of course, this program is not necessarily looking at SNPs and IDs. In filevector parlance SNPs are Variables (rows) and IDs are Observations (rows).<br><br>At the moment it can only extract one SNP at a time (given as command line option). This may change in the future. For example, we may want the program to read SNP names from stdin too. <br>The -d option for debug info prints out all SNP names and IDs in the filevector file, which may be too much for your screen.<br><br><br>Modified: pkg/ProbABEL/src/Makefile.am<br>===================================================================<br>--- pkg/ProbABEL/src/Makefile.am<span class="Apple-tab-span" style="white-space:pre"> </span>2012-07-06 05:14:23 UTC (rev 928)<br>+++ pkg/ProbABEL/src/Makefile.am<span class="Apple-tab-span" style="white-space:pre"> </span>2012-07-12 12:16:00 UTC (rev 929)<br>@@ -24,7 +24,7 @@<br> include/R_ext/Print.h include/R_ext/Random.h include/R_ext/Utils.h<span class="Apple-tab-span" style="white-space:pre"> </span>\<br> include/R_ext/RS.h<br><br>-bin_PROGRAMS = palinear palogist<br>+bin_PROGRAMS = palinear palogist extract-snp<br> if BUILD_pacoxph<br> ## Extra files for pacoxph<br> COXBASE = coxfit2 chinv2 cholesky2 chsolve2 dmatrix<br>@@ -47,6 +47,8 @@<br> pacoxph_CPPFLAGS = $(AM_CPPFLAGS)<br> pacoxph_CFLAGS = -DCOXPH -I $(top_srcdir)/src/include $(AM_CFLAGS)<br><br>+extract_snp_SOURCES = extract-snp.cpp $(FVSRC) $(FVHEADERS)<br>+<br> ## Install these scripts in the bin directory as well:<br> dist_bin_SCRIPTS = probabel.pl extIDS.pl<br><br><br>Added: pkg/ProbABEL/src/extract-snp.cpp<br>===================================================================<br>--- pkg/ProbABEL/src/extract-snp.cpp<span class="Apple-tab-span" style="white-space:pre"> </span> (rev 0)<br>+++ pkg/ProbABEL/src/extract-snp.cpp<span class="Apple-tab-span" style="white-space:pre"> </span>2012-07-12 12:16:00 UTC (rev 929)<br>@@ -0,0 +1,209 @@<br>+/*******************************************************************************<br>+ *<br>+ * This program extracts a given row (dosages for all individuals for a given<br>+ * SNP) from filevector files.<br>+ *<br>+ * (C) 2012 L.C. Karssen<br>+ *<br>+ * This program is free software: you can redistribute it and/or modify<br>+ * it under the terms of the GNU General Public License as published by<br>+ * the Free Software Foundation, either version 3 of the License, or<br>+ * (at your option) any later version.<br>+ *<br>+ * This program is distributed in the hope that it will be useful,<br>+ * but WITHOUT ANY WARRANTY; without even the implied warranty of<br>+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>+ * GNU General Public License for more details.<br>+ *<br>+ * You should have received a copy of the GNU General Public License<br>+ * along with this program. If not, see <<a href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses/</a>>.<br>+ *<br>+ ******************************************************************************/<br>+<br>+#include <stdio.h><br>+#include <string><br>+#include <iostream><br>+#include <fstream><br>+#include <map><br>+#include <getopt.h><br>+<br>+using namespace std;<br>+<br>+#include "fvlib/Logger.h"<br>+#include "fvlib/FileVector.h"<br>+#include "fvlib/CastUtils.h"<br>+<br>+<br>+void info(char *program_name)<br>+{<br>+ cout << program_name<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> << " extracts the dosage for a SNP for one or more individuals from a"<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> << "file in filevector format (.fvi/.fvd)." << endl;<br>+ cout << endl;<br>+ cout << "Usage: " << program_name << " --file <fv file> --snp <snpname>"<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> << endl;<br>+ cout << " or: " << program_name << " -f <fv file> -s <snpname>" << endl;<br>+ cout << endl;<br>+ cout << "Additional options:" << endl;<br>+ cout << "\t--id (-i) <idname>: only print the dosage for the given SNP and ID"<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> << endl;<br>+ cout << "\t--debug (-d): show debugging output" << endl;<br>+ cout << "\t--help (-h): show this information" << endl;<br>+}<br>+<br>+<br>+int main(int argc, char* argv[])<br>+{<br>+ if (argc < 2)<br>+ {<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>info(argv[0]);<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>exit(3);<br>+ }<br>+<br>+ int next_option;<br>+ const char * const short_options = "df:hi:s:";<br>+ const struct option long_options [] =<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>{<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> {"debug", 0, NULL, 'd'},<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> {"file", 1, NULL, 'f'},<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> {"help", 0, NULL, 'h'},<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> {"id", 1, NULL, 'i'},<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> {"snp", 1, NULL, 's'},<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> {NULL , 0, NULL, 0 }<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>};<br>+ char *program_name = argv[0];<br>+ bool debug = false;<br>+ string inputFileName = "";<br>+ string idname = "";<br>+ string snpname = "";<br>+ do<br>+ {<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>next_option = getopt_long(argc, argv, short_options, long_options, NULL);<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>switch (next_option)<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>{<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>case 'h':<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> info(program_name);<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> exit(0);<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>case 'd':<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> debug = true;<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> break;<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>case 'f':<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> inputFileName = string(optarg);<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> break;<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>case 'i':<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> idname = string(optarg);<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> break;<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>case 's':<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> snpname = string(optarg);<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> break;<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>case '?': break;<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>case -1 : break;<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>}<br>+ }<br>+ while (next_option != -1);<br>+<br>+ if (snpname.compare("") == 0)<br>+ {<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>cerr << "Error: No SNP name given" << endl << endl;<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>info(program_name);<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>exit(2);<br>+ }<br>+<br>+ unsigned long int snprow = 0;<br>+ bool snpfound = false;<br>+ bool idfound = false;<br>+<br>+ if (debug) cout << "Input file is '" << inputFileName << "'." << endl;<br>+<br>+ FileVector fv(inputFileName, 64, true);<br>+<br>+ if (debug)<br>+ {<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>for (unsigned long int col=0; col<fv.getNumObservations(); col++)<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>{<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> cout << fv.readObservationName(col).name << " ";<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>}<br>+<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>cout << endl;<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>cout << "----------------------" << endl;<br>+ }<br>+<br>+ // Look at the SNPs (rows) first<br>+ for (unsigned long int row=0; row<fv.getNumVariables(); row++)<br>+ {<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>string current_snpname = fv.readVariableName(row).name;<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>if (current_snpname.compare(snpname) == 0)<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>{<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> snpfound = true;<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> snprow = row;<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> if (debug)<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> {<br>+<span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span>cout << "*" << current_snpname << "* ";<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> }<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>} else {<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> if (debug)<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> {<br>+<span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span>cout << current_snpname << " ";<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> }<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>}<br>+ }<br>+ if (debug)<br>+ {<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>cout << endl;<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>cout << "----------------------" << endl;<br>+<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>cout << "N_obs = " << fv.getNumObservations() << "\telement size: "<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> << fv.getElementSize() << "\tDataType: "<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> << dataTypeToString(fv.getElementType()) << endl;<br>+ }<br>+<br>+ if(!snpfound)<br>+ {<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>cerr << "SNP name '" << snpname << "' not found in data file "<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> << inputFileName << endl;<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>exit(1);<br>+ }<br>+<br>+ char * data = new (nothrow) char[fv.getNumObservations() *<br>+<span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span> fv.getElementSize()];<br>+ if (!data)<br>+ {<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>cerr << "Cannot allocate memory for data vector. Exiting..." << endl;<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>exit(2);<br>+ }<br>+<br>+ fv.readVariable(snprow, data);<br>+ for (unsigned long int col=0; col<fv.getNumObservations(); col++)<br>+ {<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>if (idname.compare("") != 0)<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>{<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> // An ID name has been set, only print the dosage for that ID<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> // and SNP combination<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> if(idname.compare(fv.readObservationName(col).name) == 0)<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> {<br>+<span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span>idfound = true;<br>+<span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span>cout << fv.readObservationName(col).name << "\t"<br>+<span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span> << bufToString(fv.getElementType(),<br>+<span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span> &data[col*fv.getElementSize()],<br>+<span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span> string("NA"))<br>+<span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span> << endl;<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> }<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>} else {<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> // Print the dosages for all IDs<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> cout << fv.readObservationName(col).name << "\t"<br>+<span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span> << bufToString(fv.getElementType(),<br>+<span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span>&data[col*fv.getElementSize()],<br>+<span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span>string("NA"))<br>+<span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span> << endl;<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>}<br>+ }<br>+<br>+ if (idfound == false && idname.compare("") != 0)<br>+ {<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>cerr << "Id '" << idname << "' not found in data file "<br>+<span class="Apple-tab-span" style="white-space:pre"> </span> << inputFileName << endl;<br>+<span class="Apple-tab-span" style="white-space:pre"> </span>exit(1);<br>+ }<br>+<br>+ delete data;<br>+}<br><br>_______________________________________________<br>Genabel-commits mailing list<br><a href="mailto:Genabel-commits@lists.r-forge.r-project.org">Genabel-commits@lists.r-forge.r-project.org</a><br>https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/genabel-commits<br></div></blockquote></div><br></div></body></html>