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

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

Peter says: Use proper classes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1// $Id: qQN.cc 1034 2009-04-07 21:22:26Z 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/OptionHelp.h>
33#include <yat/utility/OptionInFile.h>
34#include <yat/utility/OptionOutFile.h>
35#include <yat/utility/OptionSwitch.h>
36
37#include <cstdlib>
38#include <fstream>
39#include <iostream>
40#include <stdexcept>
41
42using namespace theplu::yat::normalizer;
43using namespace theplu::yat::utility;
44
45void create_target(std::vector<double>&, const Matrix&);
46void create_target(std::vector<double>&, const Matrix&, const std::string&);
47
48
49int main(int argc, char* argv[])
50{
51  CommandLine cmd;
52  OptionInFile assay(cmd, "assay-data", "assay annotations", true);
53  OptionInFile indata(cmd, "in-data", "data to be normalized");
54  OptionOutFile outdata(cmd, "out-data", "normalized data");
55  OptionHelp help(cmd);
56  help.synopsis()=(std::string("See ") +
57                   "http://baseplugins.thep.lu.se/net.sf.basedb.normalizers " +
58                   "for\ndetails on this program\n");
59  OptionSwitch version(cmd, "version", "output version and exit");
60  std::stringstream copyright;
61  copyright << PACKAGE_STRING << '\n'
62            << "Copyright (C) 2009 Jari Häkkinen\n\n"
63            << "This is free software see the source for copying "
64            << "conditions. There is NO\nwarranty; not even for "
65            << "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
66  try {
67    cmd.parse(argc, argv);
68  }
69  catch (cmd_error& e) {
70    if (version.present()) {
71      std::cout << copyright.str();
72      return EXIT_SUCCESS;
73    }
74    std::cout << e.what() << std::endl;
75    return EXIT_FAILURE;
76  }
77  if (version.present()) {
78    std::cout << copyright.str();
79    return EXIT_SUCCESS;
80  }
81
82  if (version.present()) {
83    std::cout << copyright.str();
84    return EXIT_SUCCESS;
85  }
86
87  std::ifstream* infile=NULL;
88  std::streambuf* cin_buffer=NULL;
89  if (indata.present()) {
90    infile=new std::ifstream(indata.value().c_str());
91    cin_buffer = std::cin.rdbuf(); // save cin's input buffer
92    std::cin.rdbuf(infile->rdbuf());
93  }
94  Matrix m(std::cin,'\t');
95  if (indata.present()) {
96    std::cin.rdbuf(cin_buffer); // restore old input buffer
97    infile->close();
98    delete infile;
99  }
100
101  std::vector<double> target(m.rows(),0);
102  ( assay.present() ? create_target(target,m,assay.value()) :
103                      create_target(target,m) );
104  qQuantileNormalizer qqn(target.begin(), target.end(), 100);
105  ColumnNormalizer<qQuantileNormalizer> cn(qqn);
106  Matrix result(m.rows(),m.columns());
107  cn(m,result);
108
109  std::ofstream* outfile=NULL;
110  std::streambuf* cout_buffer = std::cout.rdbuf();
111  if (outdata.present()) {
112    outfile=new std::ofstream(outdata.value().c_str());
113    cout_buffer = std::cout.rdbuf(); // save cout's output buffer
114    std::cout.rdbuf(outfile->rdbuf());
115  }
116  std::cout << result << std::endl;
117  if (outdata.present()) {
118    std::cout.rdbuf(cout_buffer); // restore old output buffer
119    outfile->close();
120    delete outfile;
121  }
122
123  return EXIT_SUCCESS;
124}
125
126
127void create_target(std::vector<double>& t, const Matrix& m,
128                   const std::string& assay)
129{
130  std::ifstream is(assay.c_str());
131  std::string line;
132  size_t column=0;
133  size_t yes=0;
134  for (size_t row=0; row<m.rows(); ++row)
135    t[row]=0;
136  while (getline(is, line)) {
137    size_t found=line.find("yes");
138    if (found!=std::string::npos) {
139      for (size_t row=0; row<m.rows(); ++row)
140        t[row]+=m(row,column);
141      ++yes;
142    }
143    ++column;
144    if (column>m.columns())
145      throw std::runtime_error("Too many annotation columns wrt data matrix");
146  }
147  if (!yes)
148    throw std::runtime_error("No columns marked as reference");
149  for (size_t row=0; row<m.rows(); ++row)
150    t[row]/=yes;
151}
152
153
154void create_target(std::vector<double>& t, const Matrix& m)
155{
156  for (size_t row=0; row<m.rows(); ++row) {
157    t[row]=0;
158    for (size_t column=0; column<m.columns(); ++column)
159      t[row]+=m(row,column);
160    t[row]/=m.columns();
161  }
162}
Note: See TracBrowser for help on using the repository browser.