source: trunk/se/lu/thep/wenni/bin/BaseFileConverter/Data.cc @ 95

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

Added copyright statement to files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1// $Id: Data.cc 95 2006-04-05 13:21:18Z jari $
2
3/*
4  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson
5
6  This file is part of WeNNI,
7  http://lev.thep.lu.se/trac/baseplugins/wiki/WeNNI
8
9  WeNNI is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  WeNNI is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
17  License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
25#include "Data.h"
26
27#include <fstream>
28#include <iostream>
29
30#include <basefile.h>
31
32namespace theplu {
33namespace wenni {
34
35
36  Data::Data(BASE::BaseFileSection& bfs,const std::string& colname, 
37             const std::string& output_file)
38    : column_name_(colname), output_file_(output_file)
39  {
40    BASE::FieldList columns, assayFields, assays;
41    std::string assay_names;
42    if( !bfs.findFieldList("columns", columns) ||
43        !bfs.findFieldList("assayFields", assayFields) ||
44        !bfs.findFieldList("assays", assays) ||
45        !bfs.findStringOpt("assays", assay_names)) {
46      std::cerr << "Missing header columns/assayFields/assays\n";
47      exit(-1);
48    }
49    nof_assays_=assays.size();
50    column_offset_=columns.size()-1;
51    nof_fields_per_assay_=assayFields.size();
52    if (!bfs.findFieldPos(column_name_,column_number_,assayFields)) {
53      std::cerr << "Cannot find assayField " << column_name_ << std::endl;
54      exit(-1);
55    }
56    total_nof_columns_= column_offset_ + nof_fields_per_assay_*nof_assays_;
57  }
58
59
60
61  void Data::push_back(const std::vector<std::string>& vals)
62  {
63    data_.push_back(std::vector<std::string>(nof_assays_));
64    for(int i=0; i<nof_assays_; i++){
65      if(vals[column_offset_+nof_fields_per_assay_*i+column_number_].empty()){
66        // In a Base File missing values are given by a blank. To be
67        // able to load the data into a gsl matrix missing values are
68        // changed to NaNs.
69        (*data_.rbegin())[i]="NaN";
70      }
71      else
72        (*data_.rbegin())[i]=vals[column_offset_ + nof_fields_per_assay_ * i +
73                                  column_number_];
74    }
75  }
76
77
78
79  void Data::write_matrix(void)
80  {
81    std::filebuf fb;
82    fb.open (output_file_.c_str(),std::ios::out);
83    std::ostream s(&fb);
84    for (size_t i=0; i<data_.size(); i++) {
85      for (size_t j=0; j<data_[i].size()-1; j++)
86        s << data_[i][j] << "\t";
87      s << data_[i][data_[i].size()-1] << std::endl;
88    }
89    fb.close();
90  }
91
92
93}} // of namespace wenni and namespace theplu
Note: See TracBrowser for help on using the repository browser.