File: CM_plotter.pl

package info (click to toggle)
microbiomeutil 20101212%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 49,268 kB
  • ctags: 353
  • sloc: perl: 4,878; ansic: 419; makefile: 94; sh: 27
file content (90 lines) | stat: -rwxr-xr-x 1,752 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/env perl

use strict;
use warnings;

my $usage = "usage: $0 CM.output\n\n";

my $CM_file = $ARGV[0] or die $usage;

open (my $fh, $CM_file) or die "Error, cannot open $CM_file";

while (<$fh>) {
	chomp;
	
	unless (/^ChimeraMaligner/) { next; }
	
	my @x = split(/\t/);
	
	unless ($x[2] eq "YES") { next; }

	my $acc = $x[1];

	my @match_files;

	my @y_coords;
	
	for (my $i = 3; $i <= $#x; $i++) {
		
		my $match_line = $x[$i];
		
		my $match_file = "$$.matches.$i.dat";
		push (@match_files, $match_file);
		open (my $ofh, ">$match_file") or die "Error, cannot write file $match_file";
		
		my @matches = split(/;/, $match_line);
		
		
		
		foreach my $match (@matches) {
			
			$match =~ /\((\d+)-(\d+),\s+\S+,\s+G:\S+,\s+L:(\d+)/ or die "Error, no regex match for $match";
			my $lend = $1;
			my $rend = $2;
			my $per_ID = $3;

			print $ofh "$lend\t$per_ID\n$rend\t$per_ID\n\n";
		
			push (@y_coords, $per_ID);
		}
		close $ofh;
	}

	# write gnuplot script
	open (my $gscript_ofh, ">gnuscript.$$.txt") or die "Error, cannot write gnuplot script";
	
	print $gscript_ofh "set term png color\n";
	my $ofile = $acc;
	$ofile =~ s/\W/_/g;
	print $gscript_ofh "set output \'CM.$ofile.png\'\n";
	
	use List::Util qw (min max);
	my $min_y = min(@y_coords);
	my $max_y = max(@y_coords);

	$min_y-=2;
	$max_y+=2;
	
	print $gscript_ofh "set yrange[$min_y:$max_y]\n";

	foreach my $match_file (@match_files) {
		$match_file = "\'$match_file\' w lp";
		#print "MATCH: $match_file\n";
		
	}
	
	my $plot_line = "plot " . join(",", @match_files) . "\n";
	
	print $gscript_ofh $plot_line . "\n";
	
	close $gscript_ofh;

	system "gnuplot gnuscript.$$.txt";

	#unlink("gnuscript.$$.txt", @match_files);

	print "Wrote image file: CM.$ofile.png\n";

}