How to prevent wget from creating duplicates

wget is used to download file from internet. For instance:

So far so good but two things may happen. First, you may interrupt the download. Second, you may load the file again. Sometimes, files are huge and you do not want to download the same file again.

In the first case, this is eve worse: imagine you have downloaded half of the file and you interrupt the process. Then, you call wget again, you wait, it is over and your are happy. However, because there was already a file called “test.csv” locally, wget downloaded the new file into test.csv.1 ! Moreover, it started the download from srcratch.

So, the solution is to used the two options -c and -N . The first one tells to continue an interrupted download where it was stopped. And, the -N option checks the timestamps to prevent the download of the same file.

Posted in Linux | Tagged | Leave a comment

Meaning of Real, User and Sys time statistics

Under Linux, the time command is quite convenient to get the elapsed time taken by a command call. It is very simple to use: just type your command preceded by the time command itself. For instance:

The output looks like

In brief, Real refers to actual elapsed time including other processes that may be running at the same time; User and Sys refer to CPU time used only by the process (here the df command).

More precisely:

  • Real is wall clock time – time from start to finish of the call including time used by other processes and time the process spends blocked (for example if it is waiting for I/O to complete).
  • User is the actual CPU time used in executing the process. Other processes and time the process spends blocked do not count.

  • Sys is the amount of CPU time spent in the kernel within the process.

So, User + Sys is the actual CPU time used by your process

For more details, you can consult this quite precise description

https://stackoverflow.com/questions/556405/what-do-real-user-and-sys-mean-in-the-output-of-time1

Posted in Linux | 2 Comments

git : How to remove a big file wrongly committed

I added a large file to a git repository (102Mb), commited and push and got an error due to size limit limitations on github

Here, you see the path of the file (coverage/sensitivity/simualted.bed).

So, the solution is actually quite simple (when you know it): you can use the filter-branch command as follows:

Posted in Computer Science | Tagged , | Leave a comment

git and github : skip password typing with https

If you clone a github repository using the https:// method (instead of ssh), you will have to type your username and passwor all the time.

In order to avoid having to type you password all the time, you can use the credential helpers since git 1.7.9 and later.

where

means “keep the credentials cached for 2 hours. (default is 15 minutes).

You can also store the credentials permanently using

Posted in Computer Science | Tagged | Leave a comment

failed to convert from cram to bam (parse error CIGAR character)

In order to convert a bioinformatic file from CRAM to BAM format, I naively used the samtools command available on a cluster but got this error:

After a few commands trying to fix the issue, I realised that the error message contained the SAM label. This indicates that samtools version is a bit old. And indeed it was. I then used version 1.6 of samtools and it worked out of the box.

Posted in bioinformatics | Tagged | Leave a comment

How to mount and create a partition on a hard drive dock (fedora)

I got a new hard drive (2.7Tb) but wanted to use it with a docking station. Here are the steps required to use it under my Fedora box.

First, I naively went into the Nautilus File Browser hoping to see the hard drive mounted automatically. Of course it was not there: the hard drive is new and has no partition.

So, first, let us discover and check that the drive can be seen. We can use the fdisk command:

You can see in this case that the disk is on device /dev/sdb.

I then started the tool gparted and in the top right corner you can see the /dev/sdb device that should also indicate the size of your hard drive as shown in this image:

As you can see the partition and file system are unallocated. First, you need to go to the menu

Device/Create Partition Table

to create a partition table on this hard drive.

Then, you can create a new partition by going to

Partition/New

Here, you get a new window that looks like:

I allocated the entire space to one partition. In the menu you need to give a label and a name. The name is for you, the label is for the system so for the label remain simple and do not use special characters (except if you know what you are doing).

For the filesystem I kept the default (gpt). Finally, once you are done, you need to press the apply button. You should be ready in a few seconds.

Go back to Nautilus File Browser and here you can see the new hard drive partition (in theory).

Change permission

Finally, you will see that in Nautilus, you can not create any folder or files: you do not have permissions. To change this, you need to be in the list of sudo users. Then, go the path where your hard disk is mounted and type:

Posted in Linux | Tagged , | Leave a comment

AWK: convert into lower or upper cases

In order to convert a bash variable to lower case with awk, just use this command:

If you want to convert the content of a file (called data.csv) to lower case:

Of course to convert into upper case, simply use the function toupper() instead of tolower().

Note also that a better tool to avoid issues with special characters might be the tr unix command:

Posted in Linux | Tagged , | Leave a comment

How to sort a dictionary by values in Python

By definition, dictionary are not sorted (to speed up access). Let us consider the following dictionary, which stores the age of several persons:

If you want to sort this dictionary by values (i.e., the age), you must be another data structure such as a list, or an ordered dictionary.

Use the sorted function and operator module

Sorted_d is a list of tuples sorted by the second element in each tuple. Each tuple contains the key and value for each item found in the dictionary. If you look at the content of this variable, you should get:

Use the sorted function and lambda function

If you do not want to use the operator module, you can use a lambda function:

The computation time is of the same order of magnitude as with the operator module. Would be interesting to test on large dictionaries.

Use the sorted function and return an ordered dictionary

In the previous methods, the returned objects are list of tuples. So we do not have a dictionary anymore. You can use an OrderedDict if you prefer:

Use sorted function and list comprehension

another method consists in using list comprehension and use the sorted function on the tuples made of (value, key).

Here the output is a list of tuples where each tuple contains the value and then the key:

Python 3.6 native sorting

In Python 3.6, the iteration through a dictionary is sorted. Consequently, you can just use:

The other advantage with this method is speed and the ability to return tuples as pairs of key/value or value/key.

Benchmark

Here is a quick benchmark made using the small dictionary from the above examples. Would be interesting to redo the test with a large dictionary. This was done within a PYthon 3.6 environment.

What you can see is that the native Python dictionary sorting is pretty cool followed by the combination of the lambda + list comprehension method. Overall using one of these four methods would be equivalent though (factor 2/3 at most).

This image was created with the following code.

Posted in Python, Uncategorized | Tagged , | Leave a comment

Python: how to copy a list

To explain how to create a copy of a list, let us first create a list. We will use a simple list of 4 items:

Why do we want to create a copy anyway ? Well, because in Python, this assignement creates a reference (not a new independent variable):

To convince yourself, change the first item of list2 and then check the content of list1, you should see that the two lists have been modified and contain the same items.

So, to actually copy a list, you have several possibilities. From the simplest to the most complex:

  • you can slice the list.
  • you can use the list() built in function
  • you can use the copy() function from the copy module. This is slower than the previous methods though.
  • finally, if items of the list are objects themselves, you should use a deep copy (see example below):
  • To convince yourself about the interest of the latter method, consider this list:

    you should see that changing list2, you also changed list1. If this is not the intended behviour, you should consider using the deepcopy.

    Posted in Python | Tagged , | Leave a comment

    Python: ternary operator

    In C language (and many other languages), there is a compact ternary conditional operator that is a compact if-else conditional construct. For instance, in C, a traditional if-else construct looks like:

    and the equivalent ternary operator looks like:

    As in the if-else code, only one expression x or y is evaluated.

    In Python, from version 2.5, you would write:

    More formally the ternary operator is written as:

    So condition is evaluated first then either x or y is returned based on the boolean value of condition.

    You can use ternary operator within list comprehension. For example:

    Posted in Python | Leave a comment