-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
149 lines (128 loc) · 4.3 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* This file contains the driver for the viterbi path finder.
*
* Author: Dan Lin, [email protected]
*/
#include <fstream>
#include <iostream>
#include <vector>
#include "dna_sequence.h"
#include "viterbi.h"
using namespace viterbi;
using namespace std;
// Extracts sequence from .FASTA file into different 'dna_sequences'.
bool ExtractSequencesFromFile(const string& filename,
vector<DNASequence> *dna_sequences);
// Same as above except DNA sequence described as raw vector of char values.
bool ExtractNumberSequencesFromFile(const string& filename,
vector<char> *const dna_sequences);
int main(int argc, char *argv[]) {
if (argc < 2) {
cout << "Not enough args provided." << endl
<< "Format: ./a.out <sequence_file>" << endl;
return -1;
}
string test_filename(argv[1]);
cout << "Reading input" << endl;
vector<DNASequence> dna_sequences;
ExtractSequencesFromFile(test_filename, &dna_sequences);
cout << "Extracted " << dna_sequences.size() << " sequences" << endl;
Viterbi::TransitionMatrix transition_matrix =
/* Beg 1 2 */
/* Beg */ { { -1 * numeric_limits<double>::max(), log(.9999), log(.0001) },
/* 1 */ { -1 * numeric_limits<double>::max(), log(.9999), log(.0001) },
/* 2 */ { -1 * numeric_limits<double>::max(), log(.01), log(.99) } };
Viterbi::EmissionMatrix emission_matrix =
/* A C G T */
/* Beg */ { { 0, 0, 0, 0 },
/* 1 */ { log(.25), log(.25), log(.25), log(.25) },
/* 2 */ { log(.20), log(.30), log(.30), log(.20) } };
vector<char> emission_sequence;
for (int ii = 0; ii < dna_sequences[0].sequence().size(); ++ii) {
emission_sequence.push_back(
static_cast<int>(CharToNucleotide(dna_sequences[0].sequence()[ii])));
}
vector<DNASequence>().swap(dna_sequences);
Viterbi v(transition_matrix, emission_matrix);
deque<int> viterbi_path =
v.ViterbiTrain(emission_sequence, 10 /* num_iterations */);
return 0;
}
bool ExtractNumberSequencesFromFile(const string& filename,
vector<char> *const emission_sequence) {
ifstream ifs;
ifs.open(filename.c_str(), std::ifstream::in);
if (!ifs.good()) {
cerr << "Unable to open file: " << filename << endl;
return false;
}
string sequence;
string identifier;
while (true) {
char peek_char = ifs.peek();
if (peek_char == EOF) {
// All input has been read.
if (!sequence.empty()) {
for (int ii = 0; ii < sequence.size(); ++ii) {
emission_sequence->push_back(sequence[ii] - '0' - 1);
}
}
break;
}
string line_str;
getline(ifs, line_str);
if (peek_char == '>') {
if (!sequence.empty()) {
for (int ii = 0; ii < sequence.size(); ++ii) {
emission_sequence->push_back(sequence[ii] - '0' - 1);
}
sequence = "";
identifier = "";
}
// Get the identifier set after '>' and prior to ':'.
int end = line_str.find(":");
identifier = line_str.substr(1, end - 1);
continue;
}
sequence += line_str;
}
return true;
}
bool ExtractSequencesFromFile(const string& filename,
vector<DNASequence> *const dna_sequences) {
assert(dna_sequences);
ifstream ifs;
ifs.open(filename.c_str(), std::ifstream::in);
if (!ifs.good()) {
cerr << "Unable to open file: " << filename << endl;
return false;
}
string sequence;
string identifier;
while (true) {
char peek_char = ifs.peek();
if (peek_char == EOF) {
// All input has been read.
if (!sequence.empty()) {
dna_sequences->push_back(DNASequence(sequence, identifier));
}
break;
}
string line_str;
getline(ifs, line_str);
if (peek_char == '>') {
if (!sequence.empty()) {
dna_sequences->push_back(DNASequence(sequence, identifier));
sequence = "";
identifier = "";
}
// Get the identifier set after '>' and prior to ':'.
int end = line_str.find(":");
identifier = line_str.substr(1, end - 1);
continue;
}
line_str = DNASequence::MakeValidDNASequence(line_str);
sequence += line_str;
}
return true;
}