source: trunk/se/lu/thep/wenni/base/base1/BaseFileConverter/Data.cc @ 594

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

Improved memory usage of BaseFileConverter?. Previous implementation was not flattering.

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