File: fac.cc

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 (234 lines) | stat: -rw-r--r-- 6,195 bytes parent folder | download | duplicates (4)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/** Calculate assembly contiguity statistics.
 * Written by Shaun Jackman <sjackman@bcgsc.ca>.
 */
#include "config.h"
#include "Common/Histogram.h"
#include "Common/IOUtil.h"
#include "Common/Sequence.h" // for isACGT
#include "Common/Uncompress.h"
#include "DataLayer/FastaReader.h"
#include "DataLayer/Options.h"
#include <algorithm>
#include <getopt.h>
#include <iostream>
#include <sstream>

using namespace std;

#define PROGRAM "abyss-fac"

static const char VERSION_MESSAGE[] =
PROGRAM " (" PACKAGE_NAME ") " VERSION "\n"
"Written by Shaun Jackman.\n"
"\n"
"Copyright 2014 Canada's Michael Smith Genome Sciences Centre\n";

static const char USAGE_MESSAGE[] =
"Usage: " PROGRAM " [OPTION]... [FILE]...\n"
"Calculate assembly contiguity statistics.\n"
"\n"
" Options:\n"
"\n"
"  -G, -e, --genome-size=N expected genome size. Used to calculate NG50\n"
"                          and associated stats [disabled]\n"
"  -s, -t, --min-length=N  ignore sequences shorter than N bp [500]\n"
"  -d, --delimiter=S       use S for the field delimiter [\\t]\n"
"  -j, --jira              output JIRA format\n"
"  -m, --mmd               output MultiMarkdown format\n"
"      --chastity          discard unchaste sequences [default]\n"
"      --no-chastity       do not discard unchaste sequences\n"
"      --trim-masked       trim masked bases from the end\n"
"      --no-trim-masked    do not trim masked bases from the ends\n"
"                          of sequences [default]\n"
"      --count-ambig       count ambiguity codes in sequences\n"
"      --no-count-ambig    do not count ambiguity codes in sequences [default]\n"
"  -v, --verbose           display verbose output\n"
"      --help              display this help and exit\n"
"      --version           output version information and exit\n"
"\n"
"Report bugs to <" PACKAGE_BUGREPORT ">.\n";

namespace opt {
	static unsigned minLength = 500;
	static long long unsigned genomeSize;
	static string delimiter = "\t";
	static int format;
	static int verbose;
	static int countAmbig;
}
enum { TAB, JIRA, MMD };

static const char shortopts[] = "d:e:G:jms:t:v";

enum { OPT_HELP = 1, OPT_VERSION };

static const struct option longopts[] = {
	{ "genome-size", required_argument, NULL, 'G' },
	{ "min-length", no_argument, NULL, 's' },
	{ "delimiter", required_argument, NULL, 'd' },
	{ "jira", no_argument, NULL, 'j' },
	{ "mmd", no_argument, NULL, 'm' },
	{ "chastity", no_argument, &opt::chastityFilter, 1 },
	{ "no-chastity", no_argument, &opt::chastityFilter, 0 },
	{ "trim-masked", no_argument, &opt::trimMasked, 1 },
	{ "no-trim-masked", no_argument, &opt::trimMasked, 0 },
	{ "count-ambig", no_argument, &opt::countAmbig, 1 },
	{ "no-count-ambig", no_argument, &opt::countAmbig, 0 },
	{ "help", no_argument, NULL, OPT_HELP },
	{ "version", no_argument, NULL, OPT_VERSION },
	{ NULL, 0, NULL, 0 }
};

/** FastaReader flags. */
static const int FASTAREADER_FLAGS = FastaReader::NO_FOLD_CASE;

/** Print contiguity statistics. */
static void printContiguityStatistics(const char* path)
{
	static bool printHeader = true;
	if (string(path) == "---") {
		if (printHeader == false)
			cout << '\n';
		printHeader = true;
		return;
	}

	// Read the sequences and count the lengths.
	Histogram h;
	FastaReader in(path, FASTAREADER_FLAGS);
	for (string s; in >> s;)
		h.insert(opt::countAmbig ? s.length() :
				count_if(s.begin(), s.end(), isACGT));
	assert(in.eof());

	// Print the table header.
	if (opt::format == JIRA && printHeader) {
		printHeader = false;
		const char* sep = "\t||";
		cout << "||"
			<< "n" << sep
			<< "n:" << opt::minLength << sep
			<< "L50" << sep;
		if (opt::genomeSize > 0)
			cout << "n:NG50" << sep
				<< "NG50" << sep;
		cout << "min" << sep
			<< "N75" << sep
			<< "N50" << sep
			<< "N25" << sep
			<< "E-size" << sep
			<< "max" << sep
			<< "sum" << sep
			<< "name" << sep << '\n';
	} else if (opt::format == MMD && printHeader) {
		printHeader = false;
		const char* sep = "\t|";
		cout << "n" << sep
			<< "n:" << opt::minLength << sep
			<< "L50" << sep;
		if (opt::genomeSize > 0)
			cout << "n:NG50" << sep
				<< "NG50" << sep;
		cout << "min" << sep
			<< "N75" << sep
			<< "N50" << sep
			<< "N25" << sep
			<< "E-size" << sep
			<< "max" << sep
			<< "sum" << sep
			<< "name" << '\n';
		if (opt::genomeSize > 0)
			cout << "------" << sep
				<< "------" << sep;
		cout << "------" << sep
			<< "------" << sep
			<< "------" << sep
			<< "------" << sep
			<< "------" << sep
			<< "------" << sep
			<< "------" << sep
			<< "------" << sep
			<< "------" << sep
			<< "------" << sep
			<< "------" << '\n';
	}

	// Print the table.
	if (opt::format == JIRA)
		cout << '|';
	printContiguityStats(cout, h, opt::minLength,
			printHeader, opt::delimiter, opt::genomeSize)
		<< opt::delimiter << path;
	if (opt::format == JIRA)
		cout << opt::delimiter;
	cout << endl;
	printHeader = false;
}

int main(int argc, char** argv)
{
	opt::trimMasked = false;

	bool die = false;
	for (int c; (c = getopt_long(argc, argv,
					shortopts, longopts, NULL)) != -1;) {
		istringstream arg(optarg != NULL ? optarg : "");
		switch (c) {
		  case '?':
			die = true;
			break;
		  case 'd':
			opt::delimiter = arg.str();
			arg.clear(ios::eofbit);
			break;
		  case 'j':
			opt::delimiter = "\t|";
			opt::format = JIRA;
			break;
		  case 'm':
			opt::delimiter = "\t|";
			opt::format = MMD;
			break;
		  case 'G':
		  case 'e':
			{
				double x;
				arg >> x;
				opt::genomeSize = x;
				break;
			}
		  case 's': case 't':
			arg >> opt::minLength;
			break;
		  case 'v':
			opt::verbose++;
			break;
		  case OPT_HELP:
			cout << USAGE_MESSAGE;
			exit(EXIT_SUCCESS);
		  case OPT_VERSION:
			cout << VERSION_MESSAGE;
			exit(EXIT_SUCCESS);
		}
		if (optarg != NULL && !arg.eof()) {
			cerr << PROGRAM ": invalid option: `-"
				<< (char)c << optarg << "'\n";
			exit(EXIT_FAILURE);
		}
	}
	if (die) {
		cerr << "Try `" << PROGRAM
			<< " --help' for more information.\n";
		exit(EXIT_FAILURE);
	}

	if (optind == argc)
		printContiguityStatistics("-");
	else
		for_each(argv + optind, argv + argc,
				printContiguityStatistics);

	cout.flush();
	assert_good(cout, "stdout");
	return 0;
}