File: clean_contam_by_index_id.sh

package info (click to toggle)
spades 3.13.1+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 22,172 kB
  • sloc: cpp: 136,213; ansic: 48,218; python: 16,809; perl: 4,252; sh: 2,115; java: 890; makefile: 507; pascal: 348; xml: 303
file content (242 lines) | stat: -rwxr-xr-x 7,475 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/bin/bash

############################################################################
# Copyright (c) 2015 Saint Petersburg State University
# Copyright (c) 2011-2014 Saint Petersburg Academic University
# All Rights Reserved
# See file LICENSE for details.
############################################################################


# Matches reads by bowtie to genomes / sequences and throws out the reads that match.

function usage ()
{
    echo "usage: $1 ( -a <single_file_with_all_reads.fastq[.gz] | -1 <first_reads.fastq[.gz]> -2 <second_reads.fastq[.gz]> [-u1 <first_unpaired.fastq[.gz]> -u2 <second_unpaired.fastq[.gz]>] ) -i <genome_index[,genome_index2,...]> [-o <output folder>] [-d <blast database>]"
}

function copy_if_not_same ()
{
    if [ ! $1 -ef $2 ]
    then
         cp $1 $2
    fi
}

function link_if_not_same ()
{
    if [ ! $1 -ef $2 ]
    then
         ln -s $1 $2
    fi
}

function clean_unpaired ()
{
    echo "processing unpaired reads"
    reads=$1
    indices=$2
    output_folder=$3
    filename=$(basename $reads)
    extension=${filename##*.}
    targetfile="${output_folder}/${filename}"
    if [ "$extension" = "gz" ]
    then
      copy_if_not_same $reads $targetfile
      gunzip $targetfile
      filename=${filename%.*}
      extension=${filename##*.}
    else
      link_if_not_same $reads $targetfile
    fi
    myreads="${output_folder}/${filename}"
    filename=${filename%.*}
    cleanedreads="${output_folder}/${filename}_cleaned.${extension}"
    mv $myreads $cleanedreads
    readstmp=$cleanedreads.tmp
    for i in "${indices[@]}"
    do
        echo "running bowtie on index $i"
	bowtie -c -q -p 16 --suppress 6,7,8 $i $cleanedreads --un $readstmp > /dev/null 2> /dev/null
        mv $readstmp $cleanedreads
    done
}

function clean_paired ()
{
    echo "processing paired reads"
    left=$1
    right=$2
    indices=$3
    output_folder=$4
    path_to_script=$5
    filename1=$(basename $left)
    extension1=${filename1##*.}
    if [ "$extension1" = "gz" ]
    then
      copy_if_not_same $left "${output_folder}/${filename1}"
      gunzip "${output_folder}/${filename1}"
      filename1=${filename1%.*}
      extension1=${filename1##*.}
    else
      link_if_not_same $left "${output_folder}/${filename1}"
    fi
    myleft="${output_folder}/${filename1}"
    filename1=${filename1%.*}

    filename2=$(basename $right)
    extension2=${filename2##*.}
    if [ "$extension2" = "gz" ]
    then
      copy_if_not_same $right "${output_folder}/${filename2}"
      gunzip "${output_folder}/${filename2}"
      filename2=${filename2%.*}
      extension2=${filename2##*.}
    else
      link_if_not_same $right "${output_folder}/${filename2}"
    fi
    myright="${output_folder}/${filename2}"
    filename2=${filename2%.*}
    cleanedleft="${output_folder}/${filename1}_cleaned.${extension1}"
    filteredleft="${output_folder}/${filename1}_cleaned_filtered.${extension1}"
    cleanedright="${output_folder}/${filename2}_cleaned.${extension2}"
    filteredright="${output_folder}/${filename2}_cleaned_filtered.${extension2}"
    mv $myleft $cleanedleft
    mv $myright $cleanedright
    timestamp=`date "+%m-%d_%T"`
    for i in "${indices[@]}"
    do
        echo "running bowtie on index $i"
        #bowtie -c -q -p 16 --suppress 6,7,8 --minins 0 --maxins 10000 --fr --tryhard $i -1 $cleanedleft -2 $cleanedright > paired_log.tmp 2> /dev/null
        bowtie -c -q -p 16 --suppress 6,7,8 $i $cleanedleft > "${output_folder}/paired_log1_${timestamp}.tmp" 2> /dev/null
        bowtie -c -q -p 16 --suppress 6,7,8 $i $cleanedright > "${output_folder}/paired_log2_${timestamp}.tmp" 2> /dev/null
        echo "running filter_reads.py"
        python3 ${path_to_script}/../reads_utils/filter/filter_reads.py $cleanedleft $cleanedright "${output_folder}/paired_log1_${timestamp}.tmp" "${output_folder}/paired_log2_${timestamp}.tmp"
        mv $filteredleft $cleanedleft
        mv $filteredright $cleanedright
    done
    rm "${output_folder}/paired_log1_${timestamp}.tmp" "${output_folder}/paired_log2_${timestamp}.tmp"

}

##### Main

left=
right=
left_unpaired=
right_unpaired=
indices=()
indexnames=()
all_reads=
script=$0
output_folder="."
blast_database="/acestorage/blast/db/nt"

scriptname=${script##*/}
path_to_script=${script%$scriptname}

while [ "$1" != "" ]; do
    case $1 in
        -a | --all_reads )         shift
                                   all_reads=$1
                                   ;;
        -1 | --first )             shift
                                   left=$1
                                   ;;
        -2 | --second )            shift
                                   right=$1
                                   ;;
        -u1 | --first_unpaired )   shift 
                                   left_unpaired=$1
                                   ;;
        -u2 | --second_unpaired )  shift
                                   right_unpaired=$1
                                   ;;
	-o | --output )            shift
                                   output_folder=$1
                                   ;;  
        -d | --database )          shift
                                   blast_database=$1
                                   ;;                      
        -i | --index )             shift
                                   indexnames=$1
                                   ;;
        -h | --help )              usage $script
                                   exit
                                   ;;
        * )                        usage $script
                                   exit 1
    esac
    shift
