Debian Codegraph @

bait-fisher-helper.cpp

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 #include "faststring2.h"
28 #include <iostream>
29 #include <list>
30
31 #include "CFile/CFile2_1.h"
32 #include "CSequences2.h"
33 #include "CSequence_Mol2_1.h"
34
35 #include "bait-fisher-helper.h"
36
37 using namespace std;
38
39 // Extract species (taxon) name from transcript name:
40 faststring extract_taxon_name(faststring sequence_name, char delim, unsigned short field_num)
41 {
42 list<faststring> container; char delim_cstring[2];
43
44 delim_cstring[0] = delim;
45 delim_cstring[1] = '\0';
46
47 split(container, sequence_name, delim_cstring);
48
49 // If the field number (0 based) is not smaller the number of fields, we have a problem.
50 if (!(field_num < container.size() ) )
51 {
52 cerr << "ERROR: In extract_taxon_name: Found unexpected number of fields in a sequence name: " << sequence_name << endl;
53 cerr << "I tried to extract field number " << field_num << " as specified in the parameter file or given as default value." << endl;
54 cerr << "But this field does not exist." << endl;
55 exit(-3);
56 }
57
58 list<faststring>::iterator it = container.begin();
59 unsigned short i;
60
61 for (i=0; i<field_num; ++i)
62 ++it;
63
64 return *it;
65 }
66
67
68 // Extract gene name from transcript name:
69 faststring extract_gene_name(faststring sequence_name, char delim, unsigned short field_num)
70 {
71 list<faststring> container;
72 char delim_cstring[2];
73
74
75
76 delim_cstring[0] = delim;
77 delim_cstring[1] = '\0';
78
79 split(container, sequence_name, delim_cstring);
80
81 // If the field number (0 based) is not smaller the number of fields, we have a problem.
82 if (!(field_num < container.size() ) )
83 {
84 cerr << "ERROR: In extract_gene_name: Found unexpected number of fields in a sequence name: " << sequence_name << endl;
85 cerr << "I tried to extract field number " << field_num << " as specified in the parameter file or given as default value." << endl;
86 cerr << "But this field does not exist." << endl;
87 exit(-3);
88 }
89
90 list<faststring>::iterator it = container.begin();
91 unsigned short i;
92
93 for (i=0; i<field_num; ++i)
94 ++it;
95
96 #ifdef SHORTEN_GENENAME_AFTER_MINUS
97 faststring pre, post;
98 it->backwards_search_divide_at_break_at('-', delim, pre, post);
99 return pre;
100 #else
101 return *it;
102 #endif
103 }
104
105 // (1) Read alignment.
106 // (2) Provide pointer to specific sequence (the one for which the name is specified, see (3)) as well as to the full alignment.
107 // (3) If species_name_query is not empty, find the sequence with this name.
108 // If species_name_query is an empty string, assign NULL to pseq.
109 // (4) Important: The CSequence_Mol and CSequences2 objects need to be deleted by the user of this function when not used any more.
110 // Version 2: Read full alignment and return both: full alignment and requested sequence.
111 void read_alignment_sequence_of_species2(faststring alignment_file,
112 faststring species_name_query,
113 CSequence_Mol*& pseq,
114 CSequences2*& pseqs,
115 unsigned& pseq_index,
116 char delim,
117 unsigned short field_num)
118 {
119 CFile infile;
120 // unsigned count_seq=0;
121 CSequences2* tmp_pseqs;
122
123
124 // fputs("Reading alignment file: ", stderr);
125 // fputs(alignment_file.c_str(), stderr);
126 // fputs(".\n", stderr);
127
128 infile.ffopen(alignment_file.c_str());
129 if ( infile.fail() )
130 {
131 fprintf(stderr, "Sorry! The input file \n \"%s\"\ncould not be opened. I guess it does not exist.\n",
132 alignment_file.c_str());
133 exit(-1);
134 }
135
136 CSequence_Mol::processing_flag pflag = CSequence_Mol::processing_flag(0);
137 // Convert all nucleotides to upper case letters and ambig to Ns.
138 pflag = CSequence_Mol::processing_flag(pflag | CSequence_Mol::convert_toupper | CSequence_Mol::convert_ambig2N);
139 tmp_pseqs = new CSequences2(CSequence_Mol::dna);
140
141 ++DEBUG_COUNT_NEW_ALIGNMENT_FROM_FILE;
142
143 tmp_pseqs->read_from_Fasta_File(infile, pflag, 0, -1, false);
144 infile.ffclose();
145
146
147 // fputsf("Alignment file has been read successfully.\n", stderr);
148
149 pseqs = tmp_pseqs;
150
151
152 if (species_name_query.empty() )
153 {
154 pseq = NULL;
155 pseq_index = -1u;
156 }
157 else
158 {
159 // Find sequence with taxon name equal to species_name_query
160 unsigned i, N;
161 N = pseqs->GetTaxaNum();
162
163 for (i=0; i<N; ++i)
164 {
165 const char* seq_name = pseqs->get_Seq_Name(i);
166
167 if (seq_name == NULL)
168 {
169 cerr << "INTERNAL ERROR: Could not retrieve sequence name for sequence of index: "
170 << i << " in read_transcript_sequence_of_species2." << endl;
171 exit(-5);
172 }
173
174 faststring species_name = extract_taxon_name(seq_name, delim, field_num);
175
176 // cout << "Index: " << i << " " << N << endl;
177 // cout << "Comparing: " << species_name << " " << species_name_query << endl;
178
179 if (species_name == species_name_query)
180 break;
181 }
182 if (i == N) // There is no sequence with species name equal to the one we search for.
183 {
184 pseq = NULL;
185 pseq_index = -1u;
186 }
187 else
188 {
189 pseq = pseqs->get_seq_by_index(i);
190 pseq_index = i;
191 }
192 }
193 }