-
Notifications
You must be signed in to change notification settings - Fork 3
/
Inpactor.c
1335 lines (1262 loc) · 45.6 KB
/
Inpactor.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mpi.h>
#include <assert.h>
#define BUFSIZE 128
char hostname[30];
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;
}
void step1(char dir[], int num_proc, int my_id, char db[], char result_directory[], int verbose){
int start=0;
int i;
char buf[BUFSIZE];
char dir_proc[1000];
char db_proc[1000];
FILE *fp;
int num_seq = 0;
int num_seq_per_proc = 0;
if(my_id==0){ //master proc
//to create step 1 directory
char commandMK[1000] = "mkdir -p";
char *cur1 = commandMK;
char * const end1 = commandMK + sizeof commandMK;
cur1 += snprintf(cur1, end1-cur1, "%s ","mkdir -p");
cur1 += snprintf(cur1, end1-cur1, "%s/step1",result_directory);
if(verbose == 1){
printf("Creating step 1 directory\n");
}
if ((fp = popen(commandMK, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
//to count TE in directory
char command[] = "ls ";
strcat(command,dir);
strcat(command,"/*_fsta.txt");
if(verbose == 1){
printf("executing command: %s\n",command);
}
if ((fp = popen(command, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
num_seq += 1;
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
num_seq_per_proc = (num_seq/(num_proc-1));
if(verbose == 1){
printf("there are %i sequences.\n",num_seq);
printf("each process has to analyze approximately %i sequences\n", num_seq_per_proc);
}
if(num_seq < num_proc){
if(my_id == 0){
printf("ERROR: you must to have more sequences than threads...\n");
}
exit(0);
}
//to split sequences in process files.
char commandSplit[1000] = "bash Parallel_splitter ";
char *cur = commandSplit;
char * const end = commandSplit + sizeof commandSplit;
cur += snprintf(cur, end-cur, "%s ","bash Parallel_splitter");
cur += snprintf(cur, end-cur, "%s %i %s/step1 1",dir,num_proc-1,result_directory);
if(verbose == 1){
printf("executing command: %s\n",commandSplit);
}
if ((fp = popen(commandSplit, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
//to send start orden to all processes
start=1;
for(i=1;i<num_proc;i++){
MPI_Send(&start, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
//to recive all finished messages from processes
for(i=1;i<num_proc;i++){
MPI_Recv(&start, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if(verbose == 1){
printf("proc %d done!\n",i);
}
}
//join all out_file.${id_proc}.tab in all_tabfiles.tab
char commandJoin[1000] = "cat ";
char *cur2 = commandJoin;
char * const end2 = commandJoin + sizeof commandJoin;
cur2 += snprintf(cur2, end2-cur2, "%s ","cat ");
cur2 += snprintf(cur2, end-cur2, "%s/step1/out_file.*.tab > %s/step1/all_tabfiles.tab",result_directory,result_directory);
if(verbose == 1){
printf("executing command: %s\n",commandJoin);
}
if ((fp = popen(commandJoin, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
printf("Preprocessing done, please check file: %s/step1/all_tabfiles.tab\n",result_directory);
}else{ //slave procs
MPI_Recv(&start, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
//executing step1
char commandStep1[1000] = "bash functions ";
char *cur2 = commandStep1;
char * const end = commandStep1 + sizeof commandStep1;
cur2 += snprintf(cur2, end-cur2, "%s ","bash functions");
cur2 += snprintf(cur2, end-cur2, "step1 %s %s %d %s/step1",dir,db,my_id,result_directory);
if(verbose == 1){
printf("executing command: %s\n",commandStep1);
}
if ((fp = popen(commandStep1, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
MPI_Send(&start, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
}
void step1_fasta(char fastafile[], int num_proc, int my_id, char db[], char result_directory[], int verbose){
int start=0;
int i;
char buf[BUFSIZE];
char dir_proc[1000];
char db_proc[1000];
FILE *fp;
int num_seq = 0;
int num_seq_per_proc = 0;
if(my_id==0){ //master proc
//to create step 1 directory
char commandMK[1000] = "mkdir -p";
char *cur1 = commandMK;
char * const end1 = commandMK + sizeof commandMK;
cur1 += snprintf(cur1, end1-cur1, "%s ","mkdir -p");
cur1 += snprintf(cur1, end1-cur1, "%s/step1",result_directory);
if(verbose == 1){
printf("Creating step 1 directory\n");
}
if ((fp = popen(commandMK, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
//to split sequences in process files.
char commandSplit[1000] = "bash Parallel_splitter ";
char *cur = commandSplit;
char * const end = commandSplit + sizeof commandSplit;
cur += snprintf(cur, end-cur, "%s ","bash Parallel_splitter");
cur += snprintf(cur, end-cur, " %s/step1 %s %i 5",result_directory,fastafile,num_proc-1);
if(verbose == 1){
printf("executing command: %s\n",commandSplit);
}
if ((fp = popen(commandSplit, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
//to send start orden to all processes
start=1;
for(i=1;i<num_proc;i++){
MPI_Send(&start, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
//to recive all finished messages from processes
for(i=1;i<num_proc;i++){
MPI_Recv(&start, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if(verbose == 1){
printf("proc %d done!\n",i);
}
}
//join all out_file.${id_proc}.tab in all_tabfiles.tab
char commandJoin[1000] = "cat ";
char *cur2 = commandJoin;
char * const end2 = commandJoin + sizeof commandJoin;
cur2 += snprintf(cur2, end2-cur2, "%s ","cat ");
cur2 += snprintf(cur2, end-cur2, "%s/step1/out_file.*.tab > %s/step1/all_tabfiles.tab",result_directory,result_directory);
if(verbose == 1){
printf("executing command: %s\n",commandJoin);
}
if ((fp = popen(commandJoin, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
printf("Preprocessing done, please check file: %s/step1/all_tabfiles.tab\n",result_directory);
}else{ //slave procs
MPI_Recv(&start, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
//executing step1
char commandStep1[1000] = "bash functions ";
char *cur2 = commandStep1;
char * const end = commandStep1 + sizeof commandStep1;
cur2 += snprintf(cur2, end-cur2, "%s ","bash functions");
cur2 += snprintf(cur2, end-cur2, "step1_fasta %s %s %d %s/step1",fastafile,db,my_id,result_directory);
if(verbose == 1){
printf("executing command: %s\n",commandStep1);
}
if ((fp = popen(commandStep1, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
MPI_Send(&start, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
}
void step1_repet(char fastafile[], int num_proc, int my_id, char db[], char result_directory[], int verbose){
int start=0;
int i;
char buf[BUFSIZE];
char dir_proc[1000];
char db_proc[1000];
FILE *fp;
int num_seq = 0;
int num_seq_per_proc = 0;
if(my_id==0){ //master proc
//to create step 1 directory
char commandMK[1000] = "mkdir -p";
char *cur1 = commandMK;
char * const end1 = commandMK + sizeof commandMK;
cur1 += snprintf(cur1, end1-cur1, "%s ","mkdir -p");
cur1 += snprintf(cur1, end1-cur1, "%s/step1",result_directory);
if(verbose == 1){
printf("Creating step 1 directory\n");
}
if ((fp = popen(commandMK, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
//to split sequences in process files.
char commandSplit[1000] = "bash Parallel_splitter ";
char *cur = commandSplit;
char * const end = commandSplit + sizeof commandSplit;
cur += snprintf(cur, end-cur, "%s ","bash Parallel_splitter");
cur += snprintf(cur, end-cur, " %s/step1 %s %i 5",result_directory,fastafile,num_proc-1);
if(verbose == 1){
printf("executing command: %s\n",commandSplit);
}
if ((fp = popen(commandSplit, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
//to send start orden to all processes
start=1;
for(i=1;i<num_proc;i++){
MPI_Send(&start, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
//to recive all finished messages from processes
for(i=1;i<num_proc;i++){
MPI_Recv(&start, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if(verbose == 1){
printf("proc %d done!\n",i);
}
}
//join all out_file.${id_proc}.tab in all_tabfiles.tab
char commandJoin[1000] = "cat ";
char *cur2 = commandJoin;
char * const end2 = commandJoin + sizeof commandJoin;
cur2 += snprintf(cur2, end2-cur2, "%s ","cat ");
cur2 += snprintf(cur2, end-cur2, "%s/step1/out_file.*.tab > %s/step1/all_tabfiles.tab",result_directory,result_directory);
if(verbose == 1){
printf("executing command: %s\n",commandJoin);
}
if ((fp = popen(commandJoin, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
printf("Preprocessing done, please check file: %s/step1/all_tabfiles.tab\n",result_directory);
}else{ //slave procs
MPI_Recv(&start, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
//executing step1
char commandStep1[1000] = "bash functions ";
char *cur2 = commandStep1;
char * const end = commandStep1 + sizeof commandStep1;
cur2 += snprintf(cur2, end-cur2, "%s ","bash functions");
cur2 += snprintf(cur2, end-cur2, "step1_repet %s %s %d %s/step1",fastafile,db,my_id,result_directory);
if(verbose == 1){
printf("executing command: %s\n",commandStep1);
}
if ((fp = popen(commandStep1, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
MPI_Send(&start, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
}
void step2(char dir[], int num_proc, int vstep1, int my_id, char tabfile[], char result_directory[], int verbose, int law808080){
int start=0;
int i;
char buf[BUFSIZE];
FILE *fp;
int num_seq = 0;
int num_seq_per_proc = 0;
if(my_id == 0){ //master
//to create step 2 directory
char commandMK[1000] = "mkdir -p";
char *cur1 = commandMK;
char * const end = commandMK + sizeof commandMK;
cur1 += snprintf(cur1, end-cur1, "%s ","mkdir -p");
cur1 += snprintf(cur1, end-cur1, "%s/step2",result_directory);
if(verbose == 1){
printf("Creating step 2 directory\n");
}
if ((fp = popen(commandMK, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
if(vstep1 == 0){ //we have to split tabfile in chunks
//call splitter
char commandSplit[1000] = "bash Parallel_splitter ";
char *cur = commandSplit;
char * const end2 = commandSplit + sizeof commandSplit;
cur += snprintf(cur, end2-cur, "%s ","bash Parallel_splitter");
cur += snprintf(cur, end2-cur, "%s/step1 %s %d 2",result_directory,tabfile,num_proc-1);
if(verbose == 1){
printf("executing command: %s\n",commandSplit);
}
if ((fp = popen(commandSplit, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
}
//send start orden to all processes
start=1;
for(i=1;i<num_proc;i++){
MPI_Send(&start, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
//recive all finished messages from processes
for(i=1;i<num_proc;i++){
MPI_Recv(&start, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if(verbose == 1){
printf("proc %d done!\n",i);
}
}
//join all out_file.${id_proc}.tab in all_tabfiles.tab
char commandJoin[4000] = "cat ";
char *cur2 = commandJoin;
char * const end3 = commandJoin + sizeof commandJoin;
cur2 += snprintf(cur2, end3-cur2, "%s ","cat ");
cur2 += snprintf(cur2, end3-cur2, "%s/step2/out_file.*.tab_ALL.RLC_RLG.TAB > %s/step2/all_tabfiles.tab_ALL.RLC_RLG.TAB; cat %s/step2/out_file.*.tab_ALL.RLC_RLG.FA > %s/step2/all_tabfiles.tab_ALL.RLC_RLG.FA; cat %s/step2/out_file.*.tab.RXX.TAB > %s/step2/all_tabfiles.tab_ALL.RXX.TAB; cat %s/step2/out_file.*.tab.RXX.FA > %s/step2/all_tabfiles.tab_ALL.RXX.FA; cat %s/step2/out_file.*.tab.TR_GAG.TAB > %s/step2/all_tabfiles.tab_ALL.TR_GAG.TAB; cat %s/step2/out_file.*.tab.TR_GAG.FA > %s/step2/all_tabfiles.tab_ALL.TR_GAG.FA; cat %s/step2/out_file.*.tab.NOC.TAB > %s/step2/all_tabfiles.tab.NOC.TAB; cat %s/step2/out_file.*.tab.NOC.FA > %s/step2/all_tabfiles.tab.NOC.FA; cat %s/step2/out_file.*.tab_ALL.RLC_RLG.TAB.families > %s/step2/all_tabfiles.tab_ALL.RLC_RLG.TAB.families; cat %s/step2/out_file.*.tab_ALL.RLC_RLG.TAB.families.FA > %s/step2/all_tabfiles.tab_ALL.RLC_RLG.TAB.families.FA; cat %s/step2/all_tabfiles.tab_ALL.RLC_RLG.TAB.families.FA %s/step2/all_tabfiles.tab_ALL.RXX.FA %s/step2/all_tabfiles.tab_ALL.TR_GAG.FA > %s/step2/classifiedTE.fa;",result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory);
if(verbose == 1){
printf("executing command: %s\n",commandJoin);
}
if ((fp = popen(commandJoin, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
if(law808080 == 1){
//to start orden to law 80-80-80
start=1;
for(i=1;i<num_proc;i++){
MPI_Send(&start, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
//recive all finished messages from processes
for(i=1;i<num_proc;i++){
MPI_Recv(&start, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if(verbose == 1){
printf("proc %d done!\n",i);
}
}
//join all out_file.${id_proc}.tab in all_tabfiles.tab
char commandJoin2[4000] = "cat ";
char *cur22 = commandJoin2;
char * const end32 = commandJoin2 + sizeof commandJoin2;
cur22 += snprintf(cur22, end32-cur22, "%s ","cat ");
cur22 += snprintf(cur22, end32-cur22, "%s/step2/out_file.*.tab_ALL.RLC_RLG.TAB > %s/step2/all_tabfiles.tab_ALL.RLC_RLG.TAB; cat %s/step2/out_file.*.tab_ALL.RLC_RLG.FA > %s/step2/all_tabfiles.tab_ALL.RLC_RLG.FA; cat %s/step2/out_file.*.tab.RXX.TAB > %s/step2/all_tabfiles.tab_ALL.RXX.TAB; cat %s/step2/out_file.*.tab.RXX.FA > %s/step2/all_tabfiles.tab_ALL.RXX.FA; cat %s/step2/out_file.*.tab.TR_GAG.TAB > %s/step2/all_tabfiles.tab_ALL.TR_GAG.TAB; cat %s/step2/out_file.*.tab.TR_GAG.FA > %s/step2/all_tabfiles.tab_ALL.TR_GAG.FA; cat %s/step2/out_file.*.tab.NOC.TAB > %s/step2/all_tabfiles.tab.NOC.TAB; cat %s/step2/out_file.*.tab.NOC.FA > %s/step2/all_tabfiles.tab.NOC.FA; cat %s/step2/out_file.*.tab_ALL.RLC_RLG.TAB.families > %s/step2/all_tabfiles.tab_ALL.RLC_RLG.TAB.families; cat %s/step2/out_file.*.tab_ALL.RLC_RLG.TAB.families.FA > %s/step2/all_tabfiles.tab_ALL.RLC_RLG.TAB.families.FA; cat %s/step2/all_tabfiles.tab_ALL.RLC_RLG.TAB.families.FA %s/step2/all_tabfiles.tab_ALL.RXX.FA %s/step2/all_tabfiles.tab_ALL.TR_GAG.FA > %s/step2/classifiedTE.fa;",result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory,result_directory);
if(verbose == 1){
printf("executing command: %s\n",commandJoin2);
}
if ((fp = popen(commandJoin2, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
}
printf("Classification done, please check files in %s/step2\n",result_directory);
}else{//processes
MPI_Recv(&start, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
//executing step2
char commandStep2[1000] = "bash functions ";
char *cur2 = commandStep2;
char * const end = commandStep2 + sizeof commandStep2;
cur2 += snprintf(cur2, end-cur2, "%s ","bash functions ");
cur2 += snprintf(cur2, end-cur2, "step2 %s/step1 %d %s/step2",result_directory,my_id,result_directory);
if(verbose == 1){
printf("executing command: %s\n",commandStep2);
}
if ((fp = popen(commandStep2, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
MPI_Send(&start, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
if(law808080 == 1){
MPI_Recv(&start, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
char commandStep21[1000] = "bash functions ";
char *cur21 = commandStep21;
char * const end21 = commandStep21 + sizeof commandStep21;
cur21 += snprintf(cur21, end-cur21, "%s ","bash functions ");
cur21 += snprintf(cur21, end-cur21, "80-80-80-law %s/step1 %d %s/step2",result_directory,my_id,result_directory);
if(verbose == 1){
printf("executing command: %s\n",commandStep21);
}
if ((fp = popen(commandStep21, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
MPI_Send(&start, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
}
}
void step3(char db[], char dir[], int num_proc, int vstep2, int my_id, char fastafile[], char result_directory[], int verbose, char references[], int RTlen, char blast_e[]){
int start=0;
int i;
char buf[BUFSIZE];
FILE *fp;
int num_seq = 0;
int num_seq_per_proc = 0;
char filefasta[1000];
if(my_id == 0){ //master
//to create step 3 directory
char commandMK[1000] = "mkdir -p";
char *cur1 = commandMK;
char * const end = commandMK + sizeof commandMK;
cur1 += snprintf(cur1, end-cur1, "%s ","mkdir -p");
cur1 += snprintf(cur1, end-cur1, "%s/step3",result_directory);
if(verbose == 1){
printf("Creating step 3 directory\n");
}
if ((fp = popen(commandMK, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
if(vstep2 == 0){ //we have to split fastafile in chunks
strcpy(filefasta,fastafile);
if(verbose == 1){
printf("fasta file: %s\n",filefasta);
}
}else{
char *cur10 = filefasta;
char * const end10 = filefasta + sizeof filefasta;
cur10 += snprintf(cur10, end10-cur10, "%s",result_directory);
cur10 += snprintf(cur10, end10-cur10, "/step2/all_tabfiles.tab_ALL.RLC_RLG.FA");
if(verbose == 1){
printf("fasta file: %s\n",filefasta);
}
}
//call splitter
char commandSplit[1000] = "bash Parallel_splitter ";
char *cur = commandSplit;
char * const end2 = commandSplit + sizeof commandSplit;
cur += snprintf(cur, end2-cur, "%s ","bash Parallel_splitter ");
cur += snprintf(cur, end2-cur, "%s/step2 %s %d 3 '%s'",result_directory,filefasta,num_proc-1,references);
if(verbose == 1){
printf("executing command: %s\n",commandSplit);
}
if ((fp = popen(commandSplit, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
//send start orden to all processes
start=1;
for(i=1;i<num_proc;i++){
MPI_Send(&start, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
//recive all finished messages from processes
for(i=1;i<num_proc;i++){
MPI_Recv(&start, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if(verbose == 1){
printf("proc %d done!\n",i);
}
}
//join all Genome.${id_proc}.RT.fa in Genome.all_ALL.RLC_RLG.fa.rt
char commandJoin[1000] = "cat ";
char *cur2 = commandJoin;
char * const end3 = commandJoin + sizeof commandJoin;
cur2 += snprintf(cur2, end3-cur2, "%s ","cat ");
cur2 += snprintf(cur2, end3-cur2, "%s/step3/Genome.*.RT.fa %s > %s/step3/Genome.all_ALL.RLC_RLG.fa.rt",result_directory,db,result_directory);
if(verbose == 1){
printf("executing command: %s\n",commandJoin);
}
if ((fp = popen(commandJoin, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
printf("Domain Extraction done, please check file: %s/step3/Genome.all_ALL.RLC_RLG.fa.rt\n",result_directory);
}else{//processes
MPI_Recv(&start, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
//executing step3
char commandStep3[1000] = "bash functions ";
char *cur2 = commandStep3;
char * const end = commandStep3 + sizeof commandStep3;
cur2 += snprintf(cur2, end-cur2, "%s ","bash functions ");
cur2 += snprintf(cur2, end-cur2, "step3 %s %d %s/step3 %s/step2 %d %s",db,my_id,result_directory,result_directory,RTlen,blast_e);
if(verbose == 1){
printf("executing command: %s\n",commandStep3);
}
if ((fp = popen(commandStep3, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
MPI_Send(&start, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
}
void step4(char tabfile[], int vstep2, int vstep3, int my_id, char dir[],char result_directory[], int num_proc, int verbose, char input[], char substitution_rate[]){
int start=0;
int i;
char buf[BUFSIZE];
FILE *fp;
int num_seq = 0;
int num_seq_per_proc = 0;
char genomeS4[1000];
char tabfile_s4[1000];
if(my_id == 0){ //master
char *cur10 = genomeS4;
char * const end10 = genomeS4 + sizeof genomeS4;
cur10 += snprintf(cur10, end10-cur10, "%s",result_directory);
cur10 += snprintf(cur10, end10-cur10, "/step3/Genome.all_ALL.RLC_RLG.fa.rt");
if(verbose == 1){
printf("genome file: %s\n",genomeS4);
}
//to create step 3 directory
char commandMK[1000] = "mkdir -p";
char *cur1 = commandMK;
char * const end = commandMK + sizeof commandMK;
cur1 += snprintf(cur1, end-cur1, "%s ","mkdir -p");
cur1 += snprintf(cur1, end-cur1, "%s/step4",result_directory);
if(verbose == 1){
printf("Creating step 4 directory\n");
}
if ((fp = popen(commandMK, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
if(vstep2 == 0){ //we must to use parameter tabfile
strcpy(tabfile_s4,tabfile);
}else{
char *cur11 = tabfile_s4;
char * const end11 = tabfile_s4 + sizeof tabfile_s4;
cur11 += snprintf(cur11, end11-cur11, "%s",result_directory);
cur11 += snprintf(cur11, end11-cur11, "/step2/all_tabfiles.tab_ALL.RLC_RLG.TAB");
}
if(verbose == 1){
printf("tabfile for step 4 is: %s\n",tabfile_s4);
}
//call splitter
char commandSplit[1000] = "bash Parallel_splitter ";
char *cur = commandSplit;
char * const end2 = commandSplit + sizeof commandSplit;
cur += snprintf(cur, end2-cur, "%s ","bash Parallel_splitter");
cur += snprintf(cur, end2-cur, "%s/step2 %s %i 4",result_directory,tabfile_s4,num_proc-1);
if(verbose == 1){
printf("executing command: %s\n",commandSplit);
}
if ((fp = popen(commandSplit, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
//send start orden to all processes
start=1;
for(i=1;i<num_proc;i++){
MPI_Send(&start, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
//recive all finished messages from processes
for(i=2;i<num_proc;i++){
MPI_Recv(&start, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if(verbose == 1){
printf("proc %d done!\n",i);
}
}
//join all partial results in only one
char commandJoin[1000] = "bash functions ";
char *cur2 = commandJoin;
char * const end3 = commandJoin + sizeof commandJoin;
cur2 += snprintf(cur2, end3-cur2, "%s ","bash functions ");
cur2 += snprintf(cur2, end3-cur2, "step4 %d %s/step4 %s %d",my_id,result_directory,genomeS4,num_proc);
if(verbose == 1){
printf("executing command: %s\n",commandJoin);
}
if ((fp = popen(commandJoin, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
printf("Distribution file done, please check file: %s/step4/Distribution-in-age.tab\n",result_directory);
MPI_Recv(&start, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Tree file done, please check file: %s/step4/multiple_align.tree\n",result_directory);
}else{//processes
MPI_Recv(&start, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
//executing step4
char commandStep4[1000] = "bash functions ";
char *cur2 = commandStep4;
char * const end = commandStep4 + sizeof commandStep4;
cur2 += snprintf(cur2, end-cur2, "%s ","bash functions ");
cur2 += snprintf(cur2, end-cur2, "step4 %d %s/step4 %s/step2 %s %s",my_id,result_directory,result_directory,input,substitution_rate);
if(verbose == 1){
printf("executing command: %s\n",commandStep4);
}
if ((fp = popen(commandStep4, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
MPI_Send(&start, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
}
void cleaner(char result_directory[], int verbose){
char buf[BUFSIZE];
FILE *fp;
char commandMK[1000] = "bash functions ";
char *cur1 = commandMK;
char * const end = commandMK + sizeof commandMK;
cur1 += snprintf(cur1, end-cur1, "%s ","bash functions");
cur1 += snprintf(cur1, end-cur1, "cleaner %s",result_directory);
if(verbose == 1){
printf("executing cleaner: %s\n",commandMK);
}
if ((fp = popen(commandMK, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(0);
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s\n",buf);
}
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
}
}
void printHelp(){
printf("Usage: mpirun -np num_processes Inpactor configuration_file\n");
printf("NOTE: Parameter num_processes has to be greater than 1\n");
printf("Example of configuration file:\n");
printf("######################### Configuration file #####################\n");
printf("input=/home/user/input_data\n");
printf("result_directory=/home/user/Inpactor_output\n");
printf("verbose=true\n");
printf("clean=true\n");
printf("#Input type can be LTR_STRUC, repet or fasta\n");
printf("input_type=LTR_STRUC\n");
printf("######################## Preprocessing ########################\n");
printf("preprocessing=true\n");
printf("database=/home/user/cores-database-wickercode.Lineage_Bianca.fa\n");
printf("######################### Classification ########################\n");
printf("classification=true\n");
printf("#this tabfile isn't necessary if preprocessing is true\n");
printf("tabfile=/home/user/Inpactor_output/step1/all_tabfiles.tab\n");
printf("80-80-80-rule=false\n");
printf("####################### Domain Extraction ######################\n");
printf("extraction=true\n");
printf("RTdatabase=/home/user/RTcores-database-wickercode.Lineage_Bianca.fa\n");
printf("references=/home/user/references.fasta\n");
printf("#this fastafile isn't necessary if classification is true\n");
printf("fastafile=/home/user/Inpactor_output/step2/all_tabfiles.tab_ALL.RLC_RLG.FA\n");
printf("#filter RT size. For a lot of RT, is better RTlength > 200, \n");
printf("#for less RTlength > 180 or 150\n");
printf("RTlength=200\n");
printf("Blast_evalue=1e-4\n");
printf("###### Insertion Time analysis and Phylogenetic tree creation ########\n");
printf("insertion=true\n");
printf("substitution_rate=0.000000013\n");
printf("#this tabfile isn't necessary if classification is true\n");
printf("tabfileS4=/home/user/Inpactor_output/step2/all_tabfiles.tab_ALL.RLC_RLG.TAB\n");
}
int main (int argc, char *argv[]){
int i, my_id, nproc, namelen, cont;
char directory[1000] = "";
char database[1000] = "";
char tab_fileS2[1000] = "";
char result_directory[1000] = "";
char databaseRT[1000] = "";
char tab_fileS3[1000] = "";
char tab_fileS4[1000] = "";
char references[1000] = "";
char blast_evalue[1000] = "";
char input_type[1000] = "";
char substitution_rate[1000] = "";
int law808080 = 0;
int RTlen = 200;
int par_step1 = 0;
int par_step2 = 0;
int par_step3 = 0;
int par_step4 = 0;
int verbose = 0;
int clean = 0;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_id);
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
MPI_Get_processor_name(hostname, &namelen);
FILE *file;
char * line = NULL;
size_t len = 0;
ssize_t read;
if(strcmp(argv[1],"help") == 0){
if(my_id == 0){
printHelp();
}
exit(0);
}
if(nproc < 2){
if(my_id == 0){
printf("You must to indicate at least 2 MPI processes!!!!\n");
printHelp();
}
exit(0);
}
if(argc < 2){
if(my_id == 0){
printf("You must to indicate the configuration file!!!!\n");
printHelp();
}
exit(0);
}else{
//read configuration file
if(my_id == 0){
printf("Reading configuration file: %s ...\n",argv[1]);
}
file = fopen(argv[1], "r");
if (file == NULL){
exit(0);
}
while ((read = getline(&line, &len, file)) != -1) {
if(line[0] != '#' && line[0] != ' '){ //non-comment lines and non-space white lines
char parameter_line[strlen(line)];
strcpy(parameter_line, line);
char** parameters = str_split(parameter_line, '=');
if(strcmp(*(parameters), "preprocessing") == 0){ // preprocessing module
if(strcmp(*(parameters+1), "true\n") == 0){
par_step1 = 1;
}else{
par_step1 = 0;
}
if(my_id == 0){
printf("preprocessing module 1 was selected? %s",*(parameters+1));
}
}
if(strcmp(*(parameters), "input_type") == 0){ // input type
strcpy(input_type, *(parameters+1));
if(input_type[strlen(input_type)-1] == '\n' ){
input_type[strlen(input_type)-1] = '\0';
}
if(my_id == 0){
printf("Input Type is: %s\n", input_type);
}
}
if(strcmp(*(parameters), "verbose") == 0){ // step 1 parameter
if(strcmp(*(parameters+1), "true\n") == 0){
verbose = 1;
}else{
verbose = 0;
}
if(my_id == 0){
printf("verbose was actived? %s",*(parameters+1));
}
}
if(strcmp(*(parameters), "clean") == 0){ // Cleaner parameter
if(strcmp(*(parameters+1), "true\n") == 0){
clean = 1;
}else{