-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeist.sh
executable file
·2251 lines (1643 loc) · 81.7 KB
/
geist.sh
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/env bash
# Operate in the current directory
#$ -cwd
# Copyright (c) 2014, Matthew C. LaFave
# The script assumes it's operating on an SGE cluster, submitted via qsub, but
# it isn't required.
# This script is designed to identify MLV, AAV, Tol2, or Ds integrations, and
# report them as a BED file, a BAM file, and a file indicating how often each
# barcode was detected. Information on every read is also produced. If access
# to the intermediate files is required, using the -k option will turn on
# recovery of the informative intermediate files. Said files are produced as a
# separate tarball.
# Source .bashrc to get local aliases, etc.
source ~/.bashrc
# Set up functions for file testing & error reporting.
function throw_error
{
echo >&2 ERROR: $1
exit 1
}
function test_file
{
if
[ -f $1 ]
then
echo "$1 produced."
else
throw_error "$1 was not produced!"
fi
}
function test_variable
{
if
[ ! $1 ]
then
print_usage
throw_error "required file or option is missing"
fi
}
function full_path ()
{
DIR=`dirname $1`
FILE=`basename $1`
PATH="`cd \"$DIR\" 2>/dev/null && pwd -P || echo \"$DIR\"`/$FILE"
echo $PATH
}
# Get input via CLI
print_usage()
{
cat <<EOF
Usage: geist.sh [options] input.[fastq | bam]
Options:
-b bowtie index path (required)
-c fragment count cutoff (default: 0)
-h print this help message and exit
-i insert: MLV, AAV, Tol2, or Ds (required)
-k keep temporary intermediate files (default: off)
-n name
-r barcode reference file (required)
-v print version and quit
EOF
}
print_version()
{
cat <<EOF
2.1.0
EOF
}
cutoff=0
keep=off
name="GeIST"
while getopts "b:c:hi:kn:r:v" OPTION
do
case $OPTION in
b)
bowtie_index=$OPTARG
;;
c)
cutoff=$OPTARG
;;
h)
print_usage
exit 0
;;
k)
keep=on
;;
i)
insert=$OPTARG
;;
n)
name=$OPTARG
;;
r)
barcode_ref=$OPTARG
;;
v)
print_version
exit 0
;;
esac
done
shift $((OPTIND-1))
# Make sure the input file exists & contains data:
if test ! -f "$1"
then echo "$1 doesn't exist!"
exit 1
elif test ! -s "$1"
then echo "$1 is empty!"
exit 1
else
echo "$1 detected."
fi
# Copy the name of the input file, and change the extension to lowercase IF
# it's a BAM file
input_name=`echo $1 | perl -pe 's/\.bam$/.bam/i'`
if [[ $input_name == *.bam ]]
then
bam_file=`basename $1`
bam_path=`full_path $1`
else
fastq_file=`basename $1`
fastq_path=`full_path $1`
fi
test_variable $bowtie_index
test_variable $insert
test_variable $barcode_ref
# Identify the absolute paths
bowtie_index_path=`full_path $bowtie_index`
barcode_ref_name=`basename $barcode_ref`
barcode_ref_path=`full_path $barcode_ref`
# Identify the number of barcode groups
group_number=`awk 'BEGIN{n=1}{if($2 > n){n=$2}}END{print n}' ${barcode_ref_path}`
# Verify that the necessary programs are installed (also hash commands for
# faster lookup later)
hash cutadapt 2>/dev/null || throw_error "cutadapt not found"
hash bowtie 2>/dev/null || throw_error "bowtie not found"
hash samtools 2>/dev/null || throw_error "samtools not found"
if [[ $bam_file ]]
then
hash bamtools 2>/dev/null || throw_error "bamtools not found (required for BAM -> FASTQ conversion)"
fi
# Verify that the programs to be called are of the correct version
ver=`cutadapt --version`
if
awk -v ver="$ver" 'BEGIN {exit !(ver >= 1.3)}'
then
echo "cutadapt is version $ver"
else
throw_error "cutadapt is not at least version 1.3."
fi
ver=0
ver=`bowtie --version | head -1 | awk '{print $3}'`
echo "bowtie is version $ver ( >= v. 1.0.0 is preferred)"
ver=0
ver=`samtools 2>&1 | head -3 | awk '/^Version/ {print $2}' | sed 's/^\(.*\)-.*$/\1/'`
echo "samtools is version $ver ( >= v. 0.1.19 is preferred)"
ver=0
if [[ $bam_file ]]
then
ver=`bamtools --version | head -2 | tail -1 | awk '{print $2}'`
echo "bamtools is version $ver ( >= v. 1.0.2 is preferred)"
ver=0
fi
# Set parameters for the indicated insert
echo $insert | grep -i -q -w 'mlv' && insert=mlv
echo $insert | grep -i -q -w 'tol2' && insert=tol2
echo $insert | grep -i -q -w 'ds' && insert=ds
echo $insert | grep -i -q -w 'aav' && insert=aav
# Note that, for purposes of this script, the terminal portion of the
# integrated element is referred to as "LTR".
if [ "$insert" = "mlv" ]
then
echo "MLV integrations will be mapped."
LTR=TGAAAGACCCCCGCTGACGGGTAGTCAATCACTC
footprint=4
elif [ "$insert" = "tol2" ]
then
echo "Tol2 integrations will be mapped."
LTR=CAGAGGTGTAAAAAGTACTCAAAAATTTTACTCAAGTGA
footprint=8
elif [ "$insert" = "ds" ]
then
echo "Ds integrations will be mapped."
LTR=TAGGGATGAAAACGGTCGGTAACGGTCGGTAAA
footprint=8
elif [ "$insert" = "aav" ]
then
echo "AAV integrations will be mapped."
LTR=CGAGCGAGCGCGCAGAGA
footprint=1
else
throw_error "Insert $insert not recognized."
fi
# Check if intermediate files are to be deleted or not; default is to delete.
if [ "$keep" = "on" ]
then
echo "Retention of intermediate files has been turned on."
else
echo "Intermediate files will be deleted."
fi
# Verify that the name does not have blanks
echo $name | grep -q [[:blank:]] && throw_error "'name' can't have blanks"
# Make a working directory for the output of this script.
workdir=$PWD/Workdir_${name}_${insert}_$JOB_ID
if [ -d $workdir ] ; then throw_error "$workdir already exists!"; fi
mkdir $workdir
# Need to move the file to $workdir because at least one perl script puts its
# output file in the same directory as the input file.
echo ""
echo "Entering working directory ${workdir}..."
cd $workdir
# Create a file of just the barcode sequences
cut -f1 ${barcode_ref_path} > ${barcode_ref_name}_seq
# Adjust for relative paths
# bowtie_index=`echo $bowtie_index | awk '{ if($1 ~ /^\//){print}else{print "../"$1} }'`
# Check to make sure certain assumptions are met: for example, that the bowtie
# index you'll need actually exists, and is in the right place.
function verify_index
{
if
test ! -e $1
then
cd ..
rmdir $workdir
throw_error "Bowtie index $1 doesn't exist!"
elif
test ! -s $1
then
cd ..
rmdir $workdir
throw_error "Bowtie index $1 is empty!"
else
echo "Index $1 verified."
fi
}
echo ""
verify_index ${bowtie_index_path}.1.ebwt
verify_index ${bowtie_index_path}.2.ebwt
verify_index ${bowtie_index_path}.rev.1.ebwt
verify_index ${bowtie_index_path}.rev.2.ebwt
# .3.ebwt and .4.ebwt aren't used for single-end alignment, so it doesn't matter
# if they're there.
################################################################################
# Step 1.
# If the input is a BAM file, use Bamtools to convert a BAM file into FASTQ
# format. Read names must end in /1 or /2.
echo ""
echo "***Step 1: Convert BAM to FASTQ, if applicable.***"
if [[ $bam_file ]]
then
fastq_file=`echo ${bam_file} | perl -pe 's/\.bam/.fastq/i'`
bamtools convert -format fastq -in ${bam_path} -out ${fastq_file}
test_file ${fastq_file}
fastq_path=`full_path ${fastq_file}`
else
echo "Input is not BAM, and is assumed to be FASTQ."
fi
################################################################################
# Step 2.
# Make cutadapt files for LTR_F, LTR_R, linker_F, and LTRcat (which is a
# concatenation of LTR F and R, then reduced with
# remove_duplicate_fastq_names_v1.0.pl). This step also makes linker_cat,
# which is needed later to remove inverted linker. "F" refers to "forward"; "R"
# refers to "reverse". Both are in reference to the orientation of the
# sequence in question (proviral LTR or linker) on a given read.
echo ""
echo "***Step 2: Make initial cutadapt files.***"
# First, we want to find "forward"-oriented LTR adapters that overlap the 5' end
# of the read, OR don't overlap either end; we want to keep the DNA downstream
# of the LTR.
# A common theme throughout the script is that cutadapt can only be used to
# trim correctly if it is searching for the reverse complement of the target
# (otherwise, it would trim the wrong side if the target did not overlap either
# end). As such, there are many points at which the fastq file needs to be
# reverse-complemented, trimmed, and then reverse-complemented back to normal.
# This maintains compatibility with slightly older versions of cutadapt.
# Make the reverse complement of the fastq file
echo ""
echo "Making reverse complement of input fastq file..."
cat ${fastq_path} | ../perl/rcFastq.pl > revcom_${fastq_file}
test_file revcom_${fastq_file}
# Use cutadapt to search for the reverse complement of the LTR sequence in the
# reverse complement of the fastq file. As explained above, the reason this is
# necessary is because cutadapt originally didn't have a dedicated function for
# finding reads that occur where we want AND for getting the DNA from the side
# that we want (specifically, for KEEPING the DNA 3' of the LTR).
# If detecting MLV, first remove reads with LTR+TTTG+GGGGCTC. These reads are
# false positives; they are amplification events off the 5' LTR into the primer
# binding domain of the provirus, and do not provide accurate integration site
# information. As such, they are removed. Note that this is only the case if
# looking for MLV.
# '-e 0' indicates that no mismatches are allowed. '-e 0.05' indicates that
# the matching portion is allowed to differ from the query by 5%.
if [ "$insert" = "mlv" ]
then
echo ""
echo "Performing LTR_F+TTTG+GGGGCTC removal with cutadapt..."
cutadapt -a GAGCCCCCAAATGAAAGACCCCCGCTGACGGGTAGTCAATCACTC -q 3 -m 11 -O 17 -e 0.05 \
--untrimmed-output=noLTR+GGGGCTCrevcom_${fastq_file} \
-o /dev/null revcom_${fastq_file}
echo ""
test_file noLTR+GGGGCTCrevcom_${fastq_file}
inputF=noLTR+GGGGCTCrevcom_${fastq_file}
else
inputF=revcom_${fastq_file}
fi
# Set quality cutoff to 3 (to ignore reads if the LTR portion is of poor
# quality; that is, if the FASTQ quality is #)
# Require that trimmed reads have at least 11 bp left
# Require an overlap of at least 17 bp between the read and the LTR (to be
# conservative). This value, and -O values that follow, were selected to
# provide reasonably conservative cutoffs for reads produced by the HiSeq 2500.
# We don't really care about the untrimmed output; this is just to separate it
# from the trimmed output. That's why it's sent to null.
echo ""
echo "Performing LTR_F detection with cutadapt..."
cutadapt -a ${LTR} -q 3 -m 11 -O 17 -e 0 \
--untrimmed-output=/dev/null -o cut_LTRrevcom_${fastq_file} \
${inputF}
echo ""
test_file cut_LTRrevcom_${fastq_file}
# If the MLV intermediate was produced, remove it.
if [ -f noLTR+GGGGCTCrevcom_${fastq_file} ]
then
rm noLTR+GGGGCTCrevcom_${fastq_file}
fi
# If analyzing AAV, trim the variable region
if [ "$insert" = "aav" ]
then
#### AAV_TRIM ####
echo ""
echo "Removing extra AAV sequence, knowing that the AAV primer was directly adjacent."
echo "aav_trim.pl says:"
perl ../perl/aav_trim.pl cut_LTRrevcom_${fastq_file} yes
test_file cut_LTRrevcom_${fastq_file}_aavtrim.fastq
test_file cut_LTRrevcom_${fastq_file}_itrbits.fastq
LTRtrim=cut_LTRrevcom_${fastq_file}_aavtrim.fastq
rm cut_LTRrevcom_${fastq_file}
if [ "$keep" = "off" ]; then rm cut_LTRrevcom_${fastq_file}_itrbits.fastq; fi
####
else
LTRtrim=cut_LTRrevcom_${fastq_file}
fi
echo ""
echo "Flipping cutadapt output back to the original orientation..."
cat ${LTRtrim} | ../perl/rcFastq.pl > cut_LTR_F_${fastq_file}
test_file cut_LTR_F_${fastq_file}
rm ${LTRtrim}
# This part is next because it also makes use of revcom_${fastq_file}; when it's
# done, it can remove it.
# Make linker_F, a file in which forward-facing linker has been detected and
# removed.
# Require an overlap of at least 10 bp between the read and the linker (this
# will end up being the same as requiring an overlap of 17 bp: 7 are the
# barcode, and 10 are the actual linker) Again, we don't really care about the
# untrimmed output; this is just to separate it from the trimmed output.
# That's why it's sent to null.
echo ""
echo "Performing linker_F detection with cutadapt..."
cutadapt \
-a ATGCGCAGTCGACCACGC -q 3 -m 18 -O 10 -e 0 \
--untrimmed-output=/dev/null -o cut_linkerrevcom_${fastq_file} revcom_${fastq_file}
test_file cut_linkerrevcom_${fastq_file}
rm revcom_${fastq_file}
echo ""
echo "Trimming linker_F barcodes. Barcode grab.pl says:"
perl ../perl/barcode_grab_v1.0.pl ${barcode_ref_name}_seq cut_linkerrevcom_${fastq_file}
test_file cut_linkerrevcom_${fastq_file}_bctrimmed.fastq
# Note that cut_linkerrevcom_${fastq_file}_barcodes.fastq is also produced here,
# the list of barcodes from reads with linker on the "forward" end.
test_file cut_linkerrevcom_${fastq_file}_barcodes.fastq
# Remove superfluous files
rm cut_linkerrevcom_${fastq_file}
rm cut_linkerrevcom_${fastq_file}_untrimmed.fastq # Lists untrimmed reads
rm cut_linkerrevcom_${fastq_file}_bcsummary # Summary of how many times each
# barcode was detected
echo "Flipping cutadapt output back to the original orientation..."
cat cut_linkerrevcom_${fastq_file}_bctrimmed.fastq | \
../perl/rcFastq.pl > cut_linker_F_${fastq_file}
test_file cut_linker_F_${fastq_file}
rm cut_linkerrevcom_${fastq_file}_bctrimmed.fastq
# Make linker_R: this will be used to pare down duplicates from linker_F, later.
# The idea is that reads that contain both forward and reverse linker sequence
# have something wrong with them, and should be removed.
echo ""
echo "Performing linker_R detection with cutadapt..."
cutadapt \
-a ATGCGCAGTCGACCACGC -q 3 -m 18 -O 10 -e 0 \
--untrimmed-output=/dev/null -o cut_linker_R_withbc_${fastq_file} ${fastq_path}
# "withbc" means it hasn't had the barcodes trimmed off yet.
test_file cut_linker_R_withbc_${fastq_file}
echo ""
echo "Trimming linker_R barcodes. Barcode grab.pl says:"
perl ../perl/barcode_grab_v1.0.pl ${barcode_ref_name}_seq cut_linker_R_withbc_${fastq_file}
# Change file names
mv cut_linker_R_withbc_${fastq_file}_bctrimmed.fastq cut_linker_R_${fastq_file}
mv cut_linker_R_withbc_${fastq_file}_barcodes.fastq cut_linker_R_${fastq_file}_barcodes.fastq
echo ""
test_file cut_linker_R_${fastq_file}
test_file cut_linker_R_${fastq_file}_barcodes.fastq
# Remove superfluous files
rm cut_linker_R_withbc_${fastq_file}
rm cut_linker_R_withbc_${fastq_file}_untrimmed.fastq
rm cut_linker_R_withbc_${fastq_file}_bcsummary
echo ""
echo "Combining linker_F and linker_R..."
cat cut_linker_F_${fastq_file} cut_linker_R_${fastq_file} > ${fastq_file}_linker_cat.fastq
test_file ${fastq_file}_linker_cat.fastq
rm cut_linker_R_${fastq_file}
# IMPORTANT - there may be duplicate names in that final output file; that's
# why I run remove_duplicate_fastq_names_v1.0.pl, to remove reads with linker_F
# and _R.
# Output is ${fastq_file}_linker_cat.fastq_nodup.fastq
echo ""
echo "The perl script remove_duplicate_fastq_names_v1.0.pl says:"
perl ../perl/remove_duplicate_fastq_names_v1.0.pl ${fastq_file}_linker_cat.fastq
echo ""
test_file ${fastq_file}_linker_cat.fastq_nodup.fastq
rm ${fastq_file}_linker_cat.fastq
# Now, use the nodup.fastq file to pare down linker_F, such that it does not
# contain any reads that have linker pointed both left and right in the same
# read.
# The first file is the list of names that you want to keep; the second file is
# the one you get the actual sequences from.
echo ""
echo "The perl script fastq_file_overlap_v1.0.pl is running to remove inverted linker"
echo "from cut_linker_F_${fastq_file}."
echo "It says:"
perl ../perl/fastq_file_overlap_v1.0.pl ${fastq_file}_linker_cat.fastq_nodup.fastq \
cut_linker_F_${fastq_file}
echo ""
test_file cut_linker_F_${fastq_file}_overlap.fastq
rm cut_linker_F_${fastq_file}
# The output is cut_linker_F_${fastq_file}_overlap.fastq, but that's a
# misleading name, so change it.
echo "Changing the name of cut_linker_F_${fastq_file}_overlap.fastq..."
mv cut_linker_F_${fastq_file}_overlap.fastq cut_linker_F_nodup_${fastq_file}
test_file cut_linker_F_nodup_${fastq_file}
# Next, look for the reverse complement of the LTR in the original "unflipped"
# fastq file. This makes the LTR_R file.
# As before, first remove reads with LTR_R+TTTG+GGGGCTC.
if [ "$insert" = "mlv" ]
then
echo ""
echo "Performing LTR_R+TTTG+GGGGCTC removal with cutadapt..."
cutadapt -a GAGCCCCCAAATGAAAGACCCCCGCTGACGGGTAGTCAATCACTC -q 3 -m 11 -O 17 -e 0.05 \
--untrimmed-output=noLTR_R-GGGGCTC_${fastq_file} \
-o /dev/null ${fastq_path}
test_file noLTR_R-GGGGCTC_${fastq_file}
inputR=noLTR_R-GGGGCTC_${fastq_file}
else
inputR=${fastq_path}
fi
# Detect/trim actual LTR.
echo ""
echo "Performing LTR_R detection with cutadapt..."
cutadapt -a ${LTR} -q 3 -m 11 -O 17 -e 0 \
--untrimmed-output=/dev/null -o cut_LTR_R_${fastq_file} \
${inputR}
test_file cut_LTR_R_${fastq_file}
if [ -f noLTR_R-GGGGCTC_${fastq_file} ]
then
rm noLTR_R-GGGGCTC_${fastq_file}
fi
if [ "$insert" = "aav" ]
then
#### AAV_TRIM ####
echo ""
echo "Removing extra AAV sequence, knowing that the AAV primer was directly adjacent..."
echo "aav_trim.pl says:"
perl ../perl/aav_trim.pl cut_LTR_R_${fastq_file} yes
test_file cut_LTR_R_${fastq_file}_aavtrim.fastq
test_file cut_LTR_R_${fastq_file}_itrbits.fastq
if [ "$keep" = "off" ]; then rm cut_LTR_R_${fastq_file}_itrbits.fastq; fi
# cut_LTR_R_${fastq_file} shows up a lot in the rest of the script, so I'll just
# rename my other output to match. This will replace the input file.
echo "Renaming the aav trimmed version of LTR_R to cut_LTR_R_${fastq_file}."
mv cut_LTR_R_${fastq_file}_aavtrim.fastq cut_LTR_R_${fastq_file}
test_file cut_LTR_R_${fastq_file}
fi
# Concatenate LTR F and R together
echo ""
echo "Combining LTR_F and LTR_R..."
cat cut_LTR_F_${fastq_file} cut_LTR_R_${fastq_file} > ${fastq_file}_LTR_cat.fastq
# IMPORTANT - there may be duplicate names in that final output file (reads
# that have both forward and reverse LTR); run
# remove_duplicate_fastq_names_v1.0.pl to get rid of them.
echo ""
echo "Removing reads with both LTR_F and LTR_R."
echo "The perl script remove_duplicate_fastq_names_v1.0.pl says:"
perl ../perl/remove_duplicate_fastq_names_v1.0.pl ${fastq_file}_LTR_cat.fastq
echo ""
test_file ${fastq_file}_LTR_cat.fastq_nodup.fastq
rm ${fastq_file}_LTR_cat.fastq
################################################################################
# Step 3.
# Use split_LTR_to_long_and_short_v1.0.pl to look through LTR_F, and sort the
# reads into long and short fragments (2 hashes). Short fragments are those in
# which LTR could be detected by cutadapt in forward orientation in one read,
# and reverse orientation on the paired read; long fragments are those in which
# only forward LTR could be detected.
echo ""
echo "***Step 3. Sort LTR_F into long and short fragments.***"
echo ""
echo "split_LTR_to_long_and_short_v1.0.pl says:"
perl ../perl/split_LTR_to_long_and_short_v1.0.pl cut_LTR_R_${fastq_file} \
cut_LTR_F_${fastq_file} ${fastq_file}_LTR_cat.fastq_nodup.fastq
echo ""
test_file cut_LTR_F_${fastq_file}_short.fastq
test_file cut_LTR_F_${fastq_file}_long.fastq
rm cut_LTR_F_${fastq_file}
############################ Formatting LTR-F-long #############################
# Step 4.
# Use cutadapt to find forward linker in the LTR_F_long file. If the read has
# F linker in the same read that has F LTR, throw it out.
echo ""
echo "***Step 4. Begin formatting of LTR_F_long.***"
# This processes trims reads that have been designated as coming from long
# fragments.
# First, remove any reads with forward-facing linker.
# In this case, I don't care what the barcode is. Further, it's MORE
# conservative to not set -e to 0, as you'll throw out more reads this way;
# the same goes for not setting -q.
echo ""
echo "Creating reverse complement of LTR_F_long..."
cat cut_LTR_F_${fastq_file}_long.fastq | ../perl/rcFastq.pl > \
revcom_cut_LTR_F_${fastq_file}_long.fastq
test_file revcom_cut_LTR_F_${fastq_file}_long.fastq
echo ""
echo "Using cutadapt to remove reads containing linker_F..."
cutadapt -a ATGCGCAGTCGACCACGC -m 11 -O 17 \
--untrimmed-output=noncut_revcom_cut_LTR_F_${fastq_file}_long.fastq \
-o /dev/null revcom_cut_LTR_F_${fastq_file}_long.fastq
test_file noncut_revcom_cut_LTR_F_${fastq_file}_long.fastq
rm revcom_cut_LTR_F_${fastq_file}_long.fastq
echo "Flipping the cutadapt untrimmed output back to the original orientation..."
cat noncut_revcom_cut_LTR_F_${fastq_file}_long.fastq | ../perl/rcFastq.pl > \
cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link
test_file cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link
rm noncut_revcom_cut_LTR_F_${fastq_file}_long.fastq
################################################################################
# Step 5.
# Retain only reads that have forward linker in the paired read. This ensures
# that all long fragments will have LTR and linker that are oriented toward each
# other.
echo ""
echo "***Step 5. Only keep LTR_F_long reads that have linker_F in the paired read.***"
# Remove reads that do not have a paired read with forward-facing linker. This
# is the reason that the linker_F file needs to be included in this portion of
# the script.
echo ""
echo "Running pair_in_other_file_v1.0.pl to print LTR_F_long reads that have linker_F"
echo "in their paired read. It says:"
perl ../perl/pair_in_other_file_v1.0.pl \
cut_linker_F_nodup_${fastq_file} cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link
# Output is cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink.fastq.
echo ""
test_file cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink.fastq
# Once I have _no-bad-F-link_pairhaslink.fastq, there's no need to keep
# _no-bad-F-link around.
rm cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link
# if [ "$keep" = "off" ]; then rm cut_linker_F_nodup_${fastq_file}; fi
################################################################################
# Step 6.
# Use cutadapt to remove reads with inappropriate reverse linker sequences.
# Note that reads that lack linker are NOT thrown out (--untrimmed is set, but
# those results are later concatenated with the others).
echo ""
echo "***Step 6. Trim linker_R from LTR_F_long reads.***"
# Use cutadapt to remove reverse linker from the output of the perl script. So,
# the final output should be the LTR-containing reads of long fragments, in
# which any reverse linker has been trimmed, and there never was any forward
# linker (on that read) in the first place. The idea is that reverse linker IS
# permissible on the read with forward LTR, but is not required.
# I use 'uo' to refer to untrimmed output of cutadapt.
echo ""
echo "Trimming linker_R using cutadapt..."
cutadapt \
-a ATGCGCAGTCGACCACGC -q 3 -m 11 -O 1 -e 0 \
--untrimmed-output=cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink_uo.fastq \
-o cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink_rm-R-link_withbc.fastq \
cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink.fastq
test_file cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink_rm-R-link_withbc.fastq
test_file cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink_uo.fastq
# Make a list of the reads that look like they might have linker_R
echo "Extracting names that MIGHT have linker_R..."
awk 'NR%4==1' \
cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink_rm-R-link_withbc.fastq \
| sort \
> cut_LTR_F_${fastq_file}_long.fastq_putative_linker_R
test_file cut_LTR_F_${fastq_file}_long.fastq_putative_linker_R
# Remove the barcodes; the relevant barcodes have already been recorded, so
# these don't need to be kept.
echo ""
echo "Trimming linker_R barcodes. Barcode grab.pl says:"
perl ../perl/barcode_grab_v1.0.pl ${barcode_ref_name}_seq \
cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink_rm-R-link_withbc.fastq
test_file cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink_rm-R-link_withbc.fastq_bctrimmed.fastq
# Delete superfluous files
rm cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink_rm-R-link_withbc.fastq
rm cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink_rm-R-link_withbc.fastq_barcodes.fastq
rm cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink_rm-R-link_withbc.fastq_untrimmed.fastq
rm cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink_rm-R-link_withbc.fastq_bcsummary
# Extract the names of the putative linker_R reads that DID have barcode
echo "Extracting names of reads that DID have barcode..."
awk 'NR%4==1' \
cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink_rm-R-link_withbc.fastq_bctrimmed.fastq \
| sort \
> cut_LTR_F_${fastq_file}_long.fastq_putative_linker_R_withbc
test_file cut_LTR_F_${fastq_file}_long.fastq_putative_linker_R_withbc
# Use join -v to identify the reads that did NOT actually have barcode; restore
# them to their pre-trimmed state.
echo ""
echo "Identifying putative linker_R reads that did NOT have barcode..."
join -v 1 \
cut_LTR_F_${fastq_file}_long.fastq_putative_linker_R \
cut_LTR_F_${fastq_file}_long.fastq_putative_linker_R_withbc \
> cut_LTR_F_${fastq_file}_long.fastq_no_linker_R_barcode
test_file cut_LTR_F_${fastq_file}_long.fastq_no_linker_R_barcode
rm cut_LTR_F_${fastq_file}_long.fastq_putative_linker_R
rm cut_LTR_F_${fastq_file}_long.fastq_putative_linker_R_withbc
echo "Restoring non-linker_R reads to pre-trimmed state..."
perl ../perl/names_to_fastq.pl \
cut_LTR_F_${fastq_file}_long.fastq_no_linker_R_barcode \
cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink.fastq
test_file cut_LTR_F_${fastq_file}_long.fastq_no_linker_R_barcode.fastq
rm cut_LTR_F_${fastq_file}_long.fastq_no_linker_R_barcode
rm cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink.fastq
echo "Trimming the # bases from the pre-trimmed reads..."
cat cut_LTR_F_${fastq_file}_long.fastq_no_linker_R_barcode.fastq \
| perl ../perl/fastq_lowqual_trim.pl \
> cut_LTR_F_${fastq_file}_long.fastq_no_linker_R_barcode_qualtrimmed.fastq
test_file cut_LTR_F_${fastq_file}_long.fastq_no_linker_R_barcode_qualtrimmed.fastq
rm cut_LTR_F_${fastq_file}_long.fastq_no_linker_R_barcode.fastq
# Combine the reads without linker to those that had linker and barcode trimmed.
# Remember that "without linker" encompasses two files: those in which linker
# was never seen, and those where the linker was thought to be there, but
# wasn't.
cat cut_LTR_F_${fastq_file}_long.fastq_no_linker_R_barcode_qualtrimmed.fastq \
cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink_uo.fastq \
cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink_rm-R-link_withbc.fastq_bctrimmed.fastq \
> cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink_rm-R-link.fastq
test_file cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink_rm-R-link.fastq
rm cut_LTR_F_${fastq_file}_long.fastq_no_linker_R_barcode_qualtrimmed.fastq
rm cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink_uo.fastq
rm cut_LTR_F_${fastq_file}_long.fastq_no-bad-F-link_pairhaslink_rm-R-link_withbc.fastq_bctrimmed.fastq
# Also, since I'm done processing LTR_F_long, I no longer need
# cut_LTR_F_${fastq_file}_long.fastq.
rm cut_LTR_F_${fastq_file}_long.fastq
########################### Formatting LTR-F-short #############################
# Step 7.
# Remove reads with inverted linker from LTR-F-short. Then, use cutadapt to
# trim reverse linker from LTR_F reads from short fragments; only keep those
# that had linker.
echo ""
echo "***Step 7. Trim linker_R from LTR_F_short.***"
# This step trims reverse linker from reads that were judged to be from short
# fragments by split_LTR_to_long_and_short_v1.0.pl.
# First, remove any reads with inverted linker (forward linker AND reverse
# linker). Any truly good read from LTR_F_short will also be in
# linker_cat (which already had inverted linkers removed), albeit trimmed
# differently. The perl script only looks at names, though, so the trimming
# doesn't matter for this step.
echo ""
echo "Running fastq_file_overlap_v1.0.pl to only keep LTR_F_short reads that"
echo "do NOT have inverted linker. It says:"
perl ../perl/fastq_file_overlap_v1.0.pl ${fastq_file}_linker_cat.fastq_nodup.fastq \
cut_LTR_F_${fastq_file}_short.fastq
# Output is cut_LTR_F_${fastq_file}_short.fastq_overlap.fastq.
test_file cut_LTR_F_${fastq_file}_short.fastq_overlap.fastq
if [ "$keep" = "off" ]; then rm cut_LTR_F_${fastq_file}_short.fastq; fi
rm ${fastq_file}_linker_cat.fastq_nodup.fastq
# Trim reads of reverse linker. As all short reads should have reverse linker,
# --untrimmed is sent to null (meaning the only reads to be kept are those with
# reverse linker). Low quality may obscure some reads that genuinely do have
# linker, but for those, we just have to hope that the paired read is caught in
# another step (for LTR_R reads).
echo ""
echo "Trimming linker_R off of LTR_F_short via cutadapt..."
cutadapt -a ATGCGCAGTCGACCACGC -q 3 -m 18 -O 1 -e 0 \
--untrimmed-output=/dev/null \
-o cut_LTR_F_${fastq_file}_short.fastq_trim-R-link_withbc.fastq \
cut_LTR_F_${fastq_file}_short.fastq_overlap.fastq
echo ""
echo "Trimming linker_R barcodes. Barcode grab.pl says:"
perl ../perl/barcode_grab_v1.0.pl ${barcode_ref_name}_seq cut_LTR_F_${fastq_file}_short.fastq_trim-R-link_withbc.fastq
test_file cut_LTR_F_${fastq_file}_short.fastq_trim-R-link_withbc.fastq_bctrimmed.fastq
rm cut_LTR_F_${fastq_file}_short.fastq_overlap.fastq
# Remove superfluous files
rm cut_LTR_F_${fastq_file}_short.fastq_trim-R-link_withbc.fastq_barcodes.fastq
rm cut_LTR_F_${fastq_file}_short.fastq_trim-R-link_withbc.fastq
rm cut_LTR_F_${fastq_file}_short.fastq_trim-R-link_withbc.fastq_untrimmed.fastq
rm cut_LTR_F_${fastq_file}_short.fastq_trim-R-link_withbc.fastq_bcsummary
# Change the name of the output file
mv cut_LTR_F_${fastq_file}_short.fastq_trim-R-link_withbc.fastq_bctrimmed.fastq \
cut_LTR_F_${fastq_file}_short.fastq_trim-R-link.fastq
test_file cut_LTR_F_${fastq_file}_short.fastq_trim-R-link.fastq
############################### Formatting LTR-R ###############################
# Step 8.
# Use fastq_file_overlap twice - once to clean LTR-R of inverted LTR reads, and
# once to clean it of inverted linker reads. Then, use cutadapt on LTR-R, only
# keeping reads that contain forward-facing linker.
echo ""
echo "***Step 8. Format LTR_R.***"
# Use fastq_file_overlap_v1.0.pl to eliminate entries in LTR_R that have inverted
# LTR. This IS a little different from split_LTR_to_long_and_short_v1.0.pl, in which
# the sequences were printed from LTR_cat; here, they're printed from LTR_R.
# The result is the same, though.
echo ""
echo "Running fastq_file_overlap_v1.0.pl to remove entries from LTR_R that have inverted LTR."
echo "It says:"
perl ../perl/fastq_file_overlap_v1.0.pl ${fastq_file}_LTR_cat.fastq_nodup.fastq \
cut_LTR_R_${fastq_file}
# Output is cut_LTR_R_${fastq_file}_overlap.fastq
echo ""
test_file cut_LTR_R_${fastq_file}_overlap.fastq
if [ "$keep" = "off" ]; then rm cut_LTR_R_${fastq_file}; fi
if [ "$keep" = "off" ]; then rm ${fastq_file}_LTR_cat.fastq_nodup.fastq; fi
# We also need to remove any reads that have BOTH forward and reverse linker.
# Do so by comparing ${LTR_R}_overlap.fastq to the linker_cat file. At this
# point, we only want reads that have forward-facing linker anyway, so the good
# reads won't be thrown out by doing this.
echo ""
echo "Running fastq_file_overlap_v1.0.pl to remove entries from LTR_R that have inverted linker."
echo "It says:"
# perl ../perl/fastq_file_overlap_v1.0.pl ${fastq_file}_linker_cat.fastq_nodup.fastq \
# cut_LTR_R_${fastq_file}_overlap.fastq
perl ../perl/fastq_file_overlap_v1.0.pl cut_linker_F_nodup_${fastq_file} \
cut_LTR_R_${fastq_file}_overlap.fastq
# Output is cut_LTR_R_${fastq_file}_overlap.fastq_overlap.fastq.
echo ""
test_file cut_LTR_R_${fastq_file}_overlap.fastq_overlap.fastq
rm cut_LTR_R_${fastq_file}_overlap.fastq
if [ "$keep" = "off" ]; then rm cut_linker_F_nodup_${fastq_file}; fi
# Then, use cutadapt to identify reads with linker_F on the same read. Trim
# these reads of linker, and keep only those reads.
# To do that, the fastq file must first be flipped.
echo ""
echo "Creating reverse complement of cut_LTR_R_${fastq_file}_overlap.fastq_overlap.fastq."
cat cut_LTR_R_${fastq_file}_overlap.fastq_overlap.fastq | ../perl/rcFastq.pl > \
revcom_cut_LTR_R_${fastq_file}_doubleoverlap.fastq
test_file revcom_cut_LTR_R_${fastq_file}_doubleoverlap.fastq
rm cut_LTR_R_${fastq_file}_overlap.fastq_overlap.fastq
# To detect forward linker and trim it correctly, it's necessary to look for
# reverse complement linker on reverse complement template.
echo ""