detection_peak.pl 11 KB
Newer Older
Benoit Valot's avatar
Benoit Valot committed
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!/usr/bin/perl -w

#Filtre le raw transform en .txt
#Ne fonctionne pas avec des zoom??

use strict;
use File::Basename;
use Wx;
use Wx qw(:filedialog wxID_CANCEL);

my $mass;
my @masses;
my $line;
my @elementline;
my $error;
my $attente = '0';

my $timemedian  = '1';
my $scanlimite  = '40';
my $timeminimun = '0.5';
my $massmax     = '1.5';
my $massmin     = '0.5';
my $endtreshold = '200000';
my $treshold    = '300000';

my $files;
my $dirin;
my $temp;
my $scantime;
my $intensity;
my %data     = ();
my %datatemp = ();
my %quant    = ();
my @scantimes;

eval {

	print STDOUT "Detection de peak\n\n";

#### Aller chercher des fichiers######################

	my ($this) = shift;
	my $opendialog = Wx::FileDialog->new(
		$this,    # Parent du Dialog, ici la Frame
		"Slectionn les fichiers raw tranform en txt",    # Titre du Dialog
		'', '',
		"txt files (*.txt)|*.txt|All files (*.*)|*.*",
		wxOPEN | wxMULTIPLE
	);

	( $opendialog->ShowModal != wxID_CANCEL ) || die("Analyse abandonnee");
	my @filestxt = $opendialog->GetPaths;
	my ( $filename, $dirname ) =
	  &File::Basename::fileparse( $opendialog->GetPath );
	$dirin = $dirname;
	$opendialog->Destroy;

#### Aller chercher un dossier de sortie ######################
	my $opendirdialog = Wx::DirDialog->new(
		$this,    # Parent du Dialog, ici la Frame
		"Slectionn le dossier dans lequel les rsultats seront enregistrs"
		,         # Titre du Dialog
		$dirin,
		wxOPEN
	);            # Flags de style

	( $opendirdialog->ShowModal != wxID_CANCEL ) || die("Analyse abandonnee");
	my $dirout = $opendirdialog->GetPath;
	$dirout .= "/";
	$opendirdialog->Destroy;
###########################

	print STDOUT "Les fichiers qui suis vont etre analyses :\n";
	foreach $files (@filestxt) {
		( -e $files ) || die( "on ne peut ouvrir le fichier " . $files );
		print STDOUT $files . "\n";
	}
	print STDOUT " \nles resultats seront enregistres dans :\n" . $dirout
	  . "\n\n";

	$error = '0';
	print STDOUT "Masses a detecter \n";
	while ( $error == 0 ) {
		$mass = <STDIN>;
		chomp $mass;
		if ( $mass and $mass =~ m/^\d+\.?\d*$/ ) {
			push( @masses, $mass );
		}
		else {
			$error += 1;
		}
	}
	print STDOUT "Analyse demarre\n\n";

####Fichier resultats combinant l'ensemble des peak dtecter et des fichiers
	open QUANFILE,
	  ">" . $dirout . "Quantification_peak_" . join( '_', @masses ) . ".txt"
	  || die "impossible d'ouvrir "
	  . "Quantification_peak_"
	  . join( '', @masses ) . ".txt";

###Parsage du fichier pour obtenir les donnes##########
	foreach $files (@filestxt) {
		%quant     = ();
		%data      = ();
		%datatemp  = ();
		@scantimes = ();
		open INFILE, "<" . $files
		  || die "impossible d'ouvrir " . $files;
		while ( $attente == 0 ) {
			$line = <INFILE>;
			chomp $line;
			if ( !$line ) {
				$attente += 1;
			}
		}
		while ( defined( $line = <INFILE> ) ) {
			@elementline = &parse($line);
			if (    @elementline gt 3
				and $elementline[0] =~ m/^Scan/
				and $elementline[3] =~ m/^\d+\.\d+\,$/ )
			{
				$scantime = $elementline[3];
				$scantime =~ s/\,//;
				if ( $scantime <= $scanlimite ) {
					push( @scantimes, $scantime );
				}
				$line = <INFILE>;

				#Rajouter test	sur ZOOM
				$line = <INFILE>;
				$line = <INFILE>;
				$line = <INFILE>;
				$line = <INFILE>;

				$attente   = '0';
				$intensity = '0';
				while ( $attente == 0 ) {
					$line        = <INFILE>;
					@elementline = &parsetabule($line);
					if ( @elementline gt 1 ) {
						foreach $mass (@masses) {
							if (    $elementline[1] >= ( $mass - $massmin )
								and $elementline[1] <= ( $mass + $massmax ) )
							{
								$datatemp{$mass}{$scantime}{'intensite'} +=
								  $elementline[2];
							}
						}
					}
					else {
						foreach $mass (@masses) {
							$datatemp{$mass}{$scantime}{'intensite'} +=
							  $intensity;
						}
						$attente += 1;
					}
				}
			}
		}
		my $tmp = $scanlimite + 0.1;
		push (@scantimes, $tmp);
		foreach $mass (@masses) {
			$datatemp{$mass}{$tmp}{'intensite'} = '0';
		}
		close INFILE;
		print STDOUT "partage terminer de " . basename($files) . "\n";
#####Parsage termin############

####Calcule des medians, des minimun, des noises
###et de la valeur final soustraite du noise#######
		my @datas = ();
		my @temps = @scantimes;
		my $key;
		foreach $mass (@masses) {
			foreach $scantime (@scantimes) {
				@datas = ();
				foreach $key (@temps) {
					if (    ( $key <= ( $scantime + $timemedian ) )
						and ( $key >= ( $scantime - $timemedian ) ) )
					{
						push( @datas, $datatemp{$mass}{$key}{'intensite'} );
					}
				}
				$temp = &median(@datas);
				$datatemp{$mass}{$scantime}{'median'} = $temp;
			}
		}

		foreach $mass (@masses) {
			foreach $scantime (@scantimes) {
				@datas = ();
				foreach $key (@temps) {
					if (    ( $key <= ( $scantime + $timeminimun ) )
						and ( $key >= ( $scantime - $timeminimun ) ) )
					{
						push( @datas, $datatemp{$mass}{$key}{'median'} );
					}
				}
				$temp = &min(@datas);
				$datatemp{$mass}{$scantime}{'min'} = $temp;
			}
		}

		foreach $mass (@masses) {
			foreach $scantime (@scantimes) {
				@datas = ();
				foreach $key (@temps) {
					if (    ( $key <= ( $scantime + $timeminimun ) )
						and ( $key >= ( $scantime - $timeminimun ) ) )
					{
						push( @datas, $datatemp{$mass}{$key}{'min'} );
					}
				}
				$temp = &max(@datas);
				$datatemp{$mass}{$scantime}{'noise'} = $temp;
			}
		}

		foreach $mass (@masses) {
			foreach $scantime (@scantimes) {
				$temp = $datatemp{$mass}{$scantime}{'intensite'} -
				  $datatemp{$mass}{$scantime}{'noise'};
				$data{$mass}{$scantime}{'intensite'}   = $temp;
				$data{$mass}{$scantime}{'originvalue'} =
				  $datatemp{$mass}{$scantime}{'intensite'};
			}
		}
###Vidage de la table de hache temporaire###
		%datatemp = ();
############################

########Recherche des peaks##########
		foreach $mass (@masses) {
			my $aire    = '0';
			my $timemax = '';
			@temps = @scantimes;
			my $keyback = shift(@temps);
			while ( $key = shift(@temps) ) {
				if ( $data{$mass}{$key}{'intensite'} >= $treshold ) {
					$aire += (
						( $key - $keyback ) * (
							$data{$mass}{$key}{'intensite'} +
							  $data{$mass}{$keyback}{'intensite'}
						)
					) / 2;
					$timemax = $key;
					$keyback = $key;
					$key     = shift(@temps);
					if ( $key and $data{$mass}{$key}{'intensite'} >= $treshold )
					{
						$aire += (
							( $key - $keyback ) * (
								$data{$mass}{$key}{'intensite'} +
								  $data{$mass}{$keyback}{'intensite'}
							)
						) / 2;
						if (    $key
							and $data{$mass}{$key}{'intensite'} >=
							$data{$mass}{$timemax}{'intensite'} )
						{
							$timemax = $key;
						}
						$attente = '0';
						while ( $attente == 0 ) {
							$keyback = $key;
							$key     = shift(@temps);
							$aire += (
								( $key - $keyback ) * (
									$data{$mass}{$key}{'intensite'} +
									  $data{$mass}{$keyback}{'intensite'}
								)
							) / 2;
							if ( $data{$mass}{$key}{'intensite'} >=
								$data{$mass}{$timemax}{'intensite'} )
							{
								$timemax = $key;
							}
							if ( $data{$mass}{$key}{'intensite'} <=
								$endtreshold )
							{
								$attente += 1;
								my $tmp = $aire * 60;
								$_ = sprintf( "%.0f", $tmp );
								$data{$mass}{$timemax}{'peak'} = $_;
								$quant{$timemax}{$mass}        = $_;
								$aire                          = '0';
								$timemax                       = '';
							}
						}

					}
					else {
						$keyback = $key;
						$aire    = '0';
						$timemax = '';
					}
				}
				else {
					$keyback = $key;
				}
			}
		}

####Impression des data brutes de dtection, un par fichier#####
		open OUTFILE,
		  '>' . $dirout . substr( basename($files), 0, -4 ) . "_data.txt"
		  || die "impossible d'crire le fichier data";
		foreach $mass (@masses) {
			print OUTFILE "\t" . $mass . "\t\t";
		}
		print OUTFILE "\n";
		foreach $scantime (@scantimes) {
			print OUTFILE $scantime . "\t";

			#			  		  . $datatemp{$mass}{$scantime}{'intensite'} . "\t"
			#			  		  . $datatemp{$mass}{$scantime}{'median'} . "\t"
			#			  		  . $datatemp{$mass}{$scantime}{'min'} . "\t"
			#			  		  . $datatemp{$mass}{$scantime}{'noise'} . "\t"

			foreach $mass (@masses) {
				print OUTFILE $data{$mass}{$scantime}{'originvalue'} . "\t";
				print OUTFILE $data{$mass}{$scantime}{'intensite'} . "\t";
				if ( $data{$mass}{$scantime}{'peak'} ) {
					print OUTFILE $data{$mass}{$scantime}{'peak'} . "\t";
				}
				else {
					print OUTFILE "\t";
				}
			}
			print OUTFILE "\n";
		}
		close OUTFILE;

##Ecriture des donnes combinant tout les Lc-MS/MS et les dtections####
		print QUANFILE basename($files) . "\n";
		print QUANFILE "\t";
		foreach $mass (@masses) {
			print QUANFILE "\t" . $mass;
		}
		print QUANFILE "\n";

		@temps = sort { $a <=> $b } keys(%quant);
		foreach $scantime (@temps) {
			print QUANFILE "\t" . $scantime;
			foreach $mass (@masses) {
				if ( $quant{$scantime}{$mass} ) {
					print QUANFILE "\t" . $quant{$scantime}{$mass};
				}
				else {
					print QUANFILE "\t";
				}
			}
			print QUANFILE "\n";
		}

		print STDOUT "Calcule terminer de " . basename($files);
		print STDOUT "\n\n";
	}
	close QUANFILE;
	print STDOUT "Analyse terminee";
	<STDIN>;
};
if ($@) {
	my $error = $@;

	#	$error =~ s/at .*\.$//s;
	print STDOUT $error;
	<STDIN>;
}

############################################
#sous-routine qui parse un fichier txt tabule
sub parse() {
	my ($string) = @_;
	my @strings;
	if ($string) {
		chomp $string;
		@strings = split( / /, $string, -1 );
	}
	else {
		@strings = ();
	}
	return (@strings);
}

#sous-routine qui parse un fichier txt tabule
sub parsetabule() {
	my ($string) = @_;
	my @strings;
	if ($string) {
		chomp $string;
		$string =~ s/ //g;
		@strings = split( /\t/, $string, -1 );
	}
	else {
		@strings = ();
	}
	return (@strings);
}

##Renvoie la valeur mediane
sub median() {
	my @string       = @_;
	my $nombremedian = "";
	my $median       = "";
	my $min          = "";
	my $max          = "";
	my @medians      = sort { $a <=> $b } @string;
	my $nombre       = @medians;
	if ( $nombre =~ m/[1,3,5,7,9]$/ ) {
		$nombremedian = $nombre / 2;
		$nombremedian += 0.5;
		my $error = '1';
		while ( $error <= $nombremedian ) {
			$median = shift(@medians);
			$error += 1;
		}
	}
	else {
		$nombremedian = $nombre / 2;
		my $error = '1';
		while ( $error <= $nombremedian ) {
			$min = shift(@medians);
			$error += 1;
		}
		$max    = shift(@medians);
		$median = ( $min + $max ) / 2;
	}
	return ($median);
}

#renvoie la valeur min
sub min() {
	my @string = @_;
	my @mins   = sort { $a <=> $b } @string;
	my $min    = shift(@mins);
	return ($min);
}

#renvoie la valeur max=noise
sub max() {
	my @string = @_;
	my @maxs   = sort { $b <=> $a } @string;
	my $max    = shift(@maxs);
	return ($max);
}