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

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

Removed duplicated code block.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1// $Id: qQN.cc 1037 2009-04-08 15:23:15Z 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  std::ifstream* infile=NULL;
83  std::streambuf* cin_buffer=NULL;
84  if (indata.present()) {
85    infile=new std::ifstream(indata.value().c_str());
86    cin_buffer = std::cin.rdbuf(); // save cin's input buffer
87    std::cin.rdbuf(infile->rdbuf());
88  }
89  Matrix m(std::cin,'\t');
90  if (indata.present()) {
91    std::cin.rdbuf(cin_buffer); // restore old input buffer
92    infile->close();
93    delete infile;
94  }
95
96  std::vector<double> target(m.rows(),0);
97  ( assay.present() ? create_target(target,m,assay.value()) :
98                      create_target(target,m) );
99  qQuantileNormalizer qqn(target.begin(), target.end(), 100);
100  ColumnNormalizer<qQuantileNormalizer> cn(qqn);
101  Matrix result(m.rows(),m.columns());
102  cn(m,result);
103
104  std::ofstream* outfile=NULL;
105  std::streambuf* cout_buffer = std::cout.rdbuf();
106  if (outdata.present()) {
107    outfile=new std::ofstream(outdata.value().c_str());
108    cout_buffer = std::cout.rdbuf(); // save cout's output buffer
109    std::cout.rdbuf(outfile->rdbuf());
110  }
111  std::cout << result << std::endl;
112  if (outdata.present()) {
113    std::cout.rdbuf(cout_buffer); // restore old output buffer
114    outfile->close();
115    delete outfile;
116  }
117
118  return EXIT_SUCCESS;
119}
120
121
122void create_target(std::vector<double>& t, const Matrix& m,
123                   const std::string& assay)
124{
125  std::ifstream is(assay.c_str());
126  std::string line;
127  size_t column=0;
128  size_t yes=0;
129  for (size_t row=0; row<m.rows(); ++row)
130    t[row]=0;
131  while (getline(is, line)) {
132    size_t found=line.find("yes");
133    if (found!=std::string::npos) {
134      for (size_t row=0; row<m.rows(); ++row)
135        t[row]+=m(row,column);
136      ++yes;
137    }
138    ++column;
139    if (column>m.columns())
140      throw std::runtime_error("Too many annotation columns wrt data matrix");
141  }
142  if (!yes)
143    throw std::runtime_error("No columns marked as reference");
144  for (size_t row=0; row<m.rows(); ++row)
145    t[row]/=yes;
146}
147
148
149void create_target(std::vector<double>& t, const Matrix& m)
150{
151  for (size_t row=0; row<m.rows(); ++row) {
152    t[row]=0;
153    for (size_t column=0; column<m.columns(); ++column)
154      t[row]+=m(row,column);
155    t[row]/=m.columns();
156  }
157}
Note: See TracBrowser for help on using the repository browser.