Advertisement
Guest User

Untitled

a guest
Feb 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <algorithm>
  5. #include <sstream>
  6. #include <bitset>
  7. #include <cmath>
  8.  
  9. #define FASTA "/home/zuzu/CLionProjects/akwbIII/sample.fasta"
  10. #define QUAL "/home/zuzu/CLionProjects/akwbIII/sample.qual"
  11.  
  12. using namespace std;
  13.  
  14. struct sequence {
  15.     string name;
  16.     string nucleotydes;
  17.     vector<long int> qual;
  18. };
  19.  
  20. struct node {
  21.     string name;
  22.     string nucleotydes;
  23.     vector<long int> qual;
  24.     unsigned int number_of_sequence = 0;
  25.     unsigned int position = 0;
  26.     vector<node *> connected_nodes;
  27.  
  28.     bool operator==(node &other) {
  29.         return name == other.name && nucleotydes == other.nucleotydes && position == other.position;
  30.     }
  31. };
  32.  
  33. struct clique {
  34.     vector<unsigned int> positions;
  35.     vector<unsigned int> number_of_seq;
  36. };
  37.  
  38. struct series_of_cliques {
  39.     vector<clique> series;
  40.     unsigned int length = 0;
  41. };
  42.  
  43. bool find_node(vector<node *> &connected, node &node1) {
  44.     for (auto &it : connected) {
  45.         if (*it == node1) return true;
  46.     }
  47.     return false;
  48. }
  49.  
  50. vector<sequence> read_from_file() {
  51.     vector<sequence> all_sequences;
  52.     sequence one_sequence;
  53.     ifstream file(FASTA);
  54.     while (file.good()) {
  55.         getline(file, one_sequence.name);
  56.         getline(file, one_sequence.nucleotydes);
  57.         all_sequences.push_back(one_sequence);
  58.     }
  59.     file.close();
  60.  
  61.     file.open(QUAL);
  62.     string name_of_seq;
  63.     string line;
  64.     unsigned int temp_qual;
  65.     while (getline(file, name_of_seq))
  66.         for (auto &sequence_r : all_sequences)
  67.             if (name_of_seq == sequence_r.name) {
  68.                 getline(file, line);
  69.                 istringstream l(line);
  70.                 while (l >> temp_qual) sequence_r.qual.push_back(temp_qual);
  71.                 if (sequence_r.nucleotydes.size() != sequence_r.qual.size()) {
  72.                     cout << "Wielkość sekwencji nukleotydów nie zgadza się z liczbą oceny wiarygodności nukleotydów"
  73.                          << endl;
  74.                     cout << "Nukleotydów: " << sequence_r.nucleotydes.size() << endl << "Wiarygodności: "
  75.                          << sequence_r.qual.size() << endl;
  76.                     exit(1);
  77.                 }
  78.                 break;
  79.             }
  80.     file.close();
  81.  
  82.     cout << endl << endl;
  83.     return all_sequences;
  84. }
  85.  
  86. vector<vector<node>> make_graph(int &n, int q, vector<sequence> &all_sequences) {
  87.     vector<node> nodes_in_seq;
  88.     vector<vector<node>> graph;
  89.     unsigned int number = 0;
  90.     unsigned int pos = 0;
  91.     for (auto &sequence_r : all_sequences) {
  92.         pos = 0;
  93.         auto it = sequence_r.nucleotydes.begin() + n;
  94.         auto sec_it = sequence_r.nucleotydes.begin();
  95.         for (unsigned short int i = 0; i < (sequence_r.nucleotydes.length() - n + 1); i++) {
  96.             node temp_node;
  97.             temp_node.number_of_sequence = number;
  98.             temp_node.name = sequence_r.name;
  99.             string temp(sec_it, it);
  100.             temp_node.nucleotydes = temp;
  101.             sec_it++;
  102.             it++;
  103.             temp_node.position = pos;
  104.             if (sequence_r.qual.empty()) fprintf(stderr, "empty");
  105.             for (unsigned int j = pos; j < pos + n; j++) {
  106.                 temp_node.qual.push_back(sequence_r.qual[j]);
  107.             }
  108.             pos++;
  109.             nodes_in_seq.push_back(temp_node);
  110.         }
  111.         number++;
  112.         graph.push_back(nodes_in_seq);
  113.     }
  114.  
  115.     for (auto &sequences : graph)
  116.         for (auto &seqenc : sequences)
  117.             for (unsigned int quality_it = 0; quality_it < seqenc.qual.size(); quality_it++)
  118.                 if (seqenc.qual[quality_it] < q) {
  119.                     seqenc.nucleotydes.erase(seqenc.nucleotydes.begin() + quality_it);
  120.                     seqenc.qual.erase(seqenc.qual.begin() + quality_it);
  121.                 }
  122.  
  123.     for (auto &seq1 : graph)
  124.         for (auto &seq : graph)
  125.             for (auto &node_of_seq : seq1)
  126.                 for (auto node_of_other : seq)
  127.                     if (node_of_seq.number_of_sequence != node_of_other.number_of_sequence &&
  128.                         (node_of_seq.nucleotydes == node_of_other.nucleotydes ||
  129.                          node_of_seq.nucleotydes.find(node_of_other.nucleotydes) != string::npos)) {
  130.                         if (!find_node(node_of_seq.connected_nodes, node_of_other))
  131.                             node_of_seq.connected_nodes.push_back(&node_of_other);
  132.                         if (!find_node(node_of_other.connected_nodes, node_of_seq))
  133.                             node_of_other.connected_nodes.push_back(&node_of_seq);
  134.                     }
  135.  
  136.     return graph;
  137. }
  138.  
  139.  
  140. series_of_cliques find_series_of_cliques(vector<vector<node>> &graph) {
  141.     series_of_cliques best_series = series_of_cliques();
  142.     series_of_cliques temp_series = series_of_cliques();
  143.     clique temp_clique = clique();
  144.     clique last = clique();
  145.     int i = 0;
  146.     for (;;) {
  147.         for (auto &current_seq : graph[i]) {
  148.             if (last.positions.empty() || current_seq.position == last.positions[i] + 1) {
  149.                 for (auto &next_seq : graph[i + 1]) {
  150.                     if (temp_clique.positions.empty()) {
  151.                         if (find_node(current_seq.connected_nodes, next_seq)) {
  152.                             if (last.positions.empty() || next_seq.position == last.positions[i + 1] + 1) {
  153.                                 temp_clique.positions.push_back(current_seq.position);
  154.                                 temp_clique.number_of_seq.push_back(current_seq.number_of_sequence);
  155.                                 temp_clique.positions.push_back(next_seq.position);
  156.                                 temp_clique.number_of_seq.push_back(next_seq.number_of_sequence);
  157.                                 break;
  158.                             }
  159.                         }
  160.                     } else {
  161.                         if (find(temp_clique.positions.begin(), temp_clique.positions.end(),
  162.                                  current_seq.position) != temp_clique.positions.end()
  163.                             && find_node(current_seq.connected_nodes, next_seq)) {
  164.                             if (last.positions.empty() || next_seq.position == last.positions[i + 1] + 1) {
  165.                                 temp_clique.positions.push_back(next_seq.position);
  166.                                 temp_clique.number_of_seq.push_back(next_seq.number_of_sequence);
  167.                                 break;
  168.                             }
  169.                         }
  170.                     }
  171.                 }
  172.             }
  173.         }
  174.         if (i + 2 != temp_clique.positions.size()) {
  175.             i = 0;
  176.             if (temp_series.length > best_series.length) {
  177.                 best_series = temp_series;
  178.             }
  179.             temp_series.length = 0;
  180.             temp_series.series.clear();
  181.         }
  182.         i++;
  183.         if (temp_clique.positions.size() == graph.size()) {
  184.             temp_series.series.push_back(temp_clique);
  185.             temp_series.length++;
  186.             last = temp_clique;
  187.             temp_clique.positions.clear();
  188.             temp_clique.number_of_seq.clear();
  189.             i = 0;
  190.         }
  191.         if (temp_series.length > best_series.length) {
  192.             best_series = temp_series;
  193.         }
  194.         if (best_series.length > graph[0].size() - best_series.length || i + 1 == graph.size()) {
  195.             break;
  196.         }
  197.     }
  198.  
  199.     return best_series;
  200.  
  201. }
  202.  
  203. vector<vector<unsigned int>> comb(unsigned int &K) {
  204.     vector<vector<unsigned int>> results;
  205.     for (unsigned int i = 0; i < pow(2, 8) - 1; i++) {
  206.         bitset<7> t(i);
  207.         if (t.count() == K) {
  208.             vector<unsigned int> v1;
  209.             for (unsigned int j = 0; j < 7; j++)
  210.                 if (t[j]) v1.push_back(j);
  211.             results.push_back(v1);
  212.         }
  213.     }
  214.     return results;
  215. }
  216.  
  217. void print_best_clique(int &n, unsigned long number_of_sequences, vector<sequence> &seq, series_of_cliques &best) {
  218.     string temp;
  219.     cout << "Najlepsza seria klik o " << number_of_sequences << " wierzchołkach" << endl;
  220.     cout << "Długość serii: " << best.length << endl;
  221.     for (int i = 0; i < best.series.size(); i++) {
  222.         for (int j = 0; j < number_of_sequences; j++) {
  223.             cout << "Nr sekwencji: " << best.series[0].number_of_seq[j] << " Pozycja: " << best.series[0].positions[j]
  224.                  << " ";
  225.             temp = seq[best.series[0].number_of_seq[j]].nucleotydes.substr(
  226.                     static_cast<unsigned long>(best.series[0].positions[j]),
  227.                     (best.length + n));
  228.             cout << temp << endl;
  229.         }
  230.     }
  231.     cout << " pierwsza pozycja w sekwencji: " << best.series[0].positions[0] << endl;
  232. }
  233.  
  234. int main() {
  235.     int n = 5;
  236.     vector<sequence> seq = read_from_file();
  237.     cout << "read_from_file completed" << endl;
  238.     vector<vector<node>> graph = make_graph(n, 25, seq); // n, q, seq
  239.     cout << "make_graph completed" << endl;
  240.     vector<vector<node>> temp_graph;
  241.     series_of_cliques temp_clique;
  242.     series_of_cliques best = series_of_cliques();
  243.     for (unsigned int k = 4; k <= 7; k++) {
  244.         cout << "k=" << k << " started" << endl;
  245.         vector<vector<unsigned int>> combinations = comb(k);
  246.         for (auto &i : combinations) {
  247.             for (auto &j: i) {
  248.                 temp_graph.push_back(graph[j]);
  249.             }
  250.             temp_clique = find_series_of_cliques(temp_graph);
  251.             cout << "find_series_of_cliques completed" << endl;
  252.             if (temp_clique.length > best.length) best = temp_clique;
  253.         }
  254.         print_best_clique(n, combinations[0].size(), seq, best);
  255.     }
  256.  
  257.     return 0;
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement