annotate SoftSearch.pl @ 3:891f20b32b2f draft

Uploaded
author plus91-technologies
date Thu, 29 May 2014 05:16:45 -0400
parents 0fc089b0d1ba
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1 #!/usr/bin/perl
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
2
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
3 ####
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
4 #### Usage: SoftSearch.pl [-lqrmsd] -b <BAM> -f <Genome.fa> -sam <samtools path> -bed <bedtools path>
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
5 #### Created 1-30-2012 by Steven Hart, PhD
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
6 #### hart.steven@mayo.edu
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
7 #### Required bedtools & samtools to be in path
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
8
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
9
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
10 use lib "/data2/bsi/reference/softsearch/lib" ;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
11
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
12 use Getopt::Long;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
13 use strict;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
14 use warnings;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
15 #use Data::Dumper;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
16 use LevD;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
17 use File::Basename;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
18
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
19 my ($INPUT_BAM,$INPUT_FASTA,$OUTPUT_FILE,$minSoft,$minSoftReads,$dist_To_Soft,$bedtools,$samtools);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
20 my ($minRP, $temp_output, $num_sd, $MapQ, $chrom, $unmated_pairs, $minBQ, $pair_only, $disable_RP_only);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
21 my ($levD_local_threshold, $levD_distl_threshold,$pe_upper_limit,$high_qual,$sv_only,$blacklist,$genome_file,$verbose);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
22
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
23 my $cmd = "";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
24
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
25 #Declare variables
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
26 GetOptions(
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
27 'b=s' => \$INPUT_BAM,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
28 'f=s' => \$INPUT_FASTA,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
29 'o:s' => \$OUTPUT_FILE,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
30 'm:i' => \$minRP,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
31 'l:i' => \$minSoft,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
32 'r:i' => \$minSoftReads,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
33 't:i' => \$temp_output,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
34 's:s' => \$num_sd,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
35 'd:i' => \$dist_To_Soft,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
36 'q:i' => \$MapQ,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
37 'c:s' => \$chrom,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
38 'u:s' => \$unmated_pairs,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
39 'x:s' => \$minBQ,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
40 'p' => \$pair_only,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
41 'g' => \$disable_RP_only,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
42 'j:s' => \$levD_local_threshold,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
43 'k:s' => \$levD_distl_threshold,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
44 'a:s' => \$pe_upper_limit,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
45 'e:s' => \$high_qual,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
46 'L' => \$sv_only,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
47 'v' => \$verbose,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
48 'blacklist:s' => \$blacklist,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
49 'genome_file:s' => \$genome_file,
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
50 "help|h|?" => \&usage);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
51
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
52 unless($sv_only){$sv_only=""};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
53 if(defined($INPUT_BAM)){$INPUT_BAM=$INPUT_BAM} else {print usage();die "Where is the BAM file?\n\n"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
54 if(defined($INPUT_FASTA)){$INPUT_FASTA=$INPUT_FASTA} else {print usage();die "Where is the fasta file?\n\n"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
55 my ($fn,$pathname) = fileparse($INPUT_BAM,".bam");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
56 my $index=`ls $pathname/$fn*bai|head -1`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
57 #my $index =`ls \${INPUT_BAM%.bam}*bai`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
58 #print "INDEX=$index\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
59 if(!$index){die "\n\nERROR: you need index your BAM file\n\n"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
60
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
61 ### get current time
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
62 print "Start Time : " . &spGetCurDateTime() . "\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
63 my $now = time;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
64
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
65 #if(defined($OUTPUT_FILE)){$OUTPUT_FILE=$OUTPUT_FILE} else {$OUTPUT_FILE="output.vcf"; print "\nNo outfile specified. Using output.vcf as default\n\n"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
66 if(defined($minSoft)){$minSoft=$minSoft} else {$minSoft=5}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
67 if(defined($minRP)){$minRP=$minRP} else {$minRP=5}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
68 if(defined($minSoftReads)){$minSoftReads=$minSoftReads} else {$minSoftReads=5}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
69 if(defined($dist_To_Soft)){$dist_To_Soft=$dist_To_Soft} else {$dist_To_Soft=300}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
70 if(defined($num_sd)){$num_sd=$num_sd} else {$num_sd=6}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
71 if(defined($MapQ)){$MapQ=$MapQ} else {$MapQ=20}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
72
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
73 unless (defined $pe_upper_limit) { $pe_upper_limit = 10000; }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
74 unless (defined $levD_local_threshold) { $levD_local_threshold = 0.05; }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
75 unless (defined $levD_distl_threshold) { $levD_distl_threshold = 0.05; }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
76 #Get sample name if available
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
77 my $SAMPLE_NAME="";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
78 my $OUTNAME ="";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
79 $SAMPLE_NAME=`samtools view -f2 -H $INPUT_BAM|awk '{if(\$1~/^\@RG/){sub("ID:","",\$2);name=\$2;print name}}'|head -1`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
80 $SAMPLE_NAME=~s/\n//g;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
81 if (!$OUTPUT_FILE){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
82 if($SAMPLE_NAME ne ""){$OUTNAME=$SAMPLE_NAME.".vcf"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
83 else {$OUTNAME="output.vcf"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
84 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
85 else{$OUTNAME=$OUTPUT_FILE}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
86
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
87 print "Writing results to $OUTNAME\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
88
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
89
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
90 ##Make sure if submitting on SGE, to prepned the "chr". Not all referecne FAST files require "chr", so we shouldn't force the issue.
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
91 if(!defined($chrom)){$chrom=""}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
92 if(!defined($unmated_pairs)){$unmated_pairs=0}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
93
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
94 my $badQualValue=chr($MapQ);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
95 if(defined($minBQ)){ $badQualValue=chr($minBQ); }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
96
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
97 if($badQualValue eq "#"){$badQualValue="\#"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
98
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
99 # adding and cheking for samtools and bedtools in the PATh
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
100 ## check for bedtools and samtools in the path
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
101 $bedtools=`which intersectBed` ;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
102 if(!defined($bedtools)){die "\nError:\n\tno bedtools. Please install bedtools and add to the path\n";}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
103 #$samtools=`samtools 2>&1`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
104 $samtools=`which samtools`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
105 if($samtools !~ /(samtools)/i){die "\nError:\n\tno samtools. Please install samtools and add to the path\n";}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
106
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
107 print "Usage = SoftSearch.pl -l $minSoft -q $MapQ -r $minSoftReads -d $dist_To_Soft -m $minRP -s $num_sd -c $chrom -b $INPUT_BAM -f $INPUT_FASTA -o $OUTNAME \n\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
108 sub usage {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
109 print "\nusage: SoftSearch.pl [-cqlrmsd] -b <BAM> -f <Genome.fa> \n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
110 print "\t-q\t\tMinimum mapping quality [20]\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
111 print "\t-l\t\tMinimum length of soft-clipped segment [5]\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
112 print "\t-r\t\tMinimum depth of soft-clipped reads at position [5]\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
113 print "\t-m\t\tMinimum number of discordant read pairs [5]\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
114 print "\t-s\t\tNumber of sd away from mean to be considered discordant [6]\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
115 print "\t-u\t\tNumber of unmated pairs [0]\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
116 print "\t-d\t\tMax distance between soft-clipped segments and discordant read pairs [300]\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
117 print "\t-o\t\tOutput file name [output.vcf]\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
118 print "\t-t\t\tPrint temp files for debugging [no|yes]\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
119 print "\t-c\t\tuse only this chrom or chr:pos1-pos2\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
120 print "\t-p\t\tuse paired-end mode only. In other words, don't try to find soft-clipping events!\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
121 print "\t-g\t\tEnable paired-only seach. This will look for discordant read pairs even without soft clips.\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
122 print "\t-a\t\tset the minimum distance for a discordant read pair without soft-clipping info [10000]\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
123 print "\t-L\t\tFlag to print out even small deletions (low quality)\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
124 print "\t-e\t\tdisable strict quality filtering of base qualities in soft-clipped reads [no]\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
125 print "\t-blacklist\tareas of the genome to skip calling. Requires -genome_file\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
126 print "\t-genome_file\ttab seperated value of chromosome name and length. Only used with -blacklist option\n\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
127
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
128 exit 1;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
129 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
130
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
131
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
132 #############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
133 # create temporary variable name
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
134 #############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
135 srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip -f`);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
136 our $random_name=join "", map { ("a".."z")[rand 26] } 1..8;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
137
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
138 #############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
139 ## create green list
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
140 ##############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
141 #
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
142 my $new_blacklist="";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
143 if($blacklist){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
144 if(!$genome_file){die "if using a blacklist, you must also specify the location of a genome_file
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
145 The format of the genome_file should be
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
146 chrom size
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
147 chr1 249250621
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
148 chr2 243199373
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
149 ...
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
150
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
151 If using hg19, you can ge the genome file by
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
152 mysql --user=genome --host=genome-mysql.cse.ucsc.edu -A -e \"select chrom, size from hg19.chromInfo\" > hg19.genome";}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
153
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
154 $cmd=join("","complementBed -i $blacklist -g $genome_file >",$random_name,".bed") ;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
155 system ($cmd);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
156 $new_blacklist=join(""," -L ",$random_name,".bed ");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
157 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
158
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
159 if($verbose){print "CMD=$cmd\nBlacklist is $new_blacklist\n";}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
160
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
161
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
162
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
163
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
164
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
165 #############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
166 # Calcualte insert size distribution of properly mated reads
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
167 #############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
168
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
169 #Change for compatability with other operating systems
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
170 #my $metrics=`samtools view -q $MapQ -f2 $INPUT_BAM $chrom|cut -f9|head -10000|awk '{if (\$1<0){\$1=-\$1}else{\$1=\$1} sum+=\$1; sumsq+=\$1*\$1} END {print sum/NR, sqrt(sumsq/NR - (sum/NR)**2)}'`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
171
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
172 my $metrics=`samtools view -q $MapQ -f2 $INPUT_BAM $chrom|cut -f9|head -10000|awk '{if (\$1<0){\$1=-\$1}else{\$1=\$1} sum+=\$1; sumsq+=\$1*\$1} END {print sum/NR, sqrt(sumsq/NR - (sum/NR)^2)}'`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
173 #my ($mean,$stdev)=split(/ /,$metrics);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
174 my ($mean,$stdev)=split(/\s/,$metrics);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
175 $stdev=~s/\n//;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
176 my $upper_limit=int($mean+($num_sd*$stdev));
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
177 my $lower_limit=int($mean-($num_sd*$stdev));
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
178 die if (!$mean);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
179 print qq{The mean insert size is $mean +/- $stdev (sd)
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
180 The upper limit = $upper_limit
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
181 The lower limit = $lower_limit\n
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
182 };
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
183 if($lower_limit<0){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
184 print "Warning!! Given this insert size distribution, we can not call small indels. No other data will be affected\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
185 $lower_limit=1;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
186 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
187 my $tmp_name=join ("",$random_name,".tmp.bam");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
188 my $random_file_sc = "";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
189 my $command = "";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
190
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
191 #############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
192 # Make sam file that has soft clipped reads
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
193 #############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
194 #give file a name
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
195 if(!defined($pair_only)){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
196 $random_file_sc=join ("",$random_name,".sc.sam");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
197 $command=join ("","samtools view -q $MapQ -F 1024 $INPUT_BAM $chrom $new_blacklist| awk '{OFS=\"\\t\"}{c=0;if(\$6~/S/){++c};if(c == 1){print}}' | perl -ane '\$TR=(\@F[10]=~tr/\#//);if(\$TR<2){print}' > ", $random_file_sc);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
198
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
199 print "Making SAM file of soft-clipped reads\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
200 if($verbose){ print "$command\n";}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
201 system("$command");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
202
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
203 #############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
204 # Find areas that have deep enough soft-clip coverage
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
205 print "Identifying soft-clipped regions that are at least $minSoft bp long \n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
206 open (FILE,"$random_file_sc")||die "Can't open soft-clipped sam file $random_file_sc\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
207
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
208 my $tmpfile=join("",$random_file_sc,".sc.passfilter");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
209 open (OUT,">$tmpfile")||die "Can't write files here!\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
210
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
211 while(<FILE>){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
212 @_ = split(/\t/, $_);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
213 #### parse CIGAR string and create a hash of array of each operation
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
214 my @CIGAR = split(/([0-9]+[SMIDNHXP])/, $_[5]);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
215 my $hash;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
216 map { push(@{$hash->{$2}}, $1) if (/(\d+)([SMIDNHXP])/) } @CIGAR;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
217
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
218 #for ($i=0; $i<=$#softclip_pos; $i++) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
219 foreach my $softclip (@{$hash->{S}}) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
220 #if ($CIGAR[$softclip_pos[$i]] > $minSoft){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
221 if ($softclip > $minSoft){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
222 ###############Make sure base qualities don't have more than 2 bad marks
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
223 my $qual=$_[10];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
224 my $TR=($qual=~tr/$badQualValue//);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
225 if($badQualValue eq "#"){ $TR=($qual=~tr/\#//); }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
226 #Skip the soft clip if there is more than 2 bad qual values
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
227 #next if($TR > 2);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
228 # if (!$high_qual){next if($TR > 2);}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
229 print OUT;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
230 last;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
231 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
232 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
233 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
234 close FILE;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
235 close OUT;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
236
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
237 $command=join(" ","mv",$tmpfile,$random_file_sc);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
238 if($verbose){ print "$command\n";}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
239 system("$command");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
240 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
241
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
242 #########################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
243 #Stack up SoftClips
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
244 #########################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
245 my $random_file=join("",$random_name,".sc.direction.bed");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
246 if(!defined($pair_only)){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
247 open (FILE,"$random_file_sc")|| die "Can't open sam file\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
248 #$random_file=join("",$random_name,".sc.direction");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
249
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
250 print "Calling sides of soft-clips\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
251 #\nTMPOUT=$random_file\tINPUT=$random_file_sc\n\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
252 open (TMPOUT,">$random_file")|| die "Can't create tmp file\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
253
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
254 while (<FILE>){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
255 @_ = split(/\t/, $_);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
256 #### parse CIGAR string and create a hash of array of each operation
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
257 my @CIGAR = split(/([0-9]+[SMIDNHXP])/, $_[5]);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
258 my $hash;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
259 map { push(@{$hash->{$2}}, $1) if (/(\d+)([SMIDNHXP])/) } @CIGAR;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
260
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
261 #### next if softclips on each end
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
262 next if ($_[5] =~ /^[0-9]+S.*S$/);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
263
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
264 #### next softclip occurs in the middle
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
265 next if ($_[5] =~ /^[0-9]+[^S][0-9].*S.+$/);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
266
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
267 my $softclip = $hash->{S}[0];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
268
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
269 my $end1 = 0;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
270 my $end2 = 0;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
271 my $softBases = "";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
272 my $right_corrected="";my $left_corrected="";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
273
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
274 if ($softclip > $minSoft) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
275
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
276 ####If the soft clip occurs at end of read and its on the minus strand, then it's a right clip
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
277 if ($_[5] =~ /^.*S$/) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
278 $end1=$_[3]+length($_[9])-$softclip-1;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
279 $end2=$end1+1;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
280 next if ($end1<0);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
281 #RIGHT clip on Minus
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
282 $softBases=substr($_[9], length($_[9])-$softclip, length($_[9]));
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
283 #Right clips don't always get clipped correctly, so fix that
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
284 # Check to see if sc base matches ref
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
285 $right_corrected=baseCheck($_[2],$end2,"right",$softBases);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
286 print TMPOUT "$right_corrected\n"
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
287
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
288 } else {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
289 #### Begins with S (left clip)
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
290 $end1=$_[3]-$softclip;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
291 next if ($end1<0);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
292
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
293 $softBases=substr($_[9], 0,$softclip);#print "TMP=$softBases\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
294 $left_corrected=baseCheck($_[2],$end1,"left",$softBases);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
295 if(!$left_corrected){print "baseCheck($_[2],$end1,left,$softBases)\n";next}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
296 print TMPOUT "$left_corrected\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
297 #print "\nSEQ=$_[9]\t\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
298
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
299 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
300 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
301 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
302 close FILE;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
303 close TMPOUT;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
304 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
305 sub baseCheck{
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
306 my ($chrom,$pos,$direction,$softBases)=@_;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
307 #skip if position is less than 0, which is caused by MT DNA
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
308 return if ($pos<0);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
309 my $exit="";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
310
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
311 while(!$exit){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
312 if($direction=~/right/){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
313 my $refBase=getSeq($chrom,$pos,$INPUT_FASTA);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
314 my $softBase=substr($softBases,0,1);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
315 if ($softBase !~ /$refBase/){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
316 my $value=join("\t",$chrom,$pos,$pos+1,join("|",$softBases,$direction));
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
317 $exit="STOP";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
318 return $value;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
319 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
320 else{
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
321 $pos=$pos+1;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
322 $softBases=substr($softBases, 1,length($softBases));
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
323 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
324 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
325 else{
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
326 my $refBase=getSeq($chrom,$pos+1,$INPUT_FASTA);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
327 my $softBase=substr($softBases,-1,1);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
328 if ($softBase !~ /$refBase/){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
329 $pos=$pos-1+length($softBases);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
330 my $value=join("\t",$chrom,$pos-1,$pos,join("|",$softBases,$direction));
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
331 $exit="STOP";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
332 return $value;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
333 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
334 else{
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
335 $pos=$pos-1;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
336 $softBases=substr($softBases, 0, -1);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
337 #print "Trying again $softBases\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
338 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
339
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
340 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
341
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
342 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
343 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
344 #Remove SAM files to conserve space
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
345 unlink($random_file_sc);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
346
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
347
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
348 my $random_file_disc="$INPUT_BAM";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
349 ###
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
350 #
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
351 ######################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
352 # Transform Read pair groups into softclip equivalents
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
353 ######################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
354 #
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
355 #
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
356 #
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
357 my $v="";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
358 #if($disable_RP_only){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
359 print "Running Bam2pair.pl\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
360 print "Looking for discordant read pairs without requiring soft-clipping information\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
361 use FindBin qw($Bin);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
362 my $path=$Bin;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
363 # print"\n\nPATH=$path\n\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
364 if($verbose){$v="-v"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
365 my $tmp_out=join("",$random_name,"RP.out");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
366 $command=join("","perl ",$path,"/Bam2pair.pl -b $random_file_disc -o $tmp_out -isize $pe_upper_limit -winsize $dist_To_Soft -min $minRP -chrom $chrom -prefix $random_name -q $MapQ -blacklist $random_name.bed $v");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
367 if($verbose){ print "$command\n"};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
368 system("$command");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
369 $command=join("","perl -ane '\$end1=\@F[1];\$end2=\@F[3];print join(\"\\t\",\@F[0..1],\$end1,\"unknown|left\");print \"\\n\";print join(\"\\t\",\@F[2..3],\$end2,\"unknown|left\");print \"\\n\"' ", $tmp_out," >> ",$random_file);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
370 if($verbose){print "$command\n"};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
371 system($command);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
372 unlink($tmp_out);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
373 #}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
374 #
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
375
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
376
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
377 ######################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
378 unlink("$random_file","$tmp_name","$random_file","$index","$random_name","$new_blacklist") if (-z $random_file || ! -e $random_file ) ;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
379 if (-z $random_file || ! -e $random_file){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
380 print "Softclipped file is empty($random_file).\nNo soft clipping found using desired paramters\n\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
381 open (OUT,">$OUTNAME")||die "Can't write files here!\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
382 &print_header();
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
383 close OUT;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
384 exit;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
385 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
386
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
387
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
388 #############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
389 # Make sure there are enough soft-clippped supporting reads
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
390 #############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
391 my $outfile=join("",$random_file,".sc.merge.bed");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
392 #sortbed -i .sc.direction | mergeBed -nms -d 25 -i stdin > .sc.merge.bed
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
393 $command=join(" ","sortBed -i",$random_file," | mergeBed -nms -i stdin","|egrep \";|,\"","|awk '{OFS=\"\t\"}(NF==4)'",">",$outfile);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
394
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
395 print "$command\n" if ($verbose);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
396 system("$command");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
397
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
398 if (-z $outfile || ! -e $outfile){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
399 unlink("$tmp_name","$random_file","$outfile","$index","$random_name","$new_blacklist");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
400 print "mergeBed file is empty.\nNo strucutral variants found\n\n" ;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
401 open (OUT,">$OUTNAME")||die "Can't write files here!\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
402 &print_header();
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
403 close OUT;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
404 exit;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
405 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
406
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
407 print "completed mergeBed\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
408
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
409 ###############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
410 # If left and right are on the same line, make into 2 lines
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
411 ###############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
412 open (INFILE,$outfile)||die "couldn't open temp file : $. \n\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
413 my $tmp2=join("",$random_name,".sc.fixed.merge.bed");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
414 #print "INFILE=$outfile\tOUTFILE=$tmp2\n\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
415 #INPUT FORMAT=chr9\t131467\t131473\tATGCTTATTAAAA|left;TTATTAAAAGCATA|left
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
416 open (OUTFILE,">$tmp2")||die "couldn't create temp file : $. \n\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
417 while(<INFILE>){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
418 chomp $_;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
419 my $l = $_;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
420
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
421 my @a = split(/\t/, $l);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
422 my $info = $a[3];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
423 my @info_arr = split(/\;/, $info);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
424 my @left_arr=();
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
425 my @right_arr=();
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
426 @left_arr = grep(/left/, @info_arr);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
427 @right_arr = grep(/right/, @info_arr);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
428
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
429 #New
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
430 my $left = join(";", @left_arr);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
431 my $right = join(";", @right_arr);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
432 $info = join(";", @info_arr);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
433
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
434 if((@left_arr) && (@right_arr)){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
435 print OUTFILE "$a[0]\t$a[1]\t$a[2]\t$left\n$a[0]\t$a[1]\t$a[2]\t$right\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
436 } else{
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
437 my $all=join("\t",@a[0..2],$info);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
438 print OUTFILE "$all\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
439 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
440 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
441
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
442 # make sure output file name is $outfile
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
443 $command=join(" ","sed -e '/ /s//\t/g'", $tmp2,"|awk 'BEGIN{OFS=\"\\t\"}(NF==4)'", "|perl -pne 's/ /\t/g'>",$outfile);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
444 system("$command");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
445 if($verbose){print "$command\n"};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
446 unlink("$tmp_name","$random_file","$tmp2","$outfile","$index","random_name","$new_blacklist") if (-z $outfile || ! -e $outfile) ;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
447 if (-z $outfile || ! -e $outfile){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
448 print "Fixed mergeBed file is empty($outfile).\nNo strucutral variants found\n\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
449 open (OUT,">$OUTNAME")||die "Can't write files here!\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
450 &print_header();
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
451 close OUT;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
452 exit;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
453 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
454
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
455 print "completed fixing mergeBed\n\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
456
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
457 ###############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
458 # Seperate directions of soft clips
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
459 ###############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
460 my $left_sc = join("", "left", $tmp2);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
461 my $right_sc = join("", "right", $tmp2);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
462 use FindBin qw($Bin);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
463 #my $path=$Bin;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
464
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
465 $command=join("","grep left ", $tmp2, " |sed -e '/left /s//left\;/g' | sed -e '/ /s//\t/g'|perl ".$path."/direction_filter.pl - >",$left_sc);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
466 system("$command");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
467 #print "$command\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
468 $command=join("","grep right ", $tmp2, " |sed -e '/right /s//right\;/g' | sed -e '/ /s//\t/g'|perl ".$path."/direction_filter.pl - >",$right_sc);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
469 #$command=join(" ","grep right ", $tmp2, " |sed -e '/right /s//right\;/g' | sed -e '/ /s//\t/g' >",$right_sc);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
470 system("$command");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
471 #print "$command\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
472 #die "CHECK $right_sc\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
473
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
474 ###############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
475 # Count the number and identify directions of soft clips
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
476 ###############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
477 print "Count the number and identify directions of soft clips\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
478 #print "looking in $outfile\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
479 $outfile=join("",$random_name,".sc.fixed.merge.bed");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
480
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
481 open (INFILE,$outfile)||die "couldn't open temp file\n\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
482 my $tmp3 = join("", $random_file, "predSV");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
483 open (OUTFILE, ">$tmp3")||die "couldn't create temp file\n\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
484 while(<INFILE>){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
485 chomp;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
486 @_=split(/\t/,$_);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
487 my $count=tr/\;//;$count+=tr/\,//;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
488 $count=$count+1;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
489 my $left=0;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
490 my $right=0;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
491
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
492 while ($_ =~ /left/g) { $left++ } # count number of right clips
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
493 while ($_ =~ /right/g) { $right++ } # count number of left clips
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
494
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
495 ###############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
496 if ($count >= $minSoftReads){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
497 ####get longets soft-clipped read
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
498 my @clips=split(/\;|,|\|/,$_[3]);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
499
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
500 my ($max, $temp, $temp2, $temp3, $dir, $maxSclip) = (0) x 6;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
501 for (my $i=0; $i<$count; $i++) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
502 my $plus1=$i+1;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
503 $temp=length($clips[$i]);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
504 $temp2=$clips[$plus1];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
505 $temp3=$clips[$i];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
506
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
507 if ($temp > $max){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
508 $maxSclip=$temp3;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
509 $max =$temp;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
510 $dir=$temp2;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
511 } else {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
512 $max=$max;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
513 $dir=$dir;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
514 $maxSclip=$maxSclip;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
515 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
516 $i++;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
517 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
518 my $order2 = join("|", $left, $right);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
519 #print join ("\t",@_[0..2],$maxSclip,$max,$dir,$count,$order2) . "\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
520 print OUTFILE join ("\t",@_[0..2],$maxSclip,$max,$dir,$count,$order2) . "\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
521 } elsif($_=~/unknown/){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
522 print OUTFILE join ("\t",@_[0..2],"NA","NA","left","NA","NA|NA") . "\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
523 print OUTFILE join ("\t",@_[0..2],"NA","NA","right","NA","NA|NA") . "\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
524 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
525 ####Format is Chrom,start, end,longest Soft-clip,length of longest Soft-clip, direction of longest soft-clip,#supporting softclips,#right Sclips|#left Sclips
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
526 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
527 close INFILE;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
528 close OUTFILE;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
529
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
530 unlink("$tmp2","$tmp_name","$random_file","$tmp3","$outfile","$index","$random_name","$right_sc","$left_sc","$new_blacklist") if (-z $tmp3 || !-e $tmp3) ;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
531
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
532 if (-z $tmp3 || !-e $tmp3){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
533 print "No structural variants found while Counting the number and identify directions of soft clips.\n" ;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
534
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
535 open (OUT,">$OUTNAME")||die "Can't write files here!\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
536 &print_header();
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
537 close OUT;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
538 exit;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
539
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
540 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
541
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
542 print "Done counting Softclipped reads\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
543 ###############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
544 #### Print header information
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
545 ###############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
546 open (OUT,">$OUTNAME")||die "Can't write files here!\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
547 &print_header();
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
548 close OUT;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
549
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
550 ###############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
551 ###############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
552 #### DO the bulk of the work
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
553 ###############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
554 use List::Util qw(min max);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
555 open (FILE,"$tmp3")|| die "Can't open file\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
556 open (OUT,">>$OUTNAME")|| die "Can't open file\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
557
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
558 #print "\nusing $tmp3 and writing to $OUTPUT_FILE \n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
559 while (<FILE>){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
560 #If left clip {+- or -- or -+ }{+- are uninformative b/c they go upstream}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
561 #If right clip {++ or -- or +-}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
562 chomp $_;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
563 my $line = $_;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
564 my @info = split(/\t/, $_);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
565
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
566 if($info[5] eq "left") {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
567 bulk_work("left", $line, $random_file_disc);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
568
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
569 } elsif ($info[5] eq "right") {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
570 bulk_work("right", $line, $random_file_disc);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
571 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
572 #if($. ==6){print "THIS IS LINE 6\n$_\n";die}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
573 print "Completed line $.\n" if ($verbose);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
574 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
575 close FILE;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
576 close OUT;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
577
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
578 ###############################################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
579 ###############################################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
580 #### Delete temp files
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
581 my $meregedBed=join("",$random_name,".sc.direction.bed.sc.merge.bed");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
582
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
583 if(defined($temp_output)){$temp_output=$temp_output} else {$temp_output="no"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
584
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
585 if ($temp_output eq "no"){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
586 unlink("$tmp_name","$random_file","$tmp2",,"$tmp3","$outfile","$index","$random_name","$right_sc","$left_sc","$meregedBed","$random_name.bed");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
587 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
588 ####Sort VCF
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
589 my $tmp=join(".",$random_name,"tmp");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
590 #Get header
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
591 $cmd="grep \"#\" $OUTNAME > $tmp";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
592 system($cmd);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
593 #sort results
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
594 $cmd="grep -v \"#\" $OUTNAME|perl -pne 's/chr//'|sort -k1,1n -k2,2n|perl -ne 'print \"chr\".\$_' >>$tmp";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
595 system($cmd);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
596 $cmd="mv $tmp $OUTNAME";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
597 system($cmd);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
598 #remove entries next to each other
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
599
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
600
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
601
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
602
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
603 #############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
604 ##May not need this anymore since filtering on left and right
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
605 #############################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
606 #my $tmpout=$OUTNAME;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
607 #$tmpout.=".tmp";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
608 #use FindBin qw($Bin);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
609 ##my $path=$Bin;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
610 #$command="perl ".$path."/Extract_nSC.pl $OUTNAME -q nSC > $tmpout";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
611 ##print "Command=$command\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
612 #system($command);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
613 #$command="perl ".$path."/reduce_redundancy.pl $tmpout $upper_limit |cut -f1-10 > $OUTNAME";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
614 ##print "$command\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
615 #system($command);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
616 #system("rm $tmpout");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
617 ########################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
618
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
619
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
620
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
621
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
622 print "Analysis Completed\n\nYou did it!!!\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
623 print "Finish Time : " . &spGetCurDateTime() . "\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
624 $now = time - $now;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
625 printf("\n\nTotal running time: %02d:%02d:%02d\n\n", int($now / 3600), int(($now % 3600) / 60),
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
626 int($now % 60));
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
627
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
628 exit;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
629
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
630 ###############################################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
631 sub rev_comp {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
632 my $dna = shift;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
633 my $revcomp = reverse($dna);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
634 $revcomp =~ tr/ACGTacgt/TGCAtgca/;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
635
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
636 return $revcomp;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
637 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
638
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
639
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
640 ###############################################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
641 #### to get reference base
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
642 sub getSeq{
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
643 my ($chr,$pos,$fasta)=@_;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
644 #don't require chr
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
645 #if($chr !~ /^chr/){die "$chr is not correct\n";}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
646 # die "$pos is not a number\n" if ($pos <0);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
647 my @result=();
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
648 if ($pos <0){print "$pos is not a valid position (likely caused by circular MT chromosome)\n";return;}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
649
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
650 @result = `samtools faidx $fasta $chr:$pos-$pos`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
651 if($result[1]){chomp($result[1]);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
652 return uc($result[1]);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
653 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
654 return("NA");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
655 #### after return will not be printed
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
656 ####print "RESULTS=@result\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
657 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
658
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
659 sub getBases{
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
660 my ($chr,$pos1,$pos2,$fasta)=@_;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
661 #don't require chr
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
662 #if($chr !~ /^chr/){die "$chr is not correct\n";}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
663 my @result=();
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
664 if ($pos1 <0){print "$pos1 is not a valid position (likely caused by circular MT chromosome)\n";return;};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
665
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
666 @result = `samtools faidx $fasta $chr:$pos1-$pos2`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
667 if(!$result[1]){$result[1]="NA"};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
668 chomp($result[1]);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
669 return uc($result[1]);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
670
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
671 #### after return will not be printed
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
672 ####print "RESULTS=@result\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
673 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
674 ###############################################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
675 #### to get time
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
676 sub spGetCurDateTime {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
677 my ($sec, $min, $hour, $mday, $mon, $year) = localtime();
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
678 my $curDateTime = sprintf "%4d-%02d-%02d %02d:%02d:%02d",
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
679 $year+1900, $mon+1, $mday, $hour, $min, $sec;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
680 return ($curDateTime);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
681 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
682
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
683
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
684 ###############################################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
685 #### print header
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
686 sub print_header {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
687 my $date=&spGetCurDateTime();
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
688 my $header = qq{##fileformat=VCFv4.1
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
689 ##fileDate=$date
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
690 ##source=SoftSearch.pl
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
691 ##reference=$INPUT_FASTA
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
692 ##Usage= SoftSearch.pl -l $minSoft -q $MapQ -r $minSoftReads -d $dist_To_Soft -m $minRP -u $unmated_pairs -s $num_sd -b $INPUT_BAM -f $INPUT_FASTA -o $OUTNAME
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
693 ##INFO=<ID=SVTYPE,Number=1,Type=String,Description="Type of structural variant">
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
694 ##INFO=<ID=EVENT,Number=1,Type=String,Description="ID of event associated to breakend">
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
695 ##INFO=<ID=MATEID,Number=.,Type=String,Description="ID of mate breakends">
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
696 ##INFO=<ID=ISIZE,Number=.,Type=String,Description="Size of the SV">
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
697 ##INFO=<ID=END,Number=1,Type=Integer,Description="End position of the variant described in this record">
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
698 ##FORMAT=<ID=lSC,Number=1,Type=Integer,Description="Length of the longest soft clips supporting the BND">
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
699 ##FORMAT=<ID=nSC,Number=1,Type=Integer,Description="Number of supporting soft-clips\">
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
700 ##FORMAT=<ID=uRP,Number=1,Type=Integer,Description="Number of unmated read pairs nearby Soft-Clips">
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
701 ##FORMAT=<ID=levD_local,Number=1,Type=Float,Description="Levenstein distance between soft-clipped bases and the area around the original soft-clipped site">
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
702 ##FORMAT=<ID=levD_distl,Number=1,Type=Float,Description="Levenstein distance between the soft-clipped bases and mate location">
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
703 ##FORMAT=<ID=CTX,Number=1,Type=Integer,Description="Number of chromosomal translocations">
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
704 ##FORMAT=<ID=DEL,Number=1,Type=Integer,Description="Number of reads supporting Large Deletions">
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
705 ##FORMAT=<ID=INS,Number=1,Type=Integer,Description="Number of reads supporting Large insertions">
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
706 ##FORMAT=<ID=NOV_INS,Number=1,Type=Integer,Description="Number of reads supporting novel sequence insertion">
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
707 ##FORMAT=<ID=TDUP,Number=1,Type=Integer,Description="Number of reads supporting a tandem duplication">
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
708 ##FORMAT=<ID=INV,Number=1,Type=Integer,Description="Number of reads supporting inversions">
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
709 ##FORMAT=<ID=sDEL,Number=1,Type=Integer,Description="Number of reads supporting novel sequence insertion">
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
710 ##INFO=<ID=NO_MATE_SC,Number=1,Type=Flag,Description="When there is no softclipping of the mate read location, an appromiate position is used">
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
711 ##FORMAT=<ID=GT,Number=1,Type=String,Description="Dummy value for maintaining VCF-Spec">
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
712 #CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\t$SAMPLE_NAME\n};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
713
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
714 print OUT $header;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
715 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
716
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
717
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
718 ###############################################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
719 sub bulk_work {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
720 print "#####################################@_\n" if ($verbose);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
721 my ($side, $line, $file) = @_;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
722 my $local_levD = 0;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
723 my $distl_levD = 0;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
724
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
725 #my @info = split(/\t/, $line);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
726 my @plus_Reads = split(/\t/, $line);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
727 $plus_Reads[7] =~ s/\n//g;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
728
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
729 #### softclip length and softclip size.
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
730 my $lSC = $plus_Reads[4];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
731 my $nSC = $plus_Reads[6];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
732
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
733
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
734 #Get all types of compatible reads
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
735 #Get improperly paired reads (@ max distance)
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
736
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
737 #### default value for left SIDE.
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
738 #If left-clip, then look downstream for match of softclipped reads to define a deletion, but look for DRPs upstream
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
739 my $sv_type = "SVTYPE=BND";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
740 my $start_local=0; my $end_local=0;my $target_local="";my $target_drp="";my $start_drp="";my $end_drp="";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
741 if ($side =~ /left/) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
742 $start_local = $plus_Reads[1]-$dist_To_Soft;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
743 $end_local = $plus_Reads[2];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
744 $start_drp = $plus_Reads[1];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
745 $end_drp = $plus_Reads[1]+$dist_To_Soft;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
746
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
747 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
748 else{
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
749 $start_local = $plus_Reads[1];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
750 $end_local = $plus_Reads[1]+$dist_To_Soft;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
751 $start_drp = $plus_Reads[1]-$dist_To_Soft;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
752 $end_drp = $plus_Reads[1];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
753 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
754
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
755 $target_local=join("", $plus_Reads[0], ":", $start_local, "-", $end_local);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
756 $target_drp=join("", $plus_Reads[0], ":", $start_drp, "-", $end_drp);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
757 my $num_unmapped_pairs="";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
758 if ($side =~ /right/) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
759 $num_unmapped_pairs=`samtools view $new_blacklist -q $MapQ -f8 -F 1536 -c $INPUT_BAM $target_drp`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
760 } else {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
761 $num_unmapped_pairs=`samtools view $new_blacklist -q $MapQ -f24 -F 1536 -c $INPUT_BAM $target_drp`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
762 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
763 if($verbose){print "samtools view $new_blacklist -q $MapQ -f24 -F 1536 -c $INPUT_BAM $target_drp\n";}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
764
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
765 $num_unmapped_pairs=~s/\n//;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
766 if($verbose){print "NUM UNMAPPED PAIRS= $num_unmapped_pairs\n";}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
767 my $REF1_base = "";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
768 my $REF2_base = "";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
769 my $INFO_1 = "";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
770 my $INFO_2 = "";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
771 my $ALT_1 = "";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
772 my $ALT_2 = "";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
773 my $isize = 0;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
774 my $QUAL = "";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
775 my $FORMAT = "GT:";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
776
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
777 #### get 8 bit rand id
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
778 my $BND1_name = join "", map { ("a".."z")[rand 26] } 1..8;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
779 my $BND2_name = join "", map { ("a".."z")[rand 26] } 1..8;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
780 $BND1_name=join "_","BND",$BND1_name;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
781 $BND2_name=join "_","BND",$BND2_name;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
782
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
783 my $counts = {CTX => 0, DEL => 0, INS => 0, INV => 0, TDUP => 0, NOV_INS => 0 };
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
784 my $event_mate_info = {CTX => "", DEL => "", INS => "", INV => "", TDUP => "", NOV_INS => "" };
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
785
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
786 #### get mate pair info and counts per event
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
787 foreach my $e (sort keys %{$counts}) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
788 my $h = get_counts_n_info($e, $side, $MapQ, $file, $dist_To_Soft, $target_drp, $upper_limit, $lower_limit);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
789
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
790 $counts->{$e} = $h->{count};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
791 $event_mate_info->{$e} = $h->{info};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
792 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
793 #print Dumper($counts);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
794
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
795 my $max = 0;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
796 my $type = "UNKNOWN";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
797 my $nRP = 0;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
798 my $mate_info = "NA\tNA\tNA\tNA";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
799 my $summary = "GT:";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
800
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
801 #### find max count of events and set type, nRP and info to corresponding
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
802 #### max count event.
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
803 #### also create a summary string of all counts to be added to VCF file.
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
804 foreach my $e (sort keys %{$counts}){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
805 # if ($counts->{$e} >=i $max){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
806 if ($counts->{$e} > $max){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
807 $type = $e .",". $counts->{$e};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
808 $nRP = $counts->{$e};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
809
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
810 $max = $counts->{$e};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
811
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
812 if (length($event_mate_info->{$e})) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
813 $mate_info = $event_mate_info->{$e};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
814 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
815 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
816
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
817 $summary .= $e .",". $counts->{$e} .":";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
818 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
819 # print "done with Summary\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
820 #### remove last colon ":" from
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
821 $summary =~ s/:$//;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
822 if (($minRP > $max)&&(!$disable_RP_only )){if ($verbose){print "FAILED BECAUSE ($minRP > $max)&&(!$disable_RP_only )"};return};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
823
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
824 #### Run Levenstein distance on softclip in target region to find out if its a small deletion/insetion
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
825 #### passing 1: clip_seq, 2: chr, 3: start, 4: end, 5: ref file.
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
826 my $levD = new LevD;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
827 ########################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
828 ########################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
829 ########################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
830
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
831 #### redefine start and end location for LevD calc.
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
832 # $start = $plus_Reads[1]-$dist_To_Soft;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
833 # $end = $plus_Reads[2];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
834 my $num_bases_to_loc=0;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
835 my $new_start=0;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
836 my $new_end=0;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
837 my $del_seq="";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
838 my $start = $start_local;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
839 my $end = $end_local;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
840 if ($lSC=~/NA/){$lSC=0}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
841
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
842 if ($side =~ /right/) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
843 $levD->search($plus_Reads[3], $plus_Reads[0], $start, $end, $INPUT_FASTA);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
844 $local_levD = sprintf("%.2f", $levD->{relative_edit_dist});
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
845 $num_bases_to_loc=$levD->{index};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
846 $new_start = $plus_Reads[2];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
847 if ($plus_Reads[2]=~/^[0-9]/){$new_end=$plus_Reads[2]+$lSC};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
848 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
849 else{
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
850 $levD->search($plus_Reads[3], $plus_Reads[0], $start, $end, $INPUT_FASTA);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
851 $local_levD = sprintf("%.2f", $levD->{relative_edit_dist});
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
852 $num_bases_to_loc=$levD->{index};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
853 if ($plus_Reads[2]=~/^[0-9]/){$new_start=$plus_Reads[2]-$lSC};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
854 $new_end = $plus_Reads[2];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
855 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
856 if((!$new_start)||(!$new_end)||($new_start<0)){print "FAILED AT ((!$new_start)||(!$new_end)||($new_start<0))\n";return};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
857
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
858 $del_seq=getBases($plus_Reads[0], $new_start,$new_end,$INPUT_FASTA);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
859 ##############################################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
860 # #If there is a match, where is the start position of the match?
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
861 #
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
862 ##############################################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
863
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
864
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
865 #if $plus_Reads[3] eq "NA", then it was found without soft-clipped reads
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
866 if($plus_Reads[3] !~ /NA/){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
867 if (($local_levD < $levD_local_threshold)) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
868 return if (!$sv_only);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
869 #### add value to summary to be written to vcf file.
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
870 $summary = "GT:sDel," . $plus_Reads[6];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
871 $type = "sDEL";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
872 ###########################################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
873 ##### Printing output
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
874
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
875 #########################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
876 ##### Get DNA info
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
877 #########################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
878 #$REF1_base = getSeq($plus_Reads[0], $plus_Reads[1], $INPUT_FASTA);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
879 $REF1_base = substr($del_seq, 0, 1);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
880
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
881 #### this is alt ref. for softclip its $plus_Reads[3]
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
882 $REF2_base = $del_seq;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
883 $QUAL = 1/($local_levD + 0.001);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
884 $QUAL = sprintf("%.2f",$QUAL);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
885 $isize = length($del_seq);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
886
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
887 #### svtype = none for sDEL
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
888 #### isize = length($info[3]);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
889 #### nRP = NA
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
890 #### mate_id = NA
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
891 #### CTX,:DEL,:....sDEL,##
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
892 $INFO_1=join(";", "SVTYPE=NA", "EVENT=$type", "ISIZE=$isize");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
893
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
894 #Add Sample infomration
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
895 my $FORMAT="GT:sDEL";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
896 $FORMAT .= ":lSC:nSC:uRP:levD_local";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
897 my $SAMPLE= "0/1:";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
898 $SAMPLE .= "$plus_Reads[6]:$lSC:$nSC:$num_unmapped_pairs:$local_levD";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
899
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
900 #### remove any white spaces.
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
901 $INFO_1=~s/\s//g;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
902 $INFO_2=~s/\s//g;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
903
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
904 $BND1_name =~ s/^BND/LEVD/;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
905 # If left, then the start position is plus_Reads[1]-isize
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
906 my $start_pos=0;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
907 #Make sure Ref1 and Ref2 bases are different
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
908 if($REF2_base eq $REF1_base){$REF1_base="NA"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
909 if($side=~/left/){$start_pos=$plus_Reads[1]-$isize}else{$start_pos=$plus_Reads[1]};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
910 print OUT join ("\t", $plus_Reads[0], $start_pos, $BND1_name, $REF2_base, $REF1_base, $QUAL, "PASS", $INFO_1,$FORMAT,$SAMPLE, "\n");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
911 if ($verbose){print "No Softclipped reads here!\n"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
912 return;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
913 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
914 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
915
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
916 #### Otherwise, look for DRP mate info
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
917 #if($nRP=~/NA/){print "MATE_INFO=$mate_info\tSide=$side\tline=$line\n";}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
918 my @mate_info_arr = split(/\t/, $mate_info);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
919 $nRP = $mate_info_arr[3];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
920 my $mate_chr=$mate_info_arr[0];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
921
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
922 if((! defined $nRP) || ($nRP =~ /na/i) || ($mate_chr =~ /NA/) ){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
923 #PRINT UNKNOWN
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
924 if ($nRP =~ /na/i){print "Can't find SC reads\n" if ($verbose);return};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
925 if ($verbose){print "There is an unknown\nNRP=$nRP Mate_CHR=$mate_chr minRP=$minRP\n"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
926 $summary .= ":unknown," . $plus_Reads[6];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
927 $type = "unknown";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
928 $REF1_base = getSeq($plus_Reads[0], $plus_Reads[1], $INPUT_FASTA);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
929 $REF2_base = $plus_Reads[3];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
930 $BND1_name =~ s/^BND/UNKNOWN/;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
931 $QUAL = 1/($local_levD + 0.001);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
932 $QUAL = sprintf("%.2f",$QUAL);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
933 $INFO_1=join(";", "SVTYPE=unknown", "EVENT=unknown", "ISIZE=unknown");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
934 #Add Sample infomration
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
935 my $FORMAT="GT:sDEL";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
936 $FORMAT .= ":lSC:nSC:uRP:levD_local";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
937 my $SAMPLE = "0/1:";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
938 $SAMPLE .= "$plus_Reads[6]:$lSC:$nSC:$num_unmapped_pairs:$local_levD";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
939 $SAMPLE=~s/NA/0/g;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
940 #### remove any white spaces.
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
941 $INFO_1=~s/\s//g;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
942 #print join ("\t", $plus_Reads[0], $plus_Reads[1], $REF2_base, $REF1_base, $QUAL, "PASS", $INFO_1,$FORMAT,$SAMPLE, "\n");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
943
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
944 print OUT join ("\t", $plus_Reads[0], $plus_Reads[1], $BND1_name, $REF1_base, $REF2_base, $QUAL, "PASS", $INFO_1,$FORMAT,$SAMPLE, "\n");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
945 return;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
946
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
947 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
948 #### end if there is no mate info or nRP+uRP<minRP
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
949 if (($nRP<$minRP)&&($unmated_pairs > ($num_unmapped_pairs+$nRP))){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
950 print "Something failed here\nif (($nRP<$minRP)&&($unmated_pairs > ($num_unmapped_pairs+$nRP)))\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
951 return}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
952
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
953 ##################################################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
954 # Find out if mates have nearby soft-clips (to refine the breakpoints)
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
955 ##################################################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
956 #Look for evidence of soft-clipping near mate
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
957 my @mate_soft_arr = ();
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
958 my $mate_start = 0;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
959 my $mate_soft = "";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
960
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
961 @mate_info_arr = split(/\t/, $mate_info);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
962
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
963 #### mate start and end locations.
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
964 my $filename = $right_sc;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
965
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
966 $start = $mate_info_arr[1] - $dist_To_Soft;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
967 $end = $mate_info_arr[1];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
968
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
969 if ($side =~ /right/) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
970 $start = $mate_info_arr[2];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
971 $end = $mate_info_arr[2] + $dist_To_Soft;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
972
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
973 $filename = $left_sc;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
974 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
975
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
976 #### add levenstein distance to Summary
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
977 #print "Calc distal Levd\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
978 $levD->search(rev_comp($plus_Reads[3]), $mate_info_arr[0], $start, $end, $INPUT_FASTA);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
979 $distl_levD = sprintf("%.2f", $levD->{relative_edit_dist});
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
980 $distl_levD = "NA" if($plus_Reads[3] =~ /NA/);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
981 #If there is no softclips to string match, then give 0 as quality value
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
982 if ($plus_Reads[3] !~ /NA/){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
983 $QUAL=1/($distl_levD + 0.001);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
984 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
985 else {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
986 $QUAL=0;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
987 };
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
988 $QUAL=sprintf("%.2f",$QUAL);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
989 #### looking for softclips to refine break point
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
990 #### if left look in right and vice-versa.
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
991 $cmd = qq{echo -e "$mate_info_arr[0]\t$start\t$end"};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
992 $cmd .= qq{ | awk -F'\t' 'NF==3' | intersectBed -a stdin -b $filename | head -1};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
993 print "$cmd\n" if $verbose;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
994 $mate_soft = `$cmd`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
995
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
996 $mate_soft =~ s/\n//g;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
997 @mate_soft_arr = split(/\s/, $mate_soft);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
998 my $NO_MATE_SC="";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
999 if(@mate_soft_arr){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1000 $mate_chr = $mate_soft_arr[0];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1001 $mate_start = $mate_soft_arr[1];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1002 $NO_MATE_SC="APPROXIMATE";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1003
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1004 } else{
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1005 @mate_info_arr = split(/\s/,$mate_info);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1006 $mate_chr = $mate_info_arr[0];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1007 $mate_start = $mate_info_arr[1];
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1008 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1009
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1010 #end if there is no mate info
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1011 return if ($mate_chr eq "");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1012 #end if there is no mate info and !disable_RP_only
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1013 return if (($lSC =~/NA/)&&(!$disable_RP_only));
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1014
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1015
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1016 ###########################################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1017 ##### Printing output
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1018
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1019 #########################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1020 # Get DNA info
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1021 #########################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1022 #print "PLUS_READS=$plus_Reads[0],$plus_Reads[1]\nMATE=$mate_chr,$mate_start,$INPUT_FASTA\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1023 $REF1_base = getSeq($plus_Reads[0], $plus_Reads[1], $INPUT_FASTA);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1024
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1025 ### this is alt ref. for softclip its $plus_Reads[3]
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1026 $REF2_base = getSeq($mate_chr, $mate_start, $INPUT_FASTA);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1027
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1028 #########################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1029 # print in VCF format
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1030 #########################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1031
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1032 #### abs value to account for left and right reads.
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1033 $isize = abs($plus_Reads[1]-$mate_start);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1034
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1035 my $event_type=$type;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1036 $event_type=~ s/,|[0-9]//g;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1037 $INFO_1=join(";", "$sv_type", "EVENT=$event_type","END=$mate_start", "ISIZE=$isize","MATEID=$BND2_name");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1038 $INFO_2=join(";", "$sv_type", "EVENT=$event_type","END=$plus_Reads[1]", "ISIZE=$isize","MATEID=$BND1_name");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1039
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1040 #### remove any white spaces.
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1041 #### ask: did you mean to remove space from ends? eg. trim()
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1042 $INFO_1=~s/\s//g;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1043 $INFO_2=~s/\s//g;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1044
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1045 $FORMAT=$summary;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1046 $FORMAT=~ s/,|[0-9]//g;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1047 $FORMAT .= ":lSC:nSC:uRP:distl_levD";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1048 if($NO_MATE_SC){$INFO_2 .= ":NO_MATE_SC"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1049 my $SAMPLE="0/1:";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1050 $SAMPLE .=$summary;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1051 # if($NO_MATE_SC){$SAMPLE.= ":$NO_MATE_SC"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1052
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1053 $SAMPLE=~s/[A-Z|,|_]//g;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1054 my $MATE_SAMPLE=$SAMPLE;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1055 $SAMPLE .= ":$lSC:$nSC:$num_unmapped_pairs:$distl_levD";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1056 $MATE_SAMPLE .=":NA:NA:NA:NA";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1057 $SAMPLE=~s/::/:/g;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1058 $MATE_SAMPLE=~s/::/:/g;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1059 $MATE_SAMPLE=~s/NA/0/g;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1060 $SAMPLE=~s/NA/0/g;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1061
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1062 if($type !~ /INV/){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1063 $ALT_1 = join("","]",$mate_chr,":",$mate_start,"]",$REF1_base);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1064 $ALT_2 = join("",$REF2_base,"[",$plus_Reads[0],":",$plus_Reads[1],"[");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1065 # 2 321682 bnd_V T ]13:123456]T 6 PASS SVTYPE=BND
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1066 # 13 123456 bnd_U C C[2:321682[ 6 PASS SVTYPE=BND
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1067 } else {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1068 $ALT_1 = join("", "]", $plus_Reads[0], ":", $plus_Reads[1], "]", $REF2_base);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1069 $ALT_2 = join("", $REF1_base, "[", $mate_chr, ":", $mate_start, "[");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1070 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1071
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1072 if(($mate_chr) && ($plus_Reads[0])){
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1073 print OUT join ("\t", $plus_Reads[0], $plus_Reads[1], $BND1_name, $REF1_base, $ALT_1, $QUAL,"PASS", $INFO_1, $FORMAT,$SAMPLE,"\n");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1074 print OUT join ("\t", $mate_chr, $mate_start, $BND2_name, $REF2_base, $ALT_2, $QUAL, "PASS", $INFO_2, $FORMAT,$MATE_SAMPLE,"\n");
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1075 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1076 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1077
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1078 ###############################################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1079 ###############################################################################
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1080 sub get_counts_n_info {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1081 my ($event, $side, $mapQ, $file, $dist, $target, $upL, $lwL) = @_;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1082
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1083 my $mate_info = "";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1084 my $cmd = "";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1085
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1086 if ($event =~ /^CTX$/i) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1087 #print "CTX side $side\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1088 if ($side =~ /right/i) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1089 $cmd = qq{samtools view $new_blacklist -q $mapQ -f 32 -F 1536 $file $target};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1090 $cmd .= qq{ | perl -ane 'if(\$F[6] ne "="){\$end=\$F[7]+1; print join ("\\t",\$F[6],\$F[7],\$end,"\\n")}'};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1091 $cmd .= qq{ | sortBed | mergeBed -d $dist -n | sort -k4nr | head -1};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1092 #if($verbose){print "$cmd\n"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1093 $mate_info=`$cmd`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1094 } else {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1095 $cmd = qq{ samtools view $new_blacklist -q $mapQ -f 16 -F 1536 $file $target};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1096 $cmd .= qq{ | perl -ane 'if(\$F[6] ne "="){\$end=\$F[7]+1; print join ("\\t",\$F[6],\$F[7],\$end,"\\n")}'};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1097 $cmd .= qq{ | sortBed | mergeBed -d $dist -n | sort -k4nr | head -1};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1098 #if($verbose){print "$cmd\n"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1099 $mate_info=`$cmd`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1100 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1101 } elsif ($event =~ /^DEL$/i) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1102 #print "DEL side $side\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1103 if ($side =~ /right/i) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1104 $cmd = qq{samtools view $new_blacklist -q $mapQ -f 32 -F 1552 $file $target};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1105 $cmd .= qq{ | awk '{OFS="\\t"}{if((\$7 ~ /=/)&&(\$9>$upL)){end=\$8+1;print \$3,\$8,end}}'};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1106 $cmd .= qq{ | sortBed | mergeBed -d $dist -n | sort -k4nr | head -1};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1107 #if($verbose){print "$cmd\n"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1108 $mate_info=`$cmd`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1109 } else {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1110 $cmd = qq{samtools view $new_blacklist -q $mapQ -F 1568 -f 16 $file $target};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1111 $cmd .= qq{ | awk '{OFS="\\t"} {if((\$7 ~ /=/)&&(\$9<-$upL)){end=\$8+1;print \$3,\$8,end}}'};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1112 $cmd .= qq{ | sortBed | mergeBed -d $dist_To_Soft -n | sort -k4nr | head -1};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1113 #if($verbose){print "$cmd\n"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1114
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1115 $mate_info=`$cmd`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1116 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1117 } elsif ($event =~ /^INS$/i) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1118 #print "INS side $side\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1119 if ($side =~ /right/i) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1120 $cmd = qq{samtools view $new_blacklist -q $mapQ -f 32 -F 1552 $file $target};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1121 $cmd .= qq{ | awk '{OFS="\\t"}{if((\$7 ~ /=/)&&(\$9<$lwL && \$9 > 0 )){end=\$8+1;print \$3,\$8,end}}'};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1122 $cmd .= qq{ | sortBed | mergeBed -d $dist -n | sort -k4nr | head -1};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1123 #if($verbose){print "$cmd\n"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1124 $mate_info = `$cmd`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1125 } else {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1126 $cmd = qq {samtools view $new_blacklist -q $mapQ -f 16 -F 1568 $file $target};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1127 $cmd .= qq{ | awk '{OFS="\\t"}{if((\$7 ~ /=/)&&(\$9>-$lwL && \$9 < 0 )){end=\$8+1;print \$3,\$8,end}}'};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1128 $cmd .= qq{ | sortBed | mergeBed -d $dist -n | sort -k4nr | head -1};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1129 #if($verbose){print "$cmd\n"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1130 $mate_info = `$cmd`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1131 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1132 } elsif ($event =~ /^INV$/i) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1133 #print "INV side $side\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1134 if ($side =~ /right/i) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1135 $cmd = qq{samtools view $new_blacklist -q $mapQ -F 1596 $file $target};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1136 $cmd .= qq{ | awk '{OFS="\\t"}{if((\$7 ~ /=/)){end=\$8+1;print \$3,\$8,end}}'};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1137 $cmd .= qq{ | sortBed | mergeBed -d $dist_To_Soft -n | sort -k4nr | head -1};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1138 #if($verbose){print "$cmd\n"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1139 $mate_info = `$cmd`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1140 } else {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1141 $cmd = qq{samtools view $new_blacklist -q $mapQ -f 48 -F 1548 $file $target};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1142 $cmd .= qq{ | awk '{OFS="\\t"}{if((\$7 ~ /=/)){end=\$8+1;print \$3,\$8,end}}'};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1143 $cmd .= qq{ | sortBed | mergeBed -d $dist -n | sort -k4nr | head -1};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1144 #if($verbose){print "$cmd\n"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1145 $mate_info = `$cmd`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1146 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1147 } elsif ($event =~ /^TDUP$/i) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1148 #print "TDUP side $side\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1149 if ($side =~ /right/i) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1150 $cmd = qq{samtools view $new_blacklist -q $mapQ -f 32 -F 1552 $file $target};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1151 # $cmd .= qq{ | awk '{OFS="\\t"}{if((\$7 ~ /=/)&&(\$9>$upL)){end=\$8+1;print \$3,\$8,end}}'};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1152 $cmd .= qq{ | awk '{OFS="\\t"}{if((\$7 ~ /=/)&&(\$4>\$8)&&(\$9<0)&& (\$9>$upL)){end=\$8+1;print \$3,\$8,end}}'};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1153 $cmd .= qq{ | sortBed | mergeBed -d $dist -n | sort -k4nr | head -1};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1154 #if($verbose){print "$cmd\n"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1155 $mate_info = `$cmd`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1156 } else {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1157 $cmd = qq{samtools view $new_blacklist -q $mapQ -f 16 -F 1568 $file $target};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1158 # $cmd .= qq{ | awk '{OFS="\\t"}{if((\$7 ~ /=/)&&(\$9<-$upL )){end=\$8+1;print \$3,\$8,end}}'};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1159 $cmd .= qq{ | awk '{OFS="\\t"}{if((\$7 ~ /=/)&&(\$4<\$8)&&(\$9>0)&&(\$9>$upL)){end=\$8+1;print \$3,\$8,end}}'};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1160 $cmd .= qq{ | sortBed | mergeBed -d $dist -n | sort -k4nr | head -1};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1161 #if($verbose){print "$cmd\n"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1162 $mate_info = `$cmd`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1163 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1164 } elsif ($event =~ /^NOV_INS$/i) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1165 #print "NOV_INS side $side\n";
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1166 if ($side =~ /right/i) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1167 $cmd = qq{samtools view $new_blacklist -q $mapQ -f 8 -F 1552 $file $target};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1168 $cmd .= qq{ | awk '{OFS="\\t"}{end=\$8+1;print \$3,\$8,end}'};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1169 $cmd .= qq{ | sortBed | mergeBed -d $dist -n | sort -k4nr | head -1};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1170 #if($verbose){print "$cmd\n"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1171 $mate_info = `$cmd`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1172 } else {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1173 $cmd = qq{samtools view $new_blacklist -q $mapQ -f 24 -F 1536 $file $target};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1174 $cmd .= qq{ | awk '{OFS="\\t"}{end=\$8+1;print \$3,\$8,end}'};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1175 $cmd .= qq{ | sortBed | mergeBed -d $dist -n | sort -k4nr | head -1};
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1176 #if($verbose){print "$cmd\n"}
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1177 $mate_info = `$cmd`;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1178 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1179 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1180
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1181 $mate_info=~s/\n//g;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1182 my @tmp=split(/\t/, $mate_info);
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1183
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1184 my $counts = 0;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1185
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1186 if (defined $tmp[3]) {
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1187 $tmp[3] =~ s/\n//g;
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1188
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1189 $counts = $tmp[3] if (length($tmp[3]));
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1190 }
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1191 return ({count=>$counts, info=>$mate_info});
0fc089b0d1ba Uploaded
plus91-technologies
parents:
diff changeset
1192 }