Advertisement
Guest User

Untitled

a guest
Nov 15th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.97 KB | None | 0 0
  1. #include "math.h"
  2.  
  3. #include <map>
  4. #include <sys/types.h>
  5. #include <cstdlib>
  6. #include <stdio.h>
  7. #include <errno.h>
  8. #include <string>
  9. #include <iostream>
  10. #include <fstream>
  11. #include <vector>
  12. #include <sstream>
  13. #include <algorithm>
  14.  
  15. using namespace std;
  16.  
  17. struct SeedStruct {
  18.     string id;
  19. };
  20.  
  21. typedef vector<SeedStruct> seed_array;
  22.  
  23. // g++ complete_linkage
  24.  
  25. int main(int argc, char *argv[]) {
  26.     seed_array seedstructarray;
  27.     vector<seed_array> doublestructarray;
  28.     vector<seed_array> tmp_doublestructarray;
  29.  
  30.     map<string, float> idmap;
  31.     map<string, float>::iterator itid;
  32.  
  33.     map<string, float> idpairmap;
  34.     map<string, float>::iterator itidpair;
  35.  
  36.     // add infile
  37.     string infile = argv[1];
  38.     char* infname = const_cast<char*>(infile.c_str());
  39.  
  40.     ifstream in(infname);
  41.     string tmp_str;
  42.  
  43.     for (; getline(in, tmp_str, '\n');) {
  44.         istringstream intmpstr(tmp_str);
  45.         string tmp_id1, tmp_id2, tmp_dist;
  46.         float dist;
  47.  
  48.         getline(intmpstr, tmp_id1, ' ');
  49.         idmap[tmp_id1] = 1.0;
  50.         getline(intmpstr, tmp_id2, ' ');
  51.         idmap[tmp_id2] = 1.0;
  52.  
  53.         string tmp_idpair = tmp_id1 + "\t" + tmp_id2;
  54.         getline(intmpstr, tmp_dist, ' ');
  55.         dist = float(atof(tmp_dist.c_str()));
  56.         idpairmap[tmp_idpair] = dist;
  57.     }
  58.  
  59.     for (itid = idmap.begin(); itid != idmap.end(); ++itid) {
  60.         SeedStruct tmp_seedstruct;
  61.         tmp_seedstruct.id = itid->first;
  62.         seedstructarray.push_back(tmp_seedstruct);
  63.     }
  64.  
  65.     for (unsigned long int i = 0; i < seedstructarray.size(); ++i) {
  66.         vector<SeedStruct> tmp;
  67.         tmp.push_back(seedstructarray[i]);
  68.         doublestructarray.push_back(tmp);
  69.     }
  70.  
  71.     string str_cutoff_str = argv[2];
  72.     float cutoff = float(atof(str_cutoff_str.c_str()));
  73.     int loop = 0;
  74.  
  75.     for (; loop == 0;) {
  76.         float mindist = 999999;
  77.         unsigned long int currenti = 0, currentj = 0;
  78.  
  79.         for (unsigned long int i = 0; i < doublestructarray.size() - 1; ++i) {
  80.             vector<SeedStruct> tmpvector1 = doublestructarray[i];
  81.             for (unsigned long int j = i + 1; j < doublestructarray.size() - 1; ++j) {
  82.                 vector<SeedStruct> tmpvector2 = doublestructarray[j];
  83.  
  84.                 float maxdist = 0;
  85.                 for (unsigned long int k = 0; k < tmpvector1.size(); ++k) {
  86.                     string id1 = tmpvector1[k].id;
  87.                     for (unsigned long int l = 0; l < tmpvector2.size(); ++l) {
  88.                         string id2 = tmpvector2[l].id;
  89.                         string idpair = id1 + "\t" + id2;
  90.                         float dist = 0;
  91.  
  92.                         if (idpairmap.find(idpair) != idpairmap.end()) {
  93.                             itidpair = idpairmap.find(idpair);
  94.                             dist = itidpair->second;
  95.                         }
  96.  
  97.                         if (maxdist < dist) {
  98.                             maxdist = dist;
  99.                         }
  100.                     }
  101.                 }
  102.                 // End of inner for.
  103.  
  104.                 if (maxdist < mindist) {
  105.                     mindist = maxdist;
  106.                     currenti = i;
  107.                     currentj = j;
  108.                 }
  109.             }
  110.         }
  111.         // End of outer for.
  112.  
  113.         if (mindist <= cutoff) {
  114.             vector<SeedStruct> tmpvector = doublestructarray[currentj];
  115.             for (unsigned long int i = 0; i < tmpvector.size(); ++i) {
  116.                 doublestructarray[currenti].push_back(tmpvector[i]);
  117.             }
  118.  
  119.             for (unsigned long int i = 0; i < doublestructarray.size(); ++i) {
  120.                 if (i != currentj) {
  121.                     tmp_doublestructarray.push_back(doublestructarray[i]);
  122.                 }
  123.             }
  124.  
  125.             doublestructarray.clear();
  126.             for (unsigned long int i = 0; i < tmp_doublestructarray.size(); ++i) {
  127.                 doublestructarray.push_back(tmp_doublestructarray[i]);
  128.             }
  129.  
  130.             tmp_doublestructarray.clear();
  131.         }
  132.         else {
  133.             loop = 1;
  134.         }
  135.     }
  136.     // Main while cycle is over.
  137.  
  138.     std::cout << "First part time is: " << mid_time - start_time << "\n";
  139.     std::cout << "Second part time is: " << end_time - mid_time << "\n";
  140.  
  141.     ofstream outfile;
  142.     outfile.open(argv[3]);
  143.  
  144.     for (unsigned long int i = 0; i < doublestructarray.size(); ++i) {
  145.         vector<SeedStruct> tmpvector = doublestructarray[i];
  146.         string* id_array = new string[tmpvector.size()];
  147.         for (unsigned long int j = 0; j < tmpvector.size(); ++j) {
  148.         id_array[j] = tmpvector[j].id;
  149.     }
  150.  
  151.     for (unsigned long int j = 0; j < tmpvector.size(); ++j) {
  152.         outfile << id_array[j] << "\t";
  153.     }
  154.  
  155.     outfile << endl;
  156.     delete id_array;
  157.     }
  158.  
  159.     outfile.close();
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement