-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDIALib-QC_RPlot.pl
305 lines (254 loc) · 11.7 KB
/
DIALib-QC_RPlot.pl
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/perl -w
###############################################################################
# $Id: DIALib-QC_RPlot.pl 5692 2020-04-24 22:00:00Z cmidha $
#
# SBEAMS is Copyright (C) 2000-2020 Institute for Systems Biology
# This program is governed by the terms of the GNU General Public License (GPL)
# version 2 as published by the Free Software Foundation. It is provided
# WITHOUT ANY WARRANTY. See the full description of GPL terms in the
# LICENSE file distributed with this software.
###############################################################################
###############################################################################
# This script is used to generate plots of spectral ion library characterstics
# after assessed by DIALib-QC.1.0 tool.
###############################################################################
use strict;
#die "Usage: array_it.pl library.fullstats" unless $ARGV[0];
#die "Usage: DIALib-QC_RPlot.pl library.fullstats library.RT" unless @ARGV;
die "Usage: DIALib-QC_RPlot.pl library.fullstats library.RT" || "Usage: DIALib-QC_RPlot.pl library.fullstats" unless (scalar@ARGV !=0);
system("rm -rf DIALibQC.log");
#Loading File
my $filename = $ARGV[0]."pdf";
#my $filename = $ARGV[0]."png"; ### uncomment if want png file
$filename =~ s/fullstats//;
print ($filename," is generating\n");
open(LOG, ">>", "DIALibQC.log");
print LOG $filename," is generating\n";
#Installing R packages to run R code
use Statistics::R;
my $R = Statistics::R->new();
$R->run( q`library(ggplot2)` );
$R->run( q`library(scales)` );
$R->run( q`install.packages("ggpubr")`);
$R->run( q`library(ggpubr)`);
$R->set( 'filename', $filename );
$R->run(q`pdf(file=filename, width=16, height=16)`);
#$R->run(q`png(file=filename, width = 465, height =465 , units='mm', res = 300)`); ### uncomment if want png file
##3 Plot C if RT file provided
if (-e $ARGV[1]){
open FIL2, $ARGV[1];
my @RTfile= <FIL2>;
my @rt2_value = ();
my @rt3_value = ();
foreach $_(@RTfile){
chomp($_);
print LOG ($_);
my ( $rt2, $rt3 ) = split( '\t', $_ );
chomp($rt3);
print LOG "RT $rt2\n";
push @rt2_value, $rt2;
push @rt3_value, $rt3;
}
foreach $_(@rt2_value){print LOG $_;}
foreach $_(@rt3_value){print LOG $_;}
my $rt_array_size = scalar(@rt2_value);
print LOG "RT ARRAY SIZE $rt_array_size\n";
$R->set( 'rt2_value', \@rt2_value );
$R->set( 'rt3_value', \@rt3_value );
$R->run( q`dfrt<- data.frame(RT2value = c(rt2_value) , RT3value = c(rt3_value))` );
$R->run( q`correlation = cor(dfrt$RT2value , dfrt$RT3value, method = "pearson")`);
$R->run( q`correlation = sprintf("%1f",correlation)`);
my $correlationval = $R->get('correlation');
print LOG "CORRELATIONVAL",$correlationval;
$R->run(q`N=NROW(dfrt)`);
my $nrowcount = $R->get('N');
print LOG "NROWCOUNT ",$nrowcount;
$R->run(q`title = paste0("Library +2/+3 pair RT correlation, n=",N, ", R2=",correlation)`);
my $title = $R->get('title');
print LOG "TITLE ",$title;
$R->run(q`p3<-ggplot(dfrt, aes(x = RT2value, y = RT3value))+
geom_point(shape = 1, size =4, color = '#008080')+
theme_bw()+ labs(title =title, x = "+2RT (mins)", y = "+3RT (mins)", size = 40)+
geom_smooth( method = "lm", color = 'black',alpha=0, size = .3 )`);
close FIL2;
}
open FIL, $ARGV[0] || die "library.fullstats is minimum file required to generate plots";
while ( my $line=<FIL> ) {
chomp $line;
my ( $key, $data ) = split( ':', $line );
#open OUT, ">$key.csv"; ##UNCOMMENT ME TO GENERATE INDIVIDUAL CSVS
my $i = 0;
my $j = 0;
my @item_array = ();
my @val_array = ();
for my $itemvalue ( split( ',', $data ) ) {
my ( $item, $val ) = split( '=', $itemvalue );
push @item_array, $item;
push @val_array, $val;
#print OUT join( ',', $item, $val ) . "\n"; ##UNCOMMENT ME TO GENERATE INDIVIDUAL CSVS
}
$R->set( 'Items', \@item_array );
$R->set( 'Values', \@val_array );
##p1 A PLOT
if ($key =~ m/PrecusorCounts/){
foreach $_(@val_array) {print LOG $_," precursor count\n";}
$R->run( q`totalfragions<-sum(Values)` );
$R->run(q` Values = round(Values*100/totalfragions, 2)`);
$R->run(q`df<-data.frame(Parameters_type=c(Items),Frequency=c(Values))`);
$R->run( q`minval<- min(Values)`);
$R->run( q`maxval<- max(Values)`);
$R->run(q`p1<-ggplot(df, aes(Parameters_type, Frequency))+ xlim(350,1300)+
theme_bw()+ geom_col(fill="#008080")+ xlab("Precusor m/z")+
ylab("Frequency in percent (%)") + ggtitle("Precusor m/z")`);
}
##p2 B POLT
#if ($key =~ m/^pre_z/){
# $R->run( q`totalfragions<-sum(Values)` );
# $R->run(q` Values = round(Values*100/totalfragions,2)`);
# $R->run(q`Items_char= as.character(Items)`);
# $R->run(q`df<-data.frame(Parameters_type=c(Items_char),Frequency=c(Values))`);
# $R->run(q`p2<-ggplot(df, aes(Parameters_type, Frequency))+ theme_bw()+
# geom_col(fill="#008080")+ xlab("Precursor charge")+
# geom_text(aes(label = Frequency), size = 6, hjust = 0.5, vjust = 0, position = "stack", colour = 'black')+
# ylab("Precursors in percent (%)")+ggtitle("Precursor charge")`);
# @item_array = @val_array = ();
#}
##p4 C/D PLOT
if ($key =~ m/stripped_len/){
$R->run( q`totalfragions<-sum(Values)` );
$R->run(q` Values = round(Values*100/totalfragions, 2)`);
$R->run(q`df<-data.frame(Parameters_type=c(Items),Frequency=c(Values))`);
$R->run(q`p4<-ggplot(df, aes(Parameters_type, Frequency))+ theme_bw()+
geom_col(fill="#008080")+ xlab("Peptide length") + ylab("Peptides in percent (%)")+
ggtitle("Peptide length")`);
}
##p5 E PLOT
if ($key =~ m/mods/){
my $null_index = 0;
for (my $i = 0; $i <@item_array; $i++){
if ($item_array[$i] =~ m/none/){
$null_index = $i;
}
}
my $any_index = 0;
for (my $i = 0; $i <@item_array; $i++){
if ($item_array[$i] =~ m/any/){
$any_index = $i;
}
}
print LOG $null_index,"\t",$val_array[$null_index],"\n" ;
splice @item_array, $null_index, 1;
splice @val_array, $null_index, 1;
splice @item_array, $any_index, 1;
splice @val_array, $any_index, 1;
$R->set( 'Items', \@item_array );
$R->set( 'Values', \@val_array );
$R->run( q`df<-data.frame(Parameters_type=c(Items),Frequency=c(Values))`);
$R->run(q`p5<-ggplot(df, aes(Parameters_type, Frequency))+theme_bw()+
geom_col(fill="#008080")+ xlab("Modification type")+
geom_text(aes(label = Frequency), size = 6, hjust = 0.5, vjust = 0, position = "stack", colour = 'black')+
ylab("Number of modifications")+ggtitle("Modifications")`);
}
##p6 F PLOT
if ($key =~ m/PepsPerProtein/){
my @first4value = ();
my @first4item = ();
my $val4abovesum = 0;
if (scalar(@item_array) <5){
$R->run(q`Items_char= as.character(Items)`);
$R->run(q`df<-data.frame(Parameters_type=c(Items_char),Frequency=c(Values))`);
$R->run(q`p6<-ggplot(df, aes(Parameters_type, Frequency))+
theme_bw()+ scale_x_discrete("Peptides per protein", limits =c(Items_char))+
geom_text(aes(label = Frequency), size = 6, hjust = 0.5, vjust = 0, position = "stack", colour = 'black')+
geom_col(fill="#008080") + ylab("Number of protiens")+
ggtitle("Peptides per protein") `);
}else{
for (my $i= 0; $i<4; $i++){
push @first4value, $val_array[$i];
push @first4item, $item_array[$i];
}
for (my $i = 4; $i< scalar@val_array; $i++){
$val4abovesum = $val4abovesum + $val_array[$i];
}
print LOG ($val4abovesum);
$first4value[4]=$val4abovesum;
$R->set( 'Items', \@first4item );
$R->run(q`Items_char= as.character(Items)`);
$R->run(q`Items_char= c(Items_char, ">5")`);
$R->set( 'Values', \@first4value );
$R->run(q`df<-data.frame(Parameters_type=c(Items_char),Frequency=c(Values))`);
$R->run(q`p6<-ggplot(df, aes(Parameters_type, Frequency))+
theme_bw()+ scale_x_discrete("Peptides per protein", limits =c(Items_char))+
geom_text(aes(label = Frequency), size = 6, hjust = 0.5, vjust = 0, position = "stack", colour = 'black')+
geom_col(fill="#008080") + ylab("Number of proteins")+
ggtitle("Peptides per protein")`);
}
}
##p7 G POLT
if ($key =~ m/FragmentsPerPrecursor/){
$R->run( q`totalfragions<-sum(Values)` );
$R->run(q` Values = round(Values*100/totalfragions, 2)`);
$R->run(q`Items_char= as.character(Items)`);
$R->run(q`df<-data.frame(Parameters_type=c(Items_char),Frequency=c(Values))`);
print LOG scalar(@item_array),"\n";
$R->run(q`p7<-ggplot(df, aes(Parameters_type, Frequency))+ theme_bw()+
geom_col(fill="#008080")+ scale_x_discrete("Fragments per precursor", limits =Items_char)+
geom_text(aes(label = Frequency), size = 3, hjust = 0.5, vjust = 0, position = "stack", colour = 'black')+
ylab("Frequency in percent (%)") + ggtitle("Fragments per precursor")`);
}
##p8 H PLOT
if ($key =~ m/frg_s/){
$R->run( q`totalfragions<-sum(Values)` );
$R->run(q` Values = signif(Values*100/totalfragions,digits = 1)`);
$R->run( q`df<- data.frame(Parameters_type = c(Items) , Frequency = c(Values))` );
$R->run(q`p8<-ggplot(df, aes(Parameters_type, Frequency))+ theme_bw()+
geom_col(fill="#008080")+ xlab("Fragment ion type") + ylab("Frequency in percent (%)")+
geom_text(aes(label = Frequency), size = 6, hjust = 0.5, vjust = 0, position = "stack", colour = 'black')+
ggtitle("Fragment ion")`);
}
##p9 I PLOT
if ($key =~ m/frg_z/){
foreach $_(@val_array){print LOG $_;}
$R->run( q`totalfragions<-sum(Values)` );
my $totalfragions = $R->get('totalfragions');
print LOG "totalfragions, $totalfragions";
$R->run(q`Values = round(Values*100/totalfragions,2)`);
$R->run(q`Items_char= as.character(Items)`);
$R->run(q`df<-data.frame(Parameters_type=c(Items_char),Frequency=c(Values))`);
$R->run(q`p9<- ggplot(df, aes(Parameters_type, Frequency))+
xlab("Fragment ion charge")+
theme_bw()+geom_col(fill="#008080")+
geom_text(aes(label = Frequency), size = 6, hjust = 0.5, vjust = 0, position = "stack", colour = 'black')+
scale_x_discrete("Fragment ion charge", limits =c(Items_char))+
ylab("Fragment ions in percent (%)") + ggtitle("Fragment ion charge") `);
}
###p10
# if ($key =~ m/frg_n/){
# $R->run( q`totalfragions<-sum(Values)` );
# $R->run(q` Values = round(Values*100/totalfragions,2)`);
# $R->run(q`df<-data.frame(Parameters_type=c(Items),Frequency=c(Values))`);
# $R->run(q`p10<-ggplot(df, aes(Parameters_type, Frequency))+ theme_bw()+
# geom_col(fill="#008080")+ xlab("Fragment ion") + ylab("Precursor in percent(%)")+
# ggtitle("Number of fragment ions per precursor")`);
# }
###p11 B PLOT
if ($key =~ m/Distinct_pre_z/){
$R->run( q`totalfragions<-sum(Values)` );
#$R->run(q` Values = signif(Values*100/totalfragions, digits =1)`);
$R->run(q` Values = round((Values*100/totalfragions),0)`);
#$R->run(q` Values = round(Values/1000,0)`);
$R->run(q`df<-data.frame(Parameters_type=c(Items),Frequency=c(Values))`);
$R->run(q`p11<-ggplot(df, aes(Parameters_type, Frequency))+ theme_bw()+
geom_col(fill="#008080")+ xlab("Precursor charge")+
geom_text(aes(label = Frequency), size = 6, hjust = 0.5, vjust = 0, position = "stack", colour = 'black')+
ylab("Precursors in percent(%)")+ggtitle("Precursor charger")`);
}
}
if (-e $ARGV[1]){
$R->run(q`ggarrange(p1, p11, p3, p4, p5, p6, p7, p8, p9 , labels = c("A", "B", "C", "D", "E", "F", "G", "H", "I"), ncol = 3, nrow = 3)`);
}else {
$R->run(q`ggarrange(p1, p11, p4, p5, p6, p7, p8, p9 , labels = c("A", "B", "C", "D", "E", "F", "G", "H"), ncol = 3, nrow = 3)`);
}
$R->run(q`dev.off()`);
#close OUT; ##UNCOMMENT ME TO GENERATE INDIVIDUAL CSVS
close LOG;