-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathupdate_database
177 lines (160 loc) · 5.74 KB
/
update_database
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
#!/bin/bash
GRID_DIR="$( cd "$( dirname "$(readlink -f ${BASH_SOURCE[0]})" )" && pwd )"
LIST=false
while getopts ":g:d:p:l:h" opt; do
case ${opt} in
h )
echo "Usage:"
echo " update_database <options>"
echo " <options>"
echo " -d GRiD database directory (required)"
echo " -g Bacterial genomes directory (required)"
echo " -p Prefix for new database (required)"
echo " -l Path to file listing specific genomes"
echo " for inclusion in database [default = include all genomes in directory]"
echo " -h Display this message"
exit 0
;;
d )
DATABASE_DIR=$OPTARG
;;
g )
GENOME_DIR=$OPTARG
;;
p )
NAME=$OPTARG
;;
l )
LIST=$OPTARG
;;
\? )
echo "Invalid Option: -$OPTARG" 1>&2
exit 1
;;
: )
echo "Invalid Option: -$OPTARG requires an argument" 1>&2
exit 1
;;
* )
echo "Unimplemented option: -$OPTARG" >&2
exit 1
;;
esac
done
###########################################################################
if [ $# -eq 0 ];
then
echo "Usage:"
echo " update_database <options>"
echo " <options>"
echo " -d GRiD database directory (required)"
echo " -g Bacterial genomes directory (required)"
echo " -p Prefix for new database (required)"
echo " -l Path to file listing specific genomes"
echo " for inclusion in database [default = include all genomes in directory]"
echo " -h Display this message"
exit 1
fi
if [ "x" == "x$DATABASE_DIR" ]; then
echo "-d [option] is required"
exit 1
fi
if [ "x" == "x$GENOME_DIR" ]; then
echo "-g [option] is required"
exit 1
fi
if [ "x" == "x$NAME" ]; then
echo "-p [option] is required"
exit 1
fi
###########################################################################
DBR=$(readlink -f $DATABASE_DIR)
GDR=$(readlink -f $GENOME_DIR)
if [ "$LIST" == "false" ]; then
cd $GDR
exec 3>&2
exec 2> /dev/null
ls *.fa >> grid_database_genomes.txt
ls *.fna >> grid_database_genomes.txt
ls *.fasta >> grid_database_genomes.txt
exec 2>&3
if [[ ! -s grid_database_genomes.txt ]]; then
echo "ERROR: No valid genome(s) found."
echo "Please ensure your genomes have either .fa, .fna, or .fasta extensions "
rm grid_database_genomes.txt
exit 1
fi
if [[ ! -e "$DBR/database_misc.txt" ]]; then touch $DBR/database_misc.txt; fi
if [[ ! -e "$DBR/bowtie.txt" ]]; then touch $DBR/bowtie.txt; fi
cat grid_database_genomes.txt | rev | cut -d'.' -f2- | rev > check_GRiD_genomes.temp.txt
grep -w -f check_GRiD_genomes.temp.txt $DBR/database_misc.txt | cut -f1 > check_GRiD_genomes.txt
if [ -s check_GRiD_genomes.txt ]; then
rm check_GRiD_genomes.temp.txt
rm grid_database_genomes.txt
echo "ERROR: Some of your genome names already exists in the GRiD database."
echo "Please rename the genomes in 'check_GRiD_genomes.txt' "
exit 1
fi
rm check_GRiD_genomes*
#######
checkName=$(grep -w "BOWTIE_$NAME" $DBR/bowtie.txt | cut -f2- -d'_')
if [[ ! -z $checkName ]]; then
echo "ERROR: Your database prefix already exists in the GRiD database."
echo "Please choose a different prefix "
rm grid_database_genomes.txt
exit 1
fi
#######
for f in `cat grid_database_genomes.txt`
do
name=$(ls "$f" | rev | cut -d'.' -f2- | rev)
sed '1d' $f | sed "s/>.*/$(printf '%.0sN' {0..100})/g" | tr -d '\n' | sed "1 i\>$name" > grid_database_$f
done
rm grid_database_genomes.txt
else
LIS=$(readlink -f $LIST)
cd $GDR
cat $LIS | rev | cut -d'.' -f2- | rev > check_GRiD_genomes.temp.txt
grep -w -f check_GRiD_genomes.temp.txt $DBR/database_misc.txt | cut -f1 > check_GRiD_genomes.txt
if [ -s check_GRiD_genomes.txt ]; then
rm check_GRiD_genomes.temp.txt
echo "ERROR: Some of your genome names already exists in the GRiD database."
echo "Please rename the genomes in 'check_GRiD_genomes.txt' "
exit 1
fi
rm check_GRiD_genomes*
checkName=$(grep -w "BOWTIE_$NAME" $DBR/bowtie.txt | cut -f2- -d'_')
if [[ ! -z $checkName ]]; then
echo "ERROR: Your database prefix already exists in the GRiD database."
echo "Please choose a different prefix "
exit 1
fi
####
for f in `cat $LIS`
do
name=$(ls "$f" | rev | cut -d'.' -f2- | rev)
sed '1d' $f | sed "s/>.*/$(printf '%.0sN' {0..100})/g" | tr -d '\n' | sed "1 i\>$name" > grid_database_$f
done
fi
###########################################
#############################################
cat grid_database_* > $NAME
bowtie2-build $NAME $DBR/BOWTIE_$NAME -q
rm $NAME
##############################################
for f in grid_database_*
do
len=$(awk '/^>/ {if (seqlen){print seqlen}; print ;seqlen=0;next; } { seqlen += length($0)}END{print seqlen}' $f | grep -v ">" | awk '{ total += $1 } END { print total }')
gen=$(grep ">" $f | sed 's/ //g' | sed 's/>//g' )
var1=$(sed '1d' $f | tr -d '\n' | grep -b -o N* | cut -f1 -d':' | awk '{print ($1-1)"p" $0 }' | sed 's/p/p\n/g' | sed '1 i\1' | sed '$s/$/\n'$len'p/' | tr '\r\n' ',')
var2=$(sed '1d' $f | tr -d '\n' | grep -b -o N* | cut -f1 -d':' | awk '{ print $0,"", ($1+99)"d" }' | sed 's/ /,/g' | tr '\r\n' ';')
fragment=$(sed '1d' $f | tr -d '\n' | grep -b -o N* | cut -f1 -d':' | awk '{print ($1-1)"p" $0 }' | sed 's/p/p\n/g' | sed '1 i\1' | sed '$s/$/\n'$len'p/' | tr '\r\n' ',' | sed 's/p,/\n/g' | wc -l)
fpmb=$(echo "scale=3; ($fragment*1000000)/$len" |bc)
echo -e "$gen\t$var1\t$var2\t$len\t$fpmb" >> $DBR/GRiD.temp.misc.txt
done
awk -F'\t' '$2=="" {$2="1,100000000p"};1' OFS='\t' $DBR/GRiD.temp.misc.txt >> $DBR/database_misc.txt
rm $DBR/GRiD.temp.misc.txt
rm grid_database_*
cd $DBR
ls BOWTIE_$NAME.rev.1.bt2* | rev | cut -d'.' -f4- | rev >> $DBR/bowtie.txt
###############################################