process_pmach.pl 3.74 KB
Newer Older
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
#use strict;
use Getopt::Long;

my ($ens,$sp,$refseq);

&GetOptions(
            
            'ens:s'=>\$ens,
            'sp:s'=>\$sp,
            'refseq:s'=>\$refseq
            );

&runpmatch();
&postprocesspmatch($sp);
&postprocesspmatch($refseq);
&finalprocess($sp);
&finalprocess($refseq);

sub runpmatch {
    print STDERR "Running pmatch\n";
 
#Run pmatch and store the data in files which will be kept for debugging
    my $pmatch1 = "/nfs/griffin2/rd/bin.ALPHA/pmatch $sp $ens > ens_sp_rawpmatch";
    my $pmatch2 = "/nfs/griffin2/rd/bin.ALPHA/pmatch $refseq $ens > ens_refseq_rawpmatch";
    
    system($pmatch1);# == 0 or die "$0\Error running '$pmatch1' : $!";
    system($pmatch2);# == 0 or die "$0\Error running '$pmatch2' : $!";
}


    
sub postprocesspmatch {
    my ($db) = @_;
    my %hash1;
    
       #open (OUT, ">../data/$db.processed");
#Post process the raw data from pmatch
    if ($db eq $sp) {
	print STDERR "Postprocessing pmatch for SP mapping\n";
	open (OUT, ">ens_sp.processed");
	open (PROC, "ens_sp_rawpmatch");
    }
    
    else {
	print STDERR "Postprocessing pmatch for REFSEQ mapping\n"; 
	open (OUT, ">ens_refseq.processed");
	open (PROC, "ens_refseq_rawpmatch") || die "Can't open file ens_refseq_rawpmatch\n";
    }
    
    while (<PROC>) {
	## 20      Q9UN99  36      55      8.1     ENSP00000051351 63      82      7.8
	my ($len,$id,$start,$end,$perc,$query,$qst,$qend,$qperc) = split;
	#print STDERR "$id:$query";
	my $uniq = "$id:$query";
	
	$hash1{$uniq} += $qperc;
    }



    foreach my $key ( keys %hash1 ) {
	($a,$b) = split(/:/,$key);
	print OUT "$a\t$b\t$hash1{$key}\n";
    }
   close (OUT);                               
}

sub finalprocess {
    my ($db) = @_;
    
    if ($db eq $sp) {
	print STDERR "Getting final mapping for SP mapping\n";
	open (PROC, "ens_sp.processed");
	open (OUT, ">ens_sp.final");
    }

    else {
	print STDERR "Getting final mapping for REFSEQ mapping\n";
	open (PROC, "ens_refseq.processed") || die "Can' open file ens_refseq.processed\n";
	open (OUT, ">refseq.final");
    }

    my %hash2;
    while (<PROC>) {
	my ($ens,$known,$perc) = split;
	#if ($perc > 100) {
	 #   print "$ens\t$known\t$perc\n";
	#}
	if( !defined $hash2{$known} ) {
	    $hash2{$known} = [];
	}
	
	$p= NamePerc->new;
	$p->name($ens);
	$p->perc($perc);
	
	push(@{$hash2{$known}},$p);
    }


    foreach my $know ( keys %hash2 ) {
	@array = @{$hash2{$know}};
	@array = sort { $b->perc <=> $a->perc } @array;
	
	$top = shift @array;
	print  OUT "$know\t",$top->name,"\t",$top->perc,"\tPRIMARY\n";
	
	foreach $ens ( @array ) {
	    if( $ens->perc > $top->perc ) {
		die "Not good....";
	    }
	}
	
	if (scalar(@array) >= 20) {
	    foreach my $repeat (@array) {
		print OUT "$know\t",$repeat->name,"\t",$repeat->perc,"\tREPEAT\n";
	    }
	}
	
	if (scalar(@array) < 20) {
	    foreach my $duplicate (@array) {
		if( $duplicate->perc+1 >= $top->perc ) {
		    print OUT "$know\t",$duplicate->name,"\t",$duplicate->perc,"\tDUPLICATE\n";
		}
		else {
		    print OUT "$know\t",$duplicate->name,"\t",$duplicate->perc,"\tPSEUDO\n";
		}
	    } 
	}              
    } 
    close (OUT);
}


package NamePerc;


sub new {
    my $class= shift;
    my $self = {};
    bless $self,$class;
    return $self;
}


=head2 name

 Title   : name
 Usage   : $obj->name($newval)
 Function:
 Returns : value of name
 Args    : newvalue (optional)


=cut

sub name{
   my $obj = shift;
   if( @_ ) {
      my $value = shift;
      $obj->{'name'} = $value;
    }
    return $obj->{'name'};

}

=head2 perc

 Title   : perc
 Usage   : $obj->perc($newval)
 Function:
 Returns : value of perc
 Args    : newvalue (optional)


=cut

sub perc{
   my $obj = shift;
   if( @_ ) {
      my $value = shift;
      $obj->{'perc'} = $value;
    }
    return $obj->{'perc'};

}