Debian Codegraph @

CTaxonNamesDictionary.h

raw
1 /* BaitFisher (version 1.2.8) a program for designing DNA target enrichment baits
2 * Copyright 2013-2016 by Christoph Mayer
3 *
4 * This source file is part of the BaitFisher-package.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with BaitFisher. If not, see <http://www.gnu.org/licenses/>.
18 *
19 *
20 * For any enquiries send an Email to Christoph Mayer
21 * [email protected]
22 *
23 * When publishing work that is based on the results please cite:
24 * Mayer et al. 2016: BaitFisher: A software package for multi-species target DNA enrichment probe design
25 *
26 */
27 #ifndef CTAXONNAMESDICTIONARY_H
28 #define CTAXONNAMESDICTIONARY_H
29
30 #include "faststring2.h"
31 #include "CDistance_matrix.h"
32 #include "CSequences2.h"
33 #include "CFile/CFile2_1.h"
34 #include "CSeqNameList.h"
35 #include <set>
36 #include <list>
37 #include <map>
38 #include <vector>
39
40 class CTaxonNamesDictionary
41 {
42 CSeqNameList *snl;
43 CSeqNameList all;
44
45 // Each index stored in the taxon_names_to_index map is equal to the index of the same taxon name in the taxon_names_vec.
46 // This allows us to translate indices and names back and forth.
47
48 std::vector<faststring> taxon_names_vec;
49 std::map<faststring, unsigned> taxon_names_to_index;
50
51 public:
52
53 CTaxonNamesDictionary(faststring directoryname, std::list<faststring> &list_of_fasta_file_names, unsigned short sequence_name_taxon_field_number, char field_delimiter):all(true)
54 {
55 // Collect all taxon names to build a taxon name dictionary
56 // The collection of all sequence names is stored in the all object.
57 faststring full_transcript_file_name;
58
59 std::list<faststring>::iterator file_names_it, file_names_it_end, file_names_it_begin;
60
61 file_names_it_begin = file_names_it = list_of_fasta_file_names.begin();
62 file_names_it_end = list_of_fasta_file_names.end();
63
64 // For each fasta file
65 while (file_names_it != file_names_it_end)
66 {
67 full_transcript_file_name = directoryname + "/" + *file_names_it;
68
69 // std::cout << "Reading sequence names in " << full_transcript_file_name << std::endl;
70 snl = new CSeqNameList(full_transcript_file_name.c_str(), true);
71
72 if (file_names_it == file_names_it_begin)
73 {
74 all.set_to(*snl, "all-names-union");
75 }
76 else
77 {
78 all.add_List_non_redundant(*snl);
79 }
80 ++file_names_it;
81 }
82 // all.print(cout,0);
83
84 // TAXON NAMES DICTIONARY: consists of a vector, and map
85 // A set is used to store all names temporarily.
86
87 std::set<faststring> taxon_names_set;
88 bool error_get_set_of_name_field;
89
90 // Take the names of the sequences, divide it according to the delimiter '|' and
91 // extract field number sequence_name_taxon_field_number. Store all names in the set.
92 error_get_set_of_name_field = all.get_set_of_name_field(sequence_name_taxon_field_number+1, field_delimiter, taxon_names_set);
93
94 if (error_get_set_of_name_field)
95 {
96 std::cerr << "WARNING: At least one error occurred when trying to read taxon names from the fasta sequence headers."
97 << " Usually this error occurs, when headers are not in the proper format, when the parameter file contains a wrong"
98 << " wrong sequence name field delimiter or if the taxon name field number is wrong." << std::endl;
99 std::cerr << "This error will influence: TODO." << std::endl;
100 }
101
102 // Now we have a set that contains all taxon names found in all fasta files.
103
104 std::set<faststring>::iterator it, it_end;
105 it = taxon_names_set.begin();
106 it_end = taxon_names_set.end();
107
108 unsigned i;
109 i=0;
110 while (it != it_end)
111 {
112 // Since the taxon_names_vec was empty originally, the names have an index equal to i.
113 taxon_names_vec.push_back(*it); // push all names into the vector. unique-index -> name
114 taxon_names_to_index[*it] = i; // for each name store its index in the map. name -> unique-index
115 ++it;
116 ++i;
117 }
118 // Now we can translate unique indices and taxon names back and forth.
119 }
120
121 void print(std::ostream &outfile)
122 {
123 outfile << "## Index <-> taxon name:" << std::endl;
124
125 unsigned i, N = taxon_names_vec.size();
126 for (i=0; i<N; ++i)
127 {
128 outfile << i << " " << taxon_names_vec[i] << std::endl;
129 }
130 }
131
132
133 // Main dictionary retrieval function.
134 unsigned dictionary_get_index_of_taxonname(faststring taxonname)
135 {
136 /* map<faststring, unsigned>::iterator it, it_end; */
137 /* it = taxon_names_to_index.begin(); */
138 /* it_end = taxon_names_to_index.end(); */
139 /* cout << "taxon_names_to_index map" << endl; */
140 /* while (it != it_end) */
141 /* { */
142 /* cout << it->first << "=>" << it->second << endl; */
143 /* ++it; */
144 /* } */
145
146
147 std::map<faststring, unsigned>::iterator it_find = taxon_names_to_index.find(taxonname);
148
149 if (it_find != taxon_names_to_index.end() )
150 return it_find->second;
151 else
152 return -1u;
153 }
154
155 const std::vector<faststring> & get_taxon_names_vec()
156 {
157 return taxon_names_vec;
158 }
159
160 void print_taxon_names_to_index(std::ostream &os)
161 {
162 std::map<faststring, unsigned>::iterator it, it_end;
163 it = taxon_names_to_index.begin();
164 it_end = taxon_names_to_index.end();
165
166 os << "Content of taxon names to global index dictionary: " << std::endl;
167 while (it != it_end)
168 {
169 os.width(30);
170 os << it->first << " " << it->second << std::endl;
171 ++it;
172 }
173 }
174
175 };
176
177
178 #endif