File: mergearrays.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 (225 lines) | stat: -rwxr-xr-x 6,063 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "pilercr.h"
#include "sw.h"

int UnalignedLength(const std::string &s)
	{
	size_t L = s.size();
	int N = 0;
	for (size_t i = 0; i < L; ++i)
		if (s[i] != '-')
			++N;
	return N;
	}

static bool GloballyAlignable(const char *Seq1, unsigned L1, const char *Seq2,
  unsigned L2)
	{
	std::string Path;
	unsigned Start1;
	unsigned Start2;
	score_t Score = SWSimple(Seq1, L1, Seq2, L2, &Start1, &Start2, Path);

	if (Score <= 0)
		return false;

	int AlnLen = (int) Path.size();
	if (GetRatio(AlnLen, (int) L1) < 0.95)
		return false;

	return true;
	}

// is i = nj for n approximately integer in range 1 .. 4?
static int Multiple(int i, int j)
	{
	if (GetRatio(i, 1*j) >= 0.95)
		return 1;

	if (GetRatio(i, 2*j) >= 0.95)
		return 2;

	if (GetRatio(i, 3*j) >= 0.95)
		return 3;

	if (GetRatio(i, 4*j) >= 0.95)
		return 4;

	return -1;
	}

int DistanceAA(const ArrayAln &AA1, const ArrayAln &AA2)
	{
	int End1 = AA1.Pos + GetArrayLength(AA1) + GetAvgSpacerLength(AA1) - 1;
	int Pos2 = AA2.Pos;

	int ContigIndex1 = GlobalPosToContigIndex(End1);
	int ContigIndex2 = GlobalPosToContigIndex(Pos2);

	if (ContigIndex1 != ContigIndex2)
		return -1;

	int ci1 = GlobalPosToContigIndex(AA1.Pos);
	if (ContigIndex1 != ci1)
		Warning("ContigIndex1=%u GlobalPosToContigIndex(AA1.Pos=%d) = %d",
		  ContigIndex1, AA1.Pos, ci1);

	return Pos2 - End1 - 1;
	}

bool MergeAAs(ArrayAln &AA1, const ArrayAln &AA2)
	{
	int RepeatLength1 = GetAvgRepeatLength(AA1);
	int RepeatLength2 = GetAvgRepeatLength(AA2);

	if (RepeatLength1 != RepeatLength2)
		return false;

	int SpacerLength1 = GetAvgSpacerLength(AA1);
	int SpacerLength2 = GetAvgSpacerLength(AA2);

	if (GetRatio(SpacerLength1, SpacerLength2) < 0.95)
		return false;

// Is distance between arrays a multiple of repeat+spacer length?
	int Distance = DistanceAA(AA1, AA2);
	int RepeatPlusSpacerLength = RepeatLength1 + SpacerLength1;
	int MissedRepeatCount = Multiple(Distance, RepeatPlusSpacerLength);
	if (MissedRepeatCount < 0)
		return false;

	int ConsSeqLength1 = AA1.ConsSeq.Length();
	int ConsSeqLength2 = AA2.ConsSeq.Length();

	char *Seq1 = all(char, ConsSeqLength1 + 1);
	char *Seq2 = all(char, ConsSeqLength2 + 1);

	AA1.ConsSeq.ToString(Seq1, ConsSeqLength1 + 1);
	AA2.ConsSeq.ToString(Seq2, ConsSeqLength2 + 1);

	if (!GloballyAlignable(Seq1, ConsSeqLength1, Seq2, ConsSeqLength2))
		return false;

	IntVec RepeatStartPos;
	IntVec RepeatLengths;

	int LastSpacerStartPos = AA1.Pos + GetArrayLength(AA1);
	int EndPosOfLastRepeat = LastSpacerStartPos - 1;
	StrVec Repeats;
	for (int i = 0; i < MissedRepeatCount; ++i)
		{
		int GuessedRepeatPos = EndPosOfLastRepeat + SpacerLength1;
		int StartPos = GuessedRepeatPos - 8;
		int EndPos = GuessedRepeatPos + RepeatLength1 + 8;
		int SeqLength = EndPos - StartPos + 1;
		char *Seq = all(char, SeqLength);
		for (int i = 0; i < SeqLength; ++i)
			Seq[i] = g_SeqQ[StartPos + i];

		char *ConsSeq = all(char, ConsSeqLength1 + 1);
		AA1.ConsSeq.ToString(ConsSeq, ConsSeqLength1 + 1);

		std::string Path;
		unsigned StartSeq;
		unsigned StartCons;
		score_t Score = SWSimple(Seq, SeqLength, ConsSeq, ConsSeqLength1, 
		  &StartSeq, &StartCons, Path);

		int AlnLength = (int) Path.size();
		if (GetRatio(AlnLength, ConsSeqLength1) < 0.95)
			return false;

		std::string &Repeat = *new std::string;
		for (int i = 0; i < (int) StartCons; ++i)
			Repeat.push_back('-');

		int SeqPos = StartPos + StartSeq;
		int RepeatPos = SeqPos;
		RepeatStartPos.push_back(RepeatPos);
		int RepeatLength = 0;
		for (int i = 0; i < AlnLength; ++i)
			{
			if (Path[i] == 'M' || Path[i] == 'D')
				{
				char c = g_SeqQ[SeqPos];
				Repeat.push_back(c);
				++SeqPos;
				++RepeatLength;
				}
			else
				Repeat.push_back('-');
			}
		if (!RepeatLengths.empty() && RepeatLength != RepeatLengths[0])
			return false;
		RepeatLengths.push_back(RepeatLength);
		EndPosOfLastRepeat = RepeatPos + RepeatLength - 1;

		while (Repeat.size() < (size_t) ConsSeqLength1)
			Repeat.push_back('-');

		Repeats.push_back(Repeat);
		}

	for (int i = 0; i < MissedRepeatCount; ++i)
		AA1.Repeats.push_back(Repeats[i]);

	for (int i = 0; i < MissedRepeatCount; ++i)
		{
		std::string &LeftFlank = *new std::string;
		int LeftFlankStartPos = RepeatStartPos[i] - g_FlankSize;
		for (int i = 0; i < g_FlankSize; ++i)
			{
			char c = g_SeqQ[LeftFlankStartPos + i];
			LeftFlank.push_back(c);
			}
		AA1.LeftFlanks.push_back(LeftFlank);
		}

	int OldCount = (int) AA1.Spacers.size();

	std::string &Spacer = *new std::string;
	int RepeatStartPos0 = RepeatStartPos[0];
	int LastSpacerEndPos = RepeatStartPos0 - 1;
	assert(LastSpacerEndPos > LastSpacerStartPos);
	int Length = LastSpacerEndPos - LastSpacerStartPos + 1;
	for (int i = 0; i < Length; ++i)
		{
		char c = g_SeqQ[LastSpacerStartPos + i];
		Spacer.push_back(c);
		}
	AA1.Spacers[OldCount-1] = Spacer;

	for (int i = 0; i < MissedRepeatCount; ++i)
		{
		std::string &Spacer = *new std::string;
		int SpacerStartPos = RepeatStartPos[i] + UnalignedLength(AA1.Repeats[OldCount+i]);
		int SpacerEndPos;
		if (i == MissedRepeatCount - 1)
			SpacerEndPos = AA2.Pos - 1;
		else
			SpacerEndPos = RepeatStartPos[i+1] - 1;
		assert(SpacerEndPos > SpacerStartPos);
		int Length = SpacerEndPos - SpacerStartPos + 1;
		for (int i = 0; i < Length; ++i)
			{
			char c = g_SeqQ[SpacerStartPos + i];
			Spacer.push_back(c);
			}
		AA1.Spacers.push_back(Spacer);
		}

	size_t RepeatCount2 = AA2.Repeats.size();
	size_t RepeatLength = 0;
	for (size_t RepeatIndex = 0; RepeatIndex < RepeatCount2; ++RepeatIndex)
		{
		AA1.LeftFlanks.push_back(AA2.LeftFlanks[RepeatIndex]);
		std::string Repeat = AA2.Repeats[RepeatIndex];
		AA1.Repeats.push_back(Repeat);
		const std::string &Spacer = AA2.Spacers[RepeatIndex];
		size_t L = Spacer.size();
		AA1.Spacers.push_back(Spacer);
		}

	ValidateAA(AA1);
	Log("MergeAAs\n");
	return true;
	}