forked from riverlee/MitoSeek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circoswrap.pm
254 lines (204 loc) · 7.31 KB
/
Circoswrap.pm
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
#!/usr/bin/perl
#############################################
#Original Author: Jiang Li
#Email: riverlee2008@gmail.com
#Vanderbilt Center for Quantitative Sciences
#############################################
#Edited By: Andy Zeng
#Email: azeng95@gmail.com
#Date: October 21st, 2014
#BC Genome Sciences Centre
#############################################
use strict;
use warnings;
use FindBin;
use Cwd;
package Circoswrap;
#Constructor
sub new{
my($class) = @_;
my $currentdir=Cwd::getcwd;
my $self={
_build=>"rCRS",
_circosbin=>"$FindBin::Bin/Resources/circos-0.56/bin/circos", #where is the circos program
_karyotype=>"$FindBin::Bin/Resources/hg19_karyotype_MT.txt", #Tell where is the karyotype file of mitochondrial genome
_genehighlight=>"$FindBin::Bin/Resources/hg19_genes_MT.highlights.txt", #genes in mitochondrial, by highlight
_genetext=>"$FindBin::Bin/Resources/hg19_genes_MT.text.txt", #genes plot in text
_configtemplate=>"$FindBin::Bin/Resources/circos-heteroplasmy.template.conf", #default is the template for heteroplasmy plot
_circosoutput=>"circos-mitoseek.png", #circos output picture file name
_configoutput=>"circos-mitoseek.conf", #circos conf file output
_textoutput=>"circos-mitoseek.text.txt", #circos text data
_scatteroutput=>"circos-mitoseek.scatter.txt", #circos scatter data
_datafile=>undef, #The file store the heteroplasmy result, will parse it into circos data format
_cwd=>$currentdir #The output directory
};
bless $self,$class;
return $self;
}
sub changeconfig{
my ($self,$var) = @_;
if(defined($var)){
if($var eq "somatic"){
$self->{_configtemplate} = "$FindBin::Bin/Resources/circos-somatic.template.conf";
}elsif($var eq "heteroplasmy"){
$self->{_configtemplate} = "$FindBin::Bin/Resources/circos-heteroplasmy.template.conf";
}elsif($var eq "heterodiff"){
$self->{_configtemplate} = "$FindBin::Bin/Resources/circos-heterodiff.template.conf";
}else{
print ERROR "Undefined template, values should be either 'somatic' or 'heteroplasmy' or 'heterodiff'\n";
exit(1);
}
}
return $self->{_configtemplate};
}
sub plot{
my ($self)=@_;
my $command="cd ".$self->{_cwd}.";".
$self->{_circosbin}." -conf ".$self->{_configoutput}. ">/dev/null 2>&1";
# print $command;
system($command);
}
sub _formatnumeric{
my ($i) = @_;
my $r=sprintf("%.3f",$i);
if($r eq '0.000' && $i!=0){#use scientific notation
$r=sprintf("%.3e",$i);
}
return $r;
}
#Copy the templateconf and change the into .text and .scatter
#
sub prepare{
my ($self,$var) = @_;
if(!defined($var)){
print ERROR "Undefined value, values should be either 'somatic' or 'heteroplasmy' or 'heterodiff'\n";
exit(1);
}elsif ( $var ne "somatic" && $var ne "heteroplasmy" && $var ne "heterodiff" ){
print ERROR "'$var' is not acceptable, values should be either 'somatic' or 'heteroplasmy' or 'heterodiff'\n";
exit(1);
}
#copy confi file
open(IN,$self->{_configtemplate}) or die $!;
open(OUT,">".$self->{_cwd}."/".$self->{_configoutput}) or die $!;
my $str="";
while(<IN>){
$str.=$_;
}
close IN;
#pattern replace
$str=~s/replacecircosoutput/$self->{_circosoutput}/g;
$str=~s/replacegenehighlight/$self->{_genehighlight}/g;
$str=~s/replacegenetext/$self->{_genetext}/g;
$str=~s/replacescatter/$self->{_scatteroutput}/g;
$str=~s/replacetext/$self->{_textoutput}/g;
$str=~s/replacekaryotype/$self->{_karyotype}/g;
print OUT $str;
close OUT;
#Convert result into circos result
if($var eq "heteroplasmy"){
open(IN,$self->{_datafile}) or die $!; <IN>; #skip header
open(TEXT,">".$self->{_cwd}."/".$self->{_textoutput}) or die $!;
open(SCATTER,">".$self->{_cwd}."/".$self->{_scatteroutput}) or die $!;
while(<IN>){
my($chr,$pos,$ref,$alt,$forward_A,$forward_T,$forward_C,$forward_G,$reverse_A,$reverse_T,$reverse_C,$reverse_G, $variant,
$hetero,undef)=split "\t";
my $text="$pos:$ref->$alt";
print SCATTER join "\t",("MT",$pos,$pos,$hetero);
print SCATTER "\n";
print TEXT join "\t",("MT",$pos,$pos,$text);
print TEXT "\n";
}
close IN;
close TEXT;
close SCATTER;
}else{
#Convert result into circos result
open(IN,$self->{_datafile}) or die $!; <IN>; #skip header
open(TEXT,">".$self->{_cwd}."/".$self->{_textoutput}) or die $!;
while(<IN>){
my($chr,$pos,$ref,$tumorGeno,$tumorDP,$tumorhetero,$normalGeno,$normalDP,$normalhetero,$gene,$geneD,undef)=split "\t";
my $heterodiff = _formatnumeric(abs( $tumorhetero - $normalhetero ));
my $text="$normalGeno->$tumorGeno:$heterodiff";
print TEXT join "\t",("MT",$pos,$pos,$text);
print TEXT "\n";
}
close IN;
close TEXT;
}
}
#get or change circos program
sub circosbin{
my ($self,$var) = @_;
$self->{_circosbin} = $var if defined($var);
return $self->{_circosbin};
}
sub cwd{
my ($self,$var) = @_;
if(defined($var)){
mkdir $var or die $! if (! -d $var);
$self->{_cwd} = $var;
}
return $self->{_cwd};
}
sub circosoutput{
my ($self,$var) = @_;
$self->{_circosoutput} = $var if defined($var);
return $self->{_circosoutput};
}
sub configoutput{
my ($self,$var) = @_;
$self->{_configoutput} = $var if defined($var);
return $self->{_configoutput};
}
sub textoutput{
my ($self,$var) = @_;
$self->{_textoutput} = $var if defined($var);
return $self->{_textoutput};
}
sub scatteroutput{
my ($self,$var) = @_;
$self->{_scatteroutput} = $var if defined($var);
return $self->{_scatteroutput};
}
#set the build or get the build info
#Will auto change the karyotype and gene files
sub build{
my ($self,$var) = @_;
if(defined($var)){
if($var ne "hg19" && $var ne "rCRS"){
warn "Only 'hg19' and 'rCRS' are accepted for mitochondria genome, will use 'rCRS' as default\n";
}else{
$self->{_build}=$var;
$self->{_karyotype}="$FindBin::Bin/Resources/".$var."_karyotype_MT.txt";
$self->{_genehighlight}="$FindBin::Bin/Resources/".$var."_genes_MT.highlights.txt";
$self->{_genetext}="$FindBin::Bin/Resources/".$var."_genes_MT.text.txt";
}
}
return $self->{_build};
}
sub karyotype{
my ($self,$var) = @_;
$self->{_karyotype}=$var if (defined($var));
return $self->{_karyotype};
}
sub genehighlight{
my ($self,$var) = @_;
$self->{_genehighlight}=$var if (defined($var));
return $self->{_genehighlight};
}
sub genetext{
my ($self,$var) = @_;
$self->{_genetext}=$var if (defined($var));
return $self->{_genetext};
}
sub configtemplate{
my ($self,$var) = @_;
$self->{_configtemplate}=$var if (defined($var));
return $self->{_configtemplate};
}
sub datafile {
my ($self,$var) = @_;
$self->{_datafile}=$var if (defined($var));
return $self->{_datafile};
}
1;