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

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

Reverting changset:1164.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.6 KB
Line 
1// $Id: qQN.cc 1224 2010-05-03 08:18:46Z 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#include <yat/statistics/utility.h>
38
39#include <cstdlib>
40#include <fstream>
41#include <functional>
42#include <iostream>
43#include <limits>
44#include <stdexcept>
45
46using namespace theplu::yat::normalizer;
47using namespace theplu::yat::utility;
48
49
50class CleanUpMatrix : std::unary_function<DataWeight, DataWeight>
51{
52public:
53  CleanUpMatrix(void) {}
54
55  inline DataWeight operator()(DataWeight x) const
56    { return ( x.data()>0 ?
57               x : DataWeight(std::numeric_limits<double>::quiet_NaN(),0.0) ); }
58};
59
60
61void create_target(std::vector<double>&, const MatrixWeighted&, bool use_median);
62void create_target(std::vector<double>&, const MatrixWeighted&,
63                   const std::string&, bool use_median);
64
65/**
66   writes the data values in the matrix ignoring the weights, i.e.,
67   produces the same output as the Matrix output operator does.
68 */
69std::ostream& operator<< (std::ostream&, const MatrixWeighted&);
70
71
72int main(int argc, char* argv[])
73{
74  CommandLine cmd;
75  OptionInFile assay(cmd, "assay-data", "assay annotations");
76  OptionInFile indata(cmd, "in-data", "data to be normalized");
77  OptionOutFile outdata(cmd, "out-data", "normalized data");
78  OptionSwitch target_median(cmd, "median",
79                             "use median instead of mean for targets", false);
80  OptionHelp help(cmd);
81  help.synopsis()=(std::string("See ") +
82                   "http://baseplugins.thep.lu.se/net.sf.basedb.normalizers " +
83                   "for\ndetails on this program.\n\n" +
84                   "All assays are used to calculate the target if no assay " +
85                   "annotion file is set.\n" +
86                   "The assay file format is pair information: " +
87                   "assay_no\\tyes/no\n" +
88                   "where assay_no is the assay number starting at 1, yes " +
89                   "indicates that it should\n" +
90                   "be used in target calculation, and no indicates that it " +
91                   "should not be a part\n" +
92                   "of target.\n");
93  OptionSwitch version(cmd, "version", "output version and exit");
94  std::stringstream copyright;
95  copyright << PACKAGE_STRING << '\n'
96            << "Copyright (C) 2009 Jari Häkkinen\n\n"
97            << "This is free software see the source for copying "
98            << "conditions. There is NO\nwarranty; not even for "
99            << "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
100  try {
101    cmd.parse(argc, argv);
102  }
103  catch (cmd_error& e) {
104    if (version.present()) {
105      std::cout << copyright.str();
106      return EXIT_SUCCESS;
107    }
108    std::cerr << e.what() << std::endl;
109    return EXIT_FAILURE;
110  }
111  if (version.present()) {
112    std::cout << copyright.str();
113    return EXIT_SUCCESS;
114  }
115  std::ifstream* infile=NULL;
116  std::streambuf* cin_buffer=NULL;
117  if (indata.present()) {
118    infile=new std::ifstream(indata.value().c_str());
119    cin_buffer = std::cin.rdbuf(); // save cin's input buffer
120    std::cin.rdbuf(infile->rdbuf());
121  }
122
123  MatrixWeighted m(std::cin,'\t');
124  if (indata.present()) {
125    std::cin.rdbuf(cin_buffer); // restore old input buffer
126    infile->close();
127    delete infile;
128  }
129
130  std::transform(m.begin(), m.end(), m.begin(), CleanUpMatrix());
131  std::vector<double> target;
132  ( assay.present() ?
133    create_target(target, m, assay.value(), target_median.value()) :
134    create_target(target, m, target_median.value()) );
135  std::transform(target.begin(), target.end(),
136                 target.begin(), theplu::yat::utility::Log<double>());
137  std::transform(data_iterator(m.begin()), data_iterator(m.end()),
138                 data_iterator(m.begin()), theplu::yat::utility::Log<double>());
139  // q = min(100,target_size/10) but no smaller than 10
140  unsigned int q=target.size()/10;
141  q = q>100 ? 100 : ( q<10 ? 10 : q );
142  qQuantileNormalizer qqn(target.begin(), target.end(), q);
143  ColumnNormalizer<qQuantileNormalizer> cn(qqn);
144  MatrixWeighted result(m.rows(),m.columns());
145  cn(m,result);
146  std::transform(data_iterator(result.begin()), data_iterator(result.end()),
147                 data_iterator(result.begin()), theplu::yat::utility::Exp<double>());
148
149  std::ofstream* outfile=NULL;
150  std::streambuf* cout_buffer = std::cout.rdbuf();
151  if (outdata.present()) {
152    outfile=new std::ofstream(outdata.value().c_str());
153    cout_buffer = std::cout.rdbuf(); // save cout's output buffer
154    std::cout.rdbuf(outfile->rdbuf());
155  }
156  std::cout << result << std::endl;
157  if (outdata.present()) {
158    std::cout.rdbuf(cout_buffer); // restore old output buffer
159    outfile->close();
160    delete outfile;
161  }
162
163  return EXIT_SUCCESS;
164}
165
166
167void create_target(std::vector<double>& t, const MatrixWeighted& m,
168                   const std::string& assay, bool use_median)
169{
170  std::vector< std::vector<double> > calc_support(m.rows());
171  for (std::vector< std::vector<double> >::iterator i=calc_support.begin();
172       i!=calc_support.end(); ++i)
173    i->reserve(m.columns());
174
175  std::ifstream is(assay.c_str());
176  std::string line;
177  size_t column=0;
178  while (getline(is, line)) {
179    size_t found=line.find("yes");
180    if (found!=std::string::npos) // use this assay as a part of reference
181      for (size_t row=0; row<m.rows(); ++row)
182        if (m(row,column).weight())       // weight either 0 or 1
183          calc_support[row].push_back(m(row,column).data());
184    ++column;
185    if (column>m.columns())
186      throw std::runtime_error("Too many annotation columns wrt data matrix");
187  }
188
189  t.reserve(m.rows());
190  for (size_t row=0; row<m.rows(); ++row) {
191    const std::vector<double>& cs=calc_support[row];
192    if (!cs.empty())
193      t.push_back( use_median ?
194                   theplu::yat::statistics::median(cs.begin(), cs.end()) :
195                   std::accumulate(cs.begin(), cs.end(), 0.0) / cs.size() );
196  }
197  if (!t.size())
198    throw std::runtime_error("Not a well defined reference, aborting");
199}
200
201
202void create_target(std::vector<double>& t, const MatrixWeighted& m,
203                   bool use_median)
204{
205  t.reserve(m.rows());
206  std::vector<double> cs;
207  cs.reserve(m.columns());
208  for (size_t row=0; row<m.rows(); ++row) {
209    cs.clear();
210    for (size_t column=0; column<m.columns(); ++column)
211      if (m(row,column).weight())      // weight either 0 or 1
212        cs.push_back(m(row,column).data());
213    if (!cs.empty())
214      t.push_back( use_median ?
215                   theplu::yat::statistics::median(cs.begin(), cs.end()) :
216                   std::accumulate(cs.begin(), cs.end(), 0.0) / cs.size() );
217  }
218  if (!t.size())
219    throw std::runtime_error("Not a well defined reference, aborting");
220}
221
222
223std::ostream& operator<< (std::ostream& s, const MatrixWeighted& m)
224{
225  s.setf(std::ios::dec);
226  s.precision(12);
227  for(size_t i=0, j=0; i<m.rows(); i++)
228    for (j=0; j<m.columns(); j++) {
229      s << m(i,j).data();
230      if (j<m.columns()-1)
231        s << s.fill();
232      else if (i<m.rows()-1)
233        s << "\n";
234    }
235  return s;
236}
Note: See TracBrowser for help on using the repository browser.