Organizing your oligos automatically

Ordering any random oligos is fairly fast. In my case, I order oligos @eurofins, and I ordered them in a couple of minutes. I normally decide on an arbitrary name (that makes at least sense in this second) and paste my sequence. At the same time, I am using ApE, a wonderful plasmid editor, and annotate the respective oligo sequence as a feature. This is kind of cumbersome, I don’t have a proper list of all my oligos and can hardly annotate for what reason I ordered that oligo. At the same time, I am working on several projects and would love to re-use oligos in one or the other way, so it would be wonderful having this “oligo list” as a feature file. So I was thinking of creating an automated workflow that catches every ordered oligo, parses all the meta data, stores it in a MySQL database and then creates every now and then an ApE feature libary. Here, I show you how I did it!

The idea


Basically, I order oligos from Eurofins and send the order confirmation mail to a specific email address that has only one purpose: Receiving confirmation emails and forward a copy to my business address. Then, I am calling once a day with a cronjob a php script on my webserver. This script executes a Python script, talking to the IMAP server of the confirmation receiving email address and parse the incoming mails. The parsed data is then returned to the php script, that fetches the parsed data as a JSON object and then inserts the data into a MySQL database. Another Python script, locally installed on my computer, is scheduled to run once a day (after the cronjob ;)) and overwrites the default ApE feature library with my extended one. Then I just use the key combo “Ctrl+K” and annotate my sequence of interest. Fairly easy, and you can’t imagine what oligos you can still use (and you have around!).

The mail parsing

The Python script is fairly easy. I am using the following libraries:

  • imaplib (accessing the IMAP server)
  • email (parsing the emails to get their [meta]data)
  • re (parsing the email body)
  • quopri (decoding email body to avoid some things, such as =3D, …)
  • json (encoding dictionary to return data to PHP script)

The following code I am using to iterate over the mails:

import imaplib as im

conn = im.IMAP4(s)

flag, mails ='ISO-8859-1', 'ALL')

for id in mails[0].split(" "):
    _, msg = conn.fetch(id,'(RFC822)')

Then, I do some magic to the mail (here the variable  msg .

msg = quopri.decodestring(msg[0][1]) # get rid of =3D and other =\r\n...

M = email.message_from_string(msg) # Parse email

body = M.get_payload().strip() # Get the body text, I'm interested in.

Because the emails are plain text, I can proceed and do some regular expressions to find the oligos. Otherwise, you can use BeautifulSoup or any other html parser to get “plain text”.

The PHP script

First, why do I use PHP __and__ Python? Well, some things are easier in PHP than in Python, for example accessing MySQL databases. In PHP it is straight forward, create a PDO object with your login information and do a query afterwards. Done! Here’s a code snippet I am using to execute the Python script and retrieving the JSON data. Then, I am just storing the stuff in my database.

$db = new PDO('mysql:dbname=MYDBNAME;host=localhost','MYDBUSER','MYPASS');

/* Execute Python script
    and fetch JSON object */
$command = escapeshellcmd('python');
$output = json_decode(shell_exec($command));

echo "<pre>";

/* If there are any new oligos */
if (is_array($output))
    /* Iterate over each oligo */
    foreach ($output as $o)
        $query = "INSERT INTO oligos 
                  (name, sequence, length, order_id, purity, scale, order_date, cost)
                  VALUES ('".$o->name."',
        /* Add oligo to database */      
        $success = $db->query($query);                  
        /* If I could succeed, tell me! */
        if ($success)
            echo "Oligo __".$o->name."__ was added to DB.\n";

Adding oligos to feature library

Well, I fetch the data from the database using another PHP script, that converts the interesting data to another JSON object, my Python script reads and decodes that, opens the default feature library as a template and adds my  oligos to the end.

If you have any questions or you would like to see more, just use the discussion box below!