-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtlex-open-v3.0.pl
More file actions
3684 lines (3265 loc) · 116 KB
/
tlex-open-v3.0.pl
File metadata and controls
3684 lines (3265 loc) · 116 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
#!/usr/bin/perl
#######################################################################################
# To check if an error message returns: #
# ------------------------------------ #
# -check the full path of the data #
# -check the concordance between the TE list, TE annotation, reference sequence files #
# -check the format of the input illumina data #
#######################################################################################
use warnings;
use strict;
use Getopt::Long;
use Time::Local;
use List::Util qw[min max];
use Data::Dumper;
use File::Basename;
&startup;
sub startup {
my $strains;
my $refgenome;
my $TE_list;
my $newTElist=0;
my $TE_map;
my $maxReadLength=100;
my $output;
my $species="drosophila";
my $noFilterTE;
my $minflankcov=0.5;
my $bwaonly;
my $junction=1000;
my $buffer=60;
my $shrimponly;
my $flank=125;
my $var=20;
my $lima=10;
my $limp=15;
my $id=95;
my $combine;
my $combine_all;
my $combine_strains;
my $freqestim;
my $align;
my $noclean;
my $help;
my $binref;
my $binreads;
my $pooleddata;
my $tsd;
my $reads=0;
my $minqual=30;
my $processes=0;
my $PE="no"; # require the reads of the same pair to be in separated files
my $minreads=3;
my $maxreads=90;
my $minpop=1;
my $startdirectory=`pwd`;
chomp $startdirectory;
GetOptions ('R=s' => \$strains,
'G=s' => \$refgenome,
'T=s' => \$TE_list,
'M=s' => \$TE_map,
'A=i' => \$maxReadLength,
'O=s' => \$output,
's=s' => \$species,
'noFilterTE' => \$noFilterTE,
'd=f' => \$minflankcov,
'q' => \$bwaonly,
'j=i' => \$junction,
'b=i' => \$buffer,
'minQ=i' => \$minqual,
'p' => \$shrimponly,
'f=i' => \$flank,
'v=i' => \$var,
'lima=i' => \$lima,
'limp=i' => \$limp,
'id=i' => \$id,
'combRes' => \$combine,
'combAll' => \$combine_all,
'combData' => \$combine_strains,
'freq' => \$freqestim,
'align' => \$align,
'tsd' => \$tsd,
'noclean' => \$noclean,
'h|help' => \$help,
'binref' => \$binref,
'binreads' => \$binreads,
'pooled' => \$pooleddata,
'pairends=s' => \$PE,
'processes=i' => \$processes,
'minR=i' => \$minreads, # V3 for setting minimum number of reads for calculating frequencies in pools
'maxR=i' => \$maxreads, # V3 for setting maximum number of reads for calculating frequencies in pools
'minP=i' => \$minpop); # V3 for setting minimum number of individuals for calculating frequencies in individual strains
if ($help) {
&help();
die "\n";
}
my $outputdir;
if($output) {
if (index($output, "_") != -1) {
print "WARNING: strain name cannot contain character '_'!!!!";
exit;
} else {
my $tmp="tlex_$output";
$outputdir=$tmp;
}
}
else{
$outputdir="tlex_output";
}
if($combine_strains){
print "\n\n **************************************************************************************************************************************\n";
print "\n\t\t\t\t\t\t\t * T-lex release 3 \n\t\t\t\tCombine the presence/absence results from different strain(s) *\n\t\t\t\t\t\t\n";
print "\t\t\t * only the parameters for the combination of the presence and absence result may be necessary *\n";
&combine_results("$startdirectory");
die "\n";
}
if($combine_all){
print "\n\n **************************************************************************************************************************************\n";
print "\n\t\t\t\t\t\t\t * T-lex release 3 \n\t\t\t\tCombine the frequency estimates, the analysis of the TE flanking regions and the TSD detection *\n\t\t\t\t\t\t\n";
if ($pooleddata){
&CombineAll("$outputdir","$pooleddata");
}
else{
&CombineAll("$outputdir","");
}
die "\n";
}
my $new_TE_list;
if($combine){
print "\n\n **************************************************************************************************************************************\n";
print "\n\t\t\t\t\t\t\t * T-lex release 3 \n\t\t\t\tCombine the presence/absence results from one strain *\n\t\t\t\t\t\t\n";
print "* Specify the project name if necessary\n";
unless ($TE_list) {
die "* Must specify the TE list after the TE filtering step\n";
}
if ($noFilterTE) {
$new_TE_list=$TE_list;
}
else{
$new_TE_list="$TE_list\_filtered";
}
print "* list of TE used : $new_TE_list\n";
print "$outputdir - $new_TE_list - $flank\n";
&FinalResults("$outputdir","$new_TE_list", "$TE_map", "$flank");
die "\n";
}
if ($freqestim){
print "$startdirectory\/$outputdir\n";
if ($pooleddata) {
if (-e "$outputdir\/Tresults"){
print "\n\n **************************************************************************************************************************************\n";
print "\n\t\t\t\t\t\t\t * T-lex release 3 \n\t\t\t\tReturn the frequency estimates of given sequence(s) in multiple strain(s) *\n\t\t\t\t\t\t\n";
&FreqEstimate("$startdirectory\/$outputdir","$pooleddata","$maxreads","$minreads","$minpop");
die "\n";
}
else{
die "* Must detect the presence/absence of given sequences before! So run T-lex first or give the good project name\n";
}
} else {
print "$startdirectory\/Tfreqs_output\/Tresults";
if (-e "$startdirectory\/Tfreqs_output\/Tresults"){
print "\n\n **************************************************************************************************************************************\n";
print "\n\t\t\t\t\t\t\t * T-lex release 3 \n\t\t\t\tReturn the frequency estimates of given sequence(s) in multiple strain(s) *\n\t\t\t\t\t\t\n";
&FreqEstimate("$startdirectory\/Tfreqs_output","$pooleddata","$maxreads","$minreads","$minpop");
die "\n";
}
else{
die "* Must detect the presence/absence of given sequences before! So run T-lex first or give the good project name\n";
}
}
}
if ($align){
unless ($TE_list && $strains){
die "* Must specify the list of TEs, the sequencing data directory and the project name!\n";
}
else{
if($tsd){
print "\n\n **************************************************************************************************************************************\n";
print "\n\t\t\t\t\t\t\t * T-lex release 3 \n\t\t\t\t\t\t TSD detection (requires the alignments) *\n\t\t\t\t\t\t\n";
&MultiAlign("$outputdir","$TE_list","$strains","$flank","0","1");
}
else{
print "\n\n **************************************************************************************************************************************\n";
print "\n\t\t\t\t\t\t\t * T-lex release 3 \n\t\t\t\t\t\t Multiple Alignment report only *\n\t\t\t\t\t\t\n";
unless ($shrimponly) {
&MultiAlign("$outputdir","$TE_list","$strains","$flank","1","0");
}
unless ($bwaonly) {
&MultiAlign("$outputdir","$TE_list","$strains","$flank","0","0");
}
else{
&MultiAlign("$outputdir","$TE_list","$strains","$flank","2","0");
}
}
die "\n";
}
}
unless ($TE_list || $TE_map || $refgenome || $strains) {
&help();
}
unless ($TE_list) {
die "* the file of the TE list is missing !\n";
}
unless ($TE_map) {
die "* the TE annotation file is missing!\n";
}
unless ($refgenome) {
die "* the file with the reference genomic sequences is missing!\n";
}
unless ($strains) {
die "* the directory of the sequencing data is missing!\n";
}
if ($shrimponly && $bwaonly) {
die "* Cannot specify [shrimponly] and [bwaonly] at the same time!\n";
}
unless ($processes) {
$processes = 1;
}
my $original_TE_list=$TE_list;
system("mkdir $outputdir");
open (PARAM, ">$outputdir\/Tparam");
print "\n\n **************************************************************************************************************************************\n";
if ($bwaonly) {
print "\n\t\t\t\t\t\t\t * T-lex release 3 \n\t\t\t Detect the presence of given sequence(s) in strain(s) using BWA program *\n";
print PARAM "T-lex release 1 Detect the presence of given sequence(s) in strain(s) using MAQ program\n";
}
elsif ($shrimponly) {
print "\n\t\t\t\t\t\t\t * T-lex release 3 \n\t\t\t Detection of the absence of given sequence(s) in strain(s) using SHRIMP program *\n";
print PARAM "T-lex release 1 Detection of the absence of given sequence(s) in strain(s) using SHRIMP program\n";
}
else {
print "\n\t\t\t\t\t\t\t * T-lex release 3 \n\t\t\t\tReport the presence/absence of given sequence(s) in strain(s) * \n\t\t\t\t\t\t and return their frequency\n";
print PARAM "T-lex release 3 Report the presence/absence of given sequence(s) in strain(s) and return their frequency\n";
}
my $start_time = localtime();
print "\t\t\t\t\t\t * $start_time * \n";
print "\n **************************************************************************************************************************************\n";
print "* Output directory: $startdirectory/$outputdir/\n";
print PARAM "List of TEs: $TE_list\nTE annotations: $TE_map\nReference genome sequence: $refgenome\nSolexa data: $strains\n";
print PARAM "Species: $species\n";
if ($noFilterTE){
print PARAM "Filter out the problematic TE insertions from the TE dataset: no\n\n";
}
else{
print PARAM "Filter TEs: yes\n";
print PARAM "==>Min, flanking coverage: $minflankcov\n";
print PARAM "==>Max. read length: $maxReadLength bp \n\n";
}
print PARAM "TE presence detection:\n\==>Length of the TE: $junction bp\n==>Length of the TE sequence: $buffer bp\n==>Min match length require in the TE sequence: $limp bp\n==>Min sequence identity inside the TE sequence: $id \% \n==>Min quality score: $minqual\n\n";
print PARAM "TE absence detection:\n==>Length of the flanking sequence: $flank bp\n==>Min length of match on the two sides of the TE: $var bp\n==>Min. non-repeated mapped region on each side of the sequence:$lima bp\n";
if ($PE =~/yes/){
print PARAM "==>Paired-end mapping\n\n";
}
else{
print PARAM "==>Single-end mapping\n\n";
}
close PARAM;
#########################################################################################################################################################
############################################################## PREPARE THE INPUT DATA ###################################################################
#########################################################################################################################################################
print "\n\n\t\t\t\t ******************** Prepare the input data ********************\n\n";
print "\nSimplify the fasta file of the reference sequences ...\n";
&simplify_fasta("$refgenome","$outputdir\/Tgenome\.fasta");
my $flank_check=$flank;
my $flank_coords_check;
print "\n\n\t\t\t\t ******************** Tjunction analysis ********************\n\n";
system("mkdir $outputdir\/Tanalysis");
# Identification of TE insertions nested or flanked by repeats
print "Identification of TE insertions nested or flanked by repeats....\n";
$flank_coords_check = &extract_flanks("$TE_list","$TE_map","$flank_check","0","no");
open (OUT, ">$outputdir\/Tflank_checking\_$flank_check\.map");
print OUT $flank_coords_check;
close OUT;
&maptodb("$outputdir\/Tflank_checking\_$flank_check\.map","$outputdir\/Tgenome\.fasta","$outputdir\/Tflank_checking\_$flank_check\.fasta");
system("RepeatMasker -species $species $outputdir\/Tflank_checking\_$flank_check\.fasta");
system("mv $outputdir\/Tflank_checking\_$flank_check\.fasta\.out $outputdir\/Tflank_checking\_$flank_check\.fasta\.masked $outputdir\/Tanalysis");
$flank_coords_check = &extract_flanks("$TE_list","$TE_map","$flank_check","0","yes");
open (OUT1, ">$outputdir\/Tpoly\_$flank_check\.map");
print OUT1 $flank_coords_check;
close OUT1;
&maptodb("$outputdir\/Tpoly\_$flank_check\.map","$outputdir\/Tgenome\.fasta","$outputdir\/Tpoly\_$flank_check\.fasta");
system("cp $outputdir\/Tpoly\_$flank_check\.map $outputdir\/Tpoly\_$flank_check\.fasta $outputdir\/Tanalysis");
# Identification of TE insertions misannotated because of a longer Poly A/T tail
print "\nIdentification of TE insertions misannotated because of a longer Poly A/T tail....\n";
&polyAT_tail_detection("$outputdir\/Tanalysis", "$TE_map", "$TE_list","$outputdir\/Tgenome\.fasta", "$flank_check");
# Identification of TE insertions part of segmental duplications
print "\nIdentification of TE insertions part of segmental duplications....\n";
&segmental_dup_detection("$outputdir\/Tanalysis", "$TE_map", "$TE_list", "$outputdir\/Tflank_checking\_$flank_check\.fasta", "$outputdir\/Tgenome\.fasta","$maxReadLength","$id");
system("mv $outputdir\/Tflank_checking\_$flank_check\.fasta\.* $outputdir\/Tanalysis");
if ($noFilterTE) {
print "\t\t\t\t\t\t\t*******************TE FILTER step is bypassed*********************\n";
}
else{
my $start_time_TEfilter = localtime();
print "\t\t\t\t\t\t\t*******************FILTER TEs starts at $start_time_TEfilter*********************\n";
chdir("$outputdir\/Tanalysis");
my $TE_list_dir = dirname($TE_list);
if (index($TE_list_dir, "/") != -1) {
open (IN, "$TE_list") or die $!;
$TE_list =~ /.*\/(.*)$/;
$new_TE_list = $1
} else {
open (IN, "$startdirectory\/$TE_list") or die $!;
$new_TE_list = $TE_list;
$TE_list_dir = $startdirectory;
}
open (IN2, "Tflank_checking\_$flank_check\.fasta\.out") or die $!;
# $TE_list =~ /.*\/(.*)$/;
# $new_TE_list = $1;
open (OUT, ">$TE_list_dir\/$new_TE_list\_filtered");
open (OUT2, ">$TE_list_dir\/$new_TE_list\_FRD");
my $n1=0;
my $n2=0;
while(<IN>){
$n2=0;
my $TE=$_;
chomp $TE;
my @positions_RIGHT;
my @positions_LEFT;
my $totcov_LEFT=0;
my $totcov_RIGHT=0;
my $density_LEFT=0;
my $density_RIGHT=0;
my $tot=0;
open (IN2, "Tflank_checking\_$flank\.fasta\.out") or die $!;
while(<IN2>){
if ($n2>2){
my ($query_name, $query_start, $query_end, $left ) = (split)[4,5,6,7];
my @name = split(/_/,$query_name);
if ($query_name =~ /^$TE/){
if ($query_end < $query_start){
$query_end = $query_start;
$query_start = $query_end;
}
if ($name[1] =~ /LEFT/){
push @positions_LEFT, [$query_start, $query_end];
}
if ($name[1] =~ /RIGHT/){
push @positions_RIGHT, [$query_start, $query_end];
}
}
}
$n2++;
}
close(IN2);
$n1++;
if ($#positions_LEFT != -1) {
@positions_LEFT = sort {$a->[0] <=> $b->[0]} @positions_LEFT;
my $prec_start = $positions_LEFT[0][0];
my $prec_end = $positions_LEFT[0][1];
for my $i ( 1 .. $#positions_LEFT ) {
if ($prec_end > $positions_LEFT[$i][0]){
if($prec_end < $positions_LEFT[$i][1]){
print "Upating of the data for the TE $TE ...\n";
$positions_LEFT[$i-1][1] = $positions_LEFT[$i][1];
$prec_start = $positions_LEFT[$i-1][0];
$prec_end = $positions_LEFT[$i-1][1];
splice(@positions_LEFT,$i,1);
}
}
}
if ($#positions_LEFT>0){
for my $j ( 0 .. $#positions_LEFT ) {
my $repeat_length = $positions_LEFT[$j][1] - $positions_LEFT[$j][0];
$repeat_length = $repeat_length + 1;
$totcov_LEFT= $totcov_LEFT + $repeat_length ;
}
}
else{
my $repeat_length = $positions_LEFT[0][1] - $positions_LEFT[0][0];
$repeat_length = $repeat_length + 1;
$totcov_LEFT= $totcov_LEFT + $repeat_length ;
}
}
if ($#positions_RIGHT != -1) {
@positions_RIGHT = sort {$a->[0] <=> $b->[0]} @positions_RIGHT;
my $prec_start = $positions_RIGHT[0][0];
my $prec_end = $positions_RIGHT[0][1];
for my $i ( 1 .. $#positions_RIGHT ) {
if ($prec_end > $positions_RIGHT[$i][0]){
if($prec_end < $positions_RIGHT[$i][1]){
print "Ovlerlap: upating of the data ...\n";
$positions_RIGHT[$i-1][1] = $positions_RIGHT[$i][1];
print "\tAFTER UPDATE: precedent = $positions_RIGHT[$i-1][0]\-$positions_RIGHT[$i-1][1]\n";
$prec_start = $positions_RIGHT[$i-1][0];
$prec_end = $positions_RIGHT[$i-1][1];
splice(@positions_RIGHT,$i,1);
}
}
}
if ($#positions_RIGHT>0){
for my $j ( 0 .. $#positions_RIGHT ) {
my $repeat_length = $positions_RIGHT[$j][1] - $positions_RIGHT[$j][0];
$repeat_length = $repeat_length + 1;
$totcov_RIGHT= $totcov_RIGHT + $repeat_length ;
}
}
else{
my $repeat_length = $positions_RIGHT[0][1] - $positions_RIGHT[0][0];
$repeat_length = $repeat_length + 1;
$totcov_RIGHT= $totcov_RIGHT + $repeat_length ;
}
}
$density_LEFT=sprintf("%.2f",($totcov_LEFT/($flank)));
$density_RIGHT=sprintf("%.2f",($totcov_RIGHT/($flank)));
if ($density_LEFT < $minflankcov && $density_RIGHT < $minflankcov){
print OUT "$TE\n";
}
print OUT2 "$TE\t$totcov_LEFT\t$totcov_RIGHT\t$density_LEFT\t$density_RIGHT\n";
}
close OUT;
close OUT2;
$TE_list="$TE_list_dir\/$new_TE_list\_filtered";
print "* The new TE list is stored in the file : $TE_list\n";
chdir("..\/..\/");
}
unless ($shrimponly) {
my $start_time_BWA = localtime();
print "\n\n\t\t\t\t ******************** Launch Presence detection start at $start_time_BWA ********************\n\n";
print "Parameters for the detection of the *PRESENCE* of the given sequence(s):\n";
print " - Length of the flanking region = $junction\n";
print " - Length of the internal region = $buffer\n";
print "Convert the TE coordinates $TE_list ...\n";
my $flank_coords = &extract_flanks("$TE_list","$TE_map","$junction","$buffer","no");
open (OUT, ">$outputdir\/Tcoords_junction\_$junction\_$buffer\.map");
print OUT $flank_coords;
close OUT;
print "Extract the TE junctions ....\n";
&maptodb("$outputdir\/Tcoords_junction\_$junction\_$buffer\.map","$outputdir\/Tgenome\.fasta","$outputdir\/Tjunction\_$junction\_$buffer\.fasta");
system("mkdir $outputdir\/Tpresence");
if ($binreads) {
$reads = 1;
}
&PresenceDetectionMultipleStrains("$strains","$reads","$outputdir\/Tpresence","$maxReadLength","$processes","$junction","$buffer","$limp","$id","$minqual","$TE_map","$refgenome");
system("cp $outputdir\/Tpresence\/results $outputdir\/Tresults_presence");
system("rm $outputdir\/Tpresence\/results");
my $end_time_BWA = localtime();
print "\n\n\t\t\t\t ******************** Presence detection end at $end_time_BWA ************\n\n";
}
unless ($bwaonly){
my $start_time_SHR = localtime();
print "\n\n\t\t\t\t ******************** Launch Absence detection start at $start_time_SHR********************\n\n";
print "Parameters for the detection of the *ABSENCE* of the given sequence(s):\n";
print " - Length of the flanking region = $flank\n";
print " - Length of the internal region = 0\n";
print "Convert the TE coordinates ...\n";
my $flank_coords = &extract_flanks("$TE_list","$TE_map","$flank","0","no");
open (OUT, ">$outputdir\/Tcoords_flank\_$flank\.map");
print OUT $flank_coords;
close OUT;
print "Extract the TE flanked regions ....\n";
&maptodb("$outputdir\/Tcoords_flank\_$flank\.map","$outputdir\/Tgenome\.fasta","$outputdir\/Tflank\_$flank\.fasta");
open (IN, "$outputdir\/Tflank\_$flank.fasta");
open (OUT, ">$outputdir\/Tconcat\_$flank\.fasta");
while (<IN>) {
unless ($_ =~ /_RIGHT$/) {
unless ($_ =~ /_LEFT$/) {
print OUT $_;
}
if ($_ =~ /(.*)_LEFT$/) {
print OUT "$1\n";
}
}
}
close IN;
close OUT;
system("mkdir $outputdir\/Tabsence");
&AbsenceDetectionMultipleStrains("$strains","$outputdir\/Tabsence","$TE_list","$outputdir\/Tconcat\_$flank\.fasta","$reads","$lima","$var","$flank","$PE","$id");
system("cp $outputdir\/Tabsence\/results $outputdir\/Tresults_absence");
system("rm $outputdir\/Tabsence\/results");
my $end_time_SHRIMP = localtime();
print "\n\n\t\t\t\t ******************** Absence detection end at $end_time_SHRIMP ************\n\n";
}
if ($noFilterTE){
$newTElist = 1;
$TE_list=$original_TE_list;
print "TE list NO cleaned\n";
}
else{
print "TE list cleaned\n";
}
#############
my $start_TSD = localtime();
print "* TSD detection process start at $start_TSD\n";
if($tsd){
&MultiAlign("$outputdir","$TE_list","$strains","$flank","0","1");
}
my $end_TSD = localtime();
print "* TSD detection end at $end_TSD\n";
#############
my $start_align = localtime();
print "* Multiple Alignment process start at $start_align\n";
unless ($shrimponly) {
&MultiAlign("$outputdir","$TE_list","$strains","$flank","1","0");
}
unless ($bwaonly) {
&MultiAlign("$outputdir","$TE_list","$strains","$flank","0","0");
}
else{
&MultiAlign("$outputdir","$TE_list","$strains","$flank","2","0");
}
my $end_align = localtime();
print "* Multiple Alignment end at $end_align\n";
unless ($shrimponly || $bwaonly){
&FinalResults("$outputdir","$TE_list", "$TE_map","$flank");
&FreqEstimate("$outputdir","$pooleddata","$maxreads","$minreads","$minpop");
}
####################
unless ($noclean) {
print "cleaning......";
system("rm -rf $outputdir\/Tconcat_*.fasta $outputdir\/Tcoords* $outputdir\/Tflank* $outputdir\/Tgenome* Tjunction* $outputdir\/tmp*");
system("rm -rf $outputdir\/Tpresence $outputdir\/Tabsence");
}
my $end_time_Tlex = localtime();
print "\n\n\t\t\t\t ******************** T-lex finished successfully at $end_time_Tlex ************\n\n";
print "\n\n\t\t\t\t ************************ Have a nice day! ************************n\n";
}
#########################################################################################################################################################
########################################################################### HELP ########################################################################
########################################################################################################################################################
sub help {
print "T-lex\ release 3\n\n";
print "T-lex 3 works in modules (Presence, Absence, Combine, TSD detection, Frequency estimation) and each module requires a T-lex run\n\n";
print "\t- Usage:\n";
print "\t ======\n\n";
print"\t tlex-open-v2.pl [ options ] [ -T TE_list ] [ -M TE_annotations ] [ -G reference_genome ] [ -R Next-Generation Sequencing (NGS) data ]\n\n";
print "\t\-T \t\tfile with the list of the transposable element (TE) identifiers\n";
print "\t\-M \t\ttabulated file with the TE annotations (line format : \'TE name location start postion end position\')\n";
print "\t\-G \t\tfile of the reference genomic sequences in FASTA\n";
print "\t\-R \t\tdirectory of the NGS data in FASTQ\n\n\n\n";
print "\t- Options:\n";
print "\t ========\n\n";
print "\t\t\-A \t\tmax. read length in the data set ( default : 100 bp )\n";
print "\t\t\-O \t\tproject name\( by default the output directory will be \"tlex_output\"\)\n";
print "\t\t\-noclean \tkeep the intermediate files\n";
print "\t\t\-h or -help \tdisplay this help\n\n";
print "\t\t*For the analysis of the flanking sequences of each annotated TE: \n";
print "\t\t\-noFilterTE \tdo not filter TEs\n";
print "\t\t\-s \t\tname the species studied ( default: drosophila, cf. RepeatMasker program)\n";
print "\t\t\-d \t\tmin. repeat density at the flanking regions of the TEs ( default : 0.5, that corresponds to a repeat density of 50 %)\n\n";
print "\t\t\-id \t\tmin. sequence mapping identity required with the TE sequence in % ( default: 95 )\n";
print "\t\t*For the TE presence detection: \n";
print "\t\t\-q \t\tlaunch only the presence detection approach\n";
print "\t\t\-j \t\tlength of the junction sequences to extract in bp ( default: 1000 )\n";
print "\t\t\-b \t\tlength of the internal region of the TE in bp ( default: 60 )\n";
print "\t\t\-limp \t\tmin. length of match required with the TE sequence in bp ( default: 15 )\n";
print "\t\t\-minQ \t\tmin. quality Phred score for the read assembly ( default: 30 )\n";
print "\t\t\-processes \tnumber of processes (only used for the NGS data reformatting; default: 1)\n\n";
print "\t\t\-pairends \tpaired-end mapping ('yes' or 'no'; default: 'no'; that option requires NGS data such as <strain name>_reads<1 or 2>.fastq)\n\n";
print "\t\t*For the TE absence detection: \n";
print "\t\t\-p \t\tlaunch only the absence detection approach\n";
print "\t\t\-f \t\tlength of the flanking sequences to extract and concatenate in bp ( default: 125 )\n";
print "\t\t\-v \t\tmin. read length spanning the two TE sides in bp ( default: 20 )\n";
print "\t\t\-lima \t\tmin. non-repeated region on each side of the sequence in bp ( default: 15 )\n";
print "\t\t\-pairends \tpaired-end mapping ('yes' or 'no'; default: 'no'; that option requires NGS data such as <strain name>_reads<1 or 2>.fastq)\n\n";
print "\t\t*For manually combine the results:\n";
print "\t\t\-combRes \tcombine the presence/absence results from one dataset\n";
print "\t\t\-combData \tcombine the presence/absence results from several datasets stored in a different tlex output directories\n";
print "\t\t\-combAll \tcombine the frequency estimates, the analysis of the TE flanking regions and the TSD detection\n";
print "\t\t*Estimate the TE frequency: \n";
print "\t\t\-freq \t\treturn the TE frequency based on the given strains\n";
print "\t\t\-minP \t\tminimum number of TE data based on given strains ( default: 1 )\n";
print "\t\t\-pooled \treturn the TE frequency based on pooled data (To use with the option -freq)\n";
print "\t\t\-minR \t\tminimum number of reads to calculate frequency ( default: 3 )\n";
print "\t\t\-maxR \t\tmaximum number of reads to calculate frequency ( default: 90 )\n";
print "\t\t*For TSD and multialignment detection:\n";
print "\t\t-align \t\treturn the multiple alignments\n";
print "\t\t\-tsd \t\treturn the Target Site Duplication (TSD) for each given TE insertion detected as absent (use with -align & -p)\n";
die "\n";
}
#########################################################################################################################################################
################################################################ MULTIPLE ALIGNMENTS ####################################################################
#########################################################################################################################################################
sub MultiAlign(){
my $outputdir = $_[0];
my $TE_list = $_[1];
my $strains = $_[2];
my $flank = $_[3];
my $type = $_[4];
my $TSD = $_[5];
print "TYPE: $type\n";
print "Output directory: $outputdir\n\n";
chdir("$outputdir");
print "mkdir Talign\n";
system ("mkdir Talign");
if( $type == 1 || $type == 2){
print "PRESENCE ALIGNMENT\n";
if (-d "Talign\/presence_detection\/"){
print "Talign\/presence_detection directory created !\n";
}
else{
print "presence_detection directory does not exist !\n";
system("mkdir Talign\/presence_detection");
}
system("pwd");
open (INT, "$TE_list") or die $!;
while (<INT>) {
my $TE = $_;
chomp $TE;
opendir (DIR, "$strains");
my $nline=0;
while (defined( my $strain_name = readdir (DIR))) {
if ($strain_name !~ /\./) {
if ($nline == 0){
$nline++;
if(-e "Tpresence\/${strain_name}\/detection\/${TE}\_LEFT\.ref"){
system ("cat Tpresence\/${strain_name}\/detection\/${TE}\_LEFT\.ref > Talign\/presence_detection\/${TE}\_LEFT\.contig_ref");
}
if(-e "Tpresence\/${strain_name}\/detection\/${TE}\_RIGHT\.ref"){
system ("cat Tpresence\/${strain_name}\/detection\/${TE}\_RIGHT\.ref > Talign\/presence_detection\/${TE}\_RIGHT\.contig_ref");
}
}
if ($nline == 1){
if (-e "Talign\/presence_detection\/${TE}\_LEFT\.contig_ref"){
system ("cat Talign\/presence_detection\/${TE}\_LEFT\.contig_ref Tpresence\/\*\/detection\/${TE}\_LEFT\.contig > Talign\/presence_detection\/${TE}\_LEFT\.contig_all");
}
if (-e "Talign\/presence_detection\/${TE}\_RIGHT\.contig_ref"){
system ("cat Talign\/presence_detection\/${TE}\_RIGHT\.contig_ref Tpresence\/\*\/detection\/${TE}\_RIGHT\.contig > Talign\/presence_detection\/${TE}\_RIGHT\.contig_all");
}
last;
}
}
}
}
system ("rm Talign\/presence_detection\/*.contig_ref");
close IN ;
closedir DIR;
}
if( $type == 0 || $type == 2){
print "ABSENCE ALIGNMENT\n";
my %Selected=();
my %SelectedM=();
my %dejavu=();
if (-d "Talign\/"){
print "Talign\/ directory exists !\n";
}
else{
system("mkdir Talign\/");
}
if (-d "Talign\/absence_detection\/"){
print "Talign\/absence_detection directory exists !\n";
}
else{
print "absence_detection directory does not exist !\n";
system("mkdir Talign\/absence_detection");
opendir (DIR, "$strains");
while (defined( my $strain_name = readdir (DIR))) {
if ($strain_name !~ /\./) {
if (-e "Tabsence\/${strain_name}/detection/results_gmapper_overjunction_nodup_format_nogap.fa.masked"){
system ("cp Tabsence\/${strain_name}\/mapping/results_gmapper2merge Talign\/absence_detection\/Talign_${strain_name}.fasta");
system ("cp Tabsence\/${strain_name}\/detection\/results_gmapper_overjunction Talign\/absence_detection\/Talign_${strain_name}.merged");
system ("cp Tabsence\/${strain_name}\/detection\/results_gmapper_overjunction_nodup_format2select_format Talign\/absence_detection\/TselectedRead_${strain_name}.list");
}
else{
print "no file called Tabsence\/${strain_name}/detection/*_nogap.fa.masked";
next;
}
}
}
close DIR ;
opendir (DIR, "$strains"); # Removed ../
while (defined( my $strain_name = readdir (DIR))) {
if ($strain_name !~ /\./) {
print "\n\n ### $strain_name : \n";
my $read_headers;
my $TE_headers;
my $readseq;
my $refseq;
my $nickname;
my @fullname;
my @shortname;
my $pe;
my $me;
my $r1;
my $r2;
my $preTE="";
my $prereadt="";
open (SE, "Talign\/absence_detection\/TselectedRead_${strain_name}.list");
while (<SE>) {
my ($TE, $read)=(split)[0,1];
my $ntype = " ";
my @shortr=split("\/",$read);
if($shortr[1]){
$ntype = $shortr[1];
}
else{
$ntype = "merge";
}
$Selected{$TE}{$read} = $ntype;
}
close SE;
open (SEF, "Talign\/absence_detection\/Talign_${strain_name}.merged");
while (<SEF>) {
my ($read, $TE, $ref, $seq)=(split)[0,1,9,10];
my $ntype = " ";
my $nickm;
my @fullm=split("\:",$read);
if ( scalar(@fullm)>1 ) {
shift(@fullm);
$nickm=join(":",@fullm);
}
my @shortm=split("\/",$nickm);
if($shortm[1]){
$ntype = $shortm[1];
}
else{
$ntype = "merge";
}
if( $ntype =~ /merge/){
$SelectedM{$TE}{$nickm} = [$ref,$seq];
}
}
close SEF;
my $preTE_headers = "";
my $prenewname = "";
my $preme = "";
my $newname = "";
open (FA, "Talign\/absence_detection\/Talign_${strain_name}.fasta");
while(<FA>){
my @l = split();
my $lg = scalar(@l);
if ( $_ =~ /^\>/ && $lg >1 ) {
my @line = split();
$read_headers=$line[0];
$TE_headers=$line[1];
chomp $read_headers;
@fullname=split("\:",$read_headers);
if ( scalar(@fullname)>1 ) {
shift(@fullname);
$nickname=join(":",@fullname);
}
elsif ( scalar(@fullname)==1 ) {
@fullname=split("\>",$read_headers);
shift(@fullname);
$nickname=join("",@fullname);
}
else{
print "error read name";
die $!;
}
@shortname=split("\/",$nickname);
$pe = $shortname[0];
$me = $shortname[1];
}
if ($_ =~ /^G/) {
$refseq= (split)[2];
chomp $refseq;
}
if ($_ =~ /^R/) {
$readseq= (split)[2];
chomp $readseq;
$newname = $pe."/merge";
my $output_ref;
my $output_read;
if (exists $Selected{$TE_headers}{$nickname}){
my $output.=">$TE_headers\n$refseq\n$read_headers\n$readseq\n";
open (OUT, ">tmp_all");
print OUT $output;
close OUT;
$output_ref.=">${TE_headers}_$nickname\n$refseq\n";
open (OUTREF, ">tmp_ref");
print OUTREF $output_ref;
close OUTREF;
$output_read.=">${nickname}\n$readseq\n";
open (OUTREAD, ">tmp_reads");
print OUTREAD $output_read;
close OUTREAD;
unless (-e "Talign\/absence_detection\/${TE_headers}_${strain_name}.fasta" ) {
system(" touch Talign/absence_detection\/${TE_headers}_${strain_name}.fasta");
}
system(" cat tmp_all >> Talign\/absence_detection\/${TE_headers}_${strain_name}.fasta");
unless (-e "Talign\/absence_detection\/${TE_headers}_${strain_name}_ref.fasta" ) {
system(" touch Talign/absence_detection\/${TE_headers}_${strain_name}_ref.fasta");
}
system(" cat tmp_ref >> Talign\/absence_detection\/${TE_headers}_${strain_name}_ref.fasta");
unless (-e "Talign\/absence_detection\/${TE_headers}_${strain_name}_reads.fasta" ) {
system(" touch Talign/absence_detection\/${TE_headers}_${strain_name}_reads.fasta");
}
system(" cat tmp_reads >> Talign\/absence_detection\/${TE_headers}_${strain_name}_reads.fasta");
}
elsif (exists $Selected{$TE_headers}{$newname}){
if (($TE_headers eq $preTE_headers) && ($newname eq $prenewname) && ( ($me == 1 && $preme == 2) || ($me == 2 && $preme == 1)) ){
print "";
}
else{
my @seqs = $SelectedM{$TE_headers}{$newname};
$refseq = $seqs[0][0];
$readseq = $seqs [0][1];
my $output.=">${TE_headers}_$newname\n$refseq\n>${newname}\n$readseq\n";
open (OUT, ">tmp_merge");
print OUT $output;
close OUT;
$output_ref.=">${TE_headers}_$newname\n$refseq\n";
open (OUTREF, ">tmp_ref_merge");
print OUTREF $output_ref;
close OUTREF;
$output_read.=">${newname}\n$readseq\n";
open (OUTREAD, ">tmp_reads_merge");
print OUTREAD $output_read;
close OUTREAD;
unless (-e "Talign\/absence_detection\/${TE_headers}_${strain_name}.fasta" ) {
system(" touch Talign/absence_detection\/${TE_headers}_${strain_name}.fasta");
}
system(" cat tmp_merge >> Talign\/absence_detection\/${TE_headers}_${strain_name}.fasta");
unless (-e "Talign\/absence_detection\/${TE_headers}_${strain_name}_ref.fasta" ) {
system(" touch Talign/absence_detection\/${TE_headers}_${strain_name}_ref.fasta");
}
system(" cat tmp_ref_merge >> Talign\/absence_detection\/${TE_headers}_${strain_name}_ref.fasta");
unless (-e "Talign\/absence_detection\/${TE_headers}_${strain_name}_reads.fasta" ) {
system(" touch Talign/absence_detection\/${TE_headers}_${strain_name}_reads.fasta");
}
system(" cat tmp_reads_merge >> Talign\/absence_detection\/${TE_headers}_${strain_name}_reads.fasta");
}
}
$preTE_headers = $TE_headers;
$prenewname = $newname;
$preme = $me;
}
}
close FA;
}
}
close DIR;
}
my %SelectedTSD=();
if($TSD==1){
opendir (DIR, "$strains");
while (defined( my $strain_name = readdir (DIR))) {
if ($strain_name !~ /\./) {
open (SE, "Talign\/absence_detection\/TselectedRead_${strain_name}.list") or die $!;
while (<SE>) {
my ($TE, $read)=(split)[0,1];
my $readt = " ";
my $type = " ";
my @shortr=split("\/",$read);
$readt = $shortr[0];
if($shortr[1]){
$type = $shortr[1];
}
else{
$type = "merge";
}
$Selected{$TE}{$readt}{$type} = "selected";
}
close SE;
}
}
print "TARGET SITE DETECTION\n\n";
open (TTSD, ">Tannot_TSD");
print TTSD "TE\tputative_target_site\tTSD\tTSDdistance\tTSDresult\treference_sequence\tcontig_sequence\n";
for my $TE_headers ( keys %Selected ) {
print "\n#### $TE_headers\n";
system("cat Talign\/absence_detection\/${TE_headers}_*_reads.fasta > Talign\/absence_detection\/${TE_headers}_reads.fasta");
system("cat Talign\/absence_detection\/${TE_headers}_*_ref.fasta > Talign\/absence_detection\/${TE_headers}_ref.fasta");
my $nbreads=&fastacount("Talign\/absence_detection\/${TE_headers}_reads.fasta");
if ($nbreads <= 3) {
print " *** no enough reads to build a contig ==> $nbreads reads ***\n";
# use the single reads to detect the TSD
# 1/ split the fasta files : reads + ref
system("split -d -l 2 Talign\/absence_detection\/${TE_headers}_reads.fasta Talign\/absence_detection\/${TE_headers}_reads.fasta");
system("split -d -l 2 Talign\/absence_detection\/${TE_headers}_ref.fasta Talign\/absence_detection\/${TE_headers}_ref.fasta");
# 2/ Blat each
if ($nbreads > 0) {
system("blat Talign\/absence_detection\/${TE_headers}_reads.fasta00 Talign\/absence_detection\/${TE_headers}_ref.fasta00 Talign\/absence_detection\/${TE_headers}_reads.fasta00_blat -out=pslx &> stdoutput");
system("cat Talign\/absence_detection\/${TE_headers}_reads.fasta00_blat >> tmp_reads_blat");
if ($nbreads > 1) {
system("blat Talign\/absence_detection\/${TE_headers}_reads.fasta01 Talign\/absence_detection\/${TE_headers}_ref.fasta01 Talign\/absence_detection\/${TE_headers}_reads.fasta01_blat -out=pslx &> stdoutput");
system("cat Talign\/absence_detection\/${TE_headers}_reads.fasta01_blat >> tmp_reads_blat");
if ($nbreads > 2) {
system("blat Talign\/absence_detection\/${TE_headers}_reads.fasta02 Talign\/absence_detection\/${TE_headers}_ref.fasta02 Talign\/absence_detection\/${TE_headers}_reads.fasta02_blat -out=pslx &> stdoutput");
system("cat Talign\/absence_detection\/${TE_headers}_reads.fasta02_blat >> tmp_reads_blat");
}
}
}