-
Notifications
You must be signed in to change notification settings - Fork 5
/
single_gene_orthogroups_tree.nf
260 lines (240 loc) · 9.11 KB
/
single_gene_orthogroups_tree.nf
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//////////////////////////////////
// SINGLE-GENE ORTHOGROUPS TREE //
//////////////////////////////////
// Assumed extension names:
// - genome: *.fna
// - annotation: *.gff
// - coding DNA: *.cds
// - proteome: *.faa
process IDENTIFY_SINGLE_GENE_ORTHOGROUPS {
label "HIGH_MEM_HIGH_CPU"
input:
val dir
val dates
output:
val dir
val dates
shell:
'''
#!/usr/bin/env bash
cd !{dir}
echo "Define the location of the results of OrthoFinder run, i.e. the most recent output folder."
DIR_ORTHOFINDER_OUT=$(ls -tr ORTHOGROUPS/OrthoFinder/ | tail -n1)
DIR_ORTHOGROUPS=$(pwd)/ORTHOGROUPS/OrthoFinder/${DIR_ORTHOFINDER_OUT}
echo "List single-gene orthogroups for all species"
ORTHOUT=$(pwd)/ORTHOGROUPS/orthogroups_gene_counts_families_go.out
NSPECIES=$(ls ORTHOGROUPS/*.faa | grep -v "orthogroups.faa" | wc -l)
julia !{projectDir}/../scripts/extract_single_gene_orthogroups.jl \
${ORTHOUT} \
${NSPECIES} ### output: single_gene_list.grep
grep -f single_gene_list.grep ${DIR_ORTHOGROUPS}/Orthogroups/Orthogroups.tsv > single_gene_list.geneNames
echo "Find the single-gene orthogroups for all species"
echo '#!/bin/bash
i=$1
line=$(head -n${i} single_gene_list.geneNames | tail -n1)
ORTHONAME=$(echo $line | cut -d" " -f1)
for name in $(echo $line | cut -d" " -f2-)
do
SPECIES=$(echo $name | cut -d"|" -f1)
GENE_NAME=$(echo $name | cut -d"|" -f2)
julia !{projectDir}/../scripts/extract_sequence_using_name_query.jl \
CDS/${SPECIES}.cds \
${GENE_NAME} \
${ORTHONAME}-${SPECIES}.fasta \
${SPECIES} \
false
done
cat ${ORTHONAME}-*.fasta > ${ORTHONAME}.fasta
rm ${ORTHONAME}-*.fasta
' > parallel_extract_single_gene_orthogroups.sh
chmod +x parallel_extract_single_gene_orthogroups.sh
parallel -j !{task.cpus} \
./parallel_extract_single_gene_orthogroups.sh \
{} ::: $(seq 1 $(cat single_gene_list.geneNames | wc -l))
echo "Cleanup"
rm single_gene_list.grep
rm parallel_extract_single_gene_orthogroups.sh
echo "Output:"
echo " (1/2) {ORTHONAME}.fasta"
echo " (2/2) {ORTHONAME}-{SPECIES}.fasta"
'''
}
process ALIGN_SINGLE_GENE_ORTHOGROUPS {
label "HIGH_MEM_HIGH_CPU"
input:
val dir
val dates
output:
val dir
val dates
shell:
'''
#!/usr/bin/env bash
cd !{dir}
echo '#!/bin/bash
f=$1
ORTHOLOG=${f%.fasta*}
# Align the CDS across species
macse \
-prog alignSequences \
-seq ${f} \
-out_NT ${ORTHOLOG}.aligned.unsorted.cds.tmp \
-out_AA ${ORTHOLOG}.aligned.unsorted.prot.tmp
# Convert stop codons and frameshifts as "---" for compatibility with downstream tools
macse \
-prog exportAlignment \
-align ${ORTHOLOG}.aligned.unsorted.cds.tmp \
-codonForFinalStop --- \
-codonForInternalStop NNN \
-codonForExternalFS --- \
-codonForInternalFS --- \
-out_NT ${ORTHOLOG}.NT.cds \
-out_AA ${ORTHOLOG}.AA.prot
# Clean-up
rm ${ORTHOLOG}*.tmp ${ORTHOLOG}.AA.prot # we are not using the amino acid sequences
' > parallel_align_cds.sh
chmod +x parallel_align_cds.sh
time \
parallel -j !{task.cpus} \
./parallel_align_cds.sh \
{} \
::: $(ls OG*.fasta)
echo "Cleanup"
rm parallel_align_cds.sh
echo "Output:"
echo " (1/1) {ORTHOLOG}.NT.cds"
'''
}
process BUILD_TREE {
label "HIGH_MEM_HIGH_CPU"
input:
val dir
val dates
output:
val 0
shell:
'''
#!/usr/bin/env bash
cd !{dir}
echo "Extract sequences per species (Outputs: {ORTHONAME}-{SPECIES}.fasta)"
TYPE=NT.cds
parallel -j !{task.cpus} \
julia !{projectDir}/../scripts/extract_sequence_using_name_query.jl \
{1}.${TYPE} \
{2} \
{1}-{2}.fasta \
{1}-{2} \
false \
::: $(ls *.${TYPE} | sed "s/.$TYPE//g") \
::: $(grep "^>" $(ls *.${TYPE} | head -n1) | sed 's/^>//g')
echo "Remove alignments which do not match lengths across species (i.e. in cases where there are actually multiple alignmets of an orthogroup probably due to mislabelling in the input coding sequences - sequences with the same names)"
for ORTHO_EXPECTED in $(ls OG*-*_*.fasta | cut -d- -f1 | sort | uniq)
do
# ORTHO_EXPECTED=$(ls OG*-*_*.fasta | cut -d- -f1 | sort | uniq | head -n1)
SPECIES_1=$(grep "^>" $(ls *.${TYPE} | head -n1) | sed 's/^>//g' | head -n1)
N_1=$(grep -v "^>" ${ORTHO_EXPECTED}-${SPECIES_1}.fasta | sed -z 's/\\n//g' | wc -c)
for SPECIES_2 in $(grep "^>" $(ls *.${TYPE} | head -n1) | sed 's/^>//g' | tail -n+2)
do
# SPECIES_2=$(grep "^>" $(ls *.${TYPE} | head -n1) | sed 's/^>//g' | tail -n+2 | head -n1)
N_2=$(grep -v "^>" ${ORTHO_EXPECTED}-${SPECIES_2}.fasta | sed -z 's/\\n//g' | wc -c)
if [ $N_1 -ne $N_2 ]
then
echo ${ORTHO_EXPECTED}-${SPECIES_1}.fasta AND ${ORTHO_EXPECTED}-${SPECIES_2}.fasta
rm ${ORTHO_EXPECTED}-*_*.fasta
break
fi
done
done
echo "Concatenate alignments per species (Outputs: {SPECIES}.aln)"
for SPECIES in $(grep "^>" $(ls *.${TYPE} | head -n1) | sed 's/^>//g')
do
echo $SPECIES
# Concatenate sequences
echo ">${SPECIES}" > ${SPECIES}.aln.tmp
for f in $(ls *-${SPECIES}.fasta)
do
sed '/^>/d' $f | sed -z 's/\\n//g' >> ${SPECIES}.aln.tmp ### Note that you need to remove one of the escape characters in the newline character, if you need to run this on bash manually
done
echo "" >> ${SPECIES}.aln.tmp
julia !{projectDir}/../scripts/reformat_fasta_sequence.jl \
${SPECIES}.aln.tmp \
50 \
${SPECIES}-${TYPE%.*}.aln
rm ${SPECIES}.aln.tmp
done
echo "Extract sequence lengths to build the sequence partitioning nexus file (Output: alignment_parition.NT.nex)"
SPECIES=$(grep "^>" $(ls *.${TYPE} | head -n1) | sed 's/^>//g' | head -n1)
echo '#nexus
begin sets;' > alignment_parition.${TYPE%.*}.nex
N0=0
for f in $(ls *-${SPECIES}.fasta)
do
# f=$(ls *-${SPECIES}.fasta | head -n1)
NAME=$(head -n1 $f | sed 's/>//g' | cut -d'-' -f1)
N1=$(cat $f | sed '/^>/d' | sed -z 's/\\n//g' | wc -c) ### Note that you need to remove one of the escape characters in the newline character, if you need to run this on bash manually
START=$(echo "$N0 + 1" | bc)
END=$(echo "$N0 + $N1" | bc)
echo "charset $NAME = $START-$END;" >> alignment_parition.${TYPE%.*}.nex
N0=$END
done
echo 'end;' >> alignment_parition.${TYPE%.*}.nex
echo "Concatenate species alignments (Output: ORTHOGROUPS_SINGLE_GENE.NT.aln)"
cat *-${TYPE%.*}.aln > ORTHOGROUPS_SINGLE_GENE.${TYPE%.*}.aln.tmp
mv ORTHOGROUPS_SINGLE_GENE.${TYPE%.*}.aln.tmp ORTHOGROUPS_SINGLE_GENE.${TYPE%.*}.aln
echo "Build tree"
BOOTSTRAP_REPS=1000
THREADS=!{task.cpus}
TIP_DATE=0
### Use bootstrapping if you have more than 4 species
n_species=$(grep "^>" ORTHOGROUPS_SINGLE_GENE.${TYPE%.*}.aln | wc -l)
if [ ${n_species} -gt 4 ]
then
iqtree2 \
-s ORTHOGROUPS_SINGLE_GENE.${TYPE%.*}.aln \
-p alignment_parition.${TYPE%.*}.nex \
-B ${BOOTSTRAP_REPS} \
-T ${THREADS} \
--date !{dates} \
--date-tip ${TIP_DATE} \
--prefix ORTHOGROUPS_SINGLE_GENE.${TYPE%.*} \
--redo
else
iqtree2 \
-s ORTHOGROUPS_SINGLE_GENE.${TYPE%.*}.aln \
-p alignment_parition.${TYPE%.*}.nex \
-T ${THREADS} \
--date !{dates} \
--date-tip ${TIP_DATE} \
--prefix ORTHOGROUPS_SINGLE_GENE.${TYPE%.*} \
--redo
fi
if [ -f "ORTHOGROUPS_SINGLE_GENE.NT.timetree.nex" ]
then
echo "The time tree was successfully created!"
else
echo "Creating the time tree failed! Please check `dates.txt` for dating inconsistencies and/or remove divergence times at your discretion."
fi
echo "Cleanup"
rm OG*.fasta
rm single_gene_list.*
rm *-${TYPE%.*}.aln
echo "Output:"
echo " (01/12) ORTHOGROUPS_SINGLE_GENE.NT.best_scheme.nex"
echo " (02/12) ORTHOGROUPS_SINGLE_GENE.NT.best_scheme"
echo " (03/12) ORTHOGROUPS_SINGLE_GENE.NT.model.gz"
echo " (04/12) ORTHOGROUPS_SINGLE_GENE.NT.mldist"
echo " (05/12) ORTHOGROUPS_SINGLE_GENE.NT.bionj"
echo " (06/12) ORTHOGROUPS_SINGLE_GENE.NT.best_model.nex"
echo " (07/12) ORTHOGROUPS_SINGLE_GENE.NT.treefile"
echo " (08/12) ORTHOGROUPS_SINGLE_GENE.NT.iqtree"
echo " (09/12) ORTHOGROUPS_SINGLE_GENE.NT.timetree.nwk"
echo " (10/12) ORTHOGROUPS_SINGLE_GENE.NT.timetree.nex"
echo " (11/12) ORTHOGROUPS_SINGLE_GENE.NT.timetree.lsd"
echo " (12/12) ORTHOGROUPS_SINGLE_GENE.NT.ckp.gz"
'''
}
workflow {
IDENTIFY_SINGLE_GENE_ORTHOGROUPS(params.dir, params.dates) | \
ALIGN_SINGLE_GENE_ORTHOGROUPS | \
BUILD_TREE
}