-
Notifications
You must be signed in to change notification settings - Fork 1
/
trs2fasta.cpp
142 lines (128 loc) · 3.35 KB
/
trs2fasta.cpp
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
#include "piler2.h"
static void LogRecords(const TRSData *TRSs, int TRSCount)
{
Log(" Contig From To Length + Family\n");
Log("-------------------- ---------- ---------- ------ - ------\n");
for (int TRSIndex = 0; TRSIndex < TRSCount; ++TRSIndex)
{
const TRSData &TRS = TRSs[TRSIndex];
Log("%20.20s %10d %10d %6d %c %d.%d\n",
TRS.ContigLabel,
TRS.ContigFrom,
TRS.ContigTo,
TRS.ContigTo - TRS.ContigFrom + 1,
TRS.Rev ? '-' : '+',
TRS.SuperFamIndex,
TRS.FamIndex);
}
}
static int CmpTRS(const void *s1, const void *s2)
{
const TRSData *TRS1 = (const TRSData *) s1;
const TRSData *TRS2 = (const TRSData *) s2;
return TRS1->FamIndex - TRS2->FamIndex;
}
static char *FamFileName(const char *Path, int Fam, int SuperFam)
{
static char FileName[256];
char s[128];
if (SuperFam == -1)
sprintf(s, "/%d", Fam);
else
sprintf(s, "/%d.%d", SuperFam, Fam);
if (strlen(Path) + strlen(s) + 3 >= sizeof(FileName))
Quit("Path name too long");
strcpy(FileName, Path);
strcat(FileName, s);
return FileName;
}
static char *TRSLabel(const char *Prefix, const TRSData &TRS)
{
int n = (int) strlen(TRS.ContigLabel) + 128;
char *s = all(char, n);
if (TRS.SuperFamIndex == -1)
{
if (0 != Prefix)
sprintf(s, "%s.%d %s:%d%c",
Prefix,
TRS.FamIndex,
TRS.ContigLabel,
TRS.ContigFrom+1,
TRS.Rev ? '-' : '+');
else
sprintf(s, "%d.%s:%d%c",
TRS.FamIndex,
TRS.ContigLabel,
TRS.ContigFrom+1,
TRS.Rev ? '-' : '+');
}
else
{
if (0 != Prefix)
sprintf(s, "%s.%d.%d %s:%d%c",
Prefix,
TRS.SuperFamIndex,
TRS.FamIndex,
TRS.ContigLabel,
TRS.ContigFrom+1,
TRS.Rev ? '-' : '+');
else
sprintf(s, "%d.%d %s:%d%c",
TRS.SuperFamIndex,
TRS.FamIndex,
TRS.ContigLabel,
TRS.ContigFrom+1,
TRS.Rev ? '-' : '+');
}
return s;
}
void TRS2Fasta()
{
const char *TRSFileName = RequiredValueOpt("trs2fasta");
const char *SeqFileName = RequiredValueOpt("seq");
const char *Path = ValueOpt("path");
const char *strMaxFam = ValueOpt("maxfam");
const char *Prefix = ValueOpt("prefix");
int MaxFam = DEFAULT_MAX_FAM;
if (strMaxFam != 0)
MaxFam = atoi(strMaxFam);
if (0 == Path)
Path = ".";
ProgressStart("Reading seq file");
int SeqLength;
const char *Seq = ReadMFA(SeqFileName, &SeqLength);
ProgressDone();
Progress("Seq length %d bases, %.3g Mb", SeqLength, SeqLength/1e6);
ProgressStart("Read TRS file");
int TRSCount;
TRSData *TRSs = ReadTRS(TRSFileName, &TRSCount);
ProgressDone();
Progress("%d records", TRSCount);
ProgressStart("Sorting by family");
qsort((void *) TRSs, TRSCount, sizeof(TRSData), CmpTRS);
ProgressDone();
FILE *f = 0;
int CurrentFamily = -1;
int MemberCount = 0;
for (int TRSIndex = 0; TRSIndex < TRSCount; ++TRSIndex)
{
const TRSData &TRS = TRSs[TRSIndex];
if (TRS.FamIndex != CurrentFamily)
{
if (f != 0)
fclose(f);
char *FastaFileName = FamFileName(Path, TRS.FamIndex, TRS.SuperFamIndex);
f = OpenStdioFile(FastaFileName, FILEIO_MODE_WriteOnly);
CurrentFamily = TRS.FamIndex;
MemberCount = 0;
}
++MemberCount;
if (MemberCount > MaxFam)
continue;
const int From = ContigToGlobal(TRS.ContigFrom, TRS.ContigLabel);
const int Length = TRS.ContigTo - TRS.ContigFrom + 1;
char *Label = TRSLabel(Prefix, TRS);
WriteFasta(f, Seq + From, Length, Label, TRS.Rev);
freemem(Label);
}
}