1 /** Convert a graph to dot format.
2  * Written by Shaun Jackman <sjackman@bcgsc.ca>.
3  */
4 #include "ContigGraph.h"
5 #include "DirectedGraph.h"
6 #include "GraphIO.h"
7 #include "GraphUtil.h"
8 #include "IOUtil.h"
9 #include "Uncompress.h"
10 #include <fstream>
11 #include <getopt.h>
12 #include <iostream>
13 
14 using namespace std;
15 
16 #define PROGRAM "abyss-gc"
17 
18 static const char VERSION_MESSAGE[] =
19 PROGRAM " (" PACKAGE_NAME ") " VERSION "\n"
20 "Written by Shaun Jackman.\n"
21 "\n"
22 "Copyright 2014 Canada's Michael Smith Genome Sciences Centre\n";
23 
24 static const char USAGE_MESSAGE[] =
25 "Usage: " PROGRAM " [FILE]...\n"
26 "Count the number of vertices and edges in a graph.\n"
27 "\n"
28 " Options:\n"
29 "\n"
30 "  -v, --verbose  display verbose output\n"
31 "      --help     display this help and exit\n"
32 "      --version  output version information and exit\n"
33 "\n"
34 "Report bugs to <" PACKAGE_BUGREPORT ">.\n";
35 
36 namespace opt {
37 	unsigned k; // used by DotIO
38 	static int verbose;
39 }
40 
41 static const char shortopts[] = "v";
42 
43 enum { OPT_HELP = 1, OPT_VERSION };
44 
45 static const struct option longopts[] = {
46 	{ "verbose", no_argument, NULL, 'v' },
47 	{ "help", no_argument, NULL, OPT_HELP },
48 	{ "version", no_argument, NULL, OPT_VERSION },
49 	{ NULL, 0, NULL, 0 }
50 };
51 
52 /** Read a graph from the specified file. */
53 template <typename Graph, typename BetterEP>
readGraph(const string & path,Graph & g,BetterEP betterEP)54 static void readGraph(const string& path, Graph& g, BetterEP betterEP)
55 {
56 	if (opt::verbose > 0)
57 		cout << "Reading `" << path << "'...\n";
58 	ifstream fin(path.c_str());
59 	istream& in = path == "-" ? cin : fin;
60 	assert_good(in, path);
61 	read_graph(in, g, betterEP);
62 	assert(in.eof());
63 	printGraphStats(cout, g);
64 	g_contigNames.lock();
65 }
66 
67 /** Read a graph from the specified files. */
68 template <typename Graph, typename It, typename BetterEP>
readGraphs(Graph & g,It first,It last,BetterEP betterEP)69 void readGraphs(Graph& g, It first, It last, BetterEP betterEP)
70 {
71 	if (first != last) {
72 		for (It it = first; it < last; ++it)
73 			readGraph(*it, g, betterEP);
74 	} else
75 		readGraph("-", g, betterEP);
76 }
77 
main(int argc,char ** argv)78 int main(int argc, char** argv)
79 {
80 	bool die = false;
81 	for (int c; (c = getopt_long(argc, argv,
82 					shortopts, longopts, NULL)) != -1;) {
83 		istringstream arg(optarg != NULL ? optarg : "");
84 		switch (c) {
85 		  case '?': die = true; break;
86 		  case 'v': opt::verbose++; break;
87 		  case OPT_HELP:
88 			cout << USAGE_MESSAGE;
89 			exit(EXIT_SUCCESS);
90 		  case OPT_VERSION:
91 			cout << VERSION_MESSAGE;
92 			exit(EXIT_SUCCESS);
93 		}
94 		if (optarg != NULL && !arg.eof()) {
95 			cerr << PROGRAM ": invalid option: `-"
96 				<< (char)c << optarg << "'\n";
97 			exit(EXIT_FAILURE);
98 		}
99 	}
100 
101 	if (die) {
102 		cerr << "Try `" << PROGRAM
103 			<< " --help' for more information.\n";
104 		exit(EXIT_FAILURE);
105 	}
106 
107 	ContigGraph<DirectedGraph<NoProperty, NoProperty> > g;
108 	readGraphs(g, argv + optind, argv + argc,
109 			DisallowParallelEdges());
110 	assert_good(cout, "-");
111 
112 	return 0;
113 }
114