-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepeatnet.c
More file actions
2309 lines (1908 loc) · 54.6 KB
/
Copy pathrepeatnet.c
File metadata and controls
2309 lines (1908 loc) · 54.6 KB
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
/*
RepeatNet: an ab initio centromeric sequence detection algorithm
*/
/*
TODO :
*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <assert.h>
#include <getopt.h>
#define SEQ_LENGTH 150
#define FASTA_FMT 0
#define FASTQ_FMT 1
#define FORWARD 0
#define REVERSE 1
#define LOAD 0
#define CREATE 1
#define ASCENDING 1
#define DESCENDING 0
typedef struct imatrix{
int i;
int j;
int eweight;
}_imatrix;
typedef struct wpair{
int id;
struct wpair *next;
}_wpair;
typedef struct Node{
int v1,v2,w;
struct Node *next;
}_Node;
typedef struct Component{
struct Node *node;
struct Component *next;
}_Component;
void my_fgets(char *, int, FILE *);
int readSingleFasta(FILE *, int);
int detectFormat(FILE *);
int readSeqRecord(FILE *, int, char **, char **);
int readInterleavedFastq(FILE *, int);
int readPaired(FILE *, FILE *);
void usage(char *);
int hash(char *, int, int);
void end2clone(char *, char *);
void rcomp(char *, char *);
int isReverse(char *);
int encode_window(char *);
void decode_window(int, char *);
int getWindow(char *, int, char *, int);
void do_end(int, int);
void insert_wloc(struct wpair **, int, int, int);
void compare_wloc(struct wpair *, struct wpair *, int *);
int hashTest(FILE *, int);
void dumpwins(FILE *, int);
void do_loaded(FILE *, char *);
void loadwins(FILE *);
char reverseindex(int);
void compare(char *, int, float, int, int, int);
unsigned int cindex(char);
int encode_window2(char *);
int revcomp_encoded(unsigned int);
void freewins(struct wpair *);
struct wpair *copyWins(struct wpair *);
void compactwins(void);
void merge(int, int);
FILE *myfopen(char *, char *);
int loadMatrix(char *, char *);
void decode_wins(char *);
void loadNames(char *);
void dumpclones(char *);
void trimwins(char [], int, char []);
/* component stuff */
void connectedComponent(int, int);
void alloc_node(struct Node **);
void alloc_component(struct Component **);
void insert_node_to_component(struct Component **, int, int, int);
void makecolors(void);
void connComp(int, int);
char **names;
char **forwards;
char **reverses;
int nseq;
int WS;
int *windowflag;
int wincnt;
struct wpair **winlocs;
int collisions;
int *windowcnt;
char **colors;
struct imatrix *interactions;
int EDGEHIST;
int WINORDER;
int main(int argc, char **argv){
FILE *in = NULL;
int i,j,k;
FILE *winlog;
FILE *namelog;
int npairs;
struct timeval start, end;
struct timezone tz;
char fname[SEQ_LENGTH];
char fname2[SEQ_LENGTH];
char namefile[SEQ_LENGTH];
char forwardfile[SEQ_LENGTH];
char reversefile[SEQ_LENGTH];
FILE *fwdfp = NULL;
FILE *revfp = NULL;
int PAIRED = 0;
char *encodearg = NULL;
int hashtype;
int hashtest=0;
int mintime;
int minhash;
float ecut;
int acut;
long inserttime;
long compacttime;
long readtime;
long inittime;
int mincollide=999999999;
int mincolhash;
int ccut; // compare cut; do not compare any kmer with cnt<ccut
int LOADWIN = 0;
int COMPARE = 0;
int MERGE = 0;
int LOADMATRIX = 0;
int DECODE = 0;
int DUMPCLONES = 0;
int LOADNAMES = 0;
int COMPONENTS = 0;
int TRIMWIN = 0;
char trimwinfile[SEQ_LENGTH];
int matrixcnt;
EDGEHIST = 0;
collisions=0;
WS = 12;
ccut = 1;
acut = 0;
if (argc < 2){
usage(argv[0]);
return 0;
}
fname[0]=0;
fname2[0]=0;
namefile[0]=0;
forwardfile[0]=0;
reversefile[0]=0;
hashtype=11;
mintime=999999999;
inserttime=0;
compacttime=0;
readtime=0;
inittime=0;
ecut = 0.0;
/* long-only option identifiers (values outside the printable-char range) */
enum {
OPT_LOADWIN = 256, OPT_LOADNAMES, OPT_LOADMATRIX, OPT_GETN,
OPT_DECODE, OPT_ENCODE, OPT_COMPONENTS, OPT_COMPARE,
OPT_EDGEHIST, OPT_CLONES, OPT_TRIM, OPT_HELP
};
static struct option long_opts[] = {
{"input", required_argument, 0, 'i'},
{"forward", required_argument, 0, 'f'},
{"reverse", required_argument, 0, 'r'},
{"hash", required_argument, 0, 'H'},
{"hashtest", required_argument, 0, 't'},
{"ecut", required_argument, 0, 'e'},
{"acut", required_argument, 0, 'a'},
{"ccut", required_argument, 0, 'c'},
{"merge", no_argument, 0, 'm'},
{"loadwin", required_argument, 0, OPT_LOADWIN},
{"loadnames", required_argument, 0, OPT_LOADNAMES},
{"loadmatrix", required_argument, 0, OPT_LOADMATRIX},
{"getn", required_argument, 0, OPT_GETN},
{"decode", required_argument, 0, OPT_DECODE},
{"encode", required_argument, 0, OPT_ENCODE},
{"components", no_argument, 0, OPT_COMPONENTS},
{"compare", no_argument, 0, OPT_COMPARE},
{"edgehist", no_argument, 0, OPT_EDGEHIST},
{"clones", required_argument, 0, OPT_CLONES},
{"trim", required_argument, 0, OPT_TRIM},
{"help", no_argument, 0, OPT_HELP},
{0, 0, 0, 0}
};
int opt;
while ((opt = getopt_long(argc, argv, "i:f:r:H:t:e:a:c:m",
long_opts, NULL)) != -1){
switch (opt){
case 'i': strcpy(fname, optarg); break;
case 'f': strcpy(forwardfile, optarg); break;
case 'r': strcpy(reversefile, optarg); break;
case 'H': hashtype = atoi(optarg); break;
case 't': hashtest = atoi(optarg); break;
case 'e': ecut = atof(optarg); break;
case 'a': acut = atoi(optarg); break;
case 'c': ccut = atof(optarg); break;
case 'm': MERGE = 1; break;
case OPT_LOADWIN: strcpy(fname, optarg); LOADWIN = 1; break;
case OPT_LOADNAMES: strcpy(namefile, optarg); LOADNAMES = 1; break;
case OPT_LOADMATRIX: strcpy(fname, optarg); LOADMATRIX = 1; break;
case OPT_GETN: strcpy(fname2, optarg); break;
case OPT_DECODE: strcpy(fname, optarg); DECODE = 1; break;
case OPT_ENCODE: encodearg = optarg; break;
case OPT_COMPONENTS: COMPONENTS = 1; break;
case OPT_COMPARE: COMPARE = 1; break;
case OPT_EDGEHIST: EDGEHIST = 1; break;
case OPT_CLONES: DUMPCLONES = 1; strcpy(fname2, optarg); break;
case OPT_TRIM: TRIMWIN = 1; strcpy(trimwinfile, optarg); break;
case OPT_HELP: usage(argv[0]); return 0;
case '?': usage(argv[0]); return 1;
}
}
/* --encode just converts a k-mer string to its integer code and exits. */
if (encodearg != NULL){
printf("%d\n", encode_window(encodearg));
return 1;
}
/* Two-file (separate forward/reverse) input requires both files. */
if (forwardfile[0] || reversefile[0]){
if (!forwardfile[0] || !reversefile[0]){
fprintf(stderr, "Both --forward and --reverse are required for two-file input.\n");
return 0;
}
PAIRED = 1;
}
if (PAIRED){
fwdfp = myfopen(forwardfile, "r");
revfp = myfopen(reversefile, "r");
/* base the output file names on the forward reads file */
strcpy(fname, forwardfile);
}
else{
if (fname[0] == 0){
fprintf(stderr, "input file?\n");
return 0;
}
in = myfopen(fname, "r");
}
if (LOADNAMES){
loadNames(namefile);
}
if (DECODE){
decode_wins(fname);
return 1;
}
if (LOADMATRIX){
matrixcnt = loadMatrix(fname, fname2);
if (COMPONENTS)
connComp(matrixcnt, wincnt);
return 1;
}
if (LOADWIN){
do_loaded(in, fname);
if (COMPARE)
compare(fname, hashtype, ecut, ccut, acut, MERGE);
if (DUMPCLONES)
dumpclones(fname2);
if (TRIMWIN)
trimwins(fname, hashtype, trimwinfile);
return 1;
}
if (hashtest){
for (i=3;i<hashtest;i++){
rewind(in);
for (j=0;j<nseq;j++)
free(names[j]);
free(names);
gettimeofday(&start, &tz);
collisions = 0;
nseq = hashTest(in, i);
gettimeofday(&end, &tz);
fprintf(stdout, "hash type %d read time %d microseconds.\tCollisions:%d\n", i, ((int)(end.tv_sec*1000000+end.tv_usec)-(int)(start.tv_sec*1000000+start.tv_usec)), collisions);
if (((int)(end.tv_sec*1000000+end.tv_usec)-(int)(start.tv_sec*1000000+start.tv_usec))<mintime){
mintime = ((int)(end.tv_sec*1000000+end.tv_usec)-(int)(start.tv_sec*1000000+start.tv_usec));
minhash=i;
}
if (collisions<mincollide){
mincollide=collisions;
mincolhash=i;
}
}
fprintf(stdout, "\n\nmintime: %d microseconds\nbesthash:%d\n",mintime, minhash);
fprintf(stdout, "\n\nmincollide: %d times\nbesthash:%d\n",mincollide, mincolhash);
return 0;
}
gettimeofday(&start, &tz);
if (PAIRED)
nseq = readPaired(fwdfp, revfp);
else if (detectFormat(in) == FASTQ_FMT)
nseq = readInterleavedFastq(in, hashtype);
else
nseq = readSingleFasta(in, hashtype);
gettimeofday(&end, &tz);
readtime = ((int)(end.tv_sec*1000000+end.tv_usec)-(int)(start.tv_sec*1000000+start.tv_usec));
npairs = 0;
/* compact seqs */
gettimeofday(&start, &tz);
sprintf(fname2, "%s.h%d.names", fname, hashtype);
namelog = fopen(fname2, "w");
fprintf(namelog, "%d\n",nseq);
for (i=0;i<nseq;i++){
fprintf(stderr, "\rCompacting %d\tof\t%d",(i+1),nseq);
if (names[i]!=NULL){
if (forwards[i]==NULL){
free(reverses[i]);
free(names[i]);
names[i] = NULL;
}
else if (reverses[i]==NULL){
free(forwards[i]);
free(names[i]);
names[i] = NULL;
}
else{
npairs++;
fprintf(namelog, "%d\t%s\n", i, names[i]);
}
}
}
fclose(namelog);
fprintf(stderr, "\n");
gettimeofday(&end, &tz);
compacttime = ((int)(end.tv_sec*1000000+end.tv_usec)-(int)(start.tv_sec*1000000+start.tv_usec));
fprintf(stderr, "Remaining pairs: %d\n", npairs);
wincnt = pow(4, WS);
fprintf(stdout, "Number of windows:%d\n", wincnt);
winlocs = (struct wpair **)malloc(sizeof(struct wpair *)*wincnt);
windowflag = (int *) malloc(sizeof(int)*wincnt);
windowcnt = (int *) malloc(sizeof(int)*wincnt);
gettimeofday(&start, &tz);
fprintf(stdout, "Initializing arrays...");
for (i=0;i<wincnt;i++){
winlocs[i]=NULL;
windowflag[i] = -1;
windowcnt[i] = 0;
}
fprintf(stdout, "\n");
fprintf(stderr, "\n");
gettimeofday(&end, &tz);
inittime = ((int)(end.tv_sec*1000000+end.tv_usec)-(int)(start.tv_sec*1000000+start.tv_usec));
k=0;
for (i=0; i<nseq; i++){
if (names[i] == NULL)
continue;
// else, both ends are here.
k++;
fprintf(stderr, "\rInserting %d\tof\t%d",k,npairs);
gettimeofday(&start, &tz);
do_end(i, FORWARD);
do_end(i, REVERSE);
gettimeofday(&end, &tz);
inserttime += ((int)(end.tv_sec*1000000+end.tv_usec)-(int)(start.tv_sec*1000000+start.tv_usec));
// remove index i, I don't need them anymore.
// but keep the names for now.
free(forwards[i]);
free(reverses[i]);
forwards[i]=NULL;
reverses[i]=NULL;
}
fprintf(stderr, "\n\n");
sprintf(fname2, "%s.h%d.winlog", fname, hashtype);
winlog = fopen(fname2, "w");
fprintf(stderr, "Comparing...");
fflush(stderr);
fprintf(winlog,"%d\n",WS);
sprintf(fname2, "%s.h%d.dump", fname, hashtype);
FILE *dumphist = fopen(fname2, "w");
for(i=0;i<wincnt;i++){
if (winlocs[i]==NULL)
continue;
dumpwins(winlog, i);
fprintf(dumphist, "%d\t%d\n", i, windowcnt[i]);
}
fclose(winlog);
fclose(dumphist);
fprintf(stderr, "\n");
fprintf(stdout,"\n-------------------------------------\n");
fprintf(stdout,"\tread time:\t%ld usec\n", readtime);
fprintf(stdout,"\tcompact time:\t%ld usec\n", compacttime);
fprintf(stdout,"\tinit time:\t%ld usec\n", inittime);
fprintf(stdout,"\tinsert time:\t%ld usec\n", inserttime);
fprintf(stdout,"-------------------------------------\n");
return 1;
}
int hashTest(FILE *fastaFile, int hashtype){
int cnt;
char myclone[SEQ_LENGTH];
int index;
char dummy[SEQ_LENGTH];
int i;
cnt = 0;
fprintf(stderr, "Counting sequences.\n");
while(fscanf(fastaFile, "%s", dummy)>0)
cnt++;
names = (char **) calloc(cnt, sizeof(char *));
rewind(fastaFile);
i=0;
while(fscanf(fastaFile, "%s", dummy)>0){
end2clone(myclone, dummy);
index = hash(myclone , cnt, hashtype); // hope it works
i++;
fprintf(stderr, "\r%d\tof\t%d", i,cnt);
if (names[index] == NULL)
names[index] = (char *) malloc(sizeof(char)*(strlen(myclone)+1));
strcpy(names[index], myclone);
}
fprintf(stderr, "\n");
return cnt;
}
int readSingleFasta(FILE *fastaFile, int hashtype){
int cnt;
char ch;
int i;
int seqcnt=0, seqlen=0;
int clonecnt;
int maxnamelen;
int maxlen;
char dummy[SEQ_LENGTH];
char myname[SEQ_LENGTH];
char myclone[SEQ_LENGTH];
int index;
char *thisseq;
char *thisrevseq;
cnt = 0; i=0;
fprintf(stderr, "Counting sequences.\n");
maxlen=0; maxnamelen=0;
rewind(fastaFile);
while (fscanf(fastaFile, "%c", &ch) > 0){
if (ch == '>'){
fscanf(fastaFile, "%s", dummy);
if (strlen(dummy)>maxnamelen)
maxnamelen=strlen(dummy);
if (seqlen>maxlen)
maxlen=seqlen;
cnt++;
seqlen=0;
my_fgets(dummy, SEQ_LENGTH, fastaFile);
}
else if (!isspace(ch))
seqlen++;
}
seqcnt = cnt;
clonecnt = seqcnt;
//seqcnt/2+1; // divide fwds & reverses
if (seqlen>maxlen)
maxlen=seqlen;
//length = (int *) malloc((clonecnt) * sizeof(int));
//int hash(char *this_name, int nseq, int mode)
cnt = 0; i=0;
fprintf(stderr, "Allocating memory for %d sequences with max length %d, maxnamelength %d.\n", clonecnt, maxlen, maxnamelen);
forwards = (char **) malloc((clonecnt) * sizeof(char *));
/*
for (i=0; i<clonecnt; i++)
forwards[i] = (char *) malloc(maxlen);
*/
reverses = (char **) malloc((clonecnt) * sizeof(char *));
/*
for (i=0; i<clonecnt; i++)
reverses[i] = (char *) malloc(maxlen);
*/
names = (char **) malloc((clonecnt) * sizeof(char *));
/*
for (i=0; i<clonecnt; i++)
names[i] = (char *) malloc(SEQ_LENGTH);
*/
thisseq = (char *) malloc(maxlen+1);
thisrevseq = (char *) malloc(maxlen+1);
for (i=0; i<clonecnt; i++){
forwards[i] = NULL;
reverses[i] = NULL;
names[i] = NULL;
/*
forwards[i][0] = 0;
reverses[i][0] = 0;
names[i][0] = 0; */
}
// FIX THE REST for FWD/REV
fprintf(stderr, "Reading sequences.\n");
rewind(fastaFile);
cnt = -1;
while (fscanf(fastaFile, "%c", &ch) > 0){
if (ch == '>'){
cnt++;
my_fgets(myname, SEQ_LENGTH, fastaFile);
myname[strlen(myname)-1] = 0;
end2clone(myclone, myname);
index = hash(myclone , clonecnt, hashtype);
/*
fprintf(stderr, "\r%d\tof\t%d\tindex\t%d\tname\t%s\tclone\t%s", (cnt+1), clonecnt*2, index, myname, myclone);
*/
fprintf(stderr, "\r%d\tof\t%d", (cnt+1),seqcnt);
if (names[index] == NULL)
names[index] = (char *) malloc(sizeof(char)*(strlen(myclone)+1));
strcpy(names[index], myclone);
}
i = 0;
if (cnt != 0){
thisseq[i++] = ch;
/*
if (isReverse(myname))
reverses[index][i++] = ch;
else
forwards[index][i++] = ch;
*/
}
do{
if (!(fscanf(fastaFile, "%c", &ch) > 0))
break;
if (ch!='>' && ch!='\r' && ch!='\n'){
thisseq[i++] = ch;
/*
if (isReverse(myname))
reverses[index][i++] = ch;
else
forwards[index][i++] = ch;
*/
}
} while (ch != '>');
thisseq[i++] = 0;
if (isReverse(myname)){
rcomp(thisseq, thisrevseq);
reverses[index] = (char *)malloc(sizeof(char)*(strlen(thisseq)+1));
strcpy(reverses[index], thisrevseq);
}
else{
forwards[index] = (char *)malloc(sizeof(char)*(strlen(thisseq)+1));
strcpy(forwards[index], thisseq);
}
if (ch == '>'){
cnt++;
fprintf(stderr, "\r%d\tof\t%d", (cnt+1), seqcnt );
if (cnt != seqcnt){
my_fgets(myname, SEQ_LENGTH, fastaFile);
myname[strlen(myname)-1] = 0;
end2clone(myclone, myname);
index = hash(myclone , clonecnt, hashtype);
if (names[index] == NULL)
names[index] = (char *) malloc(sizeof(char)*(strlen(myclone)+1));
strcpy(names[index], myclone);
}
} // if
} // while
fprintf(stderr, "\n[OK] %d sequences read from fasta file.\n",clonecnt);
free(thisseq); free(thisrevseq);
return clonecnt;
}
/* Read the next FASTA record from f.
On success returns 1 and stores freshly malloc'd strings in *name_out
(header line, without the leading '>') and *seq_out (sequence with all
whitespace stripped). Returns 0 at end of file. The caller frees both. */
/* Peek at the first non-whitespace character to decide the file format:
'>' means FASTA, '@' means FASTQ. The character is pushed back so the
record readers see an intact stream. */
int detectFormat(FILE *f){
int c;
while ((c = fgetc(f)) != EOF && isspace(c))
;
if (c != EOF)
ungetc(c, f);
if (c == '>')
return FASTA_FMT;
if (c == '@')
return FASTQ_FMT;
fprintf(stderr, "Unrecognized input format: expected FASTA ('>') or FASTQ ('@').\n");
exit(1);
}
/* Read the next sequence record (FASTA or FASTQ) from f.
On success returns 1 and stores freshly malloc'd strings in *name_out
(header line, without the leading '>'/'@') and *seq_out (sequence with all
whitespace stripped). Returns 0 at end of file. The caller frees both.
For FASTQ, the '+' separator and the quality line are consumed and
discarded; exactly as many quality characters as sequence characters are
read, so quality symbols such as '@' or '+' can never be mistaken for the
start of the next record. */
int readSeqRecord(FILE *f, int format, char **name_out, char **seq_out){
int c;
int ncap = 0, nlen = 0;
int scap = 0, slen = 0;
char *name = NULL;
char *seq = NULL;
char header = (format == FASTQ_FMT) ? '@' : '>';
/* find the start of the next record */
while ((c = fgetc(f)) != EOF && c != header)
;
if (c == EOF)
return 0;
/* header line (up to end of line) */
while ((c = fgetc(f)) != EOF && c != '\n' && c != '\r'){
if (nlen + 1 >= ncap){
ncap = ncap ? ncap * 2 : 128;
name = (char *) realloc(name, ncap);
}
name[nlen++] = c;
}
if (name == NULL)
name = (char *) malloc(1);
name[nlen] = 0;
if (format == FASTQ_FMT){
/* sequence: lines until one that starts with the '+' separator */
int atlinestart = 1;
while ((c = fgetc(f)) != EOF){
if (atlinestart && c == '+'){
while ((c = fgetc(f)) != EOF && c != '\n') /* consume separator line */
;
break;
}
if (c == '\n' || c == '\r'){
atlinestart = 1;
continue;
}
atlinestart = 0;
if (isspace(c))
continue;
if (slen + 1 >= scap){
scap = scap ? scap * 2 : 256;
seq = (char *) realloc(seq, scap);
}
seq[slen++] = c;
}
/* discard exactly slen quality characters (whitespace does not count) */
int q = 0;
while (q < slen && (c = fgetc(f)) != EOF)
if (!isspace(c))
q++;
}
else{
/* FASTA sequence: everything up to the next '>' or EOF, whitespace removed */
while ((c = fgetc(f)) != EOF){
if (c == '>'){
ungetc(c, f);
break;
}
if (isspace(c))
continue;
if (slen + 1 >= scap){
scap = scap ? scap * 2 : 256;
seq = (char *) realloc(seq, scap);
}
seq[slen++] = c;
}
}
if (seq == NULL)
seq = (char *) malloc(1);
seq[slen] = 0;
*name_out = name;
*seq_out = seq;
return 1;
}
/* Interleaved FASTQ counterpart of readSingleFasta: the forward and reverse
mate of each pair are in one file, paired by matching clone name (via the
hash table), exactly as in the FASTA path. */
int readInterleavedFastq(FILE *f, int hashtype){
int clonecnt = 0;
int done = 0;
char *name, *seq;
/* first pass: count records so the hash table can be sized */
rewind(f);
while (readSeqRecord(f, FASTQ_FMT, &name, &seq)){
free(name); free(seq);
clonecnt++;
}
fprintf(stderr, "Allocating memory for %d sequences.\n", clonecnt);
forwards = (char **) calloc(clonecnt, sizeof(char *));
reverses = (char **) calloc(clonecnt, sizeof(char *));
names = (char **) calloc(clonecnt, sizeof(char *));
/* second pass: store forward/reverse ends keyed by the clone's hash */
fprintf(stderr, "Reading sequences.\n");
rewind(f);
while (readSeqRecord(f, FASTQ_FMT, &name, &seq)){
char *clone = (char *) malloc(strlen(name) + 1);
int index;
end2clone(clone, name);
index = hash(clone, clonecnt, hashtype);
fprintf(stderr, "\r%d\tof\t%d", (done + 1), clonecnt);
if (names[index] == NULL){
names[index] = (char *) malloc(strlen(clone) + 1);
strcpy(names[index], clone);
}
if (isReverse(name)){
reverses[index] = (char *) malloc(strlen(seq) + 1);
rcomp(seq, reverses[index]);
}
else{
forwards[index] = (char *) malloc(strlen(seq) + 1);
strcpy(forwards[index], seq);
}
free(clone); free(name); free(seq);
done++;
}
fprintf(stderr, "\n[OK] %d sequences read from fastq file.\n", clonecnt);
return clonecnt;
}
/* Two-file input: forward reads in one file, reverse reads in another, paired
by position (the i-th record of each file form a forward/reverse pair, so no
name matching is needed). Each file's format (FASTA or FASTQ) is detected
independently. Fills the global forwards/reverses/names arrays and returns
the number of pairs. Reverse reads are reverse-complemented, matching how
readSingleFasta stores them. */
int readPaired(FILE *fwd, FILE *rev){
int cap = 0, n = 0;
int fwdfmt, revfmt;
char *fwdname, *fwdseq;
char *revname, *revseq;
forwards = NULL;
reverses = NULL;
names = NULL;
fwdfmt = detectFormat(fwd);
revfmt = detectFormat(rev);
fprintf(stderr, "Reading paired forward/reverse sequences.\n");
while (readSeqRecord(fwd, fwdfmt, &fwdname, &fwdseq)){
char *clone;
char *revcomp;
if (!readSeqRecord(rev, revfmt, &revname, &revseq)){
fprintf(stderr,
"Warning: forward file has more reads than reverse; extra forward reads ignored.\n");
free(fwdname); free(fwdseq);
break;
}
if (n >= cap){
cap = cap ? cap * 2 : 1024;
forwards = (char **) realloc(forwards, cap * sizeof(char *));
reverses = (char **) realloc(reverses, cap * sizeof(char *));
names = (char **) realloc(names, cap * sizeof(char *));
}
/* forward read stored as-is */
forwards[n] = fwdseq;
/* reverse read stored reverse-complemented */
revcomp = (char *) malloc(strlen(revseq) + 1);
rcomp(revseq, revcomp);
reverses[n] = revcomp;
free(revseq);
/* clone identifier for the .names file (strip any FORWARD/REVERSE tag) */
clone = (char *) malloc(strlen(fwdname) + 1);
end2clone(clone, fwdname);
names[n] = clone;
free(fwdname);
free(revname);
n++;
fprintf(stderr, "\r%d pairs", n);
}
/* drain any leftover reverse reads just to report the mismatch */
if (readSeqRecord(rev, revfmt, &revname, &revseq)){
fprintf(stderr,
"\nWarning: reverse file has more reads than forward; extra reverse reads ignored.\n");
free(revname); free(revseq);
}
fprintf(stderr, "\n[OK] %d forward/reverse pairs read.\n", n);
return n;
}
void usage(char *prog){
fprintf(stderr,
"RepeatNet: an ab initio centromeric sequence detection algorithm\n"
"\n"
"Usage: %s [options]\n"
"\n"
"Input (choose one). FASTA and FASTQ are both accepted and auto-detected\n"
"from the first character ('>' = FASTA, '@' = FASTQ):\n"
" -i, --input FILE interleaved reads (forward/reverse in one file)\n"
" -f, --forward FILE forward reads (use together with --reverse)\n"
" -r, --reverse FILE reverse reads (paired by position with --forward)\n"
"\n"
"Parameters:\n"
" -H, --hash N hash function type (default 11)\n"
" -e, --ecut F edge-weight cutoff (default 0.0)\n"
" -a, --acut N absolute co-occurrence cutoff (0 = use --ecut)\n"
" -c, --ccut N vertex count cutoff (default 1)\n"
" -m, --merge merge reverse-complement k-mers before comparing\n"
" -t, --hashtest N benchmark hash functions 3..N-1 and exit\n"
"\n"
"Pipeline stages:\n"
" --loadwin FILE load a .winlog produced by an earlier run\n"
" --loadnames FILE load a .names file\n"
" --loadmatrix FILE load a binary .matrix file\n"
" --getn FILE list of vertices whose neighbors to report (with --loadmatrix)\n"
" --compare build the co-occurrence graph/matrix\n"
" --components split a loaded matrix into connected components\n"
" --clones FILE dump clone names for the vertices listed in FILE\n"
" --trim FILE drop the windows listed in FILE from a loaded .winlog\n"
" --edgehist also write an edge-weight histogram\n"
" --decode FILE decode integer window codes back to k-mer strings\n"
" --encode KMER print the integer code for a k-mer and exit\n"
" --help show this message\n",
prog);
}
void my_fgets(char *str, int length, FILE *in){
char ch;
int i=0;
while (i<length && fscanf(in, "%c", &ch)){
if ((ch==' ' || ch=='\t') && i!=0){
if (str[i-1] == ' ' || str[i] == '\t')
;
else
str[i++] = ch;
}
else
str[i++] = ch;
if (ch == '\n' || ch=='\r')
break;
}
str[i] = 0;
}
int hash(char *this_name, int nseq, int type){
int i;
int len = strlen(this_name);
unsigned long sum;
int index;
unsigned int a,b;
sum = 0;
a=378551;
b=63689;
if (type == 0){
for (i=0;i<len;i++){
if (this_name[i]!=' ')
sum += i*this_name[i]+i;
}
}
else if (type == 1){
for (i=0;i<len;i++){
if (this_name[i]!=' ')
sum += this_name[i];
}
}
else if (type == 2){
for (i=0;i<len;i++){
if (this_name[i]!=' ')
sum += i*this_name[i];
}
}
else if (type == 3){
for (i=0;i<len;i++){
if (this_name[i]!=' ')
sum += this_name[i] * pow(7,i); // rabin-karp style
}
}
else if (type == 4){
for (i=0;i<len;i++){
if (this_name[i]!=' ')
sum += this_name[i] * pow(11,i); // rabin-karp style, part 2
}
}
else if (type == 5){
for (i=0;i<len;i++){