-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfastmin-sg.c
1304 lines (1186 loc) · 51.7 KB
/
fastmin-sg.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
/*
###############################################################################
# Author: Alex Di Genova
# Laboratory: ERABLE/INRIA
# Copyright (c)
# year: 2019
###############################################################################
*/
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <zlib.h>
#include <pthread.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include "minimap.h"
#include "kseq.h"
#include <time.h>
#include "math.h"
KSEQ_INIT(gzFile, gzread)
//revcomp
//global array for to compute revcomp
char comp_tab[] = {
0, 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, 'T', 'V', 'G', 'H', 'E', 'F', 'C', 'D', 'I', 'J', 'M', 'L', 'K', 'N', 'O',
'P', 'Q', 'Y', 'S', 'A', 'A', 'B', 'W', 'X', 'R', 'Z', 91, 92, 93, 94, 95,
64, 't', 'v', 'g', 'h', 'e', 'f', 'c', 'd', 'i', 'j', 'm', 'l', 'k', 'n', 'o',
'p', 'q', 'y', 's', 'a', 'a', 'b', 'w', 'x', 'r', 'z', 123, 124, 125, 126, 127
};
//split_str
char** str_split(char* a_str, const char a_delim)
{
char** result = 0;
size_t count = 0;
char* tmp = a_str;
char* last_comma = 0;
char delim[2];
delim[0] = a_delim;
delim[1] = 0;
/* Count how many elements will be extracted. */
while (*tmp)
{
if (a_delim == *tmp)
{
count++;
last_comma = tmp;
}
tmp++;
}
/* Add space for trailing token. */
count += last_comma < (a_str + strlen(a_str) - 1);
/* Add space for terminating null string so caller
knows where the list of returned strings ends. */
count++;
result = malloc(sizeof(char*) * count);
if (result)
{
size_t idx = 0;
char* token = strtok(a_str, delim);
while (token)
{
assert(idx < count);
*(result + idx++) = strdup(token);
token = strtok(0, delim);
}
assert(idx == count - 1);
*(result + idx) = 0;
}
return result;
}
//heng li revcomp this is for printing only
void revcomp(char* kmer, int len) {
int c0, c1;
int klen=len;
for (int i = 0; i < klen>>1; ++i) { // reverse complement sequence
c0 = comp_tab[(int)kmer[i]];
c1 = comp_tab[(int)kmer[klen - 1 - i]];
kmer[i] = c1;
kmer[klen - 1 - i] = c0;
}
if (klen & 1) // complement the remaining base
kmer[klen>>1] = comp_tab[(int)kmer[klen>>1]];
//return kmer;
}
//funtions for rename long reads
static void stk_printstr(const kstring_t *s, unsigned line_len,FILE* outlongr)
{
if (line_len != UINT_MAX && line_len != 0) {
int i, rest = s->l;
for (i = 0; i < s->l; i += line_len, rest -= line_len) {
putc('\n',outlongr);
if (rest > line_len) fwrite(s->s + i, 1, line_len, outlongr);
else fwrite(s->s + i, 1, rest, outlongr);
}
putc('\n',outlongr);
} else {
putc('\n',outlongr);
fputs(s->s,outlongr);
}
}
static inline void stk_printseq_renamed(const kseq_t *s, int line_len, const char *prefix, int64_t n, FILE* outlongr)
{
putc('>',outlongr);
if (n >= 0) {
if (prefix) fputs(prefix, outlongr);
fprintf(outlongr,"%lld", (long long)n);
} else fputs(s->name.s, outlongr);
stk_printstr(&s->seq, line_len,outlongr);
}
#define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
typedef struct{
int std;
int avg;
} pe_stats;
//global variables
int inserts[50];
//total long thread_seqs
int64_t total_long_reads;
//global mutexs
pthread_mutex_t gseq;//for reading sequence
pthread_mutex_t pres;//for printing results
//struct for mapping variables
typedef struct {
int read_len;
int minq;
int kmer_size;
int minimizer_d;
int moving_w;
int qcov;
int numseq2read;//long reads per thread
int min_len_long_read;//minimal long read length
int n_threads;
int pair_rescue;//flag for pair rescue
} aligment_parameters;
//strcuture for threads
typedef struct {
//pointer to read the seq from file
kseq_t* ks;//forward read
kseq_t* kr;//reverse reads
//minimap2 index
mm_idx_t *mi;//pointer to minimap2 database
mm_mapopt_t mopt;//parameters for db construction in minimap
mm_tbuf_t *tbuf; //buffer for minimap2
//output files
int libcount;//number of synthetic libs to create
FILE** outFiles;//synthetic mate pair libraries
FILE* outlong;//renamed long read file
//mapping variables
aligment_parameters *alnp;//pointer to aligment parameters
pe_stats *pes;
/*int read_len,minq,kmer_size,moving_w,qcov;
int numseq2read;//long reads per thread
int min_len_long_read;//minimal long read length*/
} thread_data_struct;
//structure that save temporary a seq in the local thread
typedef struct{
char *name;
char *seq;
int length;
int64_t id;
} thread_seqs;
//we need ks->name.s,d,l,fo,mi->seq[fwdh->rid].name,fwdh->rs,fwdh->mapq,kmer_size,fwdh->mlen,fwdh->blen
//dinamical list that saves the hits founds
typedef struct phit{
//char* lrid;//long read id
int64_t lrid;//long read unique id
char* ctgid;//target name
int32_t plr;
int32_t orientation;//match orientation
int32_t pos;//position in target
int32_t mapq;//mapping quality
int32_t mlen, blen; // seeded exact match length; seeded alignment block length
int32_t qcov;
//kmer seq
char* k_seq;
struct phit *next; //next hit result
} phit_t;
//we create a list to handle the hits
void push_hit(phit_t **head, int64_t lrid, char* ctgid,int32_t plr,int32_t o, int32_t pos, int32_t mapq, int32_t mlen, int32_t blen, char* k_seq, int qcov) {
phit_t * new_node;
new_node = malloc(sizeof(phit_t));
//new_node->val = val;
new_node->plr=plr;
new_node->orientation=o;
new_node->pos=pos;
new_node->mapq=mapq;
new_node->mlen=mlen;
new_node->blen=blen;
new_node->qcov=qcov;
//we allocate the strings and copy them
//new_node->lrid=malloc(sizeof(char) * (strlen(lrid)));
//strcpy(new_node->lrid,lrid);
new_node->lrid=lrid;
new_node->ctgid=malloc(sizeof(char*) * (strlen(ctgid)));
strcpy(new_node->ctgid,ctgid);
new_node->k_seq=malloc(sizeof(char*) * (strlen(k_seq)));
strcpy(new_node->k_seq,k_seq);
//we update the header of the list
new_node->next = *head;
*head = new_node;
}
//thread mapping of long reads
void* maplongreads(void *arg)
{
//we get the given variables
//thread_data_struct *dargs = arg;
thread_data_struct *dargs = (thread_data_struct*)arg;
kseq_t *lks =dargs->ks;
aligment_parameters *alnp = (aligment_parameters*)dargs->alnp;
thread_seqs *localseqs=malloc(sizeof(thread_seqs) * (alnp->numseq2read));
FILE** outFiles = dargs->outFiles;
FILE* outlongr=dargs->outlong;
//we set the mapping variables
int read_len=alnp->read_len;
int minq=alnp->minq;
int kmer_size=alnp->kmer_size;
int moving_w=alnp->moving_w;
int qcov=alnp->qcov;//query coverage
mm_idx_t *mi=dargs->mi;
mm_mapopt_t mopt = dargs->mopt;
mm_tbuf_t *tbuf=dargs->tbuf;
int libcount=dargs->libcount;
//we create a list of hits for each library to store the results
phit_t** headhits = malloc(sizeof(phit_t*) * (libcount) );
for (int i = 0; i < libcount; ++i) headhits[i]=NULL;
//we read the sequence from the reads
int rseq=0;
//we create the fwd rev containers and the kmer_seq
char* fwd=malloc(sizeof(char*) * (read_len));
char* rev=malloc(sizeof(char*) * (read_len));
char* kfwdseq=malloc(sizeof(char*) * (kmer_size));
int fcov=0,rcov=0;
//I set the values to 0 in the memory
memset(fwd,0,read_len);
memset(fwd,0,read_len);
memset(fwd,0,kmer_size);
//pointer to regions
mm_reg1_t *regfwd; //fwd
mm_reg1_t *regrev; //rev
int n_regfwd=0; // number of regions to consider fwd
int n_regrev=0; // number of regions to consider rev
//pointers to hits
mm_reg1_t *fwdh;
mm_reg1_t *revh;
//local threads variables
int64_t total_pairs=0;
int64_t aling_pairs=0;
int64_t number_long_reads=0;
int getjob=1;//flag to control the execution of threads jobs
while(getjob){
pthread_mutex_lock(&gseq);
rseq=0;
//the order is always important
while(rseq < alnp->numseq2read-1 && kseq_read(lks) >=0 ){
//we copy the sequence in
if(lks->seq.l >= alnp->min_len_long_read) {
stk_printseq_renamed(lks, 60, 0, total_long_reads, outlongr);
localseqs[rseq].name = malloc(sizeof(char *) * (lks->name.l));
localseqs[rseq].id = total_long_reads;
memset(localseqs[rseq].name, 0, lks->name.l);
memcpy(localseqs[rseq].name, lks->name.s, lks->name.l);
localseqs[rseq].name[lks->name.l] = '\0';
localseqs[rseq].length = lks->seq.l;
localseqs[rseq].seq = malloc(sizeof(char *) * (lks->seq.l));
memset(localseqs[rseq].seq, 0, lks->seq.l);
memcpy(localseqs[rseq].seq, lks->seq.s, lks->seq.l);
localseqs[rseq].seq[lks->seq.l] = '\0';
//we check that the sequence leenght is equal to the sequence
assert(strlen(localseqs[rseq].seq) == lks->seq.l);
rseq++;
}
total_long_reads++;
}
pthread_mutex_unlock(&gseq);
if(rseq){
//we map the reads;
for(int i=0; i< rseq ; i++){ // each kseq_read() call reads one query sequence
number_long_reads++;
for(int ii=0; ii<libcount; ii++){
int d=inserts[ii];
int max_d = localseqs[i].length - d;
if(max_d < 0){
continue;
}
//create the read of the current library
for(int l=0; l<max_d; l+=moving_w){
//we clean the statics containers
memset(fwd,0,read_len);
memset(rev,0,read_len);
memset(kfwdseq,0,kmer_size);
int pa=l+1;
memcpy(fwd, localseqs[i].seq+pa, read_len);
fwd[read_len]='\0';
int pb=l+d-read_len;
memcpy(rev, localseqs[i].seq+pb, read_len);
rev[read_len]='\0';
revcomp(rev,read_len);
rev[read_len]='\0';
//we check that the length of fwd, rev is equal to the read_len
assert(strlen(rev)==strlen(fwd) && strlen(fwd)==read_len);
n_regfwd=0;
n_regrev=0;
total_pairs++;
//we actually map the reads using minimpa2 API
regfwd = mm_map(mi, read_len, fwd, &n_regfwd, tbuf, &mopt, 0); // get all hits for the query
if(n_regfwd > 0){
regrev = mm_map(mi, read_len, rev, &n_regrev, tbuf, &mopt, 0); // get all hits for the query
if(n_regrev > 0){
if(n_regrev == 1 && n_regfwd == 1){
//we print the aligment results
fwdh=®fwd[0];
revh=®rev[0];
//coverage
fcov=(int)(((float)fwdh->blen/(float)read_len) * 100);
rcov=(int)(((float)revh->blen/(float)read_len) * 100);
//printf("%d %d\n",fcov,rcov );
//we check that the aligment cover the query
if(fwdh->mapq >= minq && revh->mapq >= minq && fcov >=qcov && rcov>=qcov ){
//we add the reverse first to the hits pool
strncpy(kfwdseq, rev, kmer_size);
kfwdseq[kmer_size]='\0';
int fr=0;
if(revh->rev){
fr=16;
}
//printf("%s %s\n",mi->seq[revh->rid].name,mi->seq[fwdh->rid].name);
push_hit(&headhits[ii],localseqs[i].id,mi->seq[revh->rid].name,l,fr,fr > 0 ? revh->re:revh->rs,revh->mapq,revh->mlen,revh->blen,kfwdseq,rcov);
//we add the fwd
int fo=0;
if(fwdh->rev){
fo=16;
}
strncpy(kfwdseq, fwd, kmer_size);
kfwdseq[kmer_size]='\0';
push_hit(&headhits[ii],localseqs[i].id,mi->seq[fwdh->rid].name,l,fo,fo > 0 ? fwdh->re:fwdh->rs,fwdh->mapq,fwdh->mlen,fwdh->blen,kfwdseq,fcov);
aling_pairs++;
}
}
}
}
//we clean the hits
if(n_regfwd > 0)
free(regfwd);
if(n_regrev > 0)
free(regrev);
}
}
}
//we printnt the results
pthread_mutex_lock(&pres);
//we store the hits in the files
phit_t* nh = NULL;//next hit
for (int f = 0; f < libcount; ++f)
{
//this way print the reverse before the foward
//the header is not null
nh=headhits[f];
while(nh != NULL){
//fprintf(outFiles[f],"%lld_FG_%d_%d\t%d\t%s\t%d\t%d\t%dM\t*\t%d\t%d\t%s\t%d\n",
// (long long)nh->lrid,inserts[f],nh->plr,nh->orientation,nh->ctgid,nh->pos,nh->mapq,kmer_size,nh->mlen,nh->blen,nh->k_seq,nh->qcov);
//read_id,orientation,ctg_id,pos,mapq,ksize,qcov
//fprintf(outshort,"%lld\t%d\t%s\t%d\t%d\t%d\t%d\n",
// (long long)nh->lrid,nh->orientation,nh->ctgid,nh->pos,nh->mapq,kmer_size,nh->qcov);
fprintf(outFiles[f],"%lld_FG_%d_%d\t%d\t%s\t%d\t%d\t%d\t%d\n",
(long long)nh->lrid,inserts[f],nh->plr,nh->orientation,nh->ctgid,nh->pos,nh->mapq,kmer_size,nh->qcov);
headhits[f]=headhits[f]->next;
//we free the allocated hits
//free(nh->lrid);
free(nh->ctgid);
free(nh->k_seq);
free(nh);
nh=headhits[f];
}
}
//if(number_long_reads%4995==0)
fprintf(stderr,"Thread_info:Partial LONGREADS=%lld MAPPED_PAIRS=%lld TOTAL_PAIRS=%lld RL=%d MINQ=%d KS=%d MW=%d\n",
(long long)number_long_reads,(long long)aling_pairs,(long long)total_pairs,read_len,minq,kmer_size,moving_w);
//we clean the container of reads before allocate more memory in a single threads fashion
for (size_t i = 0; i < rseq; i++) {
free(localseqs[i].seq);
free(localseqs[i].name);
}
pthread_mutex_unlock(&pres);
}else{
getjob=0;
}
}
//we print some thread information
pthread_mutex_lock(&pres);
fprintf(stderr,"Final_Thread_info:Total LONGREADS=%lld MAPPED_PAIRS=%lld TOTAL_PAIRS=%lld RL=%d MINQ=%d KS=%d MW=%d\n",
(long long)number_long_reads,(long long)aling_pairs,(long long)total_pairs,read_len,minq,kmer_size,moving_w);
pthread_mutex_unlock(&pres);
//all done in this thread
return NULL;
}
int map_long_read_files(char* linserts, char* queryfile, char* prefix, aligment_parameters *alnp, mm_idx_t *mi, mm_mapopt_t mopt){
//we populate the array of libraries
char* token=strtok(linserts,",");
int libcount=0;
while(token != NULL)
{
//printf("IS=%d\n",atoi(token));
inserts[libcount]=atoi(token);
token=strtok(NULL,",");
libcount++;
if(libcount >= 50){
fprintf(stderr,"Max number of synthetic libraries is 50\n");
return 1;
}
}
printf("Query file = %s\n",queryfile);
// open query file for reading; you may use your favorite FASTA/Q parser
gzFile f = gzopen(queryfile, "r");
//printf("%d",f);
if (! f) {
fprintf (stderr, "gzopen of '%s' failed: %s.\n", queryfile,
strerror (errno));
exit (EXIT_FAILURE);
}
//assert(f);
kseq_t *ks = kseq_init(f);
FILE** outFiles = malloc(sizeof(FILE*) * (libcount));
char filename[100];//max length of file name
memset(filename, '\0', sizeof(filename));
sprintf(filename, "longreads.%s.fa",prefix);
FILE* outlong = fopen(filename,"w");
for(int i=0; i<libcount; i++){
//sprintf(filename, "lib.I%d.%s.fastmin-sg.sam", inserts[i],prefix);
sprintf(filename, "%s.I%d.fm.sam", prefix,inserts[i]);
printf("Lib : %s\n",filename);
outFiles[i] = fopen(filename,"w");
}
printf("A total of %d syntethic libraries will be created by FastMin-SG\n",libcount);
//we start the mapping of the reads
//we create the threads
if (pthread_mutex_init(&gseq, NULL) != 0){
printf("\n mutex for file reading failed\n");
return 1;
}
if (pthread_mutex_init(&pres, NULL) != 0){
printf("\n mutex for printing failed\n");
return 1;
}
//we start the mapping
pthread_t *tid=(pthread_t *)malloc(alnp->n_threads * sizeof(pthread_t ));
mm_tbuf_t **tbuf;// = mm_tbuf_init(n_threads); // thread buffer; for multi-threading, allocate one tbuf for each thread
tbuf = (mm_tbuf_t**)calloc(alnp->n_threads, sizeof(mm_tbuf_t*));
for(int i =0; i < alnp->n_threads; ++i) tbuf[i] = mm_tbuf_init();
for(int i=0; i < alnp->n_threads; i++){
thread_data_struct *data = malloc(sizeof *data);
data->ks=ks;
data->outFiles=outFiles;
data->outlong=outlong;
data->mi=mi;
data->libcount=libcount;
data->mopt=mopt;
data->tbuf=tbuf[i];
//mapping variables
data->alnp=alnp;
int err = pthread_create(&tid[i], NULL, &maplongreads, data);
if (err != 0){
printf("\ncan't create thread :[%s]", strerror(err));
}
}
//we wait for the thread to finish
for(int i=0; i< alnp->n_threads; ++i) pthread_join(tid[i], NULL);
//we destroy the thhread buffers
for(int i=0; i<alnp->n_threads; i++) mm_tbuf_destroy(tbuf[i]);
//end of threading code
kseq_destroy(ks); // close the query file
gzclose(f);//flose gzfile
//close sinthetic library files and fasta file of long reads
for(int i=0; i<libcount ; i++){
fclose(outFiles[i]);
}
// todo: be a variable file ?
fclose(outlong);
return 0;
}
int rescue_pair(mm_reg1_t *r1,mm_reg1_t *ra,int n_reg,pe_stats *ilib){
//we check return the pair satisfiying the first distance, otherwise we return a negative number indicating a null result
int d1= r1->rev ? r1->re:r1->rs;
int min=ilib->avg-(int)2.5*ilib->std;
int max=ilib->avg+(int)2.5*ilib->std;
mm_reg1_t *r2;//region that iter for looking for a candidate to rescue
for(int i=0; i<n_reg; i++){
r2=&ra[i];
int d2=r2->rev ? r2->re:r2->rs;
//same contig
if(r1->rid == r2->rid){
int d=abs(d2-d1);
//expected insert size
if(d>=min && d<=max){
//printf("RR:%d %d %d %d %d %d\n",min, d,max,r1->rid,r1->mapq,r2->mapq);
return i;
}
}
}
return -1;
}
//thread mapping of short reads
void* mapshortreads(void *arg) {
//we get the given variables
//thread_data_struct *dargs = arg;
thread_data_struct *dargs = (thread_data_struct*)arg;
kseq_t *lks =dargs->ks;//fwd
kseq_t *lkr =dargs->kr;//rev
aligment_parameters *alnp = (aligment_parameters*)dargs->alnp;
pe_stats *ilib=(pe_stats*)dargs->pes;
//fprintf(stderr,"Average insert size %d std %d [-10 outlayers,]\n",ilib->avg, ilib->std);
//fwd pool
thread_seqs *localseqs=malloc(sizeof(thread_seqs) * (alnp->numseq2read));
//rev pool
thread_seqs *localseqr=malloc(sizeof(thread_seqs) * (alnp->numseq2read));
//FILE** outFiles = dargs->outFiles;
FILE* outshort=dargs->outlong;
//we set the mapping variables
int read_len=0;//todo make it a variable
int minq=alnp->minq;
int kmer_size=alnp->kmer_size;
int moving_w=alnp->moving_w;
int qcov=alnp->qcov;//query coverage
mm_idx_t *mi=dargs->mi;
mm_mapopt_t mopt = dargs->mopt;
mm_tbuf_t *tbuf=dargs->tbuf;
//we have oonly one library with short reads
int libcount=1;
//we create a list of hits for each library to store the results
phit_t** headhits = malloc(sizeof(phit_t*) * (libcount) );
for (int i = 0; i < libcount; ++i) headhits[i]=NULL;
//we read the sequence from the reads
int rseq=0;
//we create the fwd rev containers and the kmer_seq
//char* fwd=malloc(sizeof(char*) * (read_len));
// char* rev=malloc(sizeof(char*) * (read_len));
char* kfwdseq=malloc(sizeof(char*) * (kmer_size));
int fcov=0,rcov=0;
//I set the values to 0 in the memory
//memset(fwd,0,read_len);
//memset(fwd,0,read_len);
memset(kfwdseq,0,kmer_size);
//pointer to regions
mm_reg1_t *regfwd; //fwd
mm_reg1_t *regrev; //rev
int n_regfwd=0; // number of regions to consider fwd
int n_regrev=0; // number of regions to consider rev
int64_t rescue=0;
int64_t prescue=0;
int pair_rescue=alnp->pair_rescue;//flag to set on off pair rescue;
//pointers to hits
mm_reg1_t *fwdh;
mm_reg1_t *revh;
//local threads variables
//int64_t total_pairs=0;
int64_t aling_pairs=0;
int64_t number_read_pairs=0;
//read_len=localseqr[rseq].length;
int getjob=1;//flag to control the execution of threads jobs
while(getjob){
pthread_mutex_lock(&gseq);
rseq=0;
//the order is always important
while(rseq < alnp->numseq2read-1 && kseq_read(lks) >=0 && kseq_read(lkr) >=0 ){
if(read_len == 0){
read_len=lks->seq.l;
}
//we copy the sequence in fwd
localseqs[rseq].name = malloc(sizeof(char *) * (lks->name.l));
localseqs[rseq].id = total_long_reads;
memset(localseqs[rseq].name, 0, lks->name.l);
memcpy(localseqs[rseq].name, lks->name.s, lks->name.l);
localseqs[rseq].name[lks->name.l] = '\0';
localseqs[rseq].length = lks->seq.l;
localseqs[rseq].seq = malloc(sizeof(char *) * (lks->seq.l));
memset(localseqs[rseq].seq, 0, lks->seq.l);
memcpy(localseqs[rseq].seq, lks->seq.s, lks->seq.l);
localseqs[rseq].seq[lks->seq.l] = '\0';
//we check that the sequence leenght is equal to the sequence
assert(strlen(localseqs[rseq].seq) == lks->seq.l);
//we copy the reverse seq
localseqr[rseq].name = malloc(sizeof(char *) * (lkr->name.l));
localseqr[rseq].id = total_long_reads;
memset(localseqr[rseq].name, 0, lkr->name.l);
memcpy(localseqr[rseq].name, lkr->name.s, lkr->name.l);
localseqr[rseq].name[lkr->name.l] = '\0';
localseqr[rseq].length = lkr->seq.l;
localseqr[rseq].seq = malloc(sizeof(char *) * (lkr->seq.l));
memset(localseqr[rseq].seq, 0, lkr->seq.l);
memcpy(localseqr[rseq].seq, lkr->seq.s, lkr->seq.l);
localseqr[rseq].seq[lkr->seq.l] = '\0';
//we check that the sequence leenght is equal to the sequence
assert(strlen(localseqr[rseq].seq) == lkr->seq.l);
//printf("%s %s\n",localseqs[rseq].name,localseqr[rseq].name);
rseq++;
total_long_reads++;
}
pthread_mutex_unlock(&gseq);
if(rseq){
//we map the reads;
for(int i=0; i< rseq ; i++){ // each kseq_read() call reads one query sequence
number_read_pairs++;
//we actually map the reads using minimpa2 API
n_regfwd=0,n_regrev=0;
regfwd = mm_map(mi, localseqs[i].length, localseqs[i].seq, &n_regfwd, tbuf, &mopt, 0); // get all hits for the query
if(n_regfwd > 0){
regrev = mm_map(mi, localseqr[i].length, localseqr[i].seq, &n_regrev, tbuf, &mopt, 0); // get all hits for the query
if(n_regrev > 0){
if(n_regrev == 1 && n_regfwd == 1){
//we print the aligment results
fwdh=®fwd[0];
revh=®rev[0];
//coverage
fcov=(int)(((float)fwdh->blen/(float)localseqs[i].length) * 100);
rcov=(int)(((float)revh->blen/(float)localseqr[i].length) * 100);
//printf("%d %d\n",fcov,rcov );
//we check that the aligment cover the query
if(fwdh->mapq >= minq && revh->mapq >= minq && fcov >=qcov && rcov>=qcov && fcov <=110 && rcov<=110){
//we add the reverse first to the hits pool
strncpy(kfwdseq, localseqr[i].seq, kmer_size);
kfwdseq[kmer_size]='\0';
int fr=0;
if(revh->rev){
fr=16;
}
push_hit(&headhits[0],localseqr[i].id,mi->seq[revh->rid].name,2,fr,fr > 0 ? revh->re:revh->rs,revh->mapq,revh->mlen,revh->blen,kfwdseq,rcov);
//we add the fwd
int fo=0;
if(fwdh->rev){
fo=16;
}
strncpy(kfwdseq, localseqs[i].seq, kmer_size);
kfwdseq[kmer_size]='\0';
push_hit(&headhits[0],localseqs[i].id,mi->seq[fwdh->rid].name,1,fo,fo > 0 ? fwdh->re:fwdh->rs,fwdh->mapq,fwdh->mlen,fwdh->blen,kfwdseq,fcov);
//printf("%lld %s %s %d %d %d %d %d %d %d %d %d %d %d %d\n",localseqs[i].id,mi->seq[fwdh->rid].name,mi->seq[revh->rid].name,
// fwdh->rs,fwdh->re,revh->rs,revh->re,fo,fr,fwdh->qs,fwdh->qe,revh->qs,revh->qe, fo > 0 ? fwdh->re:fwdh->rs, fr > 0 ? revh->re:revh->rs);
aling_pairs++;
}
}else{
//we can try rescue the reads
//printf("%d %d\n",n_regfwd,n_regrev);
if((n_regfwd == 1 || n_regrev == 1) && pair_rescue == 1){
prescue++; //we can rescue 10% of read pairs, focus within a contig rescue only, not between contigs
int p=-1;
//we try to rescue the pair within a contig
//we fix fwd
if(n_regfwd == 1){
p=rescue_pair(®fwd[0],regrev,n_regrev,ilib);
fwdh=®fwd[0];
if(p>=0)
revh=®rev[p];
}else{
//we fix rev
p=rescue_pair(®rev[0],regfwd,n_regfwd,ilib);
revh=®rev[0];
if(p>=0)
fwdh=®fwd[p];
}
//we rescue the pair
if(p>=0){
//printf("Rescue pair %lld %s %s %d %d %d %d %d %d %d %d %d %d %d %d\n",localseqs[i].id,mi->seq[fwdh->rid].name,mi->seq[revh->rid].name,
// fwdh->rs,fwdh->re,revh->rs,revh->re,fwdh->rev,revh->rev,fwdh->qs,fwdh->qe,revh->qs,revh->qe, fwdh->rev > 0 ? fwdh->re:fwdh->rs, revh->rev > 0 ? revh->re:revh->rs);
//coverage
fcov=(int)(((float)fwdh->blen/(float)localseqs[i].length) * 100);
rcov=(int)(((float)revh->blen/(float)localseqr[i].length) * 100);
//printf("%d %d\n",fcov,rcov );
//we check that the aligment cover the query
if(fcov >=qcov && rcov>=qcov && fcov <=110 && rcov<=110 && fwdh->mapq >= minq && revh->mapq >= minq){
//we add the reverse first to the hits pool
strncpy(kfwdseq, localseqr[i].seq, kmer_size);
kfwdseq[kmer_size]='\0';
int fr=0;
if(revh->rev){
fr=16;
}
push_hit(&headhits[0],localseqr[i].id,mi->seq[revh->rid].name,2,fr,fr > 0 ? revh->re:revh->rs,revh->mapq,revh->mlen,revh->blen,kfwdseq,rcov);
//we add the fwd
int fo=0;
if(fwdh->rev){
fo=16;
}
strncpy(kfwdseq, localseqs[i].seq, kmer_size);
kfwdseq[kmer_size]='\0';
push_hit(&headhits[0],localseqs[i].id,mi->seq[fwdh->rid].name,1,fo,fo > 0 ? fwdh->re:fwdh->rs,fwdh->mapq,fwdh->mlen,fwdh->blen,kfwdseq,fcov);
aling_pairs++;
rescue++;
}
}
}
}
}
}
//we clean the hits
if(n_regfwd > 0)
free(regfwd);
if(n_regrev > 0)
free(regrev);
}
//we printnt the results
pthread_mutex_lock(&pres);
//we store the hits in the files
phit_t* nh = NULL;//next hit
for (int f = 0; f < libcount; ++f)
{
//this way print the reverse before the foward
//the header is not null
nh=headhits[f];
while(nh != NULL){
//fprintf(outshort,"%lld/%d\t%d\t%s\t%d\t%d\t%dM\t*\t%d\t%d\t%s\t%d\n",
// nh->lrid,nh->plr,nh->orientation,nh->ctgid,nh->pos,nh->mapq,kmer_size,nh->mlen,nh->blen,nh->k_seq,nh->qcov);
//fprintf(outshort,"%lld\t%d\t%s\t%d\t%d\t%dM\t*\t%d\t%d\t%s\t%d\n",
// (long long)nh->lrid,nh->orientation,nh->ctgid,nh->pos,nh->mapq,kmer_size,nh->mlen,nh->blen,nh->k_seq,nh->qcov);
//read_id,orientation,ctg_id,pos,mapq,ksize,qcov
fprintf(outshort,"%lld\t%d\t%s\t%d\t%d\t%d\t%d\n",
(long long)nh->lrid,nh->orientation,nh->ctgid,nh->pos,nh->mapq,kmer_size,nh->qcov);
headhits[f]=headhits[f]->next;
//we free the allocated hits
//free(nh->lrid);
free(nh->ctgid);
free(nh->k_seq);
free(nh);
nh=headhits[f];
}
}
//if(number_long_reads%4995==0)
if(pair_rescue)
fprintf(stderr,"Thread_info:Partial TOTAL_PAIRS=%lld MAPPED_PAIRS=%lld RL=%d MINQ=%d KS=%d MW=%d PRESCUE=%lld RESCUE=%lld\n",
(long long)number_read_pairs,(long long)aling_pairs,read_len,minq,kmer_size,moving_w,(long long)prescue,(long long)rescue);
else
fprintf(stderr,"Thread_info:Partial TOTAL_PAIRS=%lld MAPPED_PAIRS=%lld RL=%d MINQ=%d KS=%d MW=%d\n",
(long long)number_read_pairs,(long long)aling_pairs,read_len,minq,kmer_size,moving_w);
//we clean the container of reads before allocate more memory in a single threads fashion
for (size_t i = 0; i < rseq; i++) {
free(localseqs[i].seq);
free(localseqs[i].name);
free(localseqr[i].seq);
free(localseqr[i].name);
}
pthread_mutex_unlock(&pres);
}else{
getjob=0;
}
} //endwhile
//we print some thread information
pthread_mutex_lock(&pres);
//print when pair rescue is enabled
if(pair_rescue)
fprintf(stderr,"Final_Thread_info:Total TOTAL_PAIRS=%lld MAPPED_PAIRS=%lld RL=%d MINQ=%d KS=%d MW=%d PRESCUE=%lld RESCUE=%lld\n",
(long long)number_read_pairs,(long long)aling_pairs,read_len,minq,kmer_size,moving_w,(long long)prescue,(long long)rescue);
else
fprintf(stderr,"Final_Thread_info:Total TOTAL_PAIRS=%lld MAPPED_PAIRS=%lld RL=%d MINQ=%d KS=%d MW=%d\n",
(long long)number_read_pairs,(long long)aling_pairs,read_len,minq,kmer_size,moving_w);
pthread_mutex_unlock(&pres);
//all done in this thread
return NULL;
}
int compare (const void * a, const void * b) {
return ( *(int*)a - *(int*)b );
}
pe_stats infer_insert_size(char* fwdfile, char* revfile, aligment_parameters *alnp, mm_idx_t *mi, mm_mapopt_t mopt){
// open query file for reading; you may use your favorite FASTA/Q parser
gzFile fwd = gzopen(fwdfile, "r");
//printf("%d",f);
if (! fwd) {
fprintf (stderr, "gzopen of '%s' failed: %s.\n", fwdfile,
strerror (errno));
exit (EXIT_FAILURE);
}
gzFile rev = gzopen(revfile, "r");
//printf("%d",f);
if (! rev) {
fprintf (stderr, "gzopen of '%s' failed: %s.\n", revfile,
strerror (errno));
exit (EXIT_FAILURE);
}
if (strcmp(fwdfile,revfile) == 0) {
fprintf (stderr, "fwd and rev file are the same fwd=%s rev=%s.\n", fwdfile,
revfile);
exit (EXIT_FAILURE);
}
//assert(f);
kseq_t *ks = kseq_init(fwd);
kseq_t *kr = kseq_init(rev);
//pointer to regions
mm_reg1_t *regfwd; //fwd
mm_reg1_t *regrev; //rev
//pointers to hits
mm_reg1_t *fwdh;
mm_reg1_t *revh;
int n_regfwd=0; // number of regions to consider fwd
int n_regrev=0; // number of regions to consider rev
int rseq=0;
int mpairs=50000;
mm_tbuf_t **tbuf;// = mm_tbuf_init(n_threads); // thread buffer; for multi-threading, allocate one tbuf for each thread
tbuf = (mm_tbuf_t**)calloc(1, sizeof(mm_tbuf_t*));
tbuf[0] = mm_tbuf_init();
int insobs[50000] = {0};
//we read both sequences and know we map them
while(rseq < mpairs && kseq_read(ks) >=0 && kseq_read(kr) >=0 ){
regfwd = mm_map(mi, ks->seq.l, ks->seq.s, &n_regfwd, tbuf[0], &mopt, 0); // get all hits for the query
regrev = mm_map(mi, kr->seq.l, kr->seq.s, &n_regrev, tbuf[0], &mopt, 0); // get all hits for the query
if(n_regfwd == 1 && n_regrev ==1){
fwdh=®fwd[0];
revh=®rev[0];
if(revh->rid == fwdh->rid) {
//coverage
int fcov = (int) (((float) fwdh->blen / (float) ks->seq.l) * 100);
int rcov = (int) (((float) revh->blen / (float) kr->seq.l) * 100);
if (fcov > 65 && rcov > 65 && fwdh->mapq >= 30 && revh->mapq >= 30) {
// us a good pair aligned to the same cluster
int d1= fwdh->rev ? fwdh->re:fwdh->rs;
int d2= revh->rev ? revh->re:revh->rs;
//printf("%d %d %d %d %d\n", fwdh->rid, revh->rid, d1, d2, abs(d1-d2));
if(rseq < mpairs)
insobs[rseq]=abs(d1-d2);
rseq++;// is a good hit
//we save the distance among the pairs
}
}
}
}
//we close the files and destriy the buffer;
//we destroy the thhread buffers
for(int i=0; i<1; i++) mm_tbuf_destroy(tbuf[i]);
//end of threading code
kseq_destroy(ks); // close the fwd file
kseq_destroy(kr); // close the rev file
gzclose(fwd);//flose gzfile
gzclose(rev);//flose gzfile
//we should return the std and avg of the insert size
qsort (insobs, mpairs, sizeof(int), compare);
//we remove 10% outlayers
int beg=(int)(mpairs * 0.1);
int end=mpairs-beg;
//we compute the average
uint64_t avg=0;
for (int n=beg; n<end+1; n++) {
//printf("%d %d\n", n, insobs[n]);
avg+=insobs[n];
}
int average = (int) (avg / (end - beg));
int64_t std=0;
//we compute the std of the sample
for (int n=beg; n<end+1; n++) {
std+=pow(insobs[n]-average,2);
}
int standar_dev=(int)sqrt(std/(end - beg));
fprintf(stderr,"Average insert size %d std %d [-10 outlayers]\n",average, standar_dev);
pe_stats a;
a.avg=average;
a.std=standar_dev;
return a;
}
int map_short_read_files(char* prefix, char* fwdfile, char* revfile, aligment_parameters *alnp, mm_idx_t *mi, mm_mapopt_t mopt){
printf("Forward file = %s\n",fwdfile);
printf("Reverse file = %s\n",revfile);
// open query file for reading; you may use your favorite FASTA/Q parser
gzFile fwd = gzopen(fwdfile, "r");
//printf("%d",f);
if (! fwd) {
fprintf (stderr, "gzopen of '%s' failed: %s.\n", fwdfile,
strerror (errno));
exit (EXIT_FAILURE);
}
gzFile rev = gzopen(revfile, "r");
//printf("%d",f);
if (! rev) {
fprintf (stderr, "gzopen of '%s' failed: %s.\n", revfile,
strerror (errno));
exit (EXIT_FAILURE);
}
if (strcmp(fwdfile,revfile) == 0) {
fprintf (stderr, "fwd and rev file are the same fwd=%s rev=%s.\n", fwdfile,
revfile);
exit (EXIT_FAILURE);
}
//we infer the values of the library from 50000 pairs mapped within contigs
pe_stats ilib=infer_insert_size(fwdfile,revfile,alnp,mi,mopt);
//fprintf(stderr,"Average insert size %d std %d [-10 outlayers,]\n",ilib.avg, ilib.std);
//exit(0);
//assert(f);
kseq_t *ks = kseq_init(fwd);
kseq_t *kr = kseq_init(rev);
char filename[100];//max length of file name
memset(filename, '\0', sizeof(filename));
//sprintf(filename, "libshort.%s.fastmin-sg.sam",prefix);
sprintf(filename, "%s.fm.sam",prefix);
FILE* outlong = fopen(filename,"w");
//we start the mapping of the reads
//we create the threads
if (pthread_mutex_init(&gseq, NULL) != 0){
printf("\n mutex for file reading failed\n");
return 1;
}
if (pthread_mutex_init(&pres, NULL) != 0){
printf("\n mutex for printing failed\n");
return 1;
}
//we start the mapping
pthread_t *tid=(pthread_t *)malloc(alnp->n_threads * sizeof(pthread_t ));
mm_tbuf_t **tbuf;// = mm_tbuf_init(n_threads); // thread buffer; for multi-threading, allocate one tbuf for each thread
tbuf = (mm_tbuf_t**)calloc(alnp->n_threads, sizeof(mm_tbuf_t*));
for(int i =0; i < alnp->n_threads; ++i) tbuf[i] = mm_tbuf_init();
for(int i=0; i < alnp->n_threads; i++){