done

if [ -z "$left" -o -z "$right" ]
then
        if [ -z "$all_reads" ]
        then
        	usage $script
		exit
	fi
fi

if [ ${#indexnames[@]} -eq 0 ]
then
	usage $script
	exit
fi

dbname=${blast_database##*/}
path_to_db=${blast_database%$dbname}

mkdir -p $output_folder

old_IFS=${IFS}
IFS=","
for i in $indexnames #"${indexnames[@]}"
do
	indices[$[${#indices[@]}]]="${path_to_db}/index/${i:0:2}/${i}"
	if [ ! -e "${path_to_db}/index/${i:0:2}/${i}.1.ebwt" ]
	then
		echo "building index for sequence $i"
		mkdir -p "${path_to_db}/index/${i:0:2}"
		python3 ${path_to_script}/read_fasta_using_index.py $i "${blast_database}.fasta" > "${i}.fasta.tmp"
		bowtie-build "${i}.fasta.tmp" "${path_to_db}/index/${i:0:2}/${i}"
		rm "${i}.fasta.tmp"
	fi
done
IFS=${old_IFS}

if [ "$all_reads" ]
then
    echo "Splitting reads file into multiple files."
    filename=$(basename $all_reads)
    extension=${filename##*.}
    if [ "$extension" = "gz" ]
    then
      cp $all_reads "${output_folder}/${filename}"
      gunzip "${output_folder}/${filename}"
      filename=${filename%.*}
      extension=${filename##*.}
    else
      ln -s $all_reads "${output_folder}/${filename}"
    fi
    python3 ${path_to_script}/../reads_utils/conversion/split_any_fastq.py "${output_folder}/${filename}"
    filename=${filename%.*}
    left="${output_folder}/${filename}_left.${extension}"
    right="${output_folder}/${filename}_right.${extension}"
    left_unpaired="${output_folder}/${filename}_single_left.${extension}"
    right_unpaired="${output_folder}/${filename}_single_right.${extension}"
fi

clean_paired $left $right $indices $output_folder $path_to_script

if [ "$left_unpaired" ]
then
        clean_unpaired $left_unpaired $indices $output_folder
fi

if [ "$right_unpaired" ]
then
        clean_unpaired $right_unpaired $indices $output_folder
fi