-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_cdxml_v6-1.pl
281 lines (255 loc) · 8.44 KB
/
parse_cdxml_v6-1.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
#! /usr/bin/perl -w
use Data::Dumper;
use Getopt::Long;
## Author: Rahil Taujale
## Date: 09/19/2016
## Version: 0.6
## Change log:
## Ver 0.4 :
## Added --OMe yes/no option
## Removed --ppm option
## --nO and --nC are not required options but --OMe is
## --dbr option can read in decimal values from user
## Ver 0.5
## Added --frag and related options
## Allows addition of multiple fragmentation patterns in the second level to the output mfql scripts
## Fixed dbr formula
## Ver 0.6
## Changes charge (+ or -) on the SUCHTHAT line based on chemical formula
## Added compatibility for nitrogen in chemical formula
## Fixed CHRG value printed out
## List of options:
## --in <infile> --out <outfile> --name <name> --code <code> --dbr auto/[1-2] --nO # --nC # --mode neg/pos2H/posNa --OMe yes/no
## --frag <fragfile1>,<fragfile2>,.. --fragcode CODE1,CODE2,.. --fragnO #,#,.. --fragnC #,#,.. --fragdbr auto/[1-2],[2-3],...
#
## Command line Sample run for script:
## Without fragmentation info:
## perl parse_cdxml_v6.pl --in Test1.cdxml --out Test1OK --name NAME --code CODE --dbr auto --nO 0 --nC 0 --mode neg --OMe no
## perl parse_cdxml_v6.pl --in *.cdxml --out *.mfql --name NAME --code CODE --dbr auto --nO 0 --nC 0 --mode neg --OMe no
## With fragmentation info added:
## perl parse_cdxml_v6.pl --in Test1.cdxml --out Test1OK --name NAME --code CODE --dbr auto --nO 0 --nC 0 --mode neg --OMe no --frag Frag1.cdxml --fragcode CODE1 --fragdbr auto --fragOMe No
## With multiple fragmentation info added:
## perl parse_cdxml_v6.pl --in Test1.cdxml --out Test1OK --name NAME --code CODE --dbr auto --nO 0 --nC 0 --mode neg --OMe no --frag Frag1.cdxml,Frag2.cdxml,Frag3.cdxml --fragcode CODE1,CODE2,CODE3 --fragdbr auto --fragOMe No,No,No
my $in = '';
my $out = '';
my $name = '';
my $code = '';
my $dbr = 'auto';
my $nO = '';
my $nC = '';
my $mode = 'neg';
my $OMe = '';
my $frag = '';
my $fragcode = '';
my $fragdbr = 'auto';
my $fragOMe = '';
my $help = '';
GetOptions (
"in=s" => \$in,
"out=s" => \$out,
"name=s" => \$name,
"code=s" => \$code,
"dbr=s" => \$dbr,
"nO=i" => \$nO,
"nC=i" => \$nC,
"mode=s" => \$mode,
"help" => \$help,
"OMe=s" => \$OMe,
"frag=s" => \$frag,
"fragcode=s" => \$fragcode,
"fragdbr=s" => \$fragdbr,
"fragOMe=s" => \$fragOMe,
)or die "Invalid arguments!";
if ($help){Usage();}
die "Missing --in!\nUse perl $0 --help for more details.\n" unless $in;
die "Missing --out!\nUse perl $0 --help for more details.\n" unless $out;
die "Missing --name!\nUse perl $0 --help for more details.\n" unless $name;
die "Missing --code!\nUse perl $0 --help for more details.\n" unless $code;
die "Missing --OMe!\nUse perl $0 --help for more details.\n" unless $OMe;
sub Usage{
print "\nUsage:\n\tperl $0 [--options]\n";
print "Options:\n";
print "\t--in Input cdxml file\n\t--out Output filename\n\t--name Name of compound\n";
print "\t--code Code for LipidXplorer\n";
print "\t--nO no. of Oxygen less\n\t--nC no. of Carbon less\n";
print "\t--dbr (optional)Default:auto; Enter in format 1-2\n";
print "\t--mode (optional)Default:neg\n";
print "\t neg for Negative mode\n";
print "\t pos2H for Positive mode with 2 hydrogen addition\n";
print "\t posNa for Positive mode with 1 hydrogen and 1 sodium ions\n";
print "\t--OMe Is Methoxy group present? (Enter Yes or No)\n";
print "\t--frag (optional) Input fragment file(s) separated by commas\n";
print "\t Needs to be accompanied by same number of fragcode,fragdbr and fragOMe options\n";
print "\t--fragcode Input fragment code(s) separated by commas\n";
print "\t--fragdbr Input fragment dbr separated by commas\n";
print "\t--fragOMe Is Methoxy group present in fragment? (Enter Yes or No separated by commas as above)\n";
exit;
}
### GetVal here
($d1,$d2,$val1,$chrg1,$ch,$val2,$valC,$valO,$check)=GetValues($in,$dbr,$OMe);
## $def
$def=GetDefine($code,$d1,$d2,\%$val1,$chrg1,$ch,\%$val2,$check,$mode);
## $ident
$ident = "\nIDENTIFY\n\t# mark $code precursor mass\n";
$ident .= "\t$code IN MS1$ch";
## $suchthat
$suchthat = "\n\nSUCHTHAT\n";
$suchthat .= "\t$code.chemsc[O] -".$$val1{'O'}." + $nO >= $code.chemsc[C] - ".$$val1{'C'}." + $nC\n";
## $report
$report = "\nREPORT \n";
$report .= "\tMASS = \"%4.4f\" % ($code.mass);\n";
$report .= "\tERROR = \"%2.2fppm\" % ($code.errppm);\n";
$report .= "\tSUMCOMPOSITION = $code.chemsc;\n";
$report .= "\tNAME = \"$code [%d:%d:%d]\" % ($code.chemsc[C] - $valC, $code.chemsc[db] - $d1, $code.chemsc[O] - $valO);\n";
$report .= "\tINTENS = sumIntensity($code.intensity);";
if ($frag){
@fr=split(/,/,$frag);
@frcode=split(/,/,$fragcode);
@frdbr=split(/,/,$fragdbr);
@frOMe=split(/,/,$fragOMe);
$fraglines="";
for($i=0;$i<=$#fr;$i++){
$fc=$frcode[$i];
if ($fragdbr=~/^auto$/){
$frdbr[$i]='auto';
}
($d1,$d2,$val1,$chrg1,$ch,$val2,$valC,$valO,$check)=GetValues($fr[$i],$frdbr[$i],$frOMe[$i]);
$def.=GetDefine($fc,$d1,$d2,\%$val1,$chrg1,$ch,\%$val2,$check,$mode);
$ident .=" AND\n\t$fc IN MS2$ch";
$j=$i+1;
$fraglines.="\n\n\t\tMASSFrag$j = \"%4.4f\" % ($fc.mass);\n";
$fraglines.="\t\tNAMEFrag$j = \"$fc [%d:%d:%d]\" % ($fc.chemsc[fragC] - $valC, $fc.chemsc[fragdb] - $d1, $fc.chemsc[fragO] - $valO);\n"; ### Do we need OMe information???
$fraglines.="\t\tINTENSFrag$j = sumIntensity($fc.intensity);\n";
$fraglines.="\t\tDifference$j = (($code.mass) - ($fc.mass));\n";
$fraglines.="\t\tSUMCOMPOSITIONFrag$j = ($code.chemsc - $fc.chemsc);\n";
$fraglines.="\t\tERRORFrag$j = \"%2.2fppm\" % ($fc.errppm);";
}
}
open (OUT,">$out");
print OUT "# Identify $name with checking the precursor mass, $name #\n\n";
print OUT "QUERYNAME = $name;\n\n";
print OUT $def;
print OUT $ident;
print OUT $suchthat;
print OUT $report;
if ($frag){print OUT $fraglines;}
print OUT ";\n\n################ end script ##################\n";
###############################################
################### Functions #####################
sub GetValues {
my ($name,$code,$d1,$d2,%val1,$chrg1,$ch,%val2,$valC,$valO,$check,@form,$c);
my ($in, $dbr, $OMe) = @_;
open(IN,$in);
while(<IN>){
chomp;
if ($_=~/>Chemical Formula/){
@a=split(/>/,$_);
foreach $b(@a){
if ($b!~/"/){
$b=~s/<\/s/\//g;
$c .= $b;
}
}
if ($c=~/\/\s*$/){
$c=~s/\/\s*$//;
}
push(@form,$c);
}
$c="";
}
close IN;
if ($dbr eq 'auto'){
print "";
}else{
if($dbr=~/^[\d.]+\-[\d.]+$/){
@dbrarr=split(/-/,$dbr);
$d1=$dbrarr[0];
$d2=$dbrarr[1];
}else{
die "Please enter dbr in format --dbr 1-2";
}
}
my $i=0;
foreach $comp(@form){
$i++;
@d=split(/\//,$comp);
if ($d[-1]!~/[-+]/){
die "Charge information not found in $in file. Please check the line with \">Chemical Formula\" in cdxml file.";
}else{
$chrg1=$d[-1];
}
$ch=substr($chrg1,0,1);
if ($i==1){
for ($j=1;$j<$#d;$j++){
if ($d[$j]=~/^[A-Z]$/i){
$val1{$d[$j]}=$d[$j+1];
}elsif ($d[$j]=~/[A-Z][A-Z]+/i){
@p=split(//,$d[$j]);
for ($k=0;$k<$#p;$k++){
$val1{$p[$k]}=1;
}
$val1{$p[-1]}=$d[$j+1];
}
}
}elsif ($i==2){
for ($j=1;$j<$#d;$j++){
if ($d[$j]=~/^[A-Z]$/i){
$val2{$d[$j]}=$d[$j+1];
}elsif ($d[$j]=~/[A-Z][A-Z]+/i){
@p=split(//,$d[$j]);
for ($k=0;$k<$#p;$k++){
$val2{$p[$k]}=1;
}
$val2{$p[-1]}=$d[$j+1];
}
}
}
if ($dbr eq 'auto'){
$d1=$val1{'C'}-$val1{'H'}/2 +1;
$d2=$d1 + 1;
}
}
$OMe=lc($OMe);
if ($OMe eq 'yes'){
$valC=$val1{'C'}-1;
$valO=$val1{'O'}-1;
}elsif ($OMe eq 'no'){
$valC=$val1{'C'};
$valO=$val1{'O'};
}
$check=$#form;
return ($d1,$d2,\%val1,$chrg1,$ch,\%val2,$valC,$valO,$check);
}
sub GetDefine {
my ($code,$d1,$d2,$val1,$chrg1,$ch,$val2,$check,$mode) = @_;
my %val1=%$val1;
my %val2=%$val2;
my ($def, $def2);
$def= "DEFINE $code = '";
if ($check==1){
$def.="C[".$val1{'C'}."..$val2{'C'}"."] H[".$val1{'H'}."..$val2{'H'}";
$def2="] O[".$val1{'O'}."..".$val2{'O'}."]";
}else{
$def.="C[".$val1{'C'}."..$val1{'C'}"."] H[".$val1{'H'}."..$val1{'H'}";
$def2="] O[".$val1{'O'}."..".$val1{'O'}."]";
}
if ($mode eq 'neg'){
$def.=$def2;
}elsif ($mode eq 'pos2H'){
$def.="+2".$def2;
}elsif ($mode eq 'posNa'){
$def.="+1".$def2." Na[1]";
}else{
print "Please enter either pos2H, posNa or neg for option --mode\nDefault is neg\nUse perl $0 --help for more details.\n";
exit;
}
if (defined ($val1{'N'})){
if ($check==1){
$def.=" N[".$val1{'N'}."..$val2{'N'}"."]";
}else{
$def.=" N[".$val1{'N'}."..$val1{'N'}"."]";
}
}
$def.="' WITH DBR = ($d1,$d2), CHG = ".$ch.length($chrg1).";\n";
return ($def);
}