-
Notifications
You must be signed in to change notification settings - Fork 0
/
Motif.cpp
130 lines (116 loc) · 3.56 KB
/
Motif.cpp
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
/*
* Copyright (C) 2006 Cold Spring Harbor Laboratory
* Authors: Andrew D. Smith, Pavel Sumazin and Michael Q. Zhang
*
* This file is part of CREAD.
*
* CREAD is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* CREAD is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CREAD; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "Motif.hpp"
using std::string;
using std::vector;
using std::map;
using std::ostream;
using std::ostringstream;
using std::pair;
using std::endl;
const char *
Motif::type_id = "Motif";
const size_t
Motif::type_id_size = 5;
const char *
Motif::matrix_start = "P0";
Motif::Motif(const Motif& m) :
Pattern(m), matrix(m.matrix), sites(m.sites) {
type = type_id;
}
Motif::Motif(const Matrix &m) : matrix(m) {
accession = "(none)";
type = type_id;
}
Motif&
Motif::operator=(const Motif& m) {
if (this != &m) {
Pattern::operator=(m);
matrix = m.matrix;
type = type_id;
sites = m.sites;
}
return *this;
}
void
Motif::format_representation(ostream& os) const {
os << matrix << endl
<< PatternID::BLANK_PATTERN_LINE << endl;
}
void
Motif::format_sites(ostream& os) const {
string sep("\n");
sep += PatternID::BINDING_SITE_START + string(" ");
if (sites.size() > 0) {
os << PatternID::BINDING_SITE_START << " ";
std::copy(sites.begin(), sites.end() - 1,
std::ostream_iterator<MotifSite>(os, sep.c_str()));
os << sites.back() << endl;
os << PatternID::BLANK_PATTERN_LINE << endl;
}
}
// TODO: in the event of an exception in the constructor below, make
// sure the state is reset so that there are no leaks, and strange
// state.
// TODO: need to verify that the information is valid -- read_accession
// isn't even actually being used.
Motif::Motif(vector<string>& lines) : Pattern(lines) {
bool read_matrix = false, read_sites = false;
for (size_t i = 0; i < lines.size(); ++i) {
string line = lines[i];
if (line_type(line, matrix_start)) {
vector<string> matrix_lines;
matrix_lines.push_back(line);
for (size_t j = i + 1; j < lines.size() && isdigit(lines[j][0]); ++j)
matrix_lines.push_back(lines[j]);
matrix = Matrix(matrix_lines);
read_matrix = true;
i += matrix_lines.size();
}
else if (!read_sites &&
line_type(line, PatternID::BINDING_SITE_START)) {
while (line_type(lines[i], PatternID::BINDING_SITE_START))
sites.push_back(MotifSite(remove_line_id(lines[i++])));
read_sites = true;
}
// TODO: need to verify format better in here
}
if (type.empty())
type = type_id;
if (!read_matrix)
throw MotifFormatException();
}
vector<Motif>
Motif::ReadMotifVector(string file_name) {
vector<vector<string> > motif_lines;
ReadPatternLines(file_name, motif_lines);
vector<Motif> motifs;
for (size_t i = 0; i < motif_lines.size(); ++i) {
bool version_record = false;
for (size_t j = 0; j < motif_lines[i].size(); ++j) {
if (line_type(motif_lines[i][j], version_line))
version_record = true;
}
if (!version_record)
motifs.push_back(Motif(motif_lines[i]));
}
return motifs;
}