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

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

Cleaned up iterator usage in qQN.cc

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.3 KB
Line 
1// $Id: qQN.cc 1054 2009-05-06 22:41:50Z 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/MatrixWeighted.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
45
46void create_target(std::vector<double>&, const MatrixWeighted&);
47void create_target(std::vector<double>&, const MatrixWeighted&,
48                   const std::string&);
49
50template<typename Iterator> void exp_numbers(Iterator first, Iterator last);
51template<typename Iterator> void log_numbers(Iterator first, Iterator last);
52
53/**
54   writes the data values in the matrix ignoring the weights, i.e.,
55   produces the same output as the Matrix output operator does.
56 */
57std::ostream& operator<< (std::ostream&, const MatrixWeighted&);
58
59
60int main(int argc, char* argv[])
61{
62  CommandLine cmd;
63  OptionInFile assay(cmd, "assay-data", "assay annotations");
64  OptionInFile indata(cmd, "in-data", "data to be normalized");
65  OptionOutFile outdata(cmd, "out-data", "normalized data");
66  OptionHelp help(cmd);
67  help.synopsis()=(std::string("See ") +
68                   "http://baseplugins.thep.lu.se/net.sf.basedb.normalizers " +
69                   "for\ndetails on this program\n");
70  OptionSwitch version(cmd, "version", "output version and exit");
71  std::stringstream copyright;
72  copyright << PACKAGE_STRING << '\n'
73            << "Copyright (C) 2009 Jari Häkkinen\n\n"
74            << "This is free software see the source for copying "
75            << "conditions. There is NO\nwarranty; not even for "
76            << "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
77  try {
78    cmd.parse(argc, argv);
79  }
80  catch (cmd_error& e) {
81    if (version.present()) {
82      std::cout << copyright.str();
83      return EXIT_SUCCESS;
84    }
85    std::cout << e.what() << std::endl;
86    return EXIT_FAILURE;
87  }
88  if (version.present()) {
89    std::cout << copyright.str();
90    return EXIT_SUCCESS;
91  }
92  std::ifstream* infile=NULL;
93  std::streambuf* cin_buffer=NULL;
94  if (indata.present()) {
95    infile=new std::ifstream(indata.value().c_str());
96    cin_buffer = std::cin.rdbuf(); // save cin's input buffer
97    std::cin.rdbuf(infile->rdbuf());
98  }
99  MatrixWeighted m(std::cin,'\t');
100  if (indata.present()) {
101    std::cin.rdbuf(cin_buffer); // restore old input buffer
102    infile->close();
103    delete infile;
104  }
105
106  std::vector<double> target;
107  ( assay.present() ? create_target(target,m,assay.value()) :
108                      create_target(target,m) );
109  log_numbers(target.begin(),target.end());
110  log_numbers(m.begin(),m.end());
111  qQuantileNormalizer qqn(target.begin(), target.end(), 100);
112  ColumnNormalizer<qQuantileNormalizer> cn(qqn);
113  MatrixWeighted result(m.rows(),m.columns());
114  cn(m,result);
115  exp_numbers(result.begin(),result.end());
116
117  std::ofstream* outfile=NULL;
118  std::streambuf* cout_buffer = std::cout.rdbuf();
119  if (outdata.present()) {
120    outfile=new std::ofstream(outdata.value().c_str());
121    cout_buffer = std::cout.rdbuf(); // save cout's output buffer
122    std::cout.rdbuf(outfile->rdbuf());
123  }
124  std::cout << result << std::endl;
125  if (outdata.present()) {
126    std::cout.rdbuf(cout_buffer); // restore old output buffer
127    outfile->close();
128    delete outfile;
129  }
130
131  return EXIT_SUCCESS;
132}
133
134
135void create_target(std::vector<double>& t, const MatrixWeighted& m,
136                   const std::string& assay)
137{
138  std::ifstream is(assay.c_str());
139  std::string line;
140  size_t column=0;
141  std::vector<size_t> column_contribs(m.rows(),0);
142  std::vector<double> temp_target(m.rows(),0.0);
143  while (getline(is, line)) {
144    size_t found=line.find("yes");
145    if (found!=std::string::npos) {
146      // use this assay as a part of reference
147      for (size_t row=0; row<m.rows(); ++row)
148        if (m(row,column).weight()) { // weight either 0 or 1
149          temp_target[row]+=m(row,column).data();
150          ++column_contribs[row];
151        }
152    }
153    ++column;
154    if (column>m.columns())
155      throw std::runtime_error("Too many annotation columns wrt data matrix");
156  }
157  t.reserve(m.rows());
158  for (size_t row=0; row<m.rows(); ++row)
159    if (column_contribs[row])
160      t.push_back(temp_target[row]/=column_contribs[row]);
161  if (!t.size())
162    throw std::runtime_error("Not a well defined reference, aborting");
163}
164
165
166void create_target(std::vector<double>& t, const MatrixWeighted& m)
167{
168  std::vector<double> temp_target(m.rows(),0.0);
169  t.reserve(m.rows());
170  for (size_t row=0; row<m.rows(); ++row) {
171    size_t column_contribs=0;
172    for (size_t column=0; column<m.columns(); ++column)
173      if (m(row,column).weight()) { // weight either 0 or 1
174        temp_target[row]+=m(row,column).data();
175        ++column_contribs;
176      }
177    if (column_contribs)
178      t.push_back(temp_target[row]/=column_contribs);
179  }
180  if (!t.size())
181    throw std::runtime_error("Not a well defined reference, aborting");
182}
183
184
185template<typename Iterator> void exp_numbers(Iterator i, Iterator last)
186{
187  while (i!=last) {
188    *data_iterator(i)=std::exp(*data_iterator(i));
189    ++i;
190  }
191}
192
193
194template<typename Iterator> void log_numbers(Iterator i, Iterator last)
195{
196  while (i!=last) {
197    *data_iterator(i)=std::log(*data_iterator(i));
198    ++i;
199  }
200}
201
202
203std::ostream& operator<< (std::ostream& s, const MatrixWeighted& m)
204{
205  s.setf(std::ios::dec);
206  s.precision(12);
207  for(size_t i=0, j=0; i<m.rows(); i++)
208    for (j=0; j<m.columns(); j++) {
209      s << m(i,j).data();
210      if (j<m.columns()-1)
211        s << s.fill();
212      else if (i<m.rows()-1)
213        s << "\n";
214    }
215  return s;
216}
Note: See TracBrowser for help on using the repository browser.