Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use auto #39

Merged
merged 9 commits into from
Sep 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compare_doubles.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace compare_doubles {
/// checks if two doubles are approximately equal
///
bool is_equal(double one, double two);
}
}

#endif /* COMPARE_DOUBLES_H */
9 changes: 5 additions & 4 deletions src/f_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,18 @@ void f_config::assign_feature_by_pattern(fasta::SequenceList& sequences,
if (pattern.size() > 0) {
boost::regex re(pattern);
for (size_t i = 0; i < sequences.size(); ++i) {
std::string seq = fasta::sequence_to_string(sequences[i]);
std::string seq_nogaps = seq;
//std::string seq = fasta::sequence_to_string(sequences[i]);
auto seq = fasta::sequence_to_string(sequences[i]);
auto seq_nogaps = seq;
seq_nogaps.erase(std::remove(seq_nogaps.begin(), seq_nogaps.end(), '-'),
seq_nogaps.end());
for(auto it = boost::sregex_iterator(seq_nogaps.begin(), seq_nogaps.end(),
re);
it != boost::sregex_iterator();
++it)
{
int match_start = find_real_pos(seq, it->position());
int match_end = find_real_pos(seq, match_start + it->str().size());
auto match_start = find_real_pos(seq, it->position());
auto match_end = find_real_pos(seq, match_start + it->str().size());
for (int j = match_start; j < match_end; ++j) {
if (sequences[i].residues[j].codon[0] != '-') {
sequences[i].residues[j].features.push_back(feat_name);
Expand Down
14 changes: 7 additions & 7 deletions src/fasta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fasta::FastaData fasta::parse_fasta(std::string const& filename,
std::string line;
std::string header;
std::string seq_line;
bool in_sequence_section = true;
auto in_sequence_section = true;
fasta::FastaData fd;
while (std::getline(fastafile, line)) {
if (line.substr(0, 1) == ">") {
Expand Down Expand Up @@ -103,7 +103,7 @@ fasta::Sequence fasta::make_sequence(const std::string& description,

for (unsigned i = 0; i < codons.size(); i += codon_length) {
boost::regex re("[a-zA-Z0-9-]{" + std::to_string(codon_length) + "}");
std::string codon = codons.substr(i, codon_length);
auto codon = codons.substr(i, codon_length);

if (!boost::regex_match(codon, re)) {
throw std::runtime_error("Invalid codon: " + codon);
Expand Down Expand Up @@ -144,11 +144,11 @@ bool fasta::check_length(fasta::SequenceList const& sequences, int limit) {
if (limit == 0) {
limit = sequences.size();
}
bool result = true;
size_t prev_length = sequences[0].residues.size();
int i = 1;
auto result = true;
auto prev_length = sequences[0].residues.size();
auto i = 1;
while (result && i < limit) {
size_t length = sequences[i].residues.size();
auto length = sequences[i].residues.size();
if (length != prev_length) {
result = false;
}
Expand All @@ -159,7 +159,7 @@ bool fasta::check_length(fasta::SequenceList const& sequences, int limit) {

fasta::SequenceList fasta::remove_gaps(
const fasta::SequenceList& sequences) {
fasta::SequenceList s = sequences;
auto s = sequences;
for (auto& seq : s) {
seq.residues.clear();
}
Expand Down
6 changes: 3 additions & 3 deletions src/feature_scores.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ double FeatureScores::score_ptm(
//pop back last character to get just the ptm type
ptm_type.pop_back();
// level of annotation - last character of feature's name
char ptm_level = ptm_name.back();
auto ptm_level = ptm_name.back();
double ptm_score = 0.;
// first set ptm_score based on annotation level of the query ptm
if (ptm_level == '0') {
Expand All @@ -109,11 +109,11 @@ double FeatureScores::score_ptm(
// get occurrence based score
for (auto feat_it = m_occurences.begin();
feat_it != m_occurences.end(); ++feat_it) {
std::string i_name = feat_it->first;
auto i_name = feat_it->first;
// get just the ptm type without its level of annotation (last char)
std::string i_type = i_name.substr(0, i_name.size() - 1);
if (i_type == ptm_type) {
char i_level = i_name.back();
auto i_level = i_name.back();
double score = feat_it->second[position];
if (i_level == '0') {
result += score;
Expand Down
9 changes: 4 additions & 5 deletions src/kmad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,17 @@ int main(int argc, char *argv[]) {
}

// Load sequence data

fasta::FastaData fasta_data;
try {
fasta_data = fasta::parse_fasta(filename, codon_length);
} catch(const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
std::exit(EXIT_FAILURE);
}
bool gapped = false;
auto gapped = false;
// Combine sequence and feature settings
//
fasta::FastaData fasta_data_cfg = f_config::get_conf_data(
auto fasta_data_cfg = f_config::get_conf_data(
fasta_data, f_set, gapped);

// Perform the alignment
Expand All @@ -182,7 +181,7 @@ int main(int argc, char *argv[]) {
first_gapped, optimize, fade_out, no_feat);
} else {
gapped = true;
fasta::FastaData fasta_data_cfg_aligned = f_config::get_conf_data(
auto fasta_data_cfg_aligned = f_config::get_conf_data(
fasta_data, f_set, gapped);
if (refine_limit == 0) {
refine_limit = fasta_data.sequences.size();
Expand All @@ -201,7 +200,7 @@ int main(int argc, char *argv[]) {

// Write alignment to file
// TODO: al_out_index is always 1. Also, what is it?
int al_out_index = 1;
auto al_out_index = 1;
if (first_gapped) {
first_gapped = 0;
}
Expand Down
81 changes: 34 additions & 47 deletions src/msa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ std::vector<fasta::SequenceList> msa::run_msa(
// query_seq_list - the profiles are built based only on the first
// sequence
fasta::SequenceList query_seq_list = {fasta_data.sequences[0]};
profile::ProfileMap profile = profile::create_score_profile(
query_seq_list, sbst_mat);
auto profile = profile::create_score_profile(query_seq_list, sbst_mat);
std::vector<double> identities = {1};
if (!no_feat) {
f_profile.update_scores(query_seq_list, f_set, identities, fade_out);
Expand All @@ -41,7 +40,7 @@ std::vector<fasta::SequenceList> msa::run_msa(


std::vector<fasta::SequenceList> alignment;
int alignments_number = 0;
auto alignments_number = 0;
double cutoff = 0;
// pointer to the function performing single round of msa, can be either
// for gapped or ungapped first sequence
Expand All @@ -67,7 +66,7 @@ std::vector<fasta::SequenceList> msa::run_msa(
if (!one_round) {
for (int i = 8; i >= 0; --i) {
cutoff = double(i) / 10;
int prev_alignments = alignments_number;
auto prev_alignments = alignments_number;
alignment = perform_msa_round_ptr(fasta_data, fasta_data, profile,
f_profile, gap_open_pen,
end_pen, gap_ext_pen, cutoff,
Expand All @@ -88,7 +87,7 @@ std::vector<fasta::SequenceList> msa::run_msa(
}
// set alignments number to 0 to align (again)
// all sequences to the profile
int iterations = 1;
auto iterations = 1;
if (one_round) {
iterations = 1;
}
Expand All @@ -107,7 +106,7 @@ std::vector<fasta::SequenceList> msa::run_msa(
profile = profile::create_score_profile(alignment[0], sbst_mat);
}
if (optimize) {
int counter = 0;
auto counter = 0;
std::vector<fasta::SequenceList> previous;
while (!msa::compare_alignments(previous, alignment)
&& counter < 15) {
Expand Down Expand Up @@ -147,12 +146,11 @@ std::vector<fasta::SequenceList> msa::refine_alignment(
const bool fade_out, int refine_seq, const bool no_feat)
{
fasta::SequenceList query_seq = {fasta_data_plain.sequences[0]};
profile::ProfileMap profile_single = profile::create_score_profile(
query_seq, sbst_mat);
FeatureScores f_profile_single(fasta_data_plain.feature_list,
domain_modifier, ptm_modifier,
motif_modifier, strct_modifier,
fasta_data_plain.probabilities);
auto profile_single = profile::create_score_profile(query_seq, sbst_mat);
FeatureScores f_profile_single(
fasta_data_plain.feature_list, domain_modifier, ptm_modifier,
motif_modifier, strct_modifier, fasta_data_plain.probabilities
);
std::vector<double> identities = {1};
f_profile_single.update_scores(query_seq, f_set, identities, fade_out);
identities = msa::set_identities(fasta_data_plain, profile_single,
Expand All @@ -169,16 +167,15 @@ std::vector<fasta::SequenceList> msa::refine_alignment(
query_seq_list.push_back(fasta_data_alignment.sequences[i]);
}
// fasta::SequenceList query_seq_list = fasta_data_alignment.sequences;
profile::ProfileMap profile = profile::create_score_profile(
query_seq_list, sbst_mat);
auto profile = profile::create_score_profile(query_seq_list, sbst_mat);
if (!no_feat) {
f_profile.update_scores(query_seq_list, f_set, identities, fade_out);
}

// Align all sequences vs first to determine the identities

std::vector<fasta::SequenceList> alignment;
int alignments_number = 0;
auto alignments_number = 0;
double cutoff = 0;
// pointer to the function performing single round of msa, can be either
// for gapped or ungapped first sequence
Expand Down Expand Up @@ -228,23 +225,14 @@ std::vector<fasta::SequenceList> msa::refine_alignment(
alignments_number, f_set,
alignment, refine_seq, no_feat);
if (optimize) {
int counter = 0;
auto counter = 0;
std::vector<fasta::SequenceList> previous;
while (!compare_alignments(previous, alignment) && counter < 15) {
previous = alignment;
alignment = optimizer::optimize_alignment(alignment, domain_modifier,
motif_modifier, ptm_modifier, sbst_mat);
++counter;
}
// f_profile.update_scores(alignment[0], f_set, identities, fade_out);
// profile = profile::create_score_profile(alignment[0], sbst_mat);
// alignments_number = 0;
// cutoff = 0;
// alignment = perform_msa_round_ptr(fasta_data_plain, profile,
// f_profile, gap_open_pen,
// end_pen, gap_ext_pen, cutoff,
// codon_length, identities,
// alignments_number, f_set, alignment);
}
return alignment;
}
Expand All @@ -263,7 +251,7 @@ std::vector<double> msa::set_identities(
fasta::Sequence aligned_seq_uppercase;
//pairwise alignment with lowercase characters where chars were removed
fasta::Sequence aligned_seq_with_lower;
bool gapped = true;
auto gapped = true;
for (size_t i = 1; i < fasta_data.sequences.size(); ++i) {
// aligned_sequence: vector
// first element is a dummy polyA sequence to indicate where are the gaps
Expand All @@ -276,9 +264,8 @@ std::vector<double> msa::set_identities(
gap_ext_pen, codon_length, gapped, no_feat);


double identity = msa::calc_identity(aligned_sequence[0],
aligned_sequence[1],
fasta_data.sequences[0]);
auto identity = msa::calc_identity(
aligned_sequence[0], aligned_sequence[1], fasta_data.sequences[0]);
identities.push_back(identity);
}
return identities;
Expand All @@ -288,7 +275,7 @@ double msa::calc_identity(const fasta::Sequence& dummy_sequence,
const fasta::Sequence& aligned_sequence,
const fasta::Sequence& query_sequence) {
double identical_residues = 0;
int gap_count = 0;
auto gap_count = 0;
// sequences should be aligned (therefore lengths should be equal)
assert(aligned_sequence.residues.size() == dummy_sequence.residues.size());
for (unsigned i = 0; i < aligned_sequence.residues.size(); ++i) {
Expand All @@ -306,8 +293,8 @@ double msa::calc_identity(const fasta::Sequence& dummy_sequence,
fasta::SequenceList msa::remove_gaps(const fasta::SequenceList& alignment) {
fasta::Sequence new_seq;
fasta::SequenceList aligned_seq = {new_seq, new_seq};
char gap = '-';
bool lower_flag = false;
auto gap = '-';
auto lower_flag = false;
for (size_t i = 0; i < alignment[0].residues.size(); ++i) {
if (alignment[0].residues[i].codon[0] == gap) {
if (aligned_seq[1].residues.size() > 0) {
Expand Down Expand Up @@ -348,7 +335,7 @@ fasta::SequenceList msa::align_pairwise(const fasta::Sequence& input_sequence,
int codon_length,
const bool first_gapped,
const bool no_feat) {
int profile_length = profile.begin()->second.size();
auto profile_length = profile.begin()->second.size();
ScoringMatrix scores(profile_length, input_sequence.residues.size(),
gap_open_pen, end_pen, gap_ext_pen, no_feat);
scores.calculate_scores(input_sequence, profile, f_profile, codon_length);
Expand Down Expand Up @@ -389,7 +376,7 @@ std::vector<fasta::SequenceList> msa::perform_msa_round_ungapped(
alignment[1].push_back(fasta_data_alignment.sequences[i]);
}
}
int next_alignments = count_alignments(identity_cutoff, identities);
auto next_alignments = count_alignments(identity_cutoff, identities);
if (next_alignments > prev_alignments) {
fasta::SequenceList aligned_seq;
for (size_t i = start; i < fasta_data.sequences.size(); ++i) {
Expand Down Expand Up @@ -417,7 +404,7 @@ std::vector<fasta::SequenceList> msa::perform_msa_round_ungapped(

int msa::count_alignments(double identity_cutoff,
const std::vector<double>& identities) {
int count = 0;
auto count = 0;
for (auto& item: identities) {
if (item >= identity_cutoff) {
++count;
Expand All @@ -444,7 +431,7 @@ std::vector<fasta::SequenceList> msa::perform_msa_round_gapped(
const bool no_feat)
{
std::vector<fasta::SequenceList> alignment = {{}, {}};
int next_alignments = count_alignments(identity_cutoff, identities);
auto next_alignments = count_alignments(identity_cutoff, identities);
if (next_alignments > prev_alignments) {
fasta::SequenceList aligned_seq;
for (size_t i = 0; i < fasta_data.sequences.size(); ++i) {
Expand Down Expand Up @@ -477,10 +464,10 @@ std::vector<fasta::SequenceList> msa::merge_alignments(
std::vector<fasta::SequenceList> multi_alignment;
/// TODO: change it so that changing the format is no longer needed at the
/// end of the function
multi_alignment = {{pairwise_alignments[0][0],
pairwise_alignments[0][0]},
{pairwise_alignments[1][0],
pairwise_alignments[1][0]}};
multi_alignment = {
{pairwise_alignments[0][0], pairwise_alignments[0][0]},
{pairwise_alignments[1][0], pairwise_alignments[1][0]}
};

assert(pairwise_alignments[0].size() == pairwise_alignments[1].size());
for (size_t i = 1; i < pairwise_alignments[0].size(); ++i) {
Expand All @@ -507,10 +494,10 @@ std::vector<fasta::SequenceList> msa::add_alignment(
fasta::Sequence s;
std::vector<fasta::SequenceList> merged(multi_alignment.size() + 1,
fasta::SequenceList(2, s));
int i = 0;
int j = 0;
const std::vector<fasta::Residue> *profile1 = &multi_alignment[0][0].residues;
const std::vector<fasta::Residue> *profile2 = &pairwise_alignment[0].residues;
auto i = 0;
auto j = 0;
const auto *profile1 = &multi_alignment[0][0].residues;
const auto *profile2 = &pairwise_alignment[0].residues;
int length1 = profile1->size();
int length2 = profile2->size();

Expand Down Expand Up @@ -563,10 +550,10 @@ std::vector<fasta::SequenceList> msa::remove_gapcolumns(
std::vector<fasta::SequenceList> alignment)
{
std::vector<fasta::SequenceList> result = alignment;
int erased = 0;
auto erased = 0;
for (size_t i = 0; i < alignment[0][0].residues.size(); ++i) {
if (alignment[0][0].residues[i].codon[0] == '-') {
bool gaps = true;
auto gaps = true;
size_t j = 1;
while (gaps && j < alignment[0].size()) {
if (alignment[0][j].residues[i].codon[0] != '-') {
Expand All @@ -591,7 +578,7 @@ std::vector<fasta::SequenceList> msa::remove_gapcolumns(

bool msa::compare_alignments(const std::vector<fasta::SequenceList>& al1,
const std::vector<fasta::SequenceList>& al2) {
bool result = true;
auto result = true;
if (al1.size() != al2.size() || al1[0].size() != al2[0].size()) {
result = false;
}
Expand Down
Loading