process_pmach.pl 6.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
use strict;

=head1 Process pmatch

=head1 Description

=head2 Aims
This script aims to run pmatch and postprocess pmatch to map Ensembl peptides to external databases (currently Swissprot and Refseq but may be extented). The first part of the script runs pmatch, the second part gets the percentage of a match of a unique Ensembl peptide which match to an unique external protein. The third part classify each ensembl match as PRIMARY match (the longest one and the one which will be used for the mapping, PSEUDO, DUPLICATE and REPEAT (pretty arbitrary criterias but may be useful for quality control).
NB: All of the intermediary files are written.

=head2 Options
-ens : Ensembl peptide fasta file
-sp : SP, SPTREMBL fasta file
-refseq: Refseq peptide fasta file

16 17 18 19
=head2 Contacts
mongin@ebi.ac.uk
birney@ebi.ac.uk

20 21
=cut  

22 23
use Getopt::Long;

24
my ($ens,$sp,$refseq,$pdb);
25 26 27 28 29

&GetOptions(
            
            'ens:s'=>\$ens,
            'sp:s'=>\$sp,
30 31
            'refseq:s'=>\$refseq,
	    'pdb:s'=>\$pdb
32 33 34 35
            );

&runpmatch();
&postprocesspmatch($sp);
Emmanuel Mongin's avatar
Emmanuel Mongin committed
36
&postprocesspmatch($refseq);
37
&postprocesspmatch($pdb);
38
&finalprocess($sp);
Emmanuel Mongin's avatar
Emmanuel Mongin committed
39
&finalprocess($refseq);
40
&finalprocess($pdb);
41 42 43 44 45

sub runpmatch {
    print STDERR "Running pmatch\n";
 
#Run pmatch and store the data in files which will be kept for debugging
46 47
    my $pmatch1 = "/nfs/griffin2/rd/bin.ALPHA/pmatch -T 14 $sp $ens > ens_sp_rawpmatch";
    my $pmatch2 = "/nfs/griffin2/rd/bin.ALPHA/pmatch -T 14 $refseq $ens > ens_refseq_rawpmatch";
48 49 50
    my $pmatch3 = "/nfs/griffin2/rd/bin.ALPHA/pmatch -T 14 $pdb $ens > ens_pdb_rawpmatch";


51 52
    system($pmatch1); # == 0 or die "$0\Error running '$pmatch1' : $!";
    system($pmatch2); #== 0 or die "$0\Error running '$pmatch2' : $!";
53 54
   system($pmatch3); #== 0 or die "$0\Error running '$pmatch2' : $!"; 

55 56 57 58 59 60 61
}


    
sub postprocesspmatch {
    my ($db) = @_;
    my %hash1;
62
    my %hashlength;
63
      
64 65 66
#Post process the raw data from pmatch
    if ($db eq $sp) {
	print STDERR "Postprocessing pmatch for SP mapping\n";
Emmanuel Mongin's avatar
Emmanuel Mongin committed
67 68
	open (OUT, ">ens_sp.processed") || die "Can't open File\n";
	open (PROC, "ens_sp_rawpmatch") || die "Can't open File\n";
69 70
    }
    
71
    elsif ($db eq $refseq) {
72
	print STDERR "Postprocessing pmatch for REFSEQ mapping\n"; 
Emmanuel Mongin's avatar
Emmanuel Mongin committed
73
	open (OUT, ">ens_refseq.processed") || die "Can't open File\n";;
74 75
	open (PROC, "ens_refseq_rawpmatch") || die "Can't open file ens_refseq_rawpmatch\n";
    }
76 77 78 79 80 81

    elsif ($db eq $pdb) {
	print STDERR "Postprocessing pmatch for PDB mapping\n"; 
	open (OUT, ">ens_pdb.processed") || die "Can't open File\n";;
	open (PROC, "ens_pdb_rawpmatch") || die "Can't open file ens_refseq_rawpmatch\n";
    }
82 83
    
    while (<PROC>) {
84
#538     COBP00000033978 1       538     35.3    Q14146  1       538     35.3 
85
	my ($len,$id,$start,$end,$tperc,$query,$qst,$qend,$perc) = split;
86 87

	if ($db eq $refseq) {
88
	    #Get only the refseq ac (NP_\d+) 
89 90 91
	    ($query) = $query =~ /\w+\|\d+\|\w+\|(\w+)/;
	}

92 93
	my $uniq = "$id:$query";
	
94 95
#Add the percentage of similarity for the Ensembl peptide for a single match
#There is a bug at this step, some similarities can be over 100% !!! This problem may be solved by changing pmatch source code
96
	$hash1{$uniq} += $perc;
97 98
	$hashlength{$uniq} += $len;
	
99 100
    }

101
#Write out the processed data
102
    foreach my $key ( keys %hash1 ) {
Emmanuel Mongin's avatar
Emmanuel Mongin committed
103
	if (($hashlength{$key} >= 20)) {
104 105 106 107 108 109
	    ($a,$b) = split(/:/,$key);
	    print OUT "$a\t$b\t$hash1{$key}\n";
	}
	#else {
	 #   print "$a\t$b\t$hash1{$key}\t$hashlength{$key}\n";
	#}
110
    }
111 112
    close (PROC);
    close (OUT);                               
113 114 115
}

sub finalprocess {
116
#This final subroutine will use the postprocessed pmatch file and get back the best Ensembl match (labelled as PRIMARY) for a given external known protein.
117 118 119 120 121 122 123 124
    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");
    }

125
    elsif ($db eq $refseq) {
126 127
	print STDERR "Getting final mapping for REFSEQ mapping\n";
	open (PROC, "ens_refseq.processed") || die "Can' open file ens_refseq.processed\n";
128
	open (OUT, ">ens_refseq.final");
129 130
    }

131 132 133 134 135 136 137
    elsif ($db eq $pdb) {
	print STDERR "Getting final mapping for PDB mapping\n";
	open (PROC, "ens_refseq.processed") || die "Can' open file ens_refseq.processed\n";
	open (OUT, ">ens_pdb.final");
    }


138 139 140 141 142 143
    my %hash2;
    while (<PROC>) {
	my ($ens,$known,$perc) = split;
	#if ($perc > 100) {
	 #   print "$ens\t$known\t$perc\n";
	#}
144 145
	

146 147 148 149
	if( !defined $hash2{$known} ) {
	    $hash2{$known} = [];
	}
	
150
#Each single external protein correspond to an array of objects dealing with the name and the percentage of similarity of the Ensembl peptide matching with the the known external protein.
151
	my $p= NamePerc->new;
152 153 154 155 156 157 158
	$p->name($ens);
	$p->perc($perc);
	
	push(@{$hash2{$known}},$p);
    }

    foreach my $know ( keys %hash2 ) {
159
	my @array = @{$hash2{$know}};
160 161
	@array = sort { $b->perc <=> $a->perc } @array;
	
162
#The Ensembl match to the known protein is labelled as PRIMARY and will be used later for the mapping 
163
	my $top = shift @array;
164 165 166 167 168 169 170 171 172 173

	#if ($top->perc >= 20) {
	    
	    print  OUT "$know\t",$top->name,"\t",$top->perc,"\tPRIMARY\n";
	    
	    foreach $ens ( @array ) {
		
		if( $ens->perc > $top->perc ) {
		    die "Not good....";
		}
174
	    }
175 176

#If there is more than 20 Ensembl peptides matching a single known protein, these Ensembl peptides are labelled as REPEAT 	
177 178 179 180 181 182 183 184 185 186
	    if (scalar(@array) >= 20) {
		foreach my $repeat (@array) {
		    if( $repeat->perc+1 >= $top->perc ) {
			print OUT "$know\t",$repeat->name,"\t",$repeat->perc,"\tDUPLICATE\n";
		    }
		    else {
			
			print OUT "$know\t",$repeat->name,"\t",$repeat->perc,"\tREPEAT\n";
		    }
		}
187 188
	    }
	
189
#If less than 20, either duplicate if percentage of identity close to the PRIMARY labelled as DUPLICATE or labelled as PSEUDO. DUPLICATEs can also be used for the mapping 
190 191 192 193 194 195 196 197 198 199 200 201
	    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";
		    }
		} 
	    }              
	} 
    #}
202
    close (PROC);
203 204 205
    close (OUT);
}

206
#Set of objects to deal with the script
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

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'};

}