File: clustcons.cpp

package info (click to toggle)
pilercr 1.06%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 844 kB
  • sloc: cpp: 14,339; makefile: 67; sh: 36
file content (143 lines) | stat: -rwxr-xr-x 2,597 bytes parent folder | download | duplicates (2)
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
#include "pilercr.h"

#define TRACE	0

// Best entry is one with largest number
// of distances smaller than the threshold
static int FindBest(const std::vector<ArrayData *> &ADVec, 
  const DistFunc &DF, BoolVec &Done, IntVec &Cluster)
	{
	Cluster.clear();

	const int Count = DF.GetCount();

	IntVec NrUnderThresh;
	for (int i = 0; i < Count; ++i)
		NrUnderThresh.push_back(0);

	for (int i = 0; i < Count; ++i)
		{
		if (Done[i])
			continue;

		for (int j = 0; j < Count; ++j)
			{
			if (i == j || Done[j])
				continue;
#if	TRACE
			Log("Dist(");
			ADVec[i]->ConsSeq.LogMeSeqOnly();
			Log(",");
			ADVec[j]->ConsSeq.LogMeSeqOnly();
			Log(") = %.1f", DF.GetDist(i, j));
#endif
			if (DF.GetDist(i, j) <= g_ClusterMaxDist)
				{
#if	TRACE
				Log(" Under\n");
#endif
				++(NrUnderThresh[i]);
				}
			else
				{
#if	TRACE
				Log(" Over\n");
#endif
				;
				}
			}
		}
#if	TRACE
	{
	Log("FindBest:\n");
	for (int i = 0; i < Count; ++i)
		Log("  i=%d Done=%c NrUnder=%d\n", i, Done[i] ? 'T' : 'F', NrUnderThresh[i]);
	}
#endif

	int Best = -1;
	unsigned BestCount = 0;
	for (int i = 0; i < Count; ++i)
		{
		if (NrUnderThresh[i] > BestCount)
			{
			Best = i;
			BestCount = NrUnderThresh[i];
			}
		}

	if (Best >= 0)
		{
		for (int i = 0; i < Count; ++i)
			{
			if (Done[i])
				continue;
			if (DF.GetDist(Best, i) <= g_ClusterMaxDist)
				{
				Done[i] = true;
				Cluster.push_back(i);
				}
			}
		}

	return Best;
	}

void ClusterCons(const std::vector<ArrayData *> &ADVec, IntVecVec &Clusters)
	{
	SeqVect Seqs;

	const size_t ArrayCount = ADVec.size();
	for (size_t i = 0; i < ArrayCount; ++i)
		{
		const ArrayData &AD = *(ADVec[i]);
		Seq *s = new Seq;
		s->Copy(AD.ConsSeq);
		Seqs.push_back(s);
		}

	DistFunc DF;
	KmerDist(Seqs, DF);

#if TRACE
	DF.LogMe();
#endif

	BoolVec Done;
	for (size_t i = 0; i < ArrayCount; ++i)
		Done.push_back(false);

	for (;;)
		{
		IntVec Cluster;
		int Best = FindBest(ADVec, DF, Done, Cluster);
		if (Best == -1)
			break;
		Clusters.push_back(Cluster);
		}

	for (size_t i = 0; i < ArrayCount; ++i)
		{
		if (!Done[i])
			{
			IntVec Cluster;
			Cluster.push_back(i);
			Clusters.push_back(Cluster);
			}
		}

#if	TRACE
	{
	Log("%d clusters\n", (int) Clusters.size());
	for (size_t i = 0; i < (int) Clusters.size(); ++i)
		{
		const IntVec &Cluster = Clusters[i];
		Log("  ");
		size_t N = Cluster.size();
		for (size_t j = 0; j < N; ++j)
			Log(" %d", Cluster[j]);
		Log("\n");
		}
	}
#endif
	}