File: TextBootstrapWriter.hpp

package info (click to toggle)
salmon 1.10.3%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,148 kB
  • sloc: cpp: 200,707; ansic: 171,082; sh: 859; python: 792; makefile: 238
file content (102 lines) | stat: -rw-r--r-- 2,555 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef __TEXT_BOOTSTRAP_WRITER_HPP__
#define __TEXT_BOOTSTRAP_WRITER_HPP__

#include <fstream>
#include <memory>
#include <mutex>
#include <vector>

#include "BootstrapWriter.hpp"
#include "SalmonSpinLock.hpp"
#include "Transcript.hpp"
#include "spdlog/spdlog.h"

class TextBootstrapWriter : public BootstrapWriter {
public:
  TextBootstrapWriter(boost::filesystem::path& outputPath,
                      std::shared_ptr<spdlog::logger> logger)
      : outputPath_(outputPath), logger_(logger) {
    // Create the directory if it doesn't exist
    if (!boost::filesystem::exists(outputPath_.parent_path())) {
      boost::filesystem::create_directories(outputPath_);
    }
    // open the file
    ofile_.open(outputPath.string());
  }

  ~TextBootstrapWriter() {
#if defined __APPLE__
    spin_lock::scoped_lock sl(writeMutex_);
#else
    std::lock_guard<std::mutex> lock(writeMutex_);
#endif
    ofile_.close();
  }

  bool writeHeader(std::string& comments,
                   std::vector<Transcript>& transcripts) override {
#if defined __APPLE__
    spin_lock::scoped_lock sl(writeMutex_);
#else
    std::lock_guard<std::mutex> lock(writeMutex_);
#endif
    ofile_ << comments;
    size_t numTxps = transcripts.size();
    if (numTxps == 0) {
      return false;
    }
    for (size_t tn = 0; tn < numTxps; ++tn) {
      auto& t = transcripts[tn];
      ofile_ << t.RefName;
      if (tn < numTxps - 1) {
        ofile_ << '\t';
      }
    }
    ofile_ << '\n';
    /*
    for (size_t tn = 0; tn < numTxps; ++tn) {
        auto& t  = transcripts[tn];
        ofile_ << t.EffectiveLength;
        if (tn < numTxps - 1) {
            ofile_ << '\t';
        }
    }
    ofile_ << '\n';
    */
    return true;
  }

  bool writeBootstrap(std::vector<double>& abund) override {
#if defined __APPLE__
    spin_lock::scoped_lock sl(writeMutex_);
#else
    std::lock_guard<std::mutex> lock(writeMutex_);
#endif
    size_t numTxps = abund.size();
    for (size_t tn = 0; tn < numTxps; ++tn) {
      auto& a = abund[tn];
      ofile_ << a;
      if (tn < numTxps - 1) {
        ofile_ << '\t';
      }
    }
    ofile_ << '\n';
    logger_->info("wrote {} bootstraps", numWritten_.load() + 1);
    ++numWritten_;
    return true;
  }

private:
  boost::filesystem::path outputPath_;
  std::ofstream ofile_;
  std::shared_ptr<spdlog::logger> logger_;
// only one writer thread at a time
#if defined __APPLE__
  spin_lock writeMutex_;
#else
  std::mutex writeMutex_;
#endif
  std::atomic<uint32_t> numWritten_{0};
};

#endif // __TEXT_BOOTSTRAP_WRITER_HPP__