-
Notifications
You must be signed in to change notification settings - Fork 2
/
FlowClus.c
3333 lines (2900 loc) · 86.2 KB
/
FlowClus.c
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
John M. Gaspar (jsh58@wildcats.unh.edu)
June 2013 (updated 1/14, 3/14)
This program can both filter and denoise reads
produced by 454 and Ion Torrent.
For a complete description of the usage and
parameters, please see the accompanying README.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "FlowClus.h"
// global variables
static char* line;
static Primer* primo;
void freeMemory(void);
/* void usage(void)
* Prints usage to stderr.
*/
void usage(void) {
fprintf(stderr, "FlowClus (version %s) by John M. Gaspar (jsh58@wildcats.unh.edu)\n", VERSION);
fprintf(stderr, "*** For a complete description of the parameters, see the README ***\n");
fprintf(stderr, "Usage: ./FlowClus {%s master.csv} [optional parameters]\n", MASTERFILE);
fprintf(stderr, "Required parameter:\n");
fprintf(stderr, " %s Input master file with primer and mid tag sequences\n", MASTERFILE);
fprintf(stderr, "Optional parameters:\n");
fprintf(stderr, " %s Option to print status updates while running\n", STATUSOPT);
fprintf(stderr, " %s Option to perform filtering only\n", CLEANOPT);
fprintf(stderr, " %s Option to perform denoising only\n", DENOPT);
fprintf(stderr, " %s Option to perform filtering and denoising\n", BOTHOPT);
fprintf(stderr, " %s Input sff.txt file (req'd if filtering)\n", SFFFILE);
fprintf(stderr, " %s File extension for filtered flowgrams\n", FLOWEXT);
fprintf(stderr, " %s Output fasta file after filtering\n", OUTFILE);
fprintf(stderr, " %s Output fasta file after denoising\n", DENFASTA);
fprintf(stderr, " %s Option to produce \"QIIME-style\" output fasta file(s)\n", NOMIDOPT);
fprintf(stderr, " %s Output file for counts of truncated and eliminated reads\n", ERRFILE);
fprintf(stderr, " %s Output file for detailed filtering information for each read\n", FILFILE);
fprintf(stderr, " %s Output file for denoising \"misses\"\n", MISSFILE);
fprintf(stderr, " %s Option to produce consensus flowgram and mapping files after denoising\n", DENPOPT);
fprintf(stderr, " %s File extension for denoised flowgrams\n", DENFEXT);
fprintf(stderr, " %s File extension for mapping files\n", DENMEXT);
fprintf(stderr, " %s Option to produce output fasta files for de novo chimera checking\n", CHIMOPT);
fprintf(stderr, " %s File extension for output fasta files for UCHIME\n", UCHEXT);
fprintf(stderr, " %s File extension for output fasta files for Perseus\n", PEREXT);
fprintf(stderr, " %s File extension for output mapping file for chimera checking\n", UMAPEXT);
fprintf(stderr, " %s Input file containing distances for each flow value\n", SDFILE);
fprintf(stderr, " %s Mismatches to mid tag sequence to allow\n", MIDMIS);
fprintf(stderr, " %s Mismatches to primer sequence to allow\n", PRIMMIS);
fprintf(stderr, " %s Minimum sequence length\n", MINSLEN);
fprintf(stderr, " %s Maximum sequence length for elimination\n", MAXSLEN);
fprintf(stderr, " %s Maximum sequence length for truncation\n", MAXTRLEN);
fprintf(stderr, " %s Maximum number of ambiguous bases to allow\n", MAXAMBIG);
fprintf(stderr, " %s Maximum number of ambiguous bases to allow before truncation\n", OKAMBIG);
fprintf(stderr, " %s Maximum homopolymer length to allow\n", MAXHOMO);
fprintf(stderr, " %s Maximum homopolymer length to allow before truncation\n", OKHOMO);
fprintf(stderr, " %s Option to remove the reverse primer from reads\n", REVMOPT);
fprintf(stderr, " %s Option to require the reverse primer in reads\n", REVQOPT);
fprintf(stderr, " %s Mismatches to reverse primer sequence to allow\n", REVMIS);
fprintf(stderr, " %s Minimum average quality score\n", AVGQUAL);
fprintf(stderr, " %s Length of sliding window of quality scores\n", WINDOWLEN);
fprintf(stderr, " %s Minimum average quality score for sliding window\n", WINDOWAVG);
fprintf(stderr, " %s Option to eliminate a read with a low quality window\n", WINDOWOPT);
fprintf(stderr, " %s Absolute maximum flow value to analyze\n", MAXFLOW);
fprintf(stderr, " %s Minimum flowgram length\n", MINFLEN);
fprintf(stderr, " %s Maximum flowgram length\n", MAXFLEN);
fprintf(stderr, " %s Noisy interval flow value minimum\n", MININT);
fprintf(stderr, " %s Noisy interval flow value maximum\n", MAXINT);
fprintf(stderr, " %s Maximum flow value (for truncation)\n", MAXNVAL);
fprintf(stderr, " %s Minimum flow value for 4 consecutive flows\n", NOFLOW);
fprintf(stderr, " %s Constant value for denoising\n", CINTER);
fprintf(stderr, " %s Number of distances for denoising\n", ZINTER);
fprintf(stderr, " %s Option to denoise using a trie\n", TRIEOPT);
freeMemory();
exit(-1);
}
/* int error(char*)
* Prints the given error message to stderr.
*/
int error(char* val, int err) {
fprintf(stderr, "Error! %s", val);
char* msg;
if (err == ERRMEM)
msg = MERRMEM;
else if (err == ERRCLOSE)
msg = MERRCLOSE;
else if (err == ERRPARAM)
msg = MERRPARAM;
else if (err == ERRFLOAT)
msg = MERRFLOAT;
else if (err == ERRINT)
msg = MERRINT;
else if (err == ERROPENR)
msg = MERROPENR;
else if (err == ERROPENW)
msg = MERROPENW;
else if (err == ERRINVAL)
msg = MERRINVAL;
else if (err == ERREXIST)
msg = MERREXIST;
else if (err == ERRLOAD)
msg = MERRLOAD;
else if (err == ERRNEED)
msg = MERRNEED;
else if (err == ERRSLEN)
msg = MERRSLEN;
else if (err == ERRWIND)
msg = MERRWIND;
else if (err == ERRINTER)
msg = MERRINTER;
else if (err == ERRFLEN)
msg = MERRFLEN;
else if (err == ERRMAXFL)
msg = MERRMAXFL;
else if (err == ERRSDVAL)
msg = MERRSDVAL;
else if (err == ERRDEN)
msg = MERRDEN;
else if (err == ERRDENVAL)
msg = MERRDENVAL;
else if (err == ERRPRIM)
msg = MERRPRIM;
else if (err == ERRPREP)
msg = MERRPREP;
else if (err == ERRMREP)
msg = MERRMREP;
else if (err == ERRNFLOWS)
msg = MERRNFLOWS;
else if (err == ERRHEAD)
msg = MERRHEAD;
else if (err == ERRORDER)
msg = MERRORDER;
else if (err == ERRMID)
msg = MERRMID;
else if (err == ERRREV)
msg = MERRREV;
else if (err == ERRNOREV)
msg = MERRNOREV;
else if (err == ERRMISM)
msg = MERRMISM;
else if (err == ERRMAXF)
msg = MERRMAXF;
else if (err == ERRLEN)
msg = MERRLEN;
else if (err == ERRCARR)
msg = MERRCARR;
else
msg = UNKNOWN;
fprintf(stderr, "%s\n", msg);
if (err != ERREXIST && err != ERRLEN)
freeMemory();
return -1;
}
/* void freeTrie(Node*)
* Recursively frees the trie.
*/
void freeTrie(Node* n) {
if (n == NULL)
return;
Read* r = n->first;
Read* tempr;
while (r != NULL) {
free(r->header);
tempr = r;
r = r->next;
free(tempr);
}
if (n->flow != NULL)
free(n->flow);
freeTrie(n->child);
freeTrie(n->next);
free(n);
}
/* void freeClus(Cluster*)
* Frees clusters and reads.
*/
void freeClus(Cluster* c) {
Cluster* tempc;
while (c != NULL) {
Read* r = c->first;
Read* tempr;
while (r != NULL) {
free(r->header);
tempr = r;
r = r->next;
free(tempr);
}
free(c->flows);
free(c->weight);
tempc = c;
c = c->next;
free(tempc);
}
}
/* void freeMemory(void)
* Frees the stored pointers.
*/
void freeMemory(void) {
Primer* p = primo;
Primer* temp;
while (p != NULL) {
free(p->name);
free(p->seq);
if (p->rev != NULL)
free(p->rev);
free(p->dummy);
Midtag* m = p->first;
Midtag* tempm;
while (m != NULL) {
free(m->name);
free(m->seq);
tempm = m->next;
free(m);
m = tempm;
}
freeClus(p->head);
freeTrie(p->root);
if ((p->out != NULL && fclose(p->out)) ||
(p->den != NULL && fclose(p->den)) ||
(p->map != NULL && fclose(p->map)) ||
(p->per != NULL && fclose(p->per)) ||
(p->cmap != NULL && fclose(p->cmap)))
exit(error("", ERRCLOSE));
temp = p->next;
free(p);
p = temp;
}
free(line);
}
/* void* memalloc(int)
* Returns pointer to allocated memory block.
*/
void* memalloc(int size) {
void* ans = malloc(size);
if (ans == NULL)
exit(error("", ERRMEM));
return ans;
}
/* float getFloat(char*)
* Converts the given char* to a float.
*/
float getFloat(char* in) {
char** endptr = NULL;
float ans = strtof(in, endptr);
if (endptr != '\0')
exit(error(in, ERRFLOAT));
return ans;
}
/* int getInt(char*)
* Converts the given char* to an int.
*/
int getInt(char* in) {
char** endptr = NULL;
int ans = (int) strtol(in, endptr, 10);
if (endptr != '\0')
exit(error(in, ERRINT));
return ans;
}
/* int clusSize(Primer*)
* Returns the number of clusters for this primer.
*/
int clusSize(Primer* p) {
int clus = 0;
for (Cluster* c = p->head; c != NULL; c = c->next)
clus++;
return clus;
}
/* void mergeSort()
* Sorts the clusters based on size.
*/
void mergeSort(Primer* p, Cluster* dummy) {
dummy->next = p->head;
int len = clusSize(p);
Cluster* last = NULL;
for (int i = 1; i < len; i *= 2) {
last = dummy;
Cluster* c = last->next;
Cluster* d = c;
while (c != NULL) {
int csize = 0;
int dsize = i;
// move d down the list
for (int k = i; k; k--) {
d = d->next;
csize++;
if (d == NULL) {
dsize = 0;
break;
}
}
// build new list
while (csize || dsize) {
if (!dsize || (csize && c->weight[0] >= d->weight[0])) {
last->next = c;
last = c;
csize--;
c = c->next;
} else {
last->next = d;
last = d;
dsize--;
d = d->next;
if (d == NULL)
dsize = 0;
}
}
// reset c
c = d;
}
last->next = NULL;
}
p->head = dummy->next;
p->tail = last;
}
/* int clusSize2(Cluster*)
* Returns the number of reads in this cluster.
*/
int clusSize2(Cluster* c) {
int read = 0;
for (Read* r = c->first; r != NULL; r = r->next)
read++;
return read;
}
/* void mergeSort2()
* Sorts the reads of a cluster based on size.
*/
void mergeSort2(Cluster* c, Read* dummy) {
dummy->next = c->first;
int len = clusSize2(c);
Read* last;
for (int i = 1; i < len; i *= 2) {
last = dummy;
Read* c = last->next;
Read* d = c;
while (c != NULL) {
int csize = 0;
int dsize = i;
// move d down the list
for (int k = i; k; k--) {
d = d->next;
csize++;
if (d == NULL) {
dsize = 0;
break;
}
}
// build new list
while (csize || dsize) {
if (!dsize || (csize && c->length <= d->length)) {
last->next = c;
last = c;
csize--;
c = c->next;
} else {
last->next = d;
last = d;
dsize--;
d = d->next;
if (d == NULL)
dsize = 0;
}
}
// reset c
c = d;
}
last->next = NULL;
}
c->first = dummy->next;
}
/* void printMiss(FILE*)
* Prints the "miss" array to the given file.
*/
void printMiss(FILE* out, int** miss, int max) {
// print array
for (int i = 0; i < max; i++) {
fprintf(out, "%d", miss[i][0]);
for (int j = 1; j < max; j++)
fprintf(out, ",%d", miss[i][j]);
fprintf(out, "\n");
}
// free array
for (int i = 0; i < max; i++)
free(miss[i]);
free(miss);
if (fclose(out))
exit(error("", ERRCLOSE));
}
/* void printPers()
* Prints to the fasta file that can be further
* analyzed for PCR chimeras by UCHIME or Perseus.
*/
void printPers(Primer* p, Cluster* c, char* seq,
int count, int clus, int perOpt) {
// print to fasta file
if (perOpt)
fprintf(p->per, ">%s%s%d%s%d\n%s\n", c->lon->header,
PER, clus, PER, count, seq);
else
fprintf(p->per, ">%s /ab=%d/\n%s\n", c->lon->header,
count, seq);
// print to mapping file
Read* r = c->first;
fprintf(p->cmap, "%s %s", c->lon->header, r->header);
for (r = r->next; r != NULL; r = r->next)
fprintf(p->cmap, "%s%s", COM, r->header);
fprintf(p->cmap, "\n");
}
/* void makePers()
* Makes the "consensus" sequence that can be
* analyzed by a chimera-checking program.
*/
void makePers(char* seq, char* perSeq, int start) {
int i;
for (i = 0; seq[i + start] != '\0'; i++)
perSeq[i] = seq[i + start];
perSeq[i] = '\0';
}
/* void makeSeq()
* Constructs a sequence for an irregular
* flow order.
*/
void makeSeq(Read* r, float* flow, int length,
char* seq, char* order, char* not, int opt) {
// create sequence
int start = r->start;
int len = 0, cons = 0;
for (int i = 0; i < length; i++) {
float val = flow[i];
if (val > MIN) {
for (; val > MIN; val--)
seq[len++] = order[i + start];
cons = 0;
not[cons++] = order[i + start];
} else {
// ambiguous base
int j;
for (j = 0; j < cons; j++)
if (not[j] == order[i + start])
break;
if (j == cons) {
not[cons] = order[i + start];
if (++cons == NUC && (len || !opt)) {
seq[len++] = 'N';
cons = 0;
not[cons++] = order[i + start];
}
}
}
}
seq[len] = '\0';
}
/* void makeIrreg(FILE*)
* Same as makeReads, but for irregular flow order.
*/
void makeIrreg(FILE* out, Primer* p, Cluster* c, char* seq,
char* perSeq, char* order, char* trim, int opt,
int perOpt, int last, int clus, int print) {
// adjust flowgram, primer
int begin;
int pos = strlen(trim);
if (opt) {
c->flows[0] = last > c->flows[0] ? 0.0f :
c->flows[0] - last;
begin = 0;
} else {
float v = c->flows[0];
int i;
for (i = 0; i < last && v > MIN; i++)
v--;
begin = i;
if (i != last) {
int j;
for (j = 0; j < last - i; j++)
trim[pos + j] = p->seq[pos + j];
trim[pos + j] = '\0';
}
}
char* not = (char*) memalloc(NUC); // checking for Ns
int count = 0;
Read* r = c->first;
while (r != NULL) {
count++;
makeSeq(r, c->flows, r->length, seq, order, not, opt);
// print output
if (opt)
fprintf(out, ">%s%s%d %s %s\n%s\n", r->mid->name,
PER, print + count, r->header, p->name, seq);
else
fprintf(out, ">%s\n%s%s%s\n", r->header,
r->mid->seq, trim, seq);
// save Perseus sequence
if (r == c->lon && p->per != NULL)
makePers(seq, perSeq, begin);
r = r->next;
}
free(not);
if (p->per != NULL)
printPers(p, c, perSeq, count, clus, perOpt);
if (!opt)
trim[pos] = '\0';
}
/* void makeReads(FILE*)
* Reconstitutes the reads from the consensus flowgrams
* and mapping information. Adds on the mid tag and
* primer sequences.
*/
void makeReads(FILE* out, Primer* p, Cluster* c, char* seq,
char* perSeq, char* order, char* trim, int offset,
int opt, int perOpt, int last, int clus, int print) {
// adjust flowgram, primer
int begin;
int pos = strlen(trim);
if (opt) {
c->flows[0] = last > c->flows[0] ? 0.0f :
c->flows[0] - last;
begin = 0;
} else {
float v = c->flows[0];
int i;
for (i = 0; i < last && v > MIN; i++)
v--;
begin = i;
if (i != last) {
int j;
for (j = 0; j < last - i; j++)
trim[pos + j] = p->seq[pos + j];
trim[pos + j] = '\0';
}
}
int len = 0, cons = 0, count = 0;
// build seq for each cluster (reads must be sorted)
Read* r = c->first;
for (int i = 0; c->flows[i] != END && r != NULL; i++) {
// ambiguous base
cons = c->flows[i] <= MIN ? cons + 1 : 0;
if (cons == 3 && (len || !opt)) {
seq[len++] = 'N';
cons = 0;
}
for (; c->flows[i] > MIN; c->flows[i]--)
seq[len++] = order[i % NUC + offset];
while (r != NULL && r->length == i + 1) {
seq[len] = '\0';
count++;
// print sequence
if (opt)
fprintf(out, ">%s%s%d %s %s\n%s\n", r->mid->name,
PER, print + count, r->header, p->name, seq);
else
fprintf(out, ">%s\n%s%s%s\n", r->header,
r->mid->seq, trim, seq);
// save Perseus sequence
if (r == c->lon && p->per != NULL)
makePers(seq, perSeq, begin);
r = r->next;
}
}
if (p->per != NULL)
printPers(p, c, perSeq, count, clus, perOpt);
if (!opt)
trim[pos] = '\0';
}
/* int findOffset(char*)
* Returns an int indicating the last base of the
* primer. Trims the last homopolymer from the
* primer (returned as input char*).
*/
int findOffset(char* primer, char* trim, char* order) {
int i = strlen(primer) - 1;
char last = primer[i];
int offset = -1;
for (int j = 0; j < NUC; j++)
if (last == order[j]) {
offset = j;
break;
}
if (offset == -1)
exit(error("", ERRPRIM));
for (; i && primer[i - 1] == last; i--) ;
trim[i] = '\0';
for (--i; i > -1; i--) {
if (primer[i] == 'A' || primer[i] == 'C' ||
primer[i] == 'G' || primer[i] == 'T')
trim[i] = primer[i];
// ambiguous bases: arbitrary preference for A, then C, G
else if (primer[i] == 'S' || primer[i] == 'Y' ||
primer[i] == 'B')
trim[i] = 'C';
else if (primer[i] == 'K')
trim[i] = 'G';
else
trim[i] = 'A';
}
return offset;
}
/* void printPrimer()
* Prints the denoised fasta files, consensus
* flowgrams and mapping files.
*/
void printPrimer(FILE* out, Primer* p, int len,
char* order, int denpOpt, int midOpt, int perOpt,
int reg, int* tclus, int* tread, int statusOpt) {
char* trim = (char*) memalloc(MIDPRIM);
int offset = findOffset(p->seq, trim, order);
int last = strlen(p->seq) - strlen(trim);
char* seq = (char*) memalloc(len);
char* perSeq = NULL;
if (p->per != NULL)
perSeq = (char*) memalloc(len);
Read* dummy = (Read*) memalloc(sizeof(Read));
int clus = 0, read = 0;
for (Cluster* c = p->head; c != NULL; c = c->next) {
// skip empty clusters
if (c->first == NULL)
continue;
// sort reads by size
mergeSort2(c, dummy);
// print outputs
if (denpOpt) {
// print to flow file
for (int i = 0; c->flows[i] != END; i++)
fprintf(p->den, "%f ", c->flows[i]);
fprintf(p->den, "\n");
// print to mapping file
for (Read* r = c->first; r != NULL; r = r->next)
fprintf(p->map, "%s%s%s%s%d ", r->header, SEP,
r->mid->name, SEP, r->length);
fprintf(p->map, "\n");
}
// print to fasta file
if (reg)
makeReads(out, p, c, seq, perSeq, order, trim,
offset, midOpt, perOpt, last, clus,
*tread + read);
else
makeIrreg(out, p, c, seq, perSeq, order, trim,
midOpt, perOpt, last, clus, *tread + read);
read += clusSize2(c);
clus++;
}
if (statusOpt)
printf("\n%s%sReads: %s%10d\n%s%sClusters:%10d",
TAB, TAB, TAB, read, TAB, TAB, clus);
*tclus += clus;
*tread += read;
free(dummy);
free(trim);
free(seq);
if (p->per != NULL)
free(perSeq);
}
/* Read* makeRead(char*, int)
* Creates a Read in memory.
*/
Read* makeRead(char* header, Midtag* m, int start,
int last, int numFlows, int reg, int trieOpt) {
Read* r = (Read*) memalloc(sizeof(Read));
r->header = (char*) memalloc(1 + strlen(header));
strcpy(r->header, header);
r->next = NULL;
r->mid = m;
if (!trieOpt) {
r->length = last - start;
r->flow = (float*) memalloc(numFlows * sizeof(float));
}
if (!reg)
r->start = start;
return r;
}
/* void adjustFlow(float*, int, int)
* Removes the 5' end (key, mid tag, and primer)
* from the flowgram.
*/
void adjustFlow(float* flow, float* copy, int start,
int last) {
int i;
for (i = 0; i < last - start; i++)
copy[i] = flow[i + start];
copy[i] = END;
}
/* void addOn(Read*, Cluster*, int)
* Adds flows from the read to the cluster
* (for second iteration).
*/
void addOn(Read* r, Cluster* c, int j) {
for (; r->flow[j] != END; j++)
c->flows[j] = r->flow[j];
c->flows[j] = END;
}
/* Cluster* checkClusters(Primer*, float*)
* Checks the clusters of the given Primer* for
* a match of the given flowgram.
*/
Cluster* checkClusters(Primer* p, Read* r,
float** inter, FILE* missFile, int** miss,
int iter) {
Cluster* c = p->head;
while (c != NULL) {
for (int j = 0; ; j++) {
if (r->flow[j] == END || c->flows[j] == END) {
if (c->flows[j] == END && iter)
addOn(r, c, j);
return c;
}
int i = (c->flows[j] + 0.005f) * 100.0f;
if (r->flow[j] < inter[i][0] || r->flow[j] > inter[i][1]) {
if (iter && missFile != NULL)
miss[i][(int) ((r->flow[j] + 0.005f) * 100.0f)]++;
break;
}
}
c = c->next;
}
return NULL;
}
/* void makeCluster(Primer*, Read*, float*, int)
* Creates a new cluster from the given read and flowgram.
*/
void makeCluster(Primer* p, Read* r, int numFlows, int iter) {
Cluster* neo = (Cluster*) memalloc(sizeof(Cluster));
if (iter) {
neo->first = neo->lon = r;
r->next = NULL;
} else
neo->first = neo->lon = NULL;
// make weights
neo->weight = (int*) memalloc(numFlows * sizeof(int));
neo->flows = (float*) memalloc(numFlows * sizeof(float));
int k;
for (k = 0; r->flow[k] != END; k++) {
neo->flows[k] = r->flow[k];
neo->weight[k] = 1;
}
neo->weight[k] = 0;
neo->flows[k] = END;
neo->next = NULL;
if (p->head == NULL)
p->head = neo;
else
p->tail->next = neo;
p->tail = neo;
}
/* void addToCluster(Read*, Cluster*)
* Adds the given read to the given cluster by making
* a weighted average of the flow values.
*/
void addToCluster(Read* r, Cluster* c, int iter) {
if (iter) {
if (c->lon == NULL || r->length > c->lon->length)
c->lon = r;
r->next = c->first;
c->first = r;
} else {
int i;
for (i = 0; i < r->length && c->weight[i]; i++) {
c->flows[i] = (c->weight[i] * c->flows[i] +
r->flow[i]) / (c->weight[i] + 1);
c->weight[i]++;
}
if (!c->weight[i]) {
for (; i < r->length; i++) {
c->flows[i] = r->flow[i];
c->weight[i] = 1;
}
c->flows[i] = END;
c->weight[i] = 0;
}
}
}
/* void denRead()
* Denoises a given read.
*/
void denRead(Primer* p, Read* r, float** inter,
FILE* missFile, int** miss, int numFlows,
int iter) {
Cluster* c = checkClusters(p, r, inter,
missFile, miss, iter);
if (c == NULL)
makeCluster(p, r, numFlows, iter);
else
addToCluster(r, c, iter);
}
/* Read* denoise(char*, Midtag*, float*)
* Controls the first iteration of the denoising
* process (for the combined clean-denoise analysis).
*/
Read* denoise(char* header, Midtag* m, float* flow,
int start, int last, int numFlows, float** inter,
int reg, FILE* missFile, int** miss) {
Read* r = makeRead(header, m, start, last,
numFlows, reg, 0);
adjustFlow(flow, r->flow, start, last);
denRead(m->prim, r, inter, missFile, miss,
numFlows, 0);
return r;
}
/* int checkOrder(char*)
* Checks if flow order is regular (repetitive).
*/
int checkOrder(char* order, int numFlows) {
int len = strlen(order);
if (len < NUC * 2)
exit(error("", ERRORDER));
for (int i = 0; i < NUC; i++) {
if (order[i] != 'A' && order[i] != 'C' &&
order[i] != 'G' && order[i] != 'T')
exit(error("", ERRORDER));
for (int j = NUC; j < len - i; j += NUC)
if (order[i] != order[i + j]) {
if (len < numFlows)
exit(error("", ERRORDER));
else
return 0;
}
}
order[NUC * 2] = '\0'; // truncate if repetitive
return 1;
}
/* Midtag* detMid(Primer*, char*)
* Parses the Read's header to get the mid tag
* and starting flow.
*/
Midtag* detMid(Primer* p, Read* r, int reg) {
char* name = strtok(r->header, SEP);
if (name == NULL)
exit(error("", ERRHEAD));
// get midtag name
char* end = reg ? DELIM : SEP;
name = strtok(NULL, end);
if (name == NULL)
exit(error("", ERRHEAD));
// find midtag
Midtag* m = p->first;
while (m != NULL && strcmp(m->name, name))
m = m->next;
if (m == NULL)
exit(error(name, ERRMID));
// determine start
if (!reg) {
char* start = strtok(NULL, DELIM);
if (start == NULL)
exit(error("", ERRHEAD));
r->start = getInt(start);
}
return m;
}
/* int getHeader(FILE*)
* Gets the header information (number of flows,
* flow order) from the cleaned flowgram file.
*/
int getHeader(FILE* file, char** order) {
if (fgets(line, MAX_SIZE, file) == NULL)
exit(error("", ERRNFLOWS));
char* in = strtok(line, DELIM);
if (in == NULL)
exit(error("", ERRNFLOWS));
int flows = getInt(in);
if (!flows)
exit(error("", ERRNFLOWS));
*order = (char*) memalloc(flows + 1);
in = strtok(NULL, DELIM);
if (in == NULL)
exit(error("", ERRNFLOWS));
strcpy(*order, in);
return flows;
}
/* int getFlows(float*)
* Loads the flow values from the input line.
* Returns the number of good flow values.
*/
int getFlows(float* flow, float max) {
char* in = strtok(NULL, DELIM);
int count = 0;
while (in != NULL) {
flow[count] = getFloat(in);
if (flow[count] > max)