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
|
=head1 NAME
Bio::Matrix::PSM::IO::mast - PSM mast parser implementation
=head1 SYNOPSIS
See Bio::Matrix::PSM::IO for detailed documentation on how to
use PSM parsers
=head1 DESCRIPTION
Parser for mast. This driver unlike meme or transfac for example is
dedicated more to PSM sequence matches, than to PSM themselves.
=head1 TO DO
Section III should be parsed too, otherwise no real sequence is
available, so we supply 'NNNNN....' as a seq which is not right.
=head1 FEEDBACK
=head2 Mailing Lists
User feedback is an integral part of the evolution of this
and other Bioperl modules. Send your comments and suggestions preferably
to one of the Bioperl mailing lists. Your participation is much appreciated.
bioperl-l@bioperl.org - General discussion
http://bioperl.org/wiki/Mailing_lists - About the mailing lists
=head2 Support
Please direct usage questions or support issues to the mailing list:
I<bioperl-l@bioperl.org>
rather than to the module maintainer directly. Many experienced and
reponsive experts will be able look at the problem and quickly
address it. Please include a thorough description of the problem
with code and data examples if at all possible.
=head2 Reporting Bugs
Report bugs to the Bioperl bug tracking system to help us keep track
the bugs and their resolution. Bug reports can be submitted via the
web:
https://github.com/bioperl/bioperl-live/issues
=head1 AUTHOR - Stefan Kirov
Email skirov@utk.edu
=head1 APPENDIX
The rest of the documentation details each of the object
methods. Internal methods are usually preceded with a _
=cut
# Let the code begin...
package Bio::Matrix::PSM::IO::mast;
$Bio::Matrix::PSM::IO::mast::VERSION = '1.7.8';
use Bio::Matrix::PSM::InstanceSite;
use Bio::Matrix::PSM::Psm;
use Bio::Root::Root;
use strict;
use base qw(Bio::Matrix::PSM::PsmHeader Bio::Matrix::PSM::IO);
=head2 new
Title : new
Usage : my $psmIO = Bio::Matrix::PSM::IO->new(-format=>'mast',
-file=>$file);
Function: Associates a file with the appropriate parser
Throws : Throws if the file passed is in HTML format or if
some criteria for the file
format are not met.
Example :
Returns : psm object, associated with a file with matrix file
Args : hash
return : "Bio::Matrix::PSM::$format"->new(@args);
=cut
sub new {
my($class, @args)=@_;
my $self = $class->SUPER::new(@args);
my (%instances,@header,$n);
my ($file)=$self->_rearrange(['FILE'], @args);
$self->{file} = $file;
$self->{_factor}=1;
$self->_initialize_io(@args) || warn "Did you intend to use STDIN?"; #Read only for now
$self->{_end}=0;
undef $self->{hid};
return $self if ($file=~/^>/);#Just writing
my $buf=$self->_readline;
$self->throw('Cannot parse HTML format yet') if ($buf =~/^<HTML>/);
# this should probably be moved to its own function
while ( defined($buf=$self->_readline)) {
chomp($buf);
if ($buf=~/DATABASE AND MOTIFS/) {
while ($buf=$self->_readline) {
if ($buf=~/DATABASE/) {
$buf=~s/^[\s\t]+//;
chomp $buf;
($n,$self->{_dbname},$self->{_dbtype})=split(/\s/,$buf);
$self->{_dbtype}=~s/[\(\)]//g;
}
if ($buf=~/MOTIFS/) {
$buf=~s/^[\s\t]+//;
chomp $buf;
($n,$self->{_mrsc},$self->{_msrctype})=split(/\s/,$buf);
$self->{_msrctype}=~s/[\(\)]//g;
last;
}
}
if ($self->{_msrctype} ne $self->{_dbtype}) {#Assume we have protein motifs, nuc DB (not handling opp.)
$self->{_factor}=3;
$self->{_mixquery}=1;
}
}
if ($buf=~m/MOTIF WIDTH BEST POSSIBLE MATCH/) {
$self->_readline;
while (defined($buf=$self->_readline)) {
last if ($buf!~/\w/);
$buf=~s/\t+//g;
$buf=~s/^\s+//g;
my ($id,$width,$seq)=split(/\s+/,$buf);
push @{$self->{hid}},$id;
$self->{length}->{$id}=$width;
$self->{seq}->{$id}=$seq;
}
next;
}
if ($buf=~m/section i:/i) {
$self->_readline;
$self->_readline;
$self->_readline;
%instances=_get_genes($self);
$self->{instances}=\%instances;
if (!(%instances)) {
$self->warn ("Your MAST analysis did not find any matches satisfying the current threshold.\nSee MAST documentation for more information.\n");
return $self; #The header might be useful so we return the object, not undef
}
next;
}
if ($buf=~m/section ii:/i) {
$self->_readline;
$self->_readline;
$self->_readline;
last;
}
$buf=~s/[\t+\s+]/ /g;
push @header,$buf unless (($buf=~/\*{10,}/)||($buf!~/\w/));
}
$self->throw('Could not read Section I, probably wrong format, make sure it is not HTML, giving up...') if !(%instances);
$self->warn( "This file might be an unreadable version, proceed with caution!\n") if (!grep(/\s+MAST\s+version\s+3/,@header));
$self->{unstructured} = \@header;
$self->_initialize;
return $self;
}
# Get the file header and put store it as a hash, which later we'll use to create
# the header for each Psm. See Bio::Matrix::PSM::PsmI for header function.
sub _get_genes {
my $self=shift;
my %llid;
my $ok=0;
my $i=0;
my %instances;
while (my $line=$self->_readline) {
last if ($line=~/^[\s\t*]/); # Well, ids can be nearly anything...???
chomp($line);
$i++;
next if ($line eq '');
$line=~s/\s+/,/g;
my ($id,$key,$eval,$len)=split(/,/,$line);
unless ($len) {
warn "Malformed data found: $line\n";
next;
}
$instances{$id}=Bio::Matrix::PSM::InstanceSite->new(-id=>$id,
-desc=>$key,
-score=>$eval,
-width=>$len,
-seq=>'ACGT');
}
return %instances;
}
=head2 next_psm
Title : next_psm
Usage : my $psm=$psmIO->next_psm();
Function: Reads the next PSM from the input file, associated with this object
Throws : Throws if there ara format violations in the input file (checking is not
very strict with all drivers).
Example :
Returns : Bio::Matrix::PSM::Psm object
Args : none
=cut
sub next_psm {
my $self=shift;
return if ($self->{_end}==1);
my (@lmotifsm,%index,$eval,$scheme,$sid);
%index= %{$self->{length}};
my (@instances,%instances);
my $line=$self->_readline;
$line=~s/[\t\n]//;
if ($line =~ /\*{10,}/) { #Endo of Section II if we do only section II
$self->{_end}=1;
return ;
}
do {
if ($line!~/^\s/) {
($sid,$eval,$scheme)=split(/\s+/,$line,3);
}
else
{ $scheme .=$line; }
$line=$self->_readline;
$line=~s/[\t\n]//;
} until ($line!~/^\s/);
my $pos=1;
$scheme=~s/\s+//g;
$scheme=~s/\n//g;
my @motifs=split(/_/,$scheme);
while (@motifs) {
my $next=shift(@motifs);
if (!($next=~/\D/)) {
last if (!@motifs);
$pos+=$next;
next;
}
my $id=$next;
my $score= $id=~m/\[/ ? 'strong' : 'weak' ;
my $frame;
my $strand = $id =~ m/\-\d/ ? -1 : 1 ;
if ($self->{_mixquery}) {
$frame = 0 if $id =~ m/\d+a/ ;
$frame = 1 if $id =~ m/\d+b/ ;
$frame = 2 if $id =~ m/\d+c/ ;
}
$id=~s/\D+//g;
my @s;
my $width=$index{$id};
#We don't know the sequence, but we know the length
my $seq='N' x ($width*$self->{_factor}); #Future version will have to parse Section tree nad get the real seq
my $instance=Bio::Matrix::PSM::InstanceSite->new
( -id=>"$id\@$sid",
-mid=>$id,
-accession_number=>$sid,
-desc=>"Motif $id occurrance in $sid",
-score=>$score,
-seq=>$seq,
-alphabet => 'dna',
-start=>$pos,
-strand=>$strand);
$instance->frame($frame) if ($self->{_mixquery});
push @instances,$instance;
$pos+=$index{$id}*$self->{_factor};
}
my $psm= Bio::Matrix::PSM::Psm->new(-instances=> \@instances,
-e_val => $eval,
-id => $sid);
$self->_pushback($line);
return $psm;
}
=head2 write_psm
Title : write_psm
Usage : #Get SiteMatrix object somehow (see Bio::Matrix::PSM::SiteMatrix)
my $matrix=$psmin->next_matrix;
#Create the stream
my $psmio=new(-file=>">psms.mast",-format=>'mast');
$psmio->write_psm($matrix);
#Will warn if only PFM data is contained in $matrix, recalculate the PWM
#based on normal distribution (A=>0.25, C=>0.25, etc)
Function: writes pwm in mast format
Throws :
Example :
Args : SiteMatrix object
Returns :
=cut
sub write_psm {
my ($self,$matrix)=@_;
# my $idline=">". $matrix->id . "\n";
my $w=$matrix->width;
my $header="ALPHABET= ACGT\nlog-odds matrix: alength= 4 w= $w\n";
$self->_print($header);
unless ($matrix->get_logs_array('A')) {
warn "No log-odds data, available, using normal distribution to recalculate the PWM";
$matrix->calc_weight({A=>0.25, C=>0.25, G=>0.25,T=>0.25});
}
while (my %h=$matrix->next_pos) {
$self->_print (join("\t",$h{lA},$h{lC},$h{lG},$h{lT},"\n"));
}
}
1;
|