File: clustconsr.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 (187 lines) | stat: -rwxr-xr-x 3,735 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "pilercr.h"

#define TRACE	0

// ClusterConsRC:
// Cluster consensus sequences considering the sequence
// or its reverse complement, but not both.

// Best entry is one with largest number
// of distances smaller than the threshold
// Indexes into DistFunc are 2i for i'th array,
// 2i+2 for reverse complement of i'th array.
static int FindBest(std::vector<ArrayAln *> &AAVec, const DistFunc &DF,
  BoolVec &Done, IntVec &Cluster)
	{
	Cluster.clear();

	const int ArrayCount = (int) AAVec.size();
	const int Count = DF.GetCount();
	assert(Count == 2*ArrayCount);

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

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

		for (int j = 0; j < Count; ++j)
			{
			if (i == j || Done[j/2])
				continue;
#if	TRACE
			Log("Dist(");
			AAVec[i/2]->ConsSeq.LogMeSeqOnly();
			if (i%2 == 1)
				Log("-");
			else
				Log("+");
			Log(",");
			AAVec[j/2]->ConsSeq.LogMeSeqOnly();
			if (j%2 == 1)
				Log("-");
			else
				Log("+");
			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/2])
				continue;
			if (DF.GetDist(Best, i) <= g_ClusterMaxDist)
				{
				ArrayAln &AA = *(AAVec[i/2]);
				AA.ClusterRevComp = (i%2 == 1);
				Done[i/2] = true;
				Cluster.push_back(i/2);
				}
			}
		}

	if (Cluster.size() == 0)
		return -1;

	return Best;
	}

// Enforce convention that first array is + strand.
static void FixStrand(std::vector<ArrayAln *> &AAVec, IntVec &Cluster)
	{
	int First = Cluster.front();
	const ArrayAln &FirstAA = *(AAVec[First]);
	if (!FirstAA.ClusterRevComp)
		return;

	const size_t N = Cluster.size();
	for (size_t i = 0; i < N; ++i)
		{
		int ArrayIndex = Cluster[i];
		assert(ArrayIndex < (int) AAVec.size());
		ArrayAln &AA = *(AAVec[ArrayIndex]);
		AA.ClusterRevComp = !AA.ClusterRevComp;
		}
	}

void ClusterConsRC(std::vector<ArrayAln *> &AAVec, IntVecVec &Clusters)
	{
	SeqVect Seqs;

	const size_t ArrayCount = AAVec.size();
	for (size_t i = 0; i < ArrayCount; ++i)
		{
		const ArrayAln &AA = *(AAVec[i]);
		Seqs.push_back((Seq *) &AA.ConsSeq);

		Seq *r = new Seq;
		r->Copy(AA.ConsSeq);
		r->RevComp();
		Seqs.push_back(r);
		}

	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(AAVec, DF, Done, Cluster);
		if (Best == -1)
			break;
		FixStrand(AAVec, Cluster);
		Clusters.push_back(Cluster);
		}

	for (size_t i = 0; i < ArrayCount; ++i)
		{
		if (!Done[i])
			{
			IntVec Cluster;
			Cluster.push_back(i);
			FixStrand(AAVec, Cluster);
			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
	}