1 #ifndef GRAPHIO_H
2 #define GRAPHIO_H 1
3 
4 #include "Graph/Options.h"
5 #include "AdjIO.h"
6 #include "AsqgIO.h"
7 #include "DistIO.h"
8 #include "DotIO.h"
9 #include "FastaIO.h"
10 #include "GfaIO.h"
11 #include "SAMIO.h"
12 #include <cassert>
13 #include <cstdlib> // for abort
14 #include <istream>
15 #include <ostream>
16 #include <string>
17 
18 /** Write the graph g to the specified output stream in the format
19  * specified by opt::format.
20  */
21 template <typename Graph>
write_graph(std::ostream & out,const Graph & g,const std::string & program,const std::string & commandLine)22 std::ostream& write_graph(std::ostream& out, const Graph& g,
23 		const std::string& program, const std::string& commandLine)
24 {
25 	switch (opt::format) {
26 	  case ADJ:
27 		return out << adj_writer<Graph>(g);
28 	  case ASQG:
29 		return write_asqg(out, g);
30 	  case DIST:
31 		return write_dist(out, g);
32 	  case DOT: case DOT_MEANCOV:
33 		return out << dot_writer(g);
34 	  case GFA1:
35 		return write_gfa1(out, g);
36 	  case GFA2:
37 		return write_gfa2(out, g);
38 	  case SAM:
39 		return write_sam(out, g, program, commandLine);
40 	  default:
41 		assert(false);
42 		abort();
43 	}
44 }
45 
46 #include "ContigGraph.h"
47 
48 /** Read a graph. */
49 template <typename Graph, typename BetterEP>
read_graph(std::istream & in,ContigGraph<Graph> & g,BetterEP betterEP)50 std::istream& read_graph(std::istream& in, ContigGraph<Graph>& g,
51 		BetterEP betterEP)
52 {
53 	in >> std::ws;
54 	assert(in);
55 	switch (in.peek()) {
56 	  case '@': // @SQ: SAM format
57 		return read_sam_header(in, g);
58 	  case 'd': // digraph: GraphViz dot format (directed graph)
59 	  case 'g': // graph:   GraphViz dot format (undirected graph)
60 		return read_dot<Graph>(in, g, betterEP);
61 	  case 'H': {
62 		in.get();
63 		char c = in.peek();
64 		in.unget();
65 		assert(in);
66 		switch (c) {
67 		  case 'T': // HT: ASQG format
68 			return read_asqg(in, g);
69 		  case '\t': // H: GAF format
70 			return read_gfa(in, g, betterEP);
71 		  default:
72 			std::cerr << "Unknown file format: `H" << c << "'\n";
73 			exit(EXIT_FAILURE);
74 		}
75 		break;
76 	  }
77 	  case '>': // FASTA format for vertices
78 		return read_fasta(in, g);
79 	  default: // adj format
80 		return read_adj(in, g, betterEP);
81 	}
82 }
83 
84 /** Disallow parallel edges. */
85 struct DisallowParallelEdges {
86 	template <typename EP>
operatorDisallowParallelEdges87 	EP operator()(const EP& a, const EP& b) const
88 	{
89 		std::cerr << "error: parallel edges:"
90 			" [" << a << "], [" << b << "]\n";
91 		exit(EXIT_FAILURE);
92 	}
93 };
94 
95 /** Read a graph. */
96 template <typename Graph>
97 std::istream& operator>>(std::istream& in, ContigGraph<Graph>& g)
98 {
99 	return read_graph(in, g, DisallowParallelEdges());
100 }
101 
102 #endif
103