| 1 | package TFBS::Ext::pwmsearch; |
| 2 | |
| 3 | require 5.005_62; |
| 4 | use strict; |
| 5 | use warnings; |
| 6 | use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION); |
| 7 | use Bio::SeqIO; |
| 8 | use File::Temp qw (:POSIX); |
| 9 | |
| 10 | require Exporter; |
| 11 | require DynaLoader; |
| 12 | |
| 13 | our @ISA = qw(Exporter DynaLoader); |
| 14 | |
| 15 | # Items to export into callers namespace by default. Note: do not export |
| 16 | # names by default without a very good reason. Use EXPORT_OK instead. |
| 17 | # Do not simply export all your public functions/methods/constants. |
| 18 | |
| 19 | # This allows declaration use TFBS::Ext::pwmsearch ':all'; |
| 20 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK |
| 21 | # will save memory. |
| 22 | %EXPORT_TAGS = ( 'all' => [ qw( |
| 23 | |
| 24 | ) ] ); |
| 25 | |
| 26 | @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
| 27 | |
| 28 | @EXPORT = qw( |
| 29 | |
| 30 | ); |
| 31 | $VERSION = '0.2'; |
| 32 | |
| 33 | bootstrap TFBS::Ext::pwmsearch $VERSION; |
| 34 | |
| 35 | # Preloaded methods go here. |
| 36 | |
| 37 | sub pwmsearch { |
| 38 | my ($matrixobj, $seqobj, $threshold, $start, $end) = @_; |
| 39 | $start = 1 if !defined($start); |
| 40 | $end = $seqobj->length if !defined($end); |
| 41 | my $matrixfile = tmpnam(); |
| 42 | open (MATRIX, ">$matrixfile") or die ("Error opening temporary file."); |
| 43 | print MATRIX $matrixobj->rawprint(); |
| 44 | close MATRIX; |
| 45 | |
| 46 | my $outfile = tmpnam(); |
| 47 | |
| 48 | # pwm_search is confused by long descriptions - we delete desc temporarily: |
| 49 | |
| 50 | my $save_desc = $seqobj->desc(); |
| 51 | $seqobj->desc(""); |
| 52 | |
| 53 | my $seqfile; |
| 54 | if ($seqobj->{_fastafile}) { |
| 55 | $seqfile = $seqobj->{_fastafile}; |
| 56 | } |
| 57 | else { |
| 58 | $seqfile = tmpnam(); |
| 59 | my $outstream = Bio::SeqIO->new(-file=>">$seqfile", -format=>"fasta"); |
| 60 | $outstream->write_seq(Bio::Seq->new(-seq =>$seqobj->subseq($start, $end), |
| 61 | -id =>$seqobj->id)); |
| 62 | $outstream->close(); |
| 63 | } |
| 64 | |
| 65 | $seqobj->desc($save_desc); |
| 66 | |
| 67 | # calculate threshold |
| 68 | |
| 69 | if ($threshold) { |
| 70 | if ($threshold =~ /(.+)%/) { |
| 71 | # percentage |
| 72 | $threshold = $matrixobj->{min_score} + |
| 73 | ($matrixobj->{max_score} - $matrixobj->{min_score})* $1/100; |
| 74 | } |
| 75 | else { |
| 76 | # absolute value |
| 77 | # $threshold = $args{-threshold}; |
| 78 | } |
| 79 | } |
| 80 | else { |
| 81 | # no threshold given |
| 82 | $threshold = $matrixobj->{min_score} -1; |
| 83 | } |
| 84 | |
| 85 | search_xs($matrixfile, $seqfile, |
| 86 | $threshold, $matrixobj->name()."", |
| 87 | $matrixobj->{'class'}."", $outfile); |
| 88 | |
| 89 | unlink $seqfile unless $seqobj->{'_fastafile'}; |
| 90 | unlink $matrixfile; |
| 91 | |
| 92 | my $hitlist = TFBS::SiteSet->new(); |
| 93 | my ($TFname, $TFclass) = ($matrixobj->{name}, $matrixobj->{class}); |
| 94 | |
| 95 | |
| 96 | my $save_delim = $/; # bugfix submitted |
| 97 | local $/ = "\n"; # by Michal Lapidot |
| 98 | |
| 99 | open (OUTFILE, $outfile) |
| 100 | or die("Could not read temporary outfile"); |
| 101 | while (my $line = <OUTFILE>) { |
| 102 | # print STDERR $line; |
| 103 | chomp $line; |
| 104 | $line =~ s/^\s+//; |
| 105 | $line =~ s/ *\t */\t/g; |
| 106 | my ($seq_id, $factor, $class, $strand, $score, $pos, $siteseq) = |
| 107 | (split /\t/, $line)[0, 2, 3, 4, 5, 7, 9]; |
| 108 | my $num_strand = ($strand eq "-")? "-1" : "1"; |
| 109 | my $site = TFBS::Site->new ( -seq_id => $seqobj->display_id()."", |
| 110 | -seqobj => $seqobj, |
| 111 | -strand => $num_strand."", |
| 112 | -pattern => $matrixobj, |
| 113 | -siteseq => $siteseq."", |
| 114 | -score => $score."", |
| 115 | -start => $pos +$start -1, |
| 116 | -end => $pos +$start |
| 117 | +length($siteseq) -2 |
| 118 | ); |
| 119 | $hitlist->add_site($site); |
| 120 | } |
| 121 | close OUTFILE; |
| 122 | $/ = $save_delim; |
| 123 | unlink $outfile; |
| 124 | return $hitlist; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | 1; |
| 129 | __END__ |
| 130 | |
| 131 | =head1 NAME |
| 132 | |
| 133 | TFBS::Ext::pwmsearch - Perl extension for scanning a DNA sequence object with a position weight matrix |
| 134 | |
| 135 | =head1 SYNOPSIS |
| 136 | |
| 137 | use TFBS::Ext::pwmsearch; |
| 138 | pwmsearch |
| 139 | |
| 140 | =head1 DESCRIPTION |
| 141 | |
| 142 | Stub documentation for TFBS::Ext::pwmsearch, created by h2xs. It looks like the |
| 143 | author of the extension was negligent enough to leave the stub |
| 144 | unedited. |
| 145 | |
| 146 | Blah blah blah. |
| 147 | |
| 148 | =head2 EXPORT |
| 149 | |
| 150 | None by default. |
| 151 | |
| 152 | |
| 153 | =head1 AUTHOR |
| 154 | |
| 155 | A. U. Thor, [email protected] |
| 156 | |
| 157 | =head1 SEE ALSO |
| 158 | |
| 159 | perl(1). |
| 160 | |
| 161 | =cut |