ʻOhana
Population structure, admixture history, and selection using learning methods.
convert/jade.main.cpp
1 /* -------------------------------------------------------------------------
2  Ohana
3  Copyright (c) 2015-2020 Jade Cheng (\___/)
4  Jade Cheng <info@jade-cheng.com> (='.'=)
5  ------------------------------------------------------------------------- */
6 
7 #include "jade.args.hpp"
8 #include "jade.bgl2lgm.hpp"
9 #include "jade.cov2nwk.hpp"
10 #include "jade.nwk2cov.hpp"
11 #include "jade.nwk2svg.hpp"
12 #include "jade.ped2dgm.hpp"
13 #include "jade.version.hpp"
14 
15 namespace
16 {
17 char const * const usage = R"(USAGE
18  convert <command> [<input> <output>]
19 
20 ARGUMENTS
21  command one of the following conversion types:
22  bgl2lgm
23  cov2nwk
24  nwk2cov
25  nwk2svg
26  ped2dgm
27 
28 COMMANDS
29  bgl2lgm Converts a beagle file to an lgm matrix. Each line of the beagle
30  file is split into a number of fields. The first three fields are
31  ignored. The remaining fields are split into individuals, each of
32  which must contain three genotype likelihoods.
33 
34  cov2nwk Converts a covariance matrix to a Newick-formatted tree. First the
35  covariance matrix is converted into a distance matrix, which is then
36  approximated into a Newick-formatted tree using the Neighbor Joining
37  algorithm.
38 
39  nwk2cov Converts a Newick-formatted tree to a covariance matrix. Each tree
40  structure is mapped to a covariance matrix unambiguously.
41 
42  nwk2svg Converts a Newick-formatted tree to an SVG representation. To find
43  an appealing arrangement of a tree, this converter takes inspiration
44  from an electrostatic field and models tree components as like-signed
45  charged particles with nodes constrained by the branches that connect
46  them. It then utilizes the Nelder-Mead algorithm to minimize the
47  total potential energy of this system and achieve an optimal tree
48  layout.
49 
50  ped2dgm Converts a plink ped file to a dgm matrix. Each line of the ped file
51  is split into a number of fields, separated by tabs. The first six
52  fields are ignored, but the remaining fields must consist of two
53  symbols separated by a space. Each pair of symbols is considered a
54  pair of a column.
55 
56  For all commands, if no arguments are given, the source file is read from
57  standard input and the output file is written to standard output. Otherwise,
58  the path to the input file and output file must be specified after the
59  conversion type argument.
60 
61 OPTIONS
62  --help,-h shows this help message and exits
63 
64 DESCRIPTION
65  Converts files.
66 
67 EXAMPLE
68  $ convert nwk2svg foo.nwk foo.svg
69  $ cat bar.bgl | convert bgl2lgm > bar.lgm
70 
71 BUGS
72  Report any bugs to Jade Cheng <info@jade-cheng.com>.
73 
74 Copyright (c) 2015-2020 Jade Cheng
75 )";
76 
77  ///
78  /// Executes the program based on the specified options.
79  ///
80  template <typename TController>
81  int execute(
82  jade::args & a) ///< The command-line arguments.
83  {
84  //
85  // If arguments were provided, read from and write to files;
86  // otherwise, read from and write to standard streams.
87  //
88  if (a.is_empty())
89  {
90  TController::execute(std::cin, std::cout);
91  }
92  else
93  {
94  const auto src = a.pop<std::string>();
95  const auto dst = a.pop<std::string>();
96  a.validate_empty();
97 
98  std::ifstream in (src);
99  if (!in.good())
100  throw jade::error() << "failed to open '" << src << "'";
101 
102  std::ofstream out (dst);
103  if (!out.good())
104  throw jade::error() << "failed to create '" << dst << "'";
105 
106  TController::execute(in, out);
107  }
108 
109  return EXIT_SUCCESS;
110  }
111 }
112 
113 ///
114 /// The main entry point of the program.
115 /// \param argc The argument count.
116 /// \param argv The argument values.
117 /// \return EXIT_SUCCESS or EXIT_FAILURE.
118 ///
119 int main(const int argc, const char * argv[])
120 {
121  try
122  {
123  jade::args args (argc, argv);
124 
125  if (args.read_flag("--help", "-h"))
126  {
127  std::cout << ::usage;
128  return EXIT_SUCCESS;
129  }
130 
131  if (args.read_flag("--version", "-v"))
132  {
133  jade::version::write("convert", std::cout);
134  return EXIT_SUCCESS;
135  }
136 
137  typedef double value_type;
138  typedef jade::basic_bgl2lgm<value_type> bgl2lgm_type;
139  typedef jade::basic_cov2nwk<value_type> cov2nwk_type;
140  typedef jade::basic_nwk2cov<value_type> nwk2cov_type;
141  typedef jade::basic_nwk2svg<value_type> nwk2svg_type;
142  typedef jade::basic_ped2dgm<value_type> ped2dgm_type;
143 
144  const auto command = args.pop<std::string>();
145 
146  if (command == "bgl2lgm") return ::execute<bgl2lgm_type>(args);
147  if (command == "cov2nwk") return ::execute<cov2nwk_type>(args);
148  if (command == "nwk2cov") return ::execute<nwk2cov_type>(args);
149  if (command == "nwk2svg") return ::execute<nwk2svg_type>(args);
150  if (command == "ped2dgm") return ::execute<ped2dgm_type>(args);
151 
152  throw jade::error() << "unsupported command '" << command << "'";
153  }
154  catch (const std::exception & e)
155  {
156  std::cerr << e.what() << std::endl;
157  return EXIT_FAILURE;
158  }
159 }
jade::basic_args::validate_empty
void validate_empty() const
Throws an exception if there are more arguments to be processed.
Definition: jade.args.hpp:161
jade::basic_nwk2cov
A template for a class that converts Newick-formatted trees to covariance matrices.
Definition: jade.nwk2cov.hpp:21
jade::basic_ped2dgm
A template for a class that converts PED-formatted data to discrete genotype matrices.
Definition: jade.ped2dgm.hpp:20
jade::basic_error::what
virtual const char * what() const
jade::basic_nwk2svg
A template for a class that converts Newick-formatted trees to SVG representations.
Definition: jade.nwk2svg.hpp:20
jade::basic_args::is_empty
bool is_empty() const
Definition: jade.args.hpp:86
jade::basic_args::pop
TValue pop()
Definition: jade.args.hpp:96
jade::basic_bgl2lgm
A template for a class that converts BEAGLE-formatted data to likelihood genotype matrices.
Definition: jade.bgl2lgm.hpp:20
jade::basic_args
A template for a class that helps process command-line arguments.
Definition: jade.args.hpp:19
jade::basic_version::write
static void write(char_type const *const title, ostream_type &out)
Writes the string displayed to the user.
Definition: jade.version.hpp:29
jade::basic_cov2nwk
A template for a class that approximates covariance matrices as Newick-formatted trees.
Definition: jade.cov2nwk.hpp:21
jade::basic_error
A template for a class representing an exception thrown from this namespace.
Definition: jade.error.hpp:20