forked from shendurelab/LACHESIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PreprocessSAMs.pl
executable file
·164 lines (126 loc) · 8.27 KB
/
PreprocessSAMs.pl
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
#!/usr/bin/perl -w
use strict;
#///////////////////////////////////////////////////////////////////////////////
#// //
#// This software and its documentation are copyright (c) 2014-2015 by Joshua //
#// N. Burton and the University of Washington. All rights are reserved. //
#// //
#// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS //
#// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF //
#// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. //
#// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY //
#// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT //
#// OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR //
#// THE USE OR OTHER DEALINGS IN THE SOFTWARE. //
#// //
#///////////////////////////////////////////////////////////////////////////////
# PreprocessSAMs.pl
#
# Syntax: PreprocessSAMs.pl <sam or bam filename> <draft assembly fasta>
#
# This Perl script prepares a SAM/BAM file for use with Lachesis.
# Specifically, it pre-processes the file with bedtools, samtools, picard to remove redundant, chimeric, and/or uninformative read pairs.
# This creates a dataset of Hi-C links with as strong a signal as possible, and it's also as small as possible, so as to reduce I/O runtime in Lachesis.
# (NOTE: As of August 24, 2013, I'm no longer removing PCR duplicates. Picard's MarkDuplicates is extremely slow and resource-intensive - far more so than
# the runtime benefit in Lachesis of having fewer reads. I don't think it's removing PCR duplicates properly, nor do I think PCR duplicate removal is even
# necessary - http://seqanswers.com/forums/showthread.php?t=6854).
#
# This script will determine whether the file is a SAM or a BAM file, and then run the following commands:
#
# COMMAND OUTPUT FILENAME WHAT THE COMMAND DOES
# make_bed_around_RE_site.pl <fasta>.near_<RE>.<range>.bed Prepare the bed file for bedtools intersect (next command)
# bedtools intersect <head>.REduced.bam Remove all reads that aren't within 500 bp of a restriction site
### picard SortSam.jar <head>.REduced.sort_coord.bam Sort the file in coordinate order so PCR duplicates can be removed
### picard MarkDuplicates.jar <head>.REduced.sort_coord.nodups.bam Remove PCR duplicates
### picard SortSam.jar <head>.REduced.nodups.bam Sort the file in query-name order so Lachesis can read it
# samtools view -F12 <head>.REduced.nodups.paired_only.bam Filter out all pairs in which both reads are not aligned
# samtools flagstat <head>.REduced.nodups.paired_only.flagstat Make a flagstat file that describes the contents of the BAM file
#
#
# The final output file will be <head>.REduced.paired_only.bam. This is what should be entered into the Lachesis INI file under the key "SAM_FILES".
#
# To pre-process several SAM/BAM files in parallel, use the script PreprocessSAMs.sh, which can be submitted to a cluster via qsub.
#
# Josh Burton
# July 2013
################################
# #
# USER-DEFINED PARAMETERS #
# #
################################
my $dry_run = 0; # if true, just print the commands to be run - don't actually run them
my $RE_site = 'AAGCTT'; # the restriction enzyme site at which the DNA was cut for the Hi-C experiment
# Paths to the necessary scripts and software packages.
my $make_bed_around_RE_site_pl = '/software/x86_64/LACHESIS/make_bed_around_RE_site.pl';
my $bedtools = '/software/x86_64/bedtools/bin/bedtools';
my $samtools = '/software/x86_64/samtools/bin/samtools';
#my $mem = "16G";
#my $picard_head = "java -d64 -Xmx$mem -jar /software/x86_64/picard-tools-1.112/";
################################
# #
# SUBROUTINES #
# #
################################
# Print and then run a command in bash (unless $dry_run, in which case just print it.)
# First argument: the command to run.
# Second argument (optional): the file to redirect stdout to.
sub run_cmd(@) {
my ($cmd,$redirect) = @_;
print localtime() . ": PreprocessSAMs.pl: $cmd\n";
return if $dry_run;
if ($redirect) { system ( "$cmd > $redirect" ) }
else { system ( $cmd ); }
}
################################
# #
# CONTROL STARTS HERE #
# #
################################
# Get the command-line arguments, or check syntax.
if ( @ARGV != 2 ) {
print STDERR "\nPreprocessSAMs.pl: A script to prepare SAM or BAM files for use with Lachesis.\n\nSyntax: $0 <sam-or-bam-filename> <draft-assembly-fasta>\n\n";
exit;
}
# Get the input filenames, and check that they actually exist.
my ( $SAM, $fasta) = @ARGV;
unless ( -e $SAM ) {
print STDERR "$0: Can't find input SAM/BAM file `$SAM`\n";
exit;
}
unless ( -e $fasta) {
print STDERR "$0: Can't find draft assembly file `$fasta`\n";
exit;
}
# Find the input file's "head" and extension.
my ($head,$extension) = $SAM =~ /^(.*)\.(.*)$/;
# Examine the extension to determine whether this is a SAM or a BAM file. If it's a SAM, convert it to BAM. If it doesn't seem to be either, throw an error.
if ( uc($extension) eq 'SAM' ) { run_cmd( "$samtools view -bS $SAM -o $head.bam" ); }
elsif ( uc($extension) eq 'BAM' ) {}
else {
print STDERR "$0: Can't determine file type for input file `$SAM`.\nFilename should end in '.SAM' or '.BAM' (not case-sensitive.)\n";
exit;
}
print "$0 @ARGV\n\n";
# COMMAND OUTPUT FILENAME WHAT THE COMMAND DOES
# make_bed_around_RE_site.pl <fasta>.near_<RE>.<range>.bed Prepare the bed file for bedtools intersect (next command)
#
# Make the BED file for the restriction sites on the draft assembly. This only needs to be done once.
my $BED_RE_file = "$fasta.near_$RE_site.500.bed";
run_cmd( "$make_bed_around_RE_site_pl $fasta $RE_site 500" ) unless -e $BED_RE_file;
# Do the pre-processing on this file.
#
# COMMAND OUTPUT FILENAME WHAT THE COMMAND DOES
# bedtools intersect <head>.REduced.bam Remove all reads that aren't within 500 bp of a restriction site
### picard SortSam.jar <head>.REduced.sort_coord.bam Sort the file in coordinate order so PCR duplicates can be removed
### picard MarkDuplicates.jar <head>.REduced.sort_coord.nodups.bam Remove PCR duplicates
### picard SortSam.jar <head>.REduced.nodups.bam Sort the file in query-name order so Lachesis can read it
# samtools view -F12 <head>.REduced.paired_only.bam Filter out all pairs in which both reads are not aligned
# samtools flagstat <head>.REduced.paired_only.flagstat Make a flagstat file that describes the contents of the BAM file
my $opts = "VALIDATION_STRINGENCY=SILENT";
my $nodups = ""; # or ".nodups", if removing PCR duplicates
run_cmd( "$bedtools intersect -abam $head.bam -b $BED_RE_file > $head.REduced.bam" );
#run_cmd( "${picard_head}SortSam.jar $opts I=$head.REduced.bam O=$head.REduced.sort_coord.bam SO=coordinate" );
#run_cmd( "${picard_head}MarkDuplicates.jar $opts I=$head.REduced.sort_coord.bam O=$head.REduced.sort_coord.nodups.bam M=$head.REduced.sort_coord.dup_metrics AS=true REMOVE_DUPLICATES=true" );
#run_cmd( "${picard_head}SortSam.jar $opts I=$head.REduced.sort_coord.nodups.bam O=$head.REduced.nodups.bam SO=queryname" );
run_cmd( "$samtools view -F12 $head.REduced$nodups.bam -b -o $head.REduced$nodups.paired_only.bam" );
run_cmd( "$samtools flagstat $head.REduced$nodups.paired_only.bam > $head.REduced$nodups.paired_only.flagstat" );