2013-02-18

Use Imagemagick to swap blue and red colors

For a presentation I wanted an image to appear in magenta, rather than in cyan. This is how the image originally looks like:




blue.jgp

With the following command I separated the R, G, B color channels and swapped the Red and the Green channel, to produce a picture in which all blue colors appear in red:

$ convert blue.jpg -separate -swap 0,1 -combine red.jpg

 And that is how the result looks like:




red.jpg


I got this information from: http://www.imagemagick.org/Usage/color_basics/#combine

2013-01-30

RepeatMasker ambiguous bases

When trying to use a chromosome fasta file with RepeatMasker, one might gets the error message that "ambiguous bases" were detected because the fasta file contains long stretches of N's.
In order to avoid this problem, you can replace the "N" characters by "X"'s before starting RepeatMasker.

2012-11-13

How to split a string in R using strsplit

Since the first google and Rseek.org results did not help me with my problem of splitting a string in R, I want to clip it here, so I do not have to click through all of these pages again :)

The command is as easy as this:


> y <- "100,200,300"
> x <- strsplit(y, ',')
> x
[[1]]
[1] "100" "200" "300"

2012-10-31

R: Function to perform t-test on all possible combinations of columns in a dataframe



Sometimes I need to perform a t-test on all possible combinations of the columns of a data-frame and rather than computing them all one by one, I wrote this small function:


ttest_all_possible  <- function(dataframe) {
    nr_columns = dim(dataframe)[2]
    results <- matrix(NA, nr_columns, nr_columns)
    for (i in seq(1, nr_columns)){
        for (j in seq(1, nr_columns)){
            results[i, j] <- t.test(dataframe[,i], dataframe[,j])$p.value
        }
    }
    return(results)
}


Works fine:
> test <- cbind(rnorm(10),rnorm(10), rnorm(10))
> test
             [,1]       [,2]       [,3]
 [1,]  0.64061950 -0.5643179  0.7450485
 [2,]  1.90703328 -0.8000197  0.2444611
 [3,]  0.96346636 -0.6278075  0.1283072
 [4,] -0.25758024 -0.9657888 -0.8832595
 [5,]  0.36934381  1.4149775  0.8837320
 [6,]  0.60584130  0.2556500 -0.2437533
 [7,] -0.24559562 -0.7774171  0.1279253
 [8,] -1.53276425  0.3557177  0.1611155
 [9,] -1.55474156  0.3488891  0.1025763
[10,] -0.08366046 -0.2823561  1.3618903
> ttest_all_possible(test)
          [,1]      [,2]      [,3]
[1,] 1.0000000 0.5603004 0.6495045
[2,] 0.5603004 1.0000000 0.1817692
[3,] 0.6495045 0.1817692 1.0000000

2012-10-30

How to substutitute characters in all files of a directory using regex and perl

perl -i -pe 's/CharacterToSubstiture/SubstituteWithThis/g' myfilename

Example:
Imagine I have got these two files:

shopping_list_groceries.txt:
- 2 apples
- 3 red paprika

shopping_list_clothes.txt:
- 2 pairs of socks
- 1 red t-shirt

And now I decide to not like red any more, but to prefer green:

perl -i -pe 's/red/green/g' shopping_list*txt


The text files now look like this:
shopping_list_groceries.txt:
- 2 apples
- 3 green paprika

shopping_list_clothes.txt:
- 2 pairs of socks
- 1 green t-shirt


What does the one-liner do?
perl # use perl :)
-i # change in the file, if you skip this part, the changes will be printed to stdout
-pe # I don't know
's/red/green/g' # RegEx: 
                 's # Substitute...
                 /red # ...every occurence of the string "red"...
                 /green # ...with the string "green"...
                 /g' # ... everywhere ...
shopping_list*.txt # ...in all files starting with 'shopping_list' and ending with '.txt



2012-10-02

How to print all the attributes of an object in python

from pprint import pprint
pprint (vars(my_object))     # e.g. class instance
 
 
This prints all the attributes/properties this object currently has in a neat way.

2012-09-19

How to create a tar archive

tar cvzf archive.tar.gz *files*

2012-09-18

Bash Shell: Check if a file exists, and has a size larger than 0

To check, whether a file exists and has a file size larger than 0 use this expression:



if [ -s someFile.txt ]
then
  ...
fi

Bash Shell: Check if a file exists

To check, whether a file exists use this expression:

First, create the file thisFileExists.txt:
touch thisFileExists.txt

Now, check if it exists:
if [ -f thisFileExists.txt ]
then
  echo It is there
fi

This is what it looks like in terminal:

Bash Shell: Repeat a command every x seconds, e.g. to update list of files

When running a command that changes/creates files and takes a while, one might be interested in seeing how progress is going.
For this the "watch" command can be useful:


watch -n 2 ls *file

This will print the output of "ls *file" every 2 seconds.


watch -n 2 -d ls *file

Adding -d will highlight all the changes in comparison to the last time the command was performed

2012-07-28

Creating an animated gif image

Imagemagick can also be used to create animations!

convert -delay 10 -loop 0 DSC003* animation.gif

Where DSC003* are the files that are going to be used for the animations. They can be in about any format.

As a (rather stupid) example, I created 16 barplots in R with different colors and animated this:

in R:
$ pdf('example.pdf')
$ for (i in seq(5,20)){barplot(1:20,col=rainbow(i))}
$ dev.off()

in Shell:
$ convert example.pdf example.png
$ convert -delay 10 -loop 1 example-*png animation.gif


The 16 different barplots.
The slightly psychedelic animation.








How to bulk resize JPEG pictures via Imagemagick

Sometimes you need to reduce the size of many pictures, for example to share them on the internet.

A nice way of doing this is via Shell:

convert FILE.JPG -resize 2448x1836 ./smaller_pictures/FILE.JPG

Or by using a for loop:

for F in *.JPG
do
convert $F -resize 2448x1836 ./smaller_pictures/$F
done

2012-07-17

How to get nice Python Syntax Highlighting in LaTeX documents

Pygmentize is a command that can be performed on a python Script to produce a .tex document with nice syntax highlighting

pygmentize -f latex script.py > script.tex

2012-07-14

How to install .deb packages on Linux

$ sudo dpkg -i package.deb

2012-07-08

Pickle module in Python

The pickle module in Python 2.3 can be used to save a dictionary in a file and makes it accessible for later.

import pickle

((I will add more information later))