Bio::M...Matrix(3pm)
๐ Bio::Matrix::PSM::SiteMatrix โ the manual page, bestie edition โจ
okay so this is the Bio::Matrix::PSM::SiteMatrix page from the Bioperl docs (it's a 3pm manpage, user-contributed perl documentation, iykyk). it holds a position scoring matrix (aka position weight matrix) AND the log-odds. let's get into it ๐
โจ NAME โจ
Bio::Matrix::PSM::SiteMatrix โ it's the SiteMatrixI implementation, and it's literally holding a position scoring matrix (or position weight matrix, same vibe) plus the log-odds for you. no cap ๐ซถ
๐ SYNOPSIS ๐
use Bio::Matrix::PSM::SiteMatrix;
# Create from memory by supplying probability matrix hash
# both as strings or arrays
# where the frequencies $a,$c,$g and $t are supplied either as
# arrayref or string. Accordingly, lA, lC, lG and lT are the log
# odds (only as arrays, no checks done right now)
my ($a,$c,$g,$t,$score,$ic, $mid)=@_;
#or
my ($a,$c,$g,$t,$score,$ic,$mid)=('05a011','110550','400001',
'100104',0.001,19.2,'CRE1');
#Where a stands for all (this frequency=1), see explanation below
my %param=(-pA=>$a,-pC=>$c,-pG=>$g,-pT=>$t,
-lA=>$la, -lC=>$lc,-lG=>$lg,-lT=>$l,
-IC=>$ic,-e_val=>$score, -id=>$mid);
my $site=Bio::Matrix::PSM::SiteMatrix->new(%param);
#Or get it from a file:
use Bio::Matrix::PSM::IO;
my $psmIO= Bio::Matrix::PSM::IO->new(-file=>$file, -format=>'transfac');
while (my $psm=$psmIO->next_psm) {
#Now we have a Bio::Matrix::PSM::Psm object,
# see Bio::Matrix::PSM::PsmI for details
#This is a Bio::Matrix::PSM::SiteMatrix object now
my $matrix=$psm->matrix;
}
# Get a simple consensus, where alphabet is {A,C,G,T,N},
# choosing the character that both satisfies a supplied or default threshold
# frequency and is the most frequenct character at each position, or N.
# So for the position with A, C, G, T frequencies of 0.5, 0.25, 0.10, 0.15,
# the simple consensus character will be 'A', whilst for 0.5, 0.5, 0, 0 it
# would be 'N'.
my $consensus=$site->consensus;
# Get the IUPAC ambiguity code representation of the data in the matrix.
# Because the frequencies may have been pseudo-count corrected, insignificant
# frequences (below 0.05 by default) are ignored. So a position with
# A, C, G, T frequencies of 0.5, 0.5, 0.01, 0.01 will get the IUPAC code 'M',
# while 0.97, 0.01, 0.01, 0.01 will get the code 'A' and
# 0.25, 0.25, 0.25, 0.25 would get 'N'.
my $iupac=$site->IUPAC;
# Getting/using regular expression (a representation of the IUPAC string)
my $regexp=$site->regexp;
my $count=grep($regexp,$seq);
my $count=($seq=~ s/$regexp/$1/eg);
print "Motif $mid is present $count times in this sequence\n";
lowkey obsessed with how much that synopsis does ๐
๐ธ DESCRIPTION ๐ธ
so SiteMatrix exists to give you some basic methods when you're working with position scoring (weight) matrices โ think transcription factor binding sites, that kind of thing. a DNA PSM is made of four vectors with frequencies {A,C,G,T}. that's the minimum info you should hand over to build a PSM object, bestie. ๐งฌ
the vectors can come in as strings, where the frequencies are multiplied by 10 and rounded to an int, going from {0..a} and 'a' means the maximum (10). this is basically MEME's compressed representation of a matrix, and it's genuinely so useful when you're working with a relational DB. iconic behaviour honestly. if you pass arrays instead (well, references to arrays actually), they can be any number โ real or integer, frequency or count. no limits, we love that for you. โจ
when you're creating the object you can ask the constructor to do a simple pseudo count correction by adding a number (typically 1) to all positions โ that's the -correction option. after adding the number, the frequencies get calculated.
โ ๏ธ only use correction when you supply counts, not frequencies. for real. do not skip this one.
it throws an exception if: ๐ญ
- you mix an array and a string as input (like, A matrix given as an array, C as a string โ chaotic, don't)
- the position vector is
(0,0,0,0) - one of the probability vectors is shorter than the rest
๐ the methods i use most frequently ๐
(details below, bestie)
iupacโ return IUPAC compliant consensus as a stringscoreโ returns the score as a real numberICโ information content. returns a real numberidโ identifier. returns a stringaccessionโ accession number. returns a stringnext_posโ return the sequence probably for each letter, IUPAC symbol, IUPAC probability and simple sequence consensus letter for this position. rewind at the end. returns a hash.posโ current position get/set. returns an integer.regexpโ construct a regular expression based on IUPAC consensus. for exampleAGWVwill be[Aa][Gg][AaTt][AaCcGg]widthโ site widthget_stringโ gets the probability vector for a single base as a string.get_arrayโ gets the probability vector for a single base as an array.get_logs_arrayโ gets the log-odds vector for a single base as an array.
๐ฆ the new methods (very main character energy) ๐ฆ
ok so if you're the kind of person who wants to store a PSM in a relational database without creating an entry for each position, these are for you. you can compress the PSM vector into a string and you'll usually lose less than 1% of the data. slay. ๐
do it like this:
my $str=$matrix->get_compressed_freq('A');
or
my $str=$matrix->get_compressed_logs('A');
loading from a database should be done with new, but it's not yet implemented (yes, the original says "not yest" โ we keep the facts, we just โจvibeโจ with them). but you can still uncompress a string like this:
my @arr=Bio::Matrix::PSM::_uncompress_string ($str,1,1); for PSM
or
my @arr=Bio::Matrix::PSM::_uncompress_string ($str,1000,2); for log odds
๐ FEEDBACK ๐
๐ฌ Mailing Lists
user feedback is an integral part of the evolution of this and other Bioperl modules โ genuinely, it's how things grow ๐ฑ 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
๐ Support
please direct usage questions or support issues to the mailing list:
bioperl-l@bioperl.org
rather than to the module maintainer directly. there are so many experienced and responsive experts who will be able to look at the problem and quickly address it. if at all possible, please include a thorough description of the problem with code and data examples. ๐
๐ Reporting Bugs
report bugs to the Bioperl bug tracking system to help us keep track of the bugs and their resolution. bug reports can be submitted via the web:
https://github.com/bioperl/bioperl-live/issues
๐ AUTHOR - Stefan Kirov
Email skirov@utk.edu
โญ CONTRIBUTORS
Sendu Bala, bix@sendu.me.uk
๐ APPENDIX ๐
okay bestie this is the โจrestโจ of the docs โ every single method this object has, even the internal ones (you can spot those because they start with _, sneaky little things ๐ซถ). Let's get into it.
๐ new ๐
- Title โ
new - Usage โ
my $site=Bio::Matrix::PSM::SiteMatrix->new(-pA=>$a,-pC=>$c,
-pG=>$g,-pT=>$t,
-IC=>$ic,
-e_val=>$score,
-id=>$mid);
- Function โ makes you a brand new
Bio::Matrix::PSM::SiteMatrixobject straight out of memory. no disk, no drama, just vibes โจ - Throws โ it WILL throw if you hand it inconsistent data across all the vectors (A, C, G and T), if you mix your input types (string vs array, pick a lane bestie ๐ญ), or if any position freq is
0. - Returns โ a
Bio::Matrix::PSM::SiteMatrixobject ๐ - Args โ
-pA=> vector with the frequencies or counts of A-pC=> vector for C-pG=> vector for G-pt=> vector for T-lA=> vector for the log of A-lC=> vector for the log of C-lG=> vector for the log of G-lT=> vector for the log of T-IC=> real number, the information content of this matrix-e_val=> real number, the expect value-id=> string, an identifier-width=> int, width of the matrix in nucleotides-sites=> int, the number of sites that went into this matrix-model=> hash ref, background frequencies for A, C, G and T-correction=> number, the number to add to all positions to achieve pseudo count correction (default0: no correction)
NB: do not use correction when your input is frequences! ๐จ like genuinely don't
-accession_number=> string, an accession number
Vectors can be strings of the frequencies, where the frequencies are multiplied by 10 and rounded to the nearest whole number, and where 'a' is used to denote the maximal frequency 10. There should be no punctuation (spaces etc.) in the string. For example, 'a0501'. ๐
Alternatively, frequencies or counts can be represented by an array ref containing the counts, frequencies or logs as any kind of number. slay, options.
๐งฎ _calculate_consensus ๐งฎ
- Title โ
_calculate_consensus - Function โ Internal stuff ๐ (that's literally all it says, iykyk)
โ๏ธ calc_weight โ๏ธ
- Title โ
calc_weight - Usage โ
$obj->calc_weight({A=>0.2562, C=>0.2438, G=>0.2432, T=>0.2568}); - Function โ recalculates the PSM (or weights) based on the PFM (the frequency matrix) and a user supplied background model.
- Throws โ if no model is supplied ๐ค
- Returns โ n/a
- Args โ reference to a hash with background frequencies for A, C, G and T
โก๏ธ next_pos โก๏ธ
- Title โ
next_pos - Usage โ (none given)
- Function โ retrieves the next position's features: frequencies for A, C, G, T, the main letter (as in consensus) and the probability for this letter to occur at this position, plus the current position.
- Returns โ hash (
pA,pC,pG,pT,logA,logC,logG,logT,base,prob,rel) - Args โ none
๐ curpos ๐
- Title โ
curpos - Usage โ (none given)
- Function โ gets/sets the current position. Converts to
0if the argument is minus and towidthif it's greater thanwidth. - Returns โ integer
- Args โ integer
๐ e_val ๐
- Title โ
e_val - Usage โ (none given)
- Function โ gets/sets the e-value
- Returns โ real number
- Args โ none to get, real number to set
๐ IC ๐
- Title โ
IC - Usage โ (none given)
- Function โ get/set the Information Content
- Returns โ real number
- Args โ none to get, real number to set
๐ accession_number ๐
- Title โ
accession_number - Function โ get/set the accession number. this will be the unique id for the
SiteMatrixobject as well for any other object inheriting fromSiteMatrix. - Returns โ string
- Args โ none to get, string to set
๐ consensus ๐
- Title โ
consensus - Usage โ (none given)
- Function โ returns the consensus
- Returns โ string
- Args โ (optional) threshold value
1to10, default5.'5'means the returned characters had a50%or higher presence at their position.
๐ width ๐
- Title โ
width - Usage โ (none given)
- Function โ returns the length of the sites used to make this matrix
- Returns โ int
- Args โ none
๐ง sites ๐ง
- Title โ
sites - Usage โ (none given)
- Function โ get/set the number of sites that were used to make this matrix
- Returns โ int
- Args โ none to get, int to set
๐ฆ IUPAC ๐ฆ
- Title โ
IUPAC - Usage โ (none given)
- Function โ returns an IUPAC compliant consensus
- Returns โ string
- Args โ optionally, also supply a whole number (int) of
1or higher to set the significance level when considering the frequencies.1(the default) means a0.05significance level: frequencies lower than0.05will be ignored.2means a0.005level, and so on. ๐ซ
๐ฌ _to_IUPAC ๐ฌ
- Title โ
_to_IUPAC - Usage โ (none given)
- Function โ converts a single position to an IUPAC compliant symbol. For rules, see the implementation ๐
- Returns โ char, real number
- Args โ real numbers for frequencies of A, C, G, T (positional)
optionally, also supply a whole number (int) of 1 or higher to set the significance level when considering the frequencies. 1 (the default) means a 0.05 significance level: frequencies lower than 0.05 will be ignored. 2 means a 0.005 level, and so on.
โ๏ธ _to_cons โ๏ธ
- Title โ
_to_cons - Usage โ (none given)
- Function โ converts a single position to a simple consensus character and returns its probability. For rules, see the implementation.
- Returns โ char, real number
- Args โ real numbers for A, C, G, T (positional), and an optional 5th argument of threshold (as a number between
1and10, where5is default and means the returned character had a50%or higher presence at this position)
๐งต get_string ๐งต
- Title โ
get_string - Usage โ (none given)
- Function โ returns a given probability vector as a string. super useful if you want to store things in a rel database, where arrays are not first choice ๐พ
- Throws โ if the argument is outside
{A,C,G,T} - Returns โ string
- Args โ character
{A,C,G,T}
๐๏ธ get_array ๐๏ธ
- Title โ
get_array - Usage โ (none given)
- Function โ returns an array with frequencies for a specified base
- Returns โ array
- Args โ char
๐ get_logs_array ๐
- Title โ
get_logs_array - Usage โ (none given)
- Function โ returns an array with log_odds for a specified base
- Returns โ array
- Args โ char
๐ชช id ๐ชช
- Title โ
id - Usage โ (none given)
- Function โ gets/sets the site id
- Returns โ string
- Args โ string
๐ regexp ๐
- Title โ
regexp - Usage โ (none given)
- Function โ returns a regular expression which matches the IUPAC convention.
Nwill matchX,N,-and. - Returns โ string
- Args โ none (works at the threshold last used for making the IUPAC string)
๐งบ regexp_array ๐งบ
- Title โ
regexp_array - Usage โ (none given)
- Function โ returns a regular expression which matches the IUPAC convention.
Nwill matchX,N,-and. - Returns โ array
- Args โ none (works at the threshold last used for making the IUPAC string)
- To do โ I have separated
regexpandregexp_array, but maybe they can be rewritten as one โ just check what should be returned ๐คท
๐๏ธ _compress_array ๐๏ธ
- Title โ
_compress_array - Usage โ (none given)
- Function โ will compress an array of real signed numbers to a string (ie vector of bytes)
-127to+127for bi-directional (signed) and0..255for unsigned. - Returns โ String
- Args โ array reference, followed by a max value and direction (optional, default
1-unsigned),1unsigned, any other is signed.
๐ _uncompress_string ๐
- Title โ
_uncompress_string - Usage โ (none given)
- Function โ will uncompress a string (vector of bytes) to create an array of real signed numbers (opposite to
_compress_array) - Returns โ string, followed by a max value and direction (optional, default
1-unsigned),1unsigned, any other is signed. - Args โ array
๐ฆ get_compressed_freq ๐ฆ
- Title โ
get_compressed_freq - Usage โ (none given)
- Function โ a method to provide a compressed frequency vector. It uses one byte to code the frequence for one of the probability vectors for one position. Useful for a relational database. An improvement of the previous
0..acoding. ๐ฅ - Example โ
my $strA=$self->get_compressed_freq('A'); - Returns โ String
- Args โ char
๐งณ get_compressed_logs ๐งณ
- Title โ
get_compressed_logs - Usage โ (none given)
- Function โ a method to provide a compressed log-odd vector. It uses one byte to code the log value for one of the log-odds vectors for one position.
- Example โ
my $strA=$self->get_compressed_logs('A'); - Returns โ String
- Args โ char
๐ฏ sequence_match_weight ๐ฏ
- Title โ
sequence_match_weight - Usage โ (none given)
- Function โ this method will calculate the score of a match, based on the PWM if such is associated with the matrix object. Returns
undefif no PWM data is available. - Throws โ if the length of the sequence is different from the matrix width ๐ต
- Example โ
my $score=$matrix->sequence_match_weight('ACGGATAG'); - Returns โ Floating point
- Args โ string
๐ธ get_all_vectors ๐ธ
- Title โ
get_all_vectors - Usage โ (none given)
- Function โ returns all possible sequence vectors to satisfy the PFM under a given threshold
- Throws โ if threshold is outside of
0..1(no sense to do that, bestie) - Example โ
my @vectors=$self->get_all_vectors(4); - Returns โ Array of strings
- Args โ (optional) floating
and that's the whole thing, no notes ๐ you made it to the end, so proud of you fr fr ๐ฅนโจ
this page is perl v5.32.1, dated 2021-08-15, and it's giving absolute legend behaviour. mwah, go write some matrices ๐๐๐ฆ
๐ฆ where this page comes from
- debian bookworm: libbio-perl-perl 1.7.8-1 ยท Perl
- debian trixie: libbio-perl-perl 1.7.8-1 ยท Perl
- ubuntu jammy: libbio-perl-perl 1.7.8-1 ยท Perl
- ubuntu noble: libbio-perl-perl 1.7.8-1 ยท Perl
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms