File: FastaWriter.cpp

package info (click to toggle)
abyss 2.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,284 kB
  • sloc: cpp: 78,182; ansic: 6,512; makefile: 2,252; perl: 672; sh: 509; haskell: 412; python: 4
file content (73 lines) | stat: -rw-r--r-- 1,802 bytes parent folder | download | duplicates (7)
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
#include "FastaWriter.h"
#include "Common/Options.h"
#include <cassert>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring> // for strerror
#include <iostream>
#include <unistd.h> // for fsync

using namespace std;

static inline void die(const string& s)
{
	cerr << "error: writing to `" << s << "': "
		<< strerror(errno) << endl;
	exit(EXIT_FAILURE);
}

FastaWriter::FastaWriter(const char* path, bool append)
	: m_path(path), m_fileHandle(fopen(path, append ? "a" : "w"))
{
	if (m_fileHandle == NULL)
		die(m_path);
}

FastaWriter::~FastaWriter()
{
	int n = fsync(fileno(m_fileHandle));
	if (n < 0)
		die(m_path);
	n = fclose(m_fileHandle);
	if (n < 0)
		die(m_path);
	m_fileHandle = NULL;
}

void FastaWriter::WriteSequence(const Sequence& seq, unsigned id,
		unsigned multiplicity, const string& comment)
{
	assert(m_fileHandle != NULL);
	const char *sep = comment.empty() ? "" : " ";
	int n = opt::rank < 0
		? fprintf(m_fileHandle, ">%llu %zu %u%s%s\n%s\n",
				(long long unsigned)id,
				seq.length(), multiplicity,
				sep, comment.c_str(),
				seq.c_str())
		: fprintf(m_fileHandle, ">%u:%llu %zu %u%s%s\n%s\n",
				opt::rank,
				(long long unsigned)id,
				seq.length(), multiplicity,
				sep, comment.c_str(),
				seq.c_str());
	if (n < 0)
		die(m_path);
}

void FastaWriter::WriteSequence(const Sequence& seq, unsigned long long id, const std::string& comment)
{
	assert(m_fileHandle != NULL);
	int n = fprintf(m_fileHandle, ">%llu %s\n%s\n", id, comment.c_str(), seq.c_str());
	if (n < 0)
		die(m_path);
}

void FastaWriter::WriteSequence(const Sequence& seq, const std::string& id, const std::string& comment)
{
	assert(m_fileHandle != NULL);
	int n = fprintf(m_fileHandle, ">%s %s\n%s\n", id.c_str(), comment.c_str(), seq.c_str());
	if (n < 0)
		die(m_path);
}