0
|
1 #!/bin/bash
|
|
2
|
|
3 #LINKYX_PATH set during toolshed installation
|
|
4
|
|
5 results=$1 #X-linked candidates written here
|
|
6
|
|
7 reference=${2}
|
|
8 mother_bam=${3}
|
|
9 mother_vcf=${4}
|
|
10 father_bam=${5}
|
|
11 father_vcf=${6}
|
|
12 daughter_bam=${7}
|
|
13 daughter_vcf=${8}
|
|
14 son_bam=${9}
|
|
15 son_vcf=${10}
|
|
16
|
|
17 results_fasta=${11}
|
|
18
|
|
19 file_exists ()
|
|
20 {
|
|
21 for ARG in "$@";
|
|
22 do
|
|
23 if [ ! -f ${ARG} ]; then
|
|
24 echo "File $ARG not found";
|
|
25 fi
|
|
26 done;
|
|
27 }
|
|
28
|
|
29 #index bam files
|
|
30
|
|
31 index_bam_file ()
|
|
32 {
|
|
33 for ARG in "$@";
|
|
34 do
|
|
35 bam_file_index=${ARG}".bai"
|
|
36 if [ -e $bam_file_index ];
|
|
37 then
|
|
38 echo "Index already exists. Not being created again.";
|
|
39 else
|
|
40 samtools index ${ARG} &
|
|
41 fi
|
|
42 done;
|
|
43 }
|
|
44
|
|
45 idxstats_bam_file ()
|
|
46 {
|
|
47 #get statistics about mapped and unmapped reads for every contig
|
|
48 echo "Idxstats" $1 $2;
|
|
49 stat_name=statistics/stat_${2};
|
|
50 samtools idxstats $1 >$stat_name #statistics for bam files mapping on male reference
|
|
51
|
|
52 #with MAPQ>0
|
|
53 samtools view -bq 1 $1 >statistics/high_score_${2}.bam
|
|
54 samtools index statistics/high_score_${2}.bam
|
|
55
|
|
56 stat_high_name=statistics/stat_${2}_high
|
|
57 samtools idxstats statistics/high_score_${2}.bam >$stat_high_name #statistics for bam files mapping on male reference with MAPQ>0
|
|
58 }
|
|
59
|
|
60
|
|
61 file_exists $testing $results $reference $mother_bam $mother_vcf $father_bam $father_vcf $daughter_bam $daughter_vcf $son_bam $son_vcf #check if all input files are present
|
|
62 index_bam_file $mother_bam $father_bam $daughter_bam $son_bam #index bam files
|
|
63
|
|
64 #retrieve statistics
|
|
65 mkdir -p statistics;
|
|
66 idxstats_bam_file $mother_bam mother
|
|
67 idxstats_bam_file $father_bam father
|
|
68 idxstats_bam_file $daughter_bam daughter
|
|
69 idxstats_bam_file $son_bam son
|
|
70
|
|
71 newline () {
|
|
72 echo -e "\n---";
|
|
73 }
|
|
74
|
|
75 #variables
|
|
76
|
|
77 dir="A/";
|
|
78 echo -e "A variant"; newline; mkdir -p A references bam;
|
|
79
|
|
80 grep ">" $reference | sed 's/>//g' >references/reference_contig_names
|
|
81 mother_contigs=`cat references/reference_contig_names | wc -l`;
|
|
82
|
|
83 #DAUGHTER PREPARE FILES
|
|
84
|
|
85 grep -E '#|AF1=0.' $daughter_vcf | cut -f1 | uniq >${dir}daughter_on_mother_one_allele_heterozygous_ids.vcf
|
|
86 perl -MFile::Slurp -ne 'BEGIN{@a{split(/\s/,read_file("A/daughter_on_mother_one_allele_heterozygous_ids.vcf"))}=()}; $b=$_; @data = split(/\s/, $b); $res=$data[0]; print if exists $a{$res}' $daughter_vcf | sort | uniq | sed '/#/d' >${dir}daughter_on_mother_heterozygous.vcf
|
|
87
|
|
88 #be aware that if there are more snps, one with allele frequency '0.' is enough for the whole contig to be in contig_pos_ref_heterozygous
|
|
89 sed '/#/d' ${dir}daughter_on_mother_heterozygous.vcf | perl -pe 's/(comp\S+)\t(\d+)\t\.\t(\S+)\t(\S+).+/$1\t$2\t$3\t$4/' | sort >${dir}contig_pos_ref_alt_daughter_heterozygous
|
|
90 cut -f 1 ${dir}contig_pos_ref_alt_daughter_heterozygous | sort | uniq >${dir}contigs_where_daughter_heterozygous
|
|
91
|
|
92 sed '/#/d' $daughter_vcf | perl -pe 's/(comp\S+)\t(\d+)\t\.\t(\S+)\t(\S+).+/$1\t$2\t$3\t$4/' | sort >${dir}contig_pos_ref_alt_daughter
|
|
93
|
|
94 #FATHER PREPARE FILES
|
|
95
|
|
96 sed '/#/d' $father_vcf | perl -pe 's/(comp\S+)\t(\d+)\t\.\t(\S+)\t(\S+).+/$1\t$2\t$3\t$4/' | sort >${dir}contig_pos_ref_alt_father
|
|
97
|
|
98 #SON PREPARE FILES
|
|
99
|
|
100 sed '/#/d' $son_vcf | perl -pe 's/(comp\S+)\t(\d+)\t\.\t(\S+)\t(\S+).+/$1\t$2\t$3\t$4/' | sort >${dir}contig_pos_ref_alt_son
|
|
101
|
|
102
|
|
103 echo -e "Step 0 of 3.\nMother must be homozygote.";
|
|
104 sed '/#/d' $mother_vcf | cut -f1 | sort | uniq >${dir}contigs_where_mother_differs_from_mother;
|
|
105 differing=`cat ${dir}contigs_where_mother_differs_from_mother | wc -l`;
|
|
106
|
|
107 perl -MFile::Slurp -ne 'BEGIN{@a{split(/\s/,read_file("A/contigs_where_mother_differs_from_mother"))}=()}; $b=$_; @data = split(/\s/, $b); $res=$data[0]; print if !exists $a{$res}' references/reference_contig_names | sort | uniq >${dir}contigs_where_mother_same_as_mother_unchecked
|
|
108
|
|
109 #check whether mother really does not contain any father variants
|
|
110 wrong_hits=0;
|
|
111 cat ${dir}contigs_where_mother_same_as_mother_unchecked | (while read line; do
|
|
112 contig_check=`bash ${LINKYX_PATH}/scripts/test.sh ${line} 4 ${dir}contig_pos_ref_alt_father $mother_bam A`
|
|
113 if [[ $contig_check -gt 0 ]]; then
|
|
114 echo "contig: " ${line} "contig_check:" $contig_check >>kontrola;
|
|
115 echo "DID NOT PASS "${line};
|
|
116 wrong_hits=`expr $wrong_hits + 1`
|
|
117 else
|
|
118 #echo "0K";
|
|
119 echo ${line} >>${dir}contigs_where_mother_same_as_mother
|
|
120 fi
|
|
121 done;
|
|
122 echo "There is this number of wrong variants: "$wrong_hits;
|
|
123 )
|
|
124
|
|
125 same=`cat ${dir}contigs_where_mother_same_as_mother | wc -l`;
|
|
126 echo -e "Number of contigs, where mother differs: $differing.";
|
|
127 echo -e "NUMBER of contigs AFTER just this filter: $same/$mother_contigs";
|
|
128
|
|
129 newline;
|
|
130
|
|
131 echo -e "Step 1 of 3.\nSons must be same as mother.";
|
|
132 #First we have to get rid of the intersection with father, possible Y variants do not matter in the calculation
|
|
133 comm -12 ${dir}contig_pos_ref_alt_son ${dir}contig_pos_ref_alt_father | sort | uniq >${dir}possible_Yvariants
|
|
134 cut -f 1 ${dir}possible_Yvariants | sort | uniq >${dir}contigs_with_possible_Yvariants
|
|
135
|
|
136 #if these possible_Yvariants are in the daughter, they are not Y variants
|
|
137 comm -12 ${dir}contig_pos_ref_alt_daughter ${dir}possible_Yvariants | sort | uniq >${dir}NOT_Yvariants
|
|
138 cut -f 1 ${dir}NOT_Yvariants | sort | uniq >${dir}contigs_with_NOT_Yvariants
|
|
139
|
|
140 perl -MFile::Slurp -ne 'BEGIN{@a{split(/\s/,read_file("A/contigs_with_NOT_Yvariants"))}=()}; $b=$_; @data = split(/\s/, $b); $res=$data[0]; print if !exists $a{$res}' ${dir}contigs_with_possible_Yvariants | sort | uniq >${dir}contigs_with_Yvariants
|
|
141
|
|
142 #contig_pos_ref_alt_Yvariants_confirmed; we deplet just those contigs that contain Y variants and then decide if sons are same as mother
|
|
143
|
|
144 touch ${dir}contig_pos_ref_alt_Yvariants_confirmed_pro #todo - needs to be created?
|
|
145 cat ${dir}contigs_with_Yvariants | (while read line; do
|
|
146 grep ${line} ${dir}contig_pos_ref_alt_father | sort >>${dir}contig_pos_ref_alt_Yvariants_confirmed_pro
|
|
147 done;
|
|
148 )
|
|
149 cat ${dir}contig_pos_ref_alt_Yvariants_confirmed_pro | sort >${dir}contig_pos_ref_alt_Yvariants_confirmed;
|
|
150
|
|
151 comm -23 ${dir}contig_pos_ref_alt_son ${dir}contig_pos_ref_alt_Yvariants_confirmed | cut -f 1 | sort | uniq >${dir}contigs_where_sons_variation_not_caused_by_father
|
|
152 #rm ${dir}contig_pos_ref_alt_Yvariants_confirmed_pro;
|
|
153
|
|
154 perl -MFile::Slurp -ne 'BEGIN{@a{split(/\s/,read_file("A/contigs_where_sons_variation_not_caused_by_father"))}=()}; $b=$_; @data = split(/\s/, $b); $res=$data[0]; print if !exists $a{$res}' references/reference_contig_names | sort | uniq >${dir}contigs_where_sons_same_as_mother
|
|
155
|
|
156 same=`cat ${dir}contigs_where_sons_same_as_mother | wc -l`;
|
|
157 echo -e "Number of contigs, where sons differ: $differing.";
|
|
158 echo -e "NUMBER of contigs AFTER just this filter: $same/$mother_contigs";
|
|
159
|
|
160
|
|
161 newline;
|
|
162 echo -e "Step 2 of 3.\nFather must differ from mother.";
|
|
163 sed '/#/d' $father_vcf | cut -f1 | sort | uniq >${dir}/contigs_where_father_differs_from_mother;
|
|
164 differing=`cat ${dir}contigs_where_father_differs_from_mother | wc -l`;
|
|
165 echo -e "NUMBER of contigs AFTER just this filter: $differing/$mother_contigs";
|
|
166
|
|
167 newline;
|
|
168
|
|
169 echo -e "Step 3 of 3.\nDaughter must have mother and father alleles.";
|
|
170 #we need daughter to have alleles from both parents (AF stands for 'allele frequency')
|
|
171
|
|
172 #unique lines in first file mean that those are not subsets of father variants - otherwise they would be in common
|
|
173 comm -23 ${dir}contig_pos_ref_alt_daughter_heterozygous ${dir}contig_pos_ref_alt_father | cut -f 1 | sort | uniq >${dir}daughter_NOT_subset_of_father
|
|
174
|
|
175 perl -MFile::Slurp -ne 'BEGIN{@a{split(/\s/,read_file("A/daughter_NOT_subset_of_father"))}=()}; $b=$_; @data = split(/\s/, $b); $res=$data[0]; print if !exists $a{$res}' ${dir}contigs_where_daughter_heterozygous | sort | uniq >${dir}/contigs_where_daughter_from_parents
|
|
176
|
|
177
|
|
178 same=`cat ${dir}contigs_where_daughter_from_parents | wc -l`;
|
|
179 echo -e "NUMBER of contigs AFTER just this filter: $same/$mother_contigs";
|
|
180
|
|
181 newline;
|
|
182
|
|
183 echo -e "Step 4 of 3.\nDaughters and sons can not have any intersection.";
|
|
184 comm -12 ${dir}contig_pos_ref_alt_daughter ${dir}contig_pos_ref_alt_son >${dir}sons_and_daughters_similiar
|
|
185 cut -f 1 ${dir}sons_and_daughters_similiar | sort | uniq >${dir}contigs_where_sons_and_daughters_similiar
|
|
186
|
|
187 perl -MFile::Slurp -ne 'BEGIN{@a{split(/\s/,read_file("A/contigs_where_sons_and_daughters_similiar"))}=()}; $b=$_; @data = split(/\s/, $b); $res=$data[0]; print if !exists $a{$res}' ${dir}contigs_where_daughter_heterozygous | sort | uniq >${dir}contigs_where_daughters_and_sons_different_pro
|
|
188
|
|
189 #check whether they are really different (small amount of variants is also wrong)
|
|
190
|
|
191 echo -e "daughter contains son variants?"
|
|
192 #check whether daughter really does not contain any of son variants
|
|
193 wrong_hits=0;
|
|
194 cat ${dir}contigs_where_daughters_and_sons_different_pro | (while read line; do
|
|
195 contig_check=`bash ${LINKYX_PATH}/scripts/test.sh ${line} 4 ${dir}contig_pos_ref_alt_daughter $son_bam A`
|
|
196 if [[ $contig_check -gt 0 ]]; then
|
|
197 echo "DID NOT PASS "${line};
|
|
198 echo "daughter contains son variants DID NOT PASS "${line} >>kontrola
|
|
199 wrong_hits=`expr $wrong_hits + 1`
|
|
200 else
|
|
201 #echo "0K";
|
|
202 echo ${line} >>${dir}contigs_where_daughters_and_sons_different_un1
|
|
203 fi
|
|
204 done;
|
|
205 echo "There is this number of wrong variants: "$wrong_hits;
|
|
206 )
|
|
207
|
|
208 echo -e "son contains daughter variants?"
|
|
209 #check whether son really does not contain any of daughter variants
|
|
210 wrong_hits=0;
|
|
211 cat ${dir}contigs_where_daughters_and_sons_different_pro | (while read line; do
|
|
212 contig_check=`bash ${LINKYX_PATH}/scripts/test.sh ${line} 4 ${dir}contig_pos_ref_alt_son $daughter_bam A`
|
|
213 if [[ $contig_check -gt 0 ]]; then
|
|
214 echo "DID NOT PASS "${line};
|
|
215 echo "son contains daughter variants DID NOT PASS "${line} >>kontrola
|
|
216 wrong_hits=`expr $wrong_hits + 1`
|
|
217 else
|
|
218 #echo "0K";
|
|
219 echo ${line} >>${dir}contigs_where_daughters_and_sons_different_un2
|
|
220 fi
|
|
221 done;
|
|
222 echo "There is this number of wrong variants: "$wrong_hits;
|
|
223 )
|
|
224
|
|
225 comm -12 ${dir}contigs_where_daughters_and_sons_different_un1 ${dir}contigs_where_daughters_and_sons_different_un2 | sort >${dir}contigs_where_daughters_and_sons_different_un
|
|
226
|
|
227 cat ${dir}contigs_where_daughters_and_sons_different_un | sort | uniq >${dir}contigs_where_daughters_and_sons_different;
|
|
228
|
|
229 #rm ${dir}contigs_where_daughters_and_sons_different_un1 ${dir}contigs_where_daughters_and_sons_different_un2 ${dir}contigs_where_daughters_and_sons_different_un;
|
|
230
|
|
231 same=`cat ${dir}contigs_where_daughters_and_sons_different | wc -l`;
|
|
232 echo -e "NUMBER of contigs AFTER just this filter: $same/$mother_contigs";
|
|
233
|
|
234 echo -e "Calculating intersection of candidate contigs from steps 0-3.";
|
|
235
|
|
236 comm -12 ${dir}contigs_where_mother_same_as_mother ${dir}contigs_where_sons_same_as_mother | sort >${dir}first_couple;
|
|
237 comm -12 ${dir}contigs_where_father_differs_from_mother ${dir}contigs_where_daughter_from_parents | sort >${dir}second_couple;
|
|
238 comm -12 ${dir}first_couple ${dir}second_couple | sort >${dir}set1
|
|
239 comm -12 ${dir}contigs_where_daughters_and_sons_different ${dir}set1 | sort >${dir}set2;
|
|
240
|
|
241 cat ${dir}set2 >>results; #todo remove
|
|
242 echo -e "Number of contigs after intersection: " `cat ${dir}set2 | wc -l`;
|
|
243
|
|
244 echo -e "Step -1 of 3.\nCoverage must be sufficient.";
|
|
245 touch ${dir}contigs_with_sufficient_coverage;
|
|
246
|
|
247 cat ${dir}set2 | (while read line; do
|
|
248
|
|
249 coverage_check1=`bash ${LINKYX_PATH}/scripts/get_average_contig_coverage.sh 0.75 ${line} $mother_bam $reference 2>/dev/null &`;
|
|
250 coverage_check2=`bash ${LINKYX_PATH}/scripts/get_average_contig_coverage.sh 0.75 ${line} $father_bam $reference 2>/dev/null &`;
|
|
251 coverage_check3=`bash ${LINKYX_PATH}/scripts/get_average_contig_coverage.sh 0.75 ${line} $son_bam $reference 2>/dev/null &`;
|
|
252 coverage_check4=`bash ${LINKYX_PATH}/scripts/get_average_contig_coverage.sh 0.75 ${line} $daughter_bam $reference 2>/dev/null &`;
|
|
253
|
|
254 #echo "coverage check: " $coverage_check1 $coverage_check2 $coverage_check3 $coverage_check4
|
|
255
|
|
256 coverage_check=$(($coverage_check1 + $coverage_check2 + $coverage_check3 + $coverage_check4))
|
|
257 if [[ $coverage_check -gt 0 ]]; then
|
|
258 echo "LOW COVERAGE "${line};
|
|
259 else
|
|
260 echo "COVERAGE 0K";
|
|
261 echo ${line} >>${dir}contigs_with_sufficient_coverage
|
|
262 fi
|
|
263 done;
|
|
264 )
|
|
265
|
|
266 cat ${dir}contigs_with_sufficient_coverage | sort >X_results.txt;
|
|
267
|
|
268 #have we found any genes?
|
|
269 final_contigs=`cat X_results.txt | wc -l`;
|
|
270 echo -e "$final_contigs contigs found fulfilling criteria.";
|
|
271
|
|
272 if [ $final_contigs -gt 0 ]; then
|
|
273 #we found SOME genes
|
|
274
|
|
275 #retrieve sequences in fasta
|
|
276 perl ${LINKYX_PATH}/scripts/get_sequences_based_on_ids.pl $reference X_results.txt >X_results_sequences.fasta;
|
|
277 #sort sequences
|
|
278 perl ${LINKYX_PATH}/scripts/sorting.pl A/ $reference ${LINKYX_PATH} >>$results
|
|
279
|
|
280 get_results_in_bam_file ()
|
|
281 {
|
|
282 #candidates full_bam_file candidates_bam_file
|
|
283 cat X_results.txt | xargs samtools view -F 4 -b $1 > $2;
|
|
284 samtools index $2
|
|
285 }
|
|
286
|
|
287 get_results_in_bam_file $mother_bam ${12} #argument for output bam file mother
|
|
288 get_results_in_bam_file $father_bam ${13} #argument for output bam file father
|
|
289 get_results_in_bam_file $daughter_bam ${14} #argument for output bam file daughter
|
|
290 get_results_in_bam_file $son_bam ${15} #argument for output bam file son
|
|
291 wait;
|
|
292
|
|
293 #send BAM files with corresponding contigs
|
|
294 #mutt -s "LINKYX: Results of computation for X-linked genes" $email_address_of_user -a bam/mother.bam -a bam/mother.bam.bai -a bam/father.bam -a bam/father.bam.bai -a bam/daughter.bam -a bam/daughter.bam.bai -a bam/son.bam -a bam/son.bam.bai < messages_to_user/message_results_sentBamX;
|
|
295
|
|
296 else
|
|
297 #we found NO genes
|
|
298 echo "No sequences found." >X_results.txt;
|
|
299 echo "No sequences found." >X_results_sequences.fasta;
|
|
300 fi
|
|
301
|
|
302 newline;
|
|
303 echo -e "Writing output to X_results.txt";
|
|
304 echo -e "Total number of found X-linked genes: $final_contigs";
|
|
305
|
|
306 #TEST OUTPUT
|
|
307 cat X_results.txt >>$results
|
|
308 cat X_results_sequences.fasta >>$results_fasta
|
|
309 ls -lah */* >>$results
|
|
310
|
|
311
|