-
Notifications
You must be signed in to change notification settings - Fork 0
/
DomainPlotter
282 lines (243 loc) · 6.26 KB
/
DomainPlotter
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/perl
# Domain plotting for candidates CDS
# Wyler Michele, October 31th 2020
use warnings;
use strict;
use English;
use File::Temp qw/ tempdir /;
use Getopt::Long 'HelpMessage';
use File::Basename;
use Cwd;
use POSIX;
### INPUT -------------------------------------------------------------
# getwd
my $dir = getcwd . "/";
# test input on the command line
GetOptions(
'Fasta=s' => \my $FASTA,
'outPrefix=s' => \my $PREFIX,
'help' => sub { HelpMessage(0) },
) or HelpMessage(1);
# check presence of mandatory inputs
HelpMessage(1) unless $FASTA;
=head1 NAME
Annotation tester
=head1 SYNOPSIS
--Fasta, -F Genes of interest in nucl fasta format (required)
--outPrefix, -o Prefix of the output files, allows redirection (optional)
Help
--help, -h Print this help
=head1 VERSION
0.01
=cut
# define hash with codons
my %CodonTable = ('TTT' => 'F',
'TCT' => 'S',
'TAT' => 'Y',
'TGT' => 'C',
'TTC' => 'F',
'TCC' => 'S',
'TAC' => 'Y',
'TGC' => 'C',
'TTA' => 'L',
'TCA' => 'S',
'TAA' => 'X',
'TGA' => 'X',
'TTG' => 'L',
'TCG' => 'S',
'TAG' => 'X',
'TGG' => 'W',
'CTT' => 'L',
'CCT' => 'P',
'CAT' => 'H',
'CGT' => 'R',
'CTC' => 'L',
'CCC' => 'P',
'CAC' => 'H',
'CGC' => 'R',
'CTA' => 'L',
'CCA' => 'P',
'CAA' => 'Q',
'CGA' => 'R',
'CTG' => 'L',
'CCG' => 'P',
'CAG' => 'Q',
'CGG' => 'R',
'ATT' => 'I',
'ACT' => 'T',
'AAT' => 'N',
'AGT' => 'S',
'ATC' => 'I',
'ACC' => 'T',
'AAC' => 'N',
'AGC' => 'S',
'ATA' => 'I',
'ACA' => 'T',
'AAA' => 'K',
'AGA' => 'R',
'ATG' => 'M',
'ACG' => 'T',
'AAG' => 'K',
'AGG' => 'R',
'GTT' => 'V',
'GCT' => 'A',
'GAT' => 'D',
'GGT' => 'G',
'GTC' => 'V',
'GCC' => 'A',
'GAC' => 'D',
'GGC' => 'G',
'GTA' => 'V',
'GCA' => 'A',
'GAA' => 'E',
'GGA' => 'G',
'GTG' => 'V',
'GCG' => 'A',
'GAG' => 'E',
'GGG' => 'G',
'GTN' => 'V',
'TCN' => 'S',
'CCN' => 'P',
'ACN' => 'T',
'GCN' => 'A',
'GGN' => 'G',
'CGN' => 'R');
### Define SUBROUTINES -------------------------------------------------------------
# converts DNA hash to AA hash
sub DNAtoAA {
# input need to be a hash containing DNA fasta
my %DNAhash = @_ ;
my %AAhash ;
# translate each sequence
foreach my $header (keys %DNAhash){
my $DNAsequence = $DNAhash{$header};
$DNAsequence = uc($DNAsequence);
# get length of the sequence
my $GeneLen = length $DNAsequence;
# take one triplet after the other
for (my $i=0; $i <= $GeneLen; $i += 3){
my $triplet = substr($DNAsequence, $i, 3); # 3 nucleotides
if(exists($CodonTable{"$triplet"})) { # remove last nucleotides
$AAhash{"$header"} .= $CodonTable{"$triplet"};
}
}
}
return %AAhash ;
}
# check presence of Pfam dataset and download in case
sub checkPfam{
unless (-e 'Pfam-A.hmm') {
print "No Pfam-A.hmm file detected. \nTry to download and press.\n";
system 'rm -f Pfam-A.hmm.h3p; rm -f Pfam-A.hmm.h3m ; rm -f Pfam-A.hmm.h3f ; rm -f Pfam-A.hmm.h3i';
system 'wget --quiet ftp://ftp.ebi.ac.uk/pub/databases/Pfam/releases/Pfam33.0/Pfam-A.hmm.gz -O Pfam-A.hmm.gz';
if (-e 'Pfam-A.hmm.gz') {
system 'gunzip Pfam-A.hmm.gz';
system 'hmmpress Pfam-A.hmm';
}else{
print "ERROR: Unable to donwload the Pfam database.\n";
print "Check the internet connection or copy manually the DB in the current folder.\n";
exit;
}
}
}
### WORKER DEPENDENCY and INPUTS -------------------------------------------------------------
# test if user want redirection of output
if (! defined $PREFIX){
$PREFIX = $dir;
}
# test if hmm is installed
if ( (`hmmsearch`)[1] !~ /^Usage/ ) {
print "hmmer seems not installed.\n";
print "Try 'sudo apt-get install hmmer'.\n";
}
# test if Pfam files are available
&checkPfam;
### Read in Genes
## read input sequence inside an HASH (geneID "\t" fasta)
# unzip if needed
if ($FASTA =~ /.gz$/) {
open(IN, "gunzip -c $FASTA |") or die "can't open $FASTA";
}
else {
open(IN, $FASTA) or die "can't open $FASTA";
}
## read input sequence inside an hash
my %seqs = ();
my $header = '';
while (my $line = <IN>){
chomp $line;
$line =~ s/^\s*$//;
if ($line =~ m/^>(.*)$/){
# remove description from header (after space)
$header = (split ' ', $1)[0];
} else {
$seqs{"$header"} .= $line;
}
}
close (IN);
## change frames
my %AA6frame;
my $Header;
my $Sequence;
# forward frames
foreach my $FRAME (1, 2, 3){
while ( ($Header, $Sequence) = each %seqs) {
my $FrameSeq = substr($Sequence, $FRAME-1);
my $FrameHeader = $Header . '_' . $FRAME;
#print ">$FrameHeader\n$FrameSeq\n";
$AA6frame{"$FrameHeader"} = $FrameSeq;
}
}
# reverse frames
foreach my $FRAME (1, 2, 3){
while ( ($Header, $Sequence) = each %seqs) {
# reverse
$Sequence = reverse $Sequence;
# complement
$Sequence =~ tr/ACGTacgt/TGCAtgca/;
my $FrameSeq = substr($Sequence, $FRAME-1);
my $FrameHeader = $Header . '_-' . $FRAME;
#print ">$FrameHeader\n$FrameSeq\n";
$AA6frame{"$FrameHeader"} = $FrameSeq;
}
}
%AA6frame = &DNAtoAA (%AA6frame) ;
## print out all into temp folder
my $TEMPfolderOutput = tempdir( DIR => $dir, CLEANUP => 1 );
while ( ($Header, $Sequence) = each %AA6frame) {
my $FILE = $TEMPfolderOutput . '/fastaAA.fa' ;
open(FH, '>>', $FILE) or die $!;
print FH ">$Header\n$Sequence\n";
close(FH);
}
## Run domain Scan
my $HMMOUT = $TEMPfolderOutput . '/candidate.hmm';
system "hmmsearch --domtblout $HMMOUT -E 1e-5 --cpu 1 Pfam-A.hmm $TEMPfolderOutput/fastaAA.fa";
# parse HMM results
# fgrep -v '#' candidate.hmm | awk '{print $1,$3,$4,$19,$20}'
my @HMMtable;
my $HMM_header = "geneName\tgeneLength\tDomain\tStart\tEnd";
push(@HMMtable, $HMM_header);
open(IN, $HMMOUT ) or die "can't open $HMMOUT";
while(<IN>){
# don't keep lines with comments
if ($_ !~ m/^#/){
$_ =~ s/ +/\t/g;
# make one array for each row
my @HMMrow = (split ' ', $_);
my $ParseHMMrow = "$HMMrow[0]\t$HMMrow[2]\t$HMMrow[3]\t$HMMrow[17]\t$HMMrow[18]";
push(@HMMtable, $ParseHMMrow);
}
}
close(IN);
# print out coordinates
my $DomCoordinates = $PREFIX . 'DomainCoordinates.txt';
open(FH, '>', $DomCoordinates) or die $!;
foreach (@HMMtable) {
print FH "$_\n";
}
close(FH);
# plot
my $SCRIPTPATH = dirname($0);
my $PlotName = $PREFIX . "DomainPlot.pdf";
system "Rscript $SCRIPTPATH/subScriptPlotting $DomCoordinates $PlotName";