| 1 | # TFBS module for TFBS::Matrix::ICM |
| 2 | # |
| 3 | # Copyright Boris Lenhard |
| 4 | # |
| 5 | # You may distribute this module under the same terms as perl itself |
| 6 | # |
| 7 | |
| 8 | # POD |
| 9 | |
| 10 | |
| 11 | =head1 NAME |
| 12 | |
| 13 | TFBS::Matrix::Alignment - class for alignment of PFM objects |
| 14 | |
| 15 | =head1 SYNOPSIS |
| 16 | |
| 17 | =over 1 |
| 18 | |
| 19 | =item * Making an alignment: |
| 20 | (See documentation of individual TFBS::DB::* modules to learn |
| 21 | how to connect to different types of pattern databases and retrieve |
| 22 | TFBS::Matrix::* objects from them.) |
| 23 | |
| 24 | my $db_obj = TFBS::DB::JASPAR2->new |
| 25 | (-connect => ["dbi:mysql:JASPAR2:myhost", |
| 26 | "myusername", "mypassword"]); |
| 27 | my $pfm1 = $db_obj->get_Matrix_by_ID("M0001", "PFM"); |
| 28 | my $pfm2 = $db_obj->get_Matrix_by_ID("M0002", "PFM"); |
| 29 | |
| 30 | my $alignment= new TFBS::Matrix::Alignment( |
| 31 | -pfm1=>$pfm1, |
| 32 | -pfm2=>$pfm2, |
| 33 | -binary=>"/TFBS/Ext/matrix_aligner", |
| 34 | ); |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | =head1 DESCRIPTION |
| 40 | |
| 41 | TFBS::Matrix::Alignment is a class for representing and performing |
| 42 | pairwise alignments of profiles (in the form of TFBS::PFM objects) |
| 43 | Alignments are preformed using a semi-global variant of the |
| 44 | Needleman-Wunsch algorithm that only permits the opening of one internal gap. |
| 45 | Fore reference, the algorithm is described in Sandelin et al |
| 46 | Funct Integr Genomics. 2003 Jun 25 |
| 47 | |
| 48 | |
| 49 | =head1 FEEDBACK |
| 50 | |
| 51 | Please send bug reports and other comments to the author. |
| 52 | |
| 53 | =head1 AUTHOR - Boris Lenhard |
| 54 | |
| 55 | Boris Lenhard E<lt>[email protected]<gt> |
| 56 | |
| 57 | =head1 APPENDIX |
| 58 | |
| 59 | The rest of the documentation details each of the object |
| 60 | methods. Internal methods are preceded with an underscore. |
| 61 | |
| 62 | =cut |
| 63 | |
| 64 | # The code starts HERE: |
| 65 | |
| 66 | package TFBS::Matrix::Alignment; |
| 67 | |
| 68 | use vars '@ISA'; |
| 69 | use strict; |
| 70 | use Bio::Root::Root; |
| 71 | use TFBS::Matrix; |
| 72 | use File::Temp qw/:POSIX/; |
| 73 | @ISA = qw(TFBS::Matrix Bio::Root::Root); |
| 74 | |
| 75 | #alignment methods: for making and storing a single matrix-alignments |
| 76 | =head2 new |
| 77 | |
| 78 | Title : new |
| 79 | Usage : my $alignment = TFBS::Matrix::Alignment->new(%args) |
| 80 | Function: constructor for the TFBS::Matrix::Alignment object |
| 81 | Returns : a new TFBS::Matrix::Alignment object |
| 82 | Args : # you must specify: |
| 83 | |
| 84 | -pfm1, # a TFBS::Matrix::PFM object |
| 85 | -pfm2, # another TFBS::Matrix::PFM object |
| 86 | -binary, # a valid path to the comparison algorithm (matrixalign) |
| 87 | |
| 88 | |
| 89 | ####### |
| 90 | |
| 91 | -ext_penalty #OPTIONAL gap extension penalty in Needleman-Wunsch |
| 92 | algorithmstring. Default 0.01 |
| 93 | -open_penalty, #OPTIONAL gap opening penalty in Needleman-Wunsch |
| 94 | algorithmstring. Default 3.0 |
| 95 | |
| 96 | |
| 97 | =cut |
| 98 | sub new { |
| 99 | #defines and createa an alignment |
| 100 | # args: two pfm objects |
| 101 | # binary file |
| 102 | #optional scoring penalites |
| 103 | my ($class, %args) = @_; |
| 104 | my $self={ |
| 105 | _pfm1=> $args{'-pfm1'}, |
| 106 | _pfm2=> $args{'-pfm2'}, |
| 107 | _ext_penalty=>$args{'-ext_penalty'}|| 0.01, |
| 108 | _open_penalty=> $args{'-open_penalty'}|| 3.00, |
| 109 | _strand=>'', |
| 110 | _align_string=>'', |
| 111 | _gaps=>'', |
| 112 | _aligned_positions =>'', |
| 113 | _score=>'', |
| 114 | }; |
| 115 | |
| 116 | bless $self, "TFBS::Matrix::Alignment"; |
| 117 | # errorcheck: |
| 118 | |
| 119 | |
| 120 | |
| 121 | # save temp files |
| 122 | my($fh1, $file1) = tmpnam(); |
| 123 | print $fh1 $args{'-pfm1'}->rawprint()|| die " Cannot save temporary files for alignment"; |
| 124 | my($fh2, $file2) = tmpnam(); |
| 125 | print $fh2 $args{'-pfm2'}->rawprint()|| die " Cannot save temporary files for alignment"; |
| 126 | |
| 127 | #align |
| 128 | my @pfm1_string; |
| 129 | my @pfm2_string; |
| 130 | |
| 131 | foreach (`$args{'-binary'} $file1 $file2 $self->{'_open_penalty'} $self->{'_ext_penalty'}`){ |
| 132 | # my @pfm2_string; |
| 133 | my $max_length=$self->{'_pfm1'}->length(); |
| 134 | $max_length=$self->{'_pfm2'}->length() if ( $self->{'_pfm2'}->length() > $self->{'_pfm1'}->length()); |
| 135 | |
| 136 | if (/^PFM1/){ |
| 137 | s/PFM1//; |
| 138 | s/\t0/\t-/g; |
| 139 | @pfm1_string= split(); |
| 140 | next; |
| 141 | } |
| 142 | if (/^PFM2/){ |
| 143 | s/PFM2//; |
| 144 | s/\t0/\t-/g; |
| 145 | @pfm2_string= split(); |
| 146 | next; |
| 147 | } |
| 148 | if (/^INFO/){ |
| 149 | my @temp=split; |
| 150 | ($self->{'_score'}, $self->{'_strand'}, $self->{'_aligned_positions'}, $self->{'_gaps'})= ($temp[3], $temp[6], $temp[7],$temp[8]); |
| 151 | next; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | my $string= ($self->{'_pfm1'}->name()||$self->{'_pfm1'}->ID()||'PFM1')."\t\t"; |
| 156 | my $string2=($self->{'_pfm2'}->name()||$self->{'_pfm2'}->ID()||'PFM2')."\t\t";; |
| 157 | if ($pfm1_string[0]==1){ |
| 158 | $string.="-\t" x ($pfm2_string[0]-1); |
| 159 | foreach (my $j=1; $j< $pfm2_string[0]; $j++){ |
| 160 | $string2.="$j\t"; |
| 161 | } |
| 162 | } |
| 163 | if ($pfm2_string[0]==1){ |
| 164 | $string2.="-\t" x ($pfm1_string[0]-1); |
| 165 | for (my $j=1; $j< $pfm1_string[0]; $j++){ |
| 166 | $string.="$j\t"; |
| 167 | } |
| 168 | } |
| 169 | $string.= join("\t", @pfm1_string); |
| 170 | $string2.= join("\t", @pfm2_string); |
| 171 | |
| 172 | if ($pfm1_string[-1]==$self->{'_pfm1'}->length()){ |
| 173 | $string.="\t-" x ($self->{'_pfm2'}->length()-$pfm2_string[-1]); |
| 174 | for (my $j=$pfm2_string[-1]+1; $j<= $self->{'_pfm2'}->length(); $j++){ |
| 175 | $string2.="\t$j"; |
| 176 | } |
| 177 | } |
| 178 | if ($pfm2_string[-1]==$self->{'_pfm2'}->length()){ |
| 179 | $string2.="\t-" x ($self->{'_pfm1'}->length()-$pfm1_string[-1]); |
| 180 | for (my $j=$pfm1_string[-1]+1; $j<= $self->{'_pfm1'}->length(); $j++){ |
| 181 | $string.="\t$j"; |
| 182 | } |
| 183 | } |
| 184 | $self->{'_align_string'}= $string ."\n". $string2; |
| 185 | |
| 186 | unlink $file2; |
| 187 | unlink $file1; |
| 188 | return $self; |
| 189 | } |
| 190 | |
| 191 | # access functions |
| 192 | =head2 score |
| 193 | |
| 194 | Title : score |
| 195 | Usage : my $score = $alignmentobject->score(); |
| 196 | Function: access an alignment score (where each aligned position can contribute max 2) |
| 197 | Returns : a floating point number |
| 198 | Args : none |
| 199 | |
| 200 | |
| 201 | =cut |
| 202 | |
| 203 | =head2 score |
| 204 | |
| 205 | Title : gaps |
| 206 | Usage : my $nr_of_gaps = $alignmentobject->gaps(); |
| 207 | Function: access the number of gaps in an alignment |
| 208 | Returns : an integer |
| 209 | Args : none |
| 210 | |
| 211 | |
| 212 | =cut |
| 213 | |
| 214 | =head2 length |
| 215 | |
| 216 | Title : length |
| 217 | Usage : my $length = $alignmentobject->length(); |
| 218 | Function: access the length of an alignment (ie thenumber of aligned positions) |
| 219 | Returns : an integer |
| 220 | Args : none |
| 221 | |
| 222 | |
| 223 | =cut |
| 224 | |
| 225 | =head2 strand |
| 226 | |
| 227 | Title : strand |
| 228 | Usage : my $strand = $alignmentobject->strand(); |
| 229 | Function: access the oriantation of the aligned patterns: |
| 230 | ++= oriented as input |
| 231 | +-= second pattern is reverse-complemented |
| 232 | Returns : a string |
| 233 | Args : none |
| 234 | |
| 235 | |
| 236 | =cut |
| 237 | |
| 238 | |
| 239 | =head2 alignment |
| 240 | |
| 241 | Title : alignment |
| 242 | Usage : my $alignment_string = $alignmentobject->alignment(); |
| 243 | Function: access a string describing the alignment |
| 244 | Returns : an string, where each number refers to a position in respective PFM. |
| 245 | Position numbering is according to orientation: ie if the second profile is |
| 246 | reversed, position 1 corresponds to the last position in the input profile. |
| 247 | Gaps are denoted as - . |
| 248 | |
| 249 | RXR-VDR - 1 2 3 - 4 5 - |
| 250 | PPARgamma-RXRal 1 2 3 4 5 6 7 8 |
| 251 | |
| 252 | |
| 253 | Args : none |
| 254 | |
| 255 | |
| 256 | =cut |
| 257 | |
| 258 | sub gaps{ return $_[0]->{'_gaps'};} |
| 259 | sub score{ return $_[0]->{'_score'};} |
| 260 | sub length{ return $_[0]->{'_aligned_positions'};} |
| 261 | sub strand{ return $_[0]->{'_strand'};} |
| 262 | sub alignment{ return $_[0]->{'_align_string'};} |
| 263 | |
| 264 | 1 |
| 265 |