Debian Codegraph @

leaff/partition.C

raw
1 #include "bio++.H"
2 #include "seqCache.H"
3
4 #include <math.h>
5
6 struct partition_s {
7 uint32 length;
8 uint32 index;
9 uint32 partition;
10 };
11
12
13 static
14 int
15 partition_s_compare(const void *A, const void *B) {
16 const partition_s *a = (const partition_s *)A;
17 const partition_s *b = (const partition_s *)B;
18 if (a->length < b->length)
19 return(1);
20 if (a->length > b->length)
21 return(-1);
22 return(0);
23 }
24
25
26 static
27 partition_s *
28 loadPartition(seqCache *F) {
29 uint32 n = F->getNumberOfSequences();
30 partition_s *p = new partition_s [n];
31
32 for (uint32 i=0; i<n; i++) {
33 p[i].length = F->getSequenceLength(i);
34 p[i].index = i;
35 p[i].partition = 0;
36 }
37
38 qsort(p, n, sizeof(partition_s), partition_s_compare);
39
40 return(p);
41 }
42
43
44 static
45 void
46 outputPartition(seqCache *F,
47 char *prefix,
48 partition_s *p, uint32 openP, uint32 n) {
49 char filename[1024];
50
51 // Check that everything has been partitioned
52 //
53 for (uint32 i=0; i<n; i++)
54 if (p[i].partition == 0)
55 fprintf(stderr, "ERROR: Failed to partition "uint32FMT"\n", i);
56
57 if (prefix) {
58
59 // This rewrites the source fasta file into partitioned fasta files
60 //
61 for (uint32 o=1; o<=openP; o++) {
62 sprintf(filename, "%s-"uint32FMTW(03)".fasta", prefix, o);
63
64 errno = 0;
65 FILE *file = fopen(filename, "w");
66 if (errno)
67 fprintf(stderr, "Couldn't open '%s' for write: %s\n", filename, strerror(errno));
68
69 for (uint32 i=0; i<n; i++)
70 if (p[i].partition == o) {
71 seqInCore *S = F->getSequenceInCore(p[i].index);
72 fprintf(file, ">%s\n", S->header());
73 fwrite(S->sequence(), sizeof(char), S->sequenceLength(), file);
74 fprintf(file, "\n");
75
76 if (S->sequenceLength() != p[i].length) {
77 fprintf(stderr, "Huh? '%s' "uint32FMT" != "uint32FMT"\n", S->header(), S->sequenceLength(), p[i].length);
78 }
79
80 delete S;
81 }
82
83 fclose(file);
84 }
85
86 } else {
87
88 // This dumps the partition information to stdout.
89 //
90 fprintf(stdout, uint32FMT"\n", openP);
91 for (uint32 o=1; o<=openP; o++) {
92 uint32 sizeP = 0;
93 for (uint32 i=0; i<n; i++)
94 if (p[i].partition == o)
95 sizeP += p[i].length;
96 fprintf(stdout, uint32FMT"]("uint32FMT")", o, sizeP);
97 for (uint32 i=0; i<n; i++)
98 if (p[i].partition == o)
99 fprintf(stdout, " "uint32FMT"("uint32FMT")", p[i].index, p[i].length);
100 fprintf(stdout, "\n");
101 }
102
103 }
104 }
105
106
107 void
108 partitionBySize(char *prefix, uint64 partitionSize, char *filename) {
109 seqCache *F = new seqCache(filename);
110 uint32 n = F->getNumberOfSequences();
111 partition_s *p = loadPartition(F);
112
113 uint32 openP = 1; // Currently open partition
114 uint32 sizeP = 0; // Size of open partition
115 uint32 seqsP = n; // Number of sequences to partition
116
117 // For any sequences larger than partitionSize, create
118 // partitions containing just one sequence
119 //
120 for (uint32 i=0; i<n; i++) {
121 if (p[i].length > partitionSize) {
122 p[i].partition = openP++;
123 seqsP--;
124 }
125 }
126
127 // For the remaining, iterate through the list,
128 // greedily placing the longest sequence that fits
129 // into the open partition
130 //
131 while (seqsP > 0) {
132 for (uint32 i=0; i<n; i++) {
133 if ((p[i].partition == 0) &&
134 (p[i].length + sizeP < partitionSize)) {
135 p[i].partition = openP;
136 sizeP += p[i].length;
137 seqsP--;
138 }
139 }
140
141 openP++;
142 sizeP = 0;
143 }
144
145 outputPartition(F, prefix, p, openP-1, n);
146
147 delete [] p;
148 delete F;
149 }
150
151
152 void
153 partitionByBucket(char *prefix, uint64 partitionSize, char *filename) {
154 seqCache *F = new seqCache(filename);
155 uint32 n = F->getNumberOfSequences();
156 partition_s *p = loadPartition(F);
157
158 if (partitionSize > n)
159 partitionSize = n;
160
161 // The size, in bases, of each partition
162 //
163 uint32 *s = new uint32 [partitionSize];
164 for (uint32 i=0; i<partitionSize; i++)
165 s[i] = 0;
166
167 // For each sequence
168 //
169 for (uint32 nextS=0; nextS<n; nextS++) {
170
171 // find the smallest partition
172 //
173 uint32 openP = 0;
174 for (uint32 i=0; i<partitionSize; i++)
175 if (s[i] < s[openP])
176 openP = i;
177
178 // add the next largest sequence to the open partition
179 //
180 s[openP] += p[nextS].length;
181 p[nextS].partition = openP+1;
182 }
183
184 outputPartition(F, prefix, p, (uint32)partitionSize, n);
185
186 delete [] p;
187 delete F;
188 }
189
190
191 void
192 partitionBySegment(char *prefix, uint64 numSegments, char *filename) {
193 seqCache *F = new seqCache(filename);
194 uint32 n = F->getNumberOfSequences();
195 partition_s *p = new partition_s [n];
196 uint32 numSeqPerPart = (uint32)ceil(n / (double)numSegments);
197
198 for (uint32 i=0; i<n; i++) {
199 p[i].length = F->getSequenceLength(i);
200 p[i].index = i;
201 p[i].partition = i / numSeqPerPart + 1;
202 }
203
204 outputPartition(F, prefix, p, numSegments, n);
205
206 delete [] p;
207 delete F;
208 }