Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "math.h"
- #include <map>
- #include <sys/types.h>
- #include <cstdlib>
- #include <stdio.h>
- #include <errno.h>
- #include <string>
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <sstream>
- #include <algorithm>
- using namespace std;
- struct SeedStruct {
- string id;
- };
- typedef vector<SeedStruct> seed_array;
- // g++ complete_linkage
- int main(int argc, char *argv[]) {
- seed_array seedstructarray;
- vector<seed_array> doublestructarray;
- vector<seed_array> tmp_doublestructarray;
- map<string, float> idmap;
- map<string, float>::iterator itid;
- map<string, float> idpairmap;
- map<string, float>::iterator itidpair;
- // add infile
- string infile = argv[1];
- char* infname = const_cast<char*>(infile.c_str());
- ifstream in(infname);
- string tmp_str;
- for (; getline(in, tmp_str, '\n');) {
- istringstream intmpstr(tmp_str);
- string tmp_id1, tmp_id2, tmp_dist;
- float dist;
- getline(intmpstr, tmp_id1, ' ');
- idmap[tmp_id1] = 1.0;
- getline(intmpstr, tmp_id2, ' ');
- idmap[tmp_id2] = 1.0;
- string tmp_idpair = tmp_id1 + "\t" + tmp_id2;
- getline(intmpstr, tmp_dist, ' ');
- dist = float(atof(tmp_dist.c_str()));
- idpairmap[tmp_idpair] = dist;
- }
- for (itid = idmap.begin(); itid != idmap.end(); ++itid) {
- SeedStruct tmp_seedstruct;
- tmp_seedstruct.id = itid->first;
- seedstructarray.push_back(tmp_seedstruct);
- }
- for (unsigned long int i = 0; i < seedstructarray.size(); ++i) {
- vector<SeedStruct> tmp;
- tmp.push_back(seedstructarray[i]);
- doublestructarray.push_back(tmp);
- }
- string str_cutoff_str = argv[2];
- float cutoff = float(atof(str_cutoff_str.c_str()));
- int loop = 0;
- for (; loop == 0;) {
- float mindist = 999999;
- unsigned long int currenti = 0, currentj = 0;
- for (unsigned long int i = 0; i < doublestructarray.size() - 1; ++i) {
- vector<SeedStruct> tmpvector1 = doublestructarray[i];
- for (unsigned long int j = i + 1; j < doublestructarray.size() - 1; ++j) {
- vector<SeedStruct> tmpvector2 = doublestructarray[j];
- float maxdist = 0;
- for (unsigned long int k = 0; k < tmpvector1.size(); ++k) {
- string id1 = tmpvector1[k].id;
- for (unsigned long int l = 0; l < tmpvector2.size(); ++l) {
- string id2 = tmpvector2[l].id;
- string idpair = id1 + "\t" + id2;
- float dist = 0;
- if (idpairmap.find(idpair) != idpairmap.end()) {
- itidpair = idpairmap.find(idpair);
- dist = itidpair->second;
- }
- if (maxdist < dist) {
- maxdist = dist;
- }
- }
- }
- // End of inner for.
- if (maxdist < mindist) {
- mindist = maxdist;
- currenti = i;
- currentj = j;
- }
- }
- }
- // End of outer for.
- if (mindist <= cutoff) {
- vector<SeedStruct> tmpvector = doublestructarray[currentj];
- for (unsigned long int i = 0; i < tmpvector.size(); ++i) {
- doublestructarray[currenti].push_back(tmpvector[i]);
- }
- for (unsigned long int i = 0; i < doublestructarray.size(); ++i) {
- if (i != currentj) {
- tmp_doublestructarray.push_back(doublestructarray[i]);
- }
- }
- doublestructarray.clear();
- for (unsigned long int i = 0; i < tmp_doublestructarray.size(); ++i) {
- doublestructarray.push_back(tmp_doublestructarray[i]);
- }
- tmp_doublestructarray.clear();
- }
- else {
- loop = 1;
- }
- }
- // Main while cycle is over.
- std::cout << "First part time is: " << mid_time - start_time << "\n";
- std::cout << "Second part time is: " << end_time - mid_time << "\n";
- ofstream outfile;
- outfile.open(argv[3]);
- for (unsigned long int i = 0; i < doublestructarray.size(); ++i) {
- vector<SeedStruct> tmpvector = doublestructarray[i];
- string* id_array = new string[tmpvector.size()];
- for (unsigned long int j = 0; j < tmpvector.size(); ++j) {
- id_array[j] = tmpvector[j].id;
- }
- for (unsigned long int j = 0; j < tmpvector.size(); ++j) {
- outfile << id_array[j] << "\t";
- }
- outfile << endl;
- delete id_array;
- }
- outfile.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement