File: outdetailaa.cpp

package info (click to toggle)
pilercr 1.06%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 804 kB
  • sloc: cpp: 14,339; makefile: 67; sh: 3
file content (158 lines) | stat: -rwxr-xr-x 4,182 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
#include "pilercr.h"

static void OutputDetailHeader(const ArrayAln &AA)
	{
	unsigned RepeatColCount = (unsigned) AA.Repeats[0].size();

	char *Label;
	GlobalToLocal(AA.Pos, &Label);
	Out("\n\n");
	Out("Array %d\n", AA.Id);
	
	Out(">%s\n", Label);
	Out("\n");

	Out("       Pos  Repeat     %%id  Spacer  Left flank    Repeat");
	int Blanks = (int) RepeatColCount - 6;
	for (int i = 0; i < Blanks; ++i)
		Out(" ");
	Out("    Spacer\n");
	Out("==========  ======  ======  ======  ==========    ");
	//   1234567890  123456  123456  123456  1234567890
	for (unsigned i = 0; i < RepeatColCount; ++i)
		Out("=");
	Out("    ======\n");
	}

int GetAvgSpacerLength(const ArrayAln &AA)
	{
	size_t RepeatCount = AA.Repeats.size();
	size_t Sum = 0;
	for (size_t RepeatIndex = 0; RepeatIndex < RepeatCount - 1; ++RepeatIndex)
		Sum += AA.Spacers[RepeatIndex].size();
	return (int) (Sum / (RepeatCount - 1));
	}

int GetAvgRepeatLength(const ArrayAln &AA)
	{
	size_t RepeatCount = AA.Repeats.size();
	size_t Sum = 0;
	for (size_t RepeatIndex = 0; RepeatIndex < RepeatCount; ++RepeatIndex)
		Sum += AA.Repeats[RepeatIndex].size();
	return (int) (Sum / RepeatCount);
	}

static void OutputDetailFooter(const ArrayAln &AA)
	{
	unsigned RepeatColCount = (unsigned) AA.Repeats[0].size();

	Out("==========  ======  ======  ======  ==========    ");
	for (unsigned i = 0; i < RepeatColCount; ++i)
		Out("=");
	Out("\n");

	int RepeatCount = (int) AA.Repeats.size();
	int RepeatLength = (int) AA.Repeats[0].size();
	int SpacerLength = GetAvgSpacerLength(AA);

	Out("%10d  %6d          %6d                ",
	  RepeatCount, RepeatLength, SpacerLength);

	assert(AA.AlignedConsSeq.size() == RepeatColCount);
	for (unsigned i = 0; i < RepeatColCount; ++i)
		Out("%c", AA.AlignedConsSeq[i]);

	Out("\n");
	}

static double GetRepeatPctId(const ArrayAln &AA, size_t RepeatIndex)
	{
	assert(RepeatIndex < AA.Repeats.size());
	const std::string &Repeat = AA.Repeats[RepeatIndex];
	const Seq &ConsSeq = AA.AlignedConsSeq;
	size_t RepeatLength = Repeat.size();
	assert(ConsSeq.size() == RepeatLength);

	int Same = 0;
	for (size_t i = 0; i < RepeatLength; ++i)
		{
		if (toupper(ConsSeq[i]) == toupper(Repeat[i]))
			++Same;
		}

	return 100.0 * (double) Same / (double) RepeatLength;
	}

static void OutDetailRepeat(const ArrayAln &AA, size_t RepeatIndex, int &Pos)
	{
	const std::string &LeftFlank = AA.LeftFlanks[RepeatIndex];
	const std::string &Repeat = AA.Repeats[RepeatIndex];
	const std::string &Spacer = AA.Spacers[RepeatIndex];
	const Seq &ConsSeq = AA.AlignedConsSeq;

	size_t LeftFlankLength = LeftFlank.size();
	size_t RepeatLength = Repeat.size();
	size_t SpacerLength = Spacer.size();

	assert(LeftFlankLength == g_FlankSize);

	double PctId = GetRepeatPctId(AA, RepeatIndex);

	const ContigData &Contig = GlobalPosToContig(Pos);
	const char *Label = Contig.Label;
	char *TmpLabel;
	int LocalLo = GlobalToLocal(Pos, &TmpLabel);

	//          Pos  Repeat     %id  Spacer  Left flank    Repeat
	//   ==========  ======  ======  ======  ==========    ======
	//   1234567890  123456  123456  123456  1234567890
	Out("%10d  %6d  %6.1f  ",
	  LocalLo + 1,	// 1-based for user's benefit
	  RepeatLength,
	  PctId);

	const size_t RepeatCount = AA.Repeats.size();
	if (RepeatIndex == RepeatCount - 1)
		Out("        ");
	else
		Out("%6d  ", SpacerLength);

	for (size_t i = 0; i < LeftFlankLength; ++i)
		Out("%c", LeftFlank[i]);

	Out("    ");

	for (size_t i = 0; i < RepeatLength; ++i)
		{
		if (Repeat[i] == ConsSeq[i])
			{
			if (Repeat[i] == '-')
				Out("-");
			else
				Out(".");
			}
		else
			Out("%c", Repeat[i]);
		}

	Out("    ");

	for (size_t i = 0; i < SpacerLength; ++i)
		Out("%c", Spacer[i]);

	Out("\n");

	Pos += (int) (RepeatLength + SpacerLength);
	}

void OutDetailAA(const ArrayAln &AA)
	{
	OutputDetailHeader(AA);

	int Pos = AA.Pos;
	size_t RepeatCount = AA.Repeats.size();
	for (size_t RepeatIndex = 0; RepeatIndex < RepeatCount; ++RepeatIndex)
		OutDetailRepeat(AA, RepeatIndex, Pos);

	OutputDetailFooter(AA);
	}