File: pilercr.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 (264 lines) | stat: -rwxr-xr-x 7,071 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "pilercr.h"

// for getpid:
#if	WIN32
#include <process.h>
#else
#include <sys/types.h>
#include <unistd.h>
#endif

int k;
char *g_SeqQ = 0;
ContigData *ContigsQ = 0;
int SeqLengthQ = -1;
int *ContigMapQ = 0;
int ContigCountQ = -1;
bool FilterComp = false;
int g_MinHitLength = 16;
static double g_MinId = 0.94;
unsigned g_AcceptedHitCount = 0;
unsigned g_NotAcceptedHitCount = 0;
std::vector<PileData> g_Piles;
int g_PileCount;
std::vector<DPHit> g_Hits;
int g_HitCount;
IntVec g_HitIndexToPileIndexA;
IntVec g_HitIndexToPileIndexB;
std::vector<ImageData> g_Images;
int g_ImageCount;
int g_Diameter;
extern bool g_ShowProgress;

FILE *fDiscardedHits = 0;
FILE *fPiles = 0;
FILE *g_fOut = 0;
FILE *g_fSeqs = 0;

unsigned g_DiscardedHitCount = 0;

static const char *GetFilterOutFileName(const char *FilterOutPrefix, const char *Ext)
	{
	if (0 == FilterOutPrefix)
		{
		static char s[32];
		sprintf(s, "./_pf%d", getpid());
		FilterOutPrefix = s;
		}
	size_t n = strlen(FilterOutPrefix) + strlen(Ext) + 1;
	char *FileName = all(char, (int) n);
	strcpy(FileName, FilterOutPrefix);
	strcat(FileName, Ext);
	assert(strlen(FileName) == n - 1);
	return FileName;
	}

void PILERCR()
	{
	const char *InFileName = RequiredValueOpt("in");
	const char *OutFileName = RequiredValueOpt("out");

	const char *HitsFileName = ValueOpt("hits");
	const char *DiscardedHitsFileName = ValueOpt("discardedhits");
	const char *PilesFileName = ValueOpt("piles");
	const char *SeqsFileName = ValueOpt("seq");
	const char *FilterOutPrefix = ValueOpt("filterout");

	if (SeqsFileName != 0)
		g_fSeqs = CreateStdioFile(SeqsFileName);

	g_ShowHits = FlagOpt("showhits");
	g_LogHits = FlagOpt("loghits");
	g_LogImages = FlagOpt("logimages");
	g_LogAlns = FlagOpt("logalns");
	g_NoInfo = FlagOpt("noinfo");

	IntOpt("minhitlength", &g_MinHitLength);
	IntOpt("minrepeat", &g_MinRepeatLength);
	IntOpt("maxrepeat", &g_MaxRepeatLength);
	IntOpt("minspacer", &g_MinSpacerLength);
	IntOpt("maxspacer", &g_MaxSpacerLength);
	IntOpt("minarray", &g_MinArraySize);
	FloatOpt("minid", &g_MinId);
	FloatOpt("minrepeatratio", &g_MinRepeatRatio);
	FloatOpt("minspacerratio", &g_MinSpacerRatio);
	FloatOpt("mincons", &g_MinCons);

	bool Quiet = FlagOpt("quiet");
	if (Quiet)
		g_ShowProgress = false;

	g_DraftMinArraySize = g_MinArraySize - 2;
	if (g_DraftMinArraySize < 3)
		g_DraftMinArraySize = 3;

	Progress("Reading sequences from %s", InFileName);
	g_SeqQ = ReadMFA(InFileName, &SeqLengthQ, &ContigsQ,
	  &ContigCountQ, &ContigMapQ);
	ProgressDone();

	Progress("%d sequences, total length %d bases (%.0f Mb)",
		ContigCountQ,
		SeqLengthQ,
		SeqLengthQ/1e6);

	int g_MinHitLength = g_DraftMinRepeatLength;
	g_Diameter = 2*g_DraftMaxRepeatLength + g_DraftMaxSpacerLength;

	if (g_Diameter > SeqLengthQ)
		g_Diameter = SeqLengthQ;

	g_MaxDPHitLen = (3*g_DraftMaxRepeatLength)/2;

	FilterParams FP;
	DPParams DP;
	GetParams(SeqLengthQ, g_Diameter, g_MinHitLength, g_MinId, &FP, &DP);

	k = FP.WordSize;

	const double SeedPctId = (1.0 - (double) FP.SeedDiffs / (double) FP.SeedLength)*100.0;
	const double MemRequired = TotalMemRequired(SeqLengthQ, FP);
	const double AvgIndexList = AvgIndexListLength(SeqLengthQ, FP);

	Log("Filter parameters:\n");
	Log("   Word size       %d\n", FP.WordSize);
	Log("   Seed length     %d\n", FP.SeedLength);
	Log("   Seed diffs      %d\n", FP.SeedDiffs);
	Log("   Seed min id     %.1f%%\n", SeedPctId);
	Log("   Tube offset     %d\n", FP.TubeOffset);
	if (AvgIndexList > 2)
		Log("   Avg index list  %.1f\n", AvgIndexList);
	else
		Log("   Avg index list  %.2g\n", AvgIndexList);
	Log("DP parameters:\n");
	Log("   Min length      %d\n", DP.g_MinHitLength);
	Log("   Min id          %.0f%%\n", DP.MinId*100.0);
	Log("Estd. memory       %.0f Mb\n", MemRequired/1e6);
	Log("RAM                %.0f Mb\n", GetRAMSize()/1e6);

	int *Finger = 0;
	int *Pos = 0;

	const char *strFilterOutFileName = GetFilterOutFileName(FilterOutPrefix, ".f.tmp");
	FILE *fFilterOut = OpenStdioFile(strFilterOutFileName, FILEIO_MODE_ReadWrite);
	SetFilterOutFile(fFilterOut);

	FilterB(g_SeqQ, SeqLengthQ, FP);

	const int FilterHitCount = GetFilterHitCount();
	Progress("%d filter hits", FilterHitCount);

	FILE *fFilterOutComp = 0;
	int FilterHitCountComp = 0;
	const char *strFilterOutFileNameComp = 0;

	Progress("Read filter hits");
	FilterHit *FilterHits = ReadFilterHits(fFilterOut, FilterHitCount);
	CloseFilterOutFile();
	if (0 == FilterOutPrefix)
		remove(strFilterOutFileName);
	Progress("%d filter hits", FilterHitCount);
//	SaveFilterHits(FilterHits, FilterHitCount);

	Progress("Merge filter hits");
	int TrapCount;
	Trapezoid *Traps = MergeFilterHits(g_SeqQ, SeqLengthQ, g_SeqQ, SeqLengthQ,
	  true, FilterHits, FilterHitCount, FP, &TrapCount);
	LogTraps(Traps);
	SaveTraps(Traps);

	freemem(FilterHits);
	FilterHits = 0;

	const int SumLengths = SumTrapLengths(Traps);
	Progress("%d trapezoids, total length %d", TrapCount, SumLengths);

	if (DiscardedHitsFileName != 0)
		fDiscardedHits = OpenStdioFile(DiscardedHitsFileName,
		  FILEIO_MODE_WriteOnly);
	
	AlignTraps(g_SeqQ, SeqLengthQ, g_SeqQ, SeqLengthQ, Traps, TrapCount, false, DP);

	if (HitsFileName != 0)
		{
		FILE *fHits = OpenStdioFile(HitsFileName, FILEIO_MODE_WriteOnly);
		WriteDPHits(fHits, false);
		fclose(fHits);
		}

	Progress("Sorting hits");
	std::sort(g_Hits.begin(), g_Hits.end(), Cmp_Lo);

	if (g_LogHits)
		LogHits();

	if (fDiscardedHits != 0)
		{
		fclose(fDiscardedHits);
		fDiscardedHits = 0;
		}

	Progress("%d DP hits, %d accepted",
	  g_NotAcceptedHitCount + g_AcceptedHitCount, g_AcceptedHitCount);
	Progress("%u discarded hits", g_DiscardedHitCount);
	HitsToImages();

	Progress("Finding piles");
	FindPiles();
	ExpandPiles();
	WritePiles();
	Progress("%d piles found", (int) g_Piles.size());

	Progress("Building graph");
	EdgeList Edges;
	MakeGraph(Edges);

	Progress("Find connected components");
	CompList Comps;
	GetConnComps(Edges, Comps, g_DraftMinArraySize);
	Progress("%d connected components", (int) Comps.size());

	Progress("Find arrays");
	std::vector<ArrayData *> ADVec;
	for (CompList::const_iterator p = Comps.begin(); p != Comps.end(); ++p)
		{
		const CompData *Comp = *p;
		FindArrays(*Comp, ADVec);
		}

	g_fOut = CreateStdioFile(OutFileName);

	if (!g_NoInfo)
		Info();

	std::vector<ArrayAln *> AAVec;
	AlignArrays(ADVec, AAVec);

	const int ArrayCount = (int) AAVec.size();
	Progress("%d arrays found", (int) AAVec.size());

	Out(PILERCR_LONG_VERSION "\n");
	Out("By Robert C. Edgar\n\n");
	Out("%s: %d putative CRISPR arrays found.\n\n", InFileName, ArrayCount);

	if (ArrayCount == 0)
		return;

	OutArrays(AAVec);
	fclose(g_fOut);
	g_fOut = 0;

	if (g_fSeqs != 0)
		{
		SaveSeqs(g_fSeqs, AAVec);
		g_fSeqs = 0;
		}

	freemem(g_SeqQ);
	g_SeqQ = 0;

	free(Traps);
	Traps = 0;

	Progress("Done");
	}