]> git.donarmstrong.com Git - rsem.git/blob - rsem-generate-data-matrix
Updated samtools to 0.1.19
[rsem.git] / rsem-generate-data-matrix
1 #!/usr/bin/perl
2
3 use strict;
4
5 if (scalar(@ARGV) == 0) {
6     print "Usage: rsem-generate-data-matrix sampleA.[genes/isoforms].results sampleB.[genes/isoforms].results ... > output_name.matrix\n";
7     print "Results files should be either all .genes.results or all .isoforms.results.\n";
8     exit(-1);
9 }
10
11 my $offsite = 4; # for new file formats
12
13 my $line;
14 my $n = scalar(@ARGV);
15 my $M = -1;
16 my @matrix = ();
17
18 # 0, file_name; 1, reference of expected count array; 2, reference of transcript_id/gene_id array
19 sub loadData {
20     open(INPUT, $_[0]);
21     my $line = <INPUT>; # The first line contains only column names
22     while ($line = <INPUT>) {
23         chomp($line); 
24         my @fields = split(/\t/, $line);
25         push(@{$_[2]}, "\"$fields[0]\"");
26         push(@{$_[1]}, $fields[$offsite]);
27     }
28     close(INPUT);
29
30     if (scalar(@{$_[1]}) == 0) {
31         print STDERR "Nothing is detected! $_[0] may not exist or is empty.\n";
32         exit(-1);
33     }
34 }
35
36 #0, M; 1, reference of @ids_arr; 2, reference of @ids
37 sub check {
38     my $size = $_[0];
39     for (my $i = 0; $i < $size; $i++) { 
40         if ($_[1]->[$i] ne $_[2]->[$i]) {
41             return 0;
42         }
43     }
44     return 1;
45 }
46
47 my @ids_arr = ();
48
49 for (my $i = 0; $i < $n; $i++) {
50     my (@ids, @ecs) = ();
51     &loadData($ARGV[$i], \@ecs, \@ids);
52
53     if ($M < 0) { 
54         $M = scalar(@ids); 
55         @ids_arr = @ids;
56     }
57     elsif (!&check($M, \@ids_arr, \@ids)) { 
58         print STDERR "Number of lines among samples are not equal!\n"; 
59         exit(-1); 
60     }
61
62     my $colname;
63     if (substr($ARGV[$i], 0, 2) eq "./") { $colname = substr($ARGV[$i], 2); }
64     else { $colname = $ARGV[$i]; }
65     $colname = "\"$colname\"";
66     @ecs = ($colname, @ecs);
67     push(@matrix, \@ecs);
68 }
69
70 @ids_arr = ("", @ids_arr);
71 @matrix = (\@ids_arr, @matrix);
72
73 for (my $i = 0; $i <= $M; $i++) {
74     for (my $j = 0; $j < $n; $j++) { print "$matrix[$j][$i]\t"; }
75     print "$matrix[$n][$i]\n";
76 }