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

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

Use functionality already in yat.

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