-
Notifications
You must be signed in to change notification settings - Fork 7
/
connectPairs.cpp
158 lines (129 loc) · 4.8 KB
/
connectPairs.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// connectPairs.cpp
/*===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
* Author: Vyacheslav Brover
*
* File Description:
* Create sets of connected items
*
*/
#undef NDEBUG
#include "common.hpp"
using namespace Common_sp;
#include "version.inc"
#include "common.inc"
namespace
{
static const string ext ("component");
struct Item : Named, DisjointCluster
{
explicit Item (const string &name_arg)
: Named (name_arg)
{}
};
struct ThisApplication final : Application
{
ThisApplication ()
: Application ("Create disjoint sets of items")
{
version = VERSION;
addPositional ("in", "List of pairs of connected items: <item1> <item2>\nwhere <item1>, <item2> are strings with no spaces");
addPositional ("out", "If -pairs then output file with pairs <item> <tab> <main item>, otherwise output directory with the sets of connected items, where each set is named by its lexicographycally smaller item with the added extension " + strQuote ("." + ext));
addKey ("subset", "List of items. Item pairs are restricted to this list");
addFlag ("pairs", "<out> is a list of pairs: <item1> <item_min>, where <item_min> is lexicographycally smallest item of the cluster");
}
void body () const final
{
const string inFName = getArg ("in");
const string outFName = getArg ("out");
const string subsetFName = getArg ("subset");
const bool pairs = getFlag ("pairs");
unique_ptr<StringVector> subset;
if (! subsetFName. empty ())
{
subset. reset (new StringVector (subsetFName, (size_t) 10000, true)); // PAR
subset->sort ();
QC_ASSERT (subset->isUniq ());
}
unordered_map<string/*Item::name*/,Item*> items; items. rehash (100000); // PAR
// Not delete'd
{
LineInput fIn (inFName);
string s1, s2;
Istringstream iss;
while (fIn. nextLine ())
{
iss. reset (fIn. line);
s2. clear ();
iss >> s1 >> s2;
QC_ASSERT (! s2. empty ());
trim (s1);
trim (s2);
if (s1 == s2)
continue;
if (subset. get ())
{
if (! subset->containsFast (s1))
continue;
if (! subset->containsFast (s2))
continue;
}
if (! contains (items, s1))
items [s1] = new Item (s1);
if (! contains (items, s2))
items [s2] = new Item (s2);
items [s1] -> DisjointCluster::merge (* items [s2]);
}
}
map<const DisjointCluster*,VectorPtr<Item>> clusters;
for (auto& it : items)
clusters [it. second->getDisjointCluster ()] << it. second;
unique_ptr<OFStream> fOut;
if (pairs)
fOut. reset (new OFStream (outFName));
for (auto& it : clusters)
{
VectorPtr<Item>& cluster = it. second;
cluster. sort (Named::lessPtr);
ASSERT (! cluster . empty ());
if (! pairs)
fOut. reset (new OFStream (outFName, cluster [0] -> name, ext));
for (const Item* item : cluster)
if (pairs)
*fOut << item->name << '\t' << cluster [0] -> name << '\n';
else
*fOut << item->name << '\n';
if (! pairs)
fOut. reset ();
}
}
};
} // namespace
int main (int argc,
const char* argv[])
{
ThisApplication app;
return app. run (argc, argv);
}