Autarchy of the Private Cave

Tiny bits of bioinformatics, [web-]programming etc

    C: how to specify comparison operators floating precision

    11th June 2009

    There is no way I’m aware of to do what the title says. However…

    I’m sure that you are aware of the fact that floats representation in any programming language is limited by the precision of the internal binary representations. In other words, you can never have an exact float representation – there will always be some precision associated with the float you are working with. The simplest example is the difference in precision between the float and double types in C.

    Suppose I have the following code fragment:
    [C] if ( result.score >= input->raw_cut_off ) [/C]

    Both result.score and input->raw_cut_off are of type float, and can have positive and negative values. When compared with the greater than or equal ( >= ) operator, it is not always that condition is true – for the precision reasons shortly mentioned above.

    As I already said, there is no precision specification for equality operators in C. But it is quite simple to “invent” precision specification; e.g. if I wanted to test for equality only, I could write
    [C] if ( fabsf( result.score – input->raw_cut_off ) < 0.000001 )[/C] In this example, I'm effectively asking for 6-digit precision for the equality comparison of floating-point values. Note, that if you replace that 0.000001 with the actual precision limit of the floating type you are using, you will be "exactly" comparing floating-point numbers - up to that type's precision, of course :) .

    The first-most example with the >= operator can be rewritten as
    [C] if ( result.score > ( input->raw_cut_off – precision) ) [/C]
    where precision is exactly what it is named, e.g. precision = 0.000001.

    Sources used:

    Share

    Posted in how-to, Programming | No Comments »

    How to use ffmpeg if libmp3lame support is not present

    12th April 2008

    I’m providing regularly updated compiled linux and freebsd ffmpeg binaries, and also described how to use ffmpeg on shared hostings if not all the required libraries (like libmp3lame) are present. However, the solution recommended might not “fit all”, so here is another one – simpler and even more portable/universal than setting LD_LIBRARY_PATH.
    Read the rest of this entry »

    Share

    Posted in Software | 4 Comments »

    MySQL: INSERT IF NOT EXISTS syntax

    18th October 2007

    To start: as of the latest MySQL, syntax presented in the title is not possible. But there are several very easy ways to accomplish what is expected using existing functionality.

    There are 3 possible solutions: using INSERT IGNORE, REPLACE, or INSERT … ON DUPLICATE KEY UPDATE.

    Imagine we have a table:

    1. CREATE TABLE `transcripts` (
    2.  `ensembl_transcript_id` varchar(20) NOT NULL,
    3.  `transcript_chrom_start` int(10) unsigned NOT NULL,
    4.  `transcript_chrom_end` int(10) unsigned NOT NULL,
    5.  PRIMARY KEY  (`ensembl_transcript_id`)
    6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    Now imagine that we have an automatic pipeline importing transcripts meta-data from Ensembl, and that due to various reasons the pipeline might be broken at any step of execution. Thus, we need to ensure two things: 1) repeated executions of the pipeline will not destroy our database, and 2) repeated executions will not die due to ‘duplicate primary key’ errors.

    Method 1: using REPLACE
    Read the rest of this entry »

    Share

    Posted in Programming | 46 Comments »

    Useful Python looping techniques

    26th September 2007

    These are all excerpts from the Python documentation.

    To synchronously and simultaneously loop over two sequences:

    1. questions = ['name', 'quest', 'favourite colour']
    2. answers = ['Lancelot', 'the holy grail', 'blue']
    3.  
    4. for q, a in zip(questions, answers):
    5.     print 'What is your %s?  It is %s.' % (q, a)

    To loop over a sequence with both key and value:
    Read the rest of this entry »

    Share

    Posted in Programming, Python | No Comments »

    How to sort Python dict (dictionary)

    26th September 2007

    Sample script (copypasted from Well House Consultants training course):
    click the PLAIN TEXT header for copy-pasteable version

    1. #!/usr/local/bin/python
    2.  
    3. author = {"php":"Rasmus Lerdorf",\
    4.     "perl":"Larry Wall",\
    5.     "tcl":"John Ousterhout",\
    6.     "awk":"Brian Kernighan",\
    7.     "java":"James Gosling",\
    8.     "parrot":"Simon Cozens",\
    9.     "python":"Guido van Rossum"}
    10.  
    11. langs = author.keys()
    12. langs.sort()
    13.  
    14. for language in langs:
    15.     print language,"is the child of",author[language]

    You can also define the Python ksort() function similar to that found in PHP:
    Read the rest of this entry »

    Share

    Posted in Programming, Python | 1 Comment »