source: plugins/base2/net.sf.basedb.normalizers/trunk/src/c++/bin/qQN.cc @ 1023

Last change on this file since 1023 was 1023, checked in by Jari Häkkinen, 14 years ago

Addresses #197. In anticipation of abort functionality the underlying binary cannot read/write too much information from standard streams. Now qQN can use files or cin/cout

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1// $Id: qQN.cc 1023 2009-04-06 22:35:48Z jari $
2
3/*
4  Copyright (C) 2009 Jari Häkkinen
5
6  This file is part of the Normalizers plug-in package for BASE
7  (net.sf.based.normalizers). The package is available at
8  http://baseplugins.thep.lu.se/ BASE main site is
9  http://base.thep.lu.se/
10
11  This is free software; you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation; either version 3 of the License, or
14  (at your option) any later version.
15
16  The software is distributed in the hope that it will be useful, but
17  WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include <config.h> // this header file is created by configure
26
27#include <yat/normalizer/ColumnNormalizer.h>
28#include <yat/normalizer/qQuantileNormalizer.h>
29
30#include <yat/utility/CommandLine.h>
31#include <yat/utility/Matrix.h>
32#include <yat/utility/OptionArg.h>
33#include <yat/utility/OptionHelp.h>
34#include <yat/utility/OptionSwitch.h>
35
36#include <cstdlib>
37#include <fstream>
38#include <iostream>
39#include <stdexcept>
40
41using namespace theplu::yat::normalizer;
42using namespace theplu::yat::utility;
43
44void create_target(std::vector<double>&, const Matrix&);
45void create_target(std::vector<double>&, const Matrix&, const std::string&);
46
47
48int main(int argc, char* argv[])
49{
50  CommandLine cmd;
51  OptionArg<std::string> assay(cmd, "assay-data", "assay annotations", true);
52  OptionArg<std::string> indata(cmd, "in-data", "data to be normalized");
53  OptionArg<std::string> outdata(cmd, "out-data", "normalized data");
54  OptionHelp help(cmd);
55  help.synopsis()=(std::string("See ") +
56                   "http://baseplugins.thep.lu.se/net.sf.basedb.normalizers " +
57                   "for\ndetails on this program\n");
58  OptionSwitch version(cmd, "version", "output version and exit");
59  std::stringstream copyright;
60  copyright << PACKAGE_STRING << '\n'
61            << "Copyright (C) 2009 Jari Häkkinen\n\n"
62            << "This is free software see the source for copying "
63            << "conditions. There is NO\nwarranty; not even for "
64            << "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
65  try {
66    cmd.parse(argc, argv);
67  }
68  catch (cmd_error& e) {
69    if (version.present()) {
70      std::cout << copyright.str();
71      return EXIT_SUCCESS;
72    }
73    std::cout << e.what() << std::endl;
74    return EXIT_FAILURE;
75  }
76  if (version.present()) {
77    std::cout << copyright.str();
78    return EXIT_SUCCESS;
79  }
80
81  if (version.present()) {
82    std::cout << copyright.str();
83    return EXIT_SUCCESS;
84  }
85
86  std::ifstream* infile=NULL;
87  std::streambuf* cin_buffer=NULL;
88  if (indata.present()) {
89    infile=new std::ifstream(indata.value().c_str());
90    cin_buffer = std::cin.rdbuf(); // save cin's input buffer
91    std::cin.rdbuf(infile->rdbuf());
92  }
93  Matrix m(std::cin,'\t');
94  if (indata.present()) {
95    std::cin.rdbuf(cin_buffer); // restore old input buffer
96    infile->close();
97    delete infile;
98  }
99
100  std::vector<double> target(m.rows(),0);
101  ( assay.present() ? create_target(target,m,assay.value()) :
102                      create_target(target,m) );
103  qQuantileNormalizer qqn(target.begin(), target.end(), 100);
104  ColumnNormalizer<qQuantileNormalizer> cn(qqn);
105  Matrix result(m.rows(),m.columns());
106  cn(m,result);
107
108  std::ofstream* outfile=NULL;
109  std::streambuf* cout_buffer = std::cout.rdbuf();
110  if (outdata.present()) {
111    outfile=new std::ofstream(outdata.value().c_str());
112    cout_buffer = std::cout.rdbuf(); // save cout's output buffer
113    std::cout.rdbuf(outfile->rdbuf());
114  }
115  std::cout << result << std::endl;
116  if (outdata.present()) {
117    std::cout.rdbuf(cout_buffer); // restore old output buffer
118    outfile->close();
119    delete outfile;
120  }
121
122  return EXIT_SUCCESS;
123}
124
125
126void create_target(std::vector<double>& t, const Matrix& m,
127                   const std::string& assay)
128{
129  std::ifstream is(assay.c_str());
130  std::string line;
131  size_t column=0;
132  size_t yes=0;
133  for (size_t row=0; row<m.rows(); ++row)
134    t[row]=0;
135  while (getline(is, line)) {
136    size_t found=line.find("yes");
137    if (found!=std::string::npos) {
138      for (size_t row=0; row<m.rows(); ++row)
139        t[row]+=m(row,column);
140      ++yes;
141    }
142    ++column;
143    if (column>m.columns())
144      throw std::runtime_error("Too many annotation columns wrt data matrix");
145  }
146  if (!yes)
147    throw std::runtime_error("No columns marked as reference");
148  for (size_t row=0; row<m.rows(); ++row)
149    t[row]/=yes;
150}
151
152
153void create_target(std::vector<double>& t, const Matrix& m)
154{
155  for (size_t row=0; row<m.rows(); ++row) {
156    t[row]=0;
157    for (size_t column=0; column<m.columns(); ++column)
158      t[row]+=m(row,column);
159    t[row]/=m.columns();
160  }
161}
Note: See TracBrowser for help on using the repository browser.