Bio::M...Matrix(3pm)

๐Ÿ“ฆ
version 1.7.8 ยท 3 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble

๐Ÿ’– 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 string
  • score โ€” returns the score as a real number
  • IC โ€” information content. returns a real number
  • id โ€” identifier. returns a string
  • accession โ€” accession number. returns a string
  • next_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 example AGWV will be [Aa][Gg][AaTt][AaCcGg]
  • width โ€” site width
  • get_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::SiteMatrix object 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::SiteMatrix object ๐ŸŽ
  • 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 (default 0: 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 0 if the argument is minus and to width if it's greater than width.
  • 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 SiteMatrix object as well for any other object inheriting from SiteMatrix.
  • 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 1 to 10, default 5. '5' means the returned characters had a 50% 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 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_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 1 and 10, where 5 is default and means the returned character had a 50% 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. N will match X, 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. N will match X, N, - and .
  • Returns โ€” array
  • Args โ€” none (works at the threshold last used for making the IUPAC string)
  • To do โ€” I have separated regexp and regexp_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) -127 to +127 for bi-directional (signed) and 0..255 for unsigned.
  • Returns โ€” String
  • Args โ€” array reference, followed by a max value and direction (optional, default 1-unsigned), 1 unsigned, 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), 1 unsigned, 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..a coding. ๐Ÿ”ฅ
  • 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 undef if 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