Showing posts with label MFISH. Show all posts
Showing posts with label MFISH. Show all posts

Thursday, March 20, 2014

Combining five images into one rgb MFISH image using scikit-image implementation of HSV color space.

MFISH is a molecular cytogenetic technics using combination of fluorescent DNA probes used to recognize chromosomes and to detect some type of rearrangements.
In a previous post, five images corresponding to different fluorochromes were combined using matrix multiplication. The matrix coefficients were chosen empiracally and validated a-posteriori by the aspect of the rgb image produced.
Scikit-image is a python library implementing color space conversions for example between RGB and HSV. To display simultaneously the images of the five spectral components, or the five fluorochromes:


Five greyscaled images of the spectral components shown in false colors (cy55, cy5, SpGreen, SpOrange, TexasRed)
 The five greyscales images were first converted into 8bits rgb colors and their hue were modified with colors chosen prior combining them:
Greyscaled images converted into rgb images
Then the five images were added to produce one rgb MFISH image of type float64:

Let' produce the HSV histograms of the image:
Since the produced image lacks some contrast, the image exposition can be corrected such that the image background becomes black and the colors become brighter:

Now the histograms of the HSV chanels look like:
The linear stretch was also chosen based on the resulting rgb image.

Code:

The whole code is available in a ipython notebook.

Notebook


Code on SageMathCloud:

The ipython notebook can be run on SageMathCloud. For some reasons, it's not possible to download images from github to sagemathcloud, so local images are used.

Public ipython notebook on sagemathcloud:

Images database:

https://github.com/jeanpat/MFISH 

Many thanks to Tony S Yu for his help



Wednesday, February 5, 2014

Making image processing on SageMathCloud. Extraction of chromosomes from multispectral images.

SageMathCloud is a plateform to do mathematics, it also offers the possibility to use a ipython notebook online. Several python libraries are available: numpy, scipy, scikit-image (0.7, 0.93), mahotas 1.1, pandas ...
Once an account is opened (it's free), a project created, some multispectral images were uploaded in a directory of the project. In that project, an ipython notebook was created, this notebook can be run in a project directly in SageMathCloud.

Removing spurious pixels brought by ndimage.find_objects():

The following model image  is a grey scaled image:


Grey image with a pseudo color map

find_objects() succeeds in extracting the letters:

Extracted particles are not convex, their bounding box can contains pixels from other particles in their neighborhood.
For example the letter A, contain pixels belonging to other segmented (labelled) letters. The image of A contains pixels belonging to the letter r and to the letter B. Those spurious pixels have to be eliminated. find_object() provides the bounding box of the segmented particles in a list. The bouding box index seems to correspond to the particle label, this could be used to erase the spurious pixels:

A python function can be written:


It takes two images as arguments: a grey level  image (or stacked images) and a label image and returns a list of images containing the extracted particles from the grey level image. This implementation is much simpler and faster than a previous one .

 

Chromosomes extraction from a MFISH image:

After having uploaded images on cloud.sagemath, for example:

The five components (DAPI excepted) spectral images can be combined in one color image. There is an additional kar image resulting of the segmentation of the MFISH images :
5 spectral images combined into 3 to make a color (rgb) image (left). kar image (right)

Let's use the kar image to produce a label image so that each chromosome is defined by a single label (or grey level):
label image (right)

Now the extraction of individual chromosomes can be done, from the DAPI image:

or from stacked images:

The kar image identifies the chromosomes:

Let's make galleries of chromosomes

The extracted chromosome images have different size. Let's add borders to the images so that the images have all the same size :



Then let's build two galleries, one with MFISH images and one with inverse DAPI chromosomes, the code is visible in the notebook; it yields:
Left: MFISH chromosomes, Right:inverse DAPI

It remains to auto rotate the chromosome. A draft code, at the end of the ipython notebook  in the SageMathCloud project, tries to explore another method than the previously proposed. It remains also to arrange the chromosome according to their classification.

Download the ipython notebook

Go to the notebook on sagemathcloud




Sunday, May 20, 2012

FlyFISH:navigation in the ADIR MFISH images database

FlyFISH allows to visualize images of the ADIR MFISH database. FlyFISH is written in python, the GUI is based on pyQt4, images are loaded and displayed with imread, qimage2ndarray. To work properly, FlyFISH requires:
The path to the DatabaseMFISH.xml file must be modified in the script:
root=et.parse('/home/simon/DatabaseMFISH.xml').getroot() 
and the path to database top directory:
workdir="/home/simon/MFISH/"

The script was tested on ubuntu 12.04, to run faster, the image size was reduced on display:

Thanks to ashren.

Wednesday, May 9, 2012

Converting a directories tree to a xml object: test with MFISH dataset

Once the MFISH dataset  reordered properly,   a xml object representing the directories hierarchy is build.
The  MFISH folder contains three subfolders: ASI, PSI and Vysis . Let's build a xml file called ASI. let's find the line 51 in the following python script and modify to fit the path to the ASI folder, for example:

topdir = '/home/user/MFISH/ASI'

then run the script:

# -*- coding: utf-8 -*-
"""
Created on Tue Jan 31 13:30:22 2012

@author: Jean-Patrick Pommier 
    with a huge help of Mont29:
    http://www.developpez.net/forums/u238430/mont29/
"""
import lxml.etree as et
import os,re

def filtreImages(files):
    filtlist=[]
    #a file name ending with  .TIF or .tif
    regex=re.compile('.(TIF|tif)$')
    if len(files)>0:
        for f in files:
            result=regex.search(f)
            if result<>None:
                #print f
                filtlist.append(f)              
    #print filtlist
    return filtlist

def makeNodes(node_dirs, root, leveldirlist, root_level, files):
    # Penser à aérer le code, c’est plus agréable à lire...
    code = {0: 'slide', 1: 'field', 2: 'fluorochrome', 3: 'TooDeep'}
    for d in leveldirlist:
        child = et.Element(code[root_level], name=d)
        if code[root_level] == 'field':
            child.set('ROI', 'x0,y0,x1,y1')
            child.set( 'position','-1,-1')
        nodes_dirs[os.path.join(root, d)] = child
        nodes_dirs[root].append(child)
    # Le "if len(file) > 0" est inutile, si une liste est vide,
    # itérer dessus revient à ne rien faire ;)
    #
    # files est indépendant de leveldirlist, il faut donc boucler dessus
    # "à part"!
    for image in files:
        #print " root:", root, " root_level:", root_level, " image:", image
        child = et.Element("image",name=image, exposure='0.0')
        # Ne surtout pas utiliser d ici! Sinon, on écrase les nœuds définis
        # dans la boucle précédente... image devrait faire l’affaire :p
        nodes_dirs[os.path.join(root, image)] = child
        nodes_dirs[root].append(child)
                  
level={}            
if __name__ == '__main__':
    #topdir = '/home/claire/Applications/ProjetPython/testxml/P'
    topdir = '/home/simon/MFISH/ASI'
    projetxml = et.Element('CytoGenet')  # racine
    parent = projetxml
    nodes_dirs = {topdir: parent}
    ln_root = len(topdir)
    
    for root, dirs, files in os.walk(topdir):
        lvl = root[ln_root:].count(os.path.sep)
        #print nodes_dirs
        filtered=filtreImages(files)
        #print filtered
        makeNodes(nodes_dirs, root, dirs, lvl,filtered)
        
        
            
    print(et.tostring(projetxml,pretty_print=True))
    print projetxml.getchildren()[0].get("name")
    slides=projetxml.getchildren()[0]
    print len(slides)
    print projetxml.getchildren()[1].get("name")
    slides=projetxml.getchildren()
    
    metaphases1=slides[0].getchildren()
    
    metaphases2=slides[1].getchildren()
    for m in metaphases2:
        print m.get("name")
    
   
The script just display the xml object:

<ASI>
  <slide name="A32">
    <field name="01" ROI="x0,y0,x1,y1" position="-1,-1">
      <fluorochrome name="SpGreen">
        <image name="A3201EXG.tif" exposure="0.0"/>
      </fluorochrome>
      <fluorochrome name="SpOrange">
        <image name="A3201EXO.tif" exposure="0.0"/>
      </fluorochrome>
      <fluorochrome name="TexasRed">
        <image name="A3201EXT.tif" exposure="0.0"/>
      </fluorochrome>
      <fluorochrome name="Cy5-5">
        <image name="A3201EX5.tif" exposure="0.0"/>
      </fluorochrome>
      <fluorochrome name="DAPI">
        <image name="A3201EXD.tif" exposure="0.0"/>
      </fluorochrome>
      <fluorochrome name="Cy5">
        <image name="A3201EXC.tif" exposure="0.0"/>
      </fluorochrome>
    </field>

Tuesday, May 8, 2012

One shot scripts to reorder images in the ASI/PSI/Vysis-MFISH dataset

The images belonging to the MFISH dataset are not ordered in a hierachy of directories defined as project/slides/field_xy/fluorochromes/image.
To navigate through that images database as in the previous post, the images need to be reordered.
The central part of the following image displays the original images arrangement, the right side shows the images after reordering. We have for example a slide called A01 belonging to the ASI MFISH dataset . For this slide, five xy fields were taken (from 01 to 05) . The folder called Kar, contains the segmented image (the chromosome 1 is set to grey level  and so on).


Three dirty python scripts (pylint 1.76/10 !) reorder the MFISH dataset. One for each images subset (ASI, PSI or Vysis). Prior running the scripts:
  • make three folders (ASI, PSI, Vysis) in the top directory (MFISH)
  • select all the slides whose name start by 'A', cut and copy them in the ASI folder and so on the the PSI slides (starting by a 'P') and the Vysis slides (starting by a 'V').
  • modify the path to the top directory inside the script and run the ASI script to reorder the images from ASI images subset.
  • run the PSI script ...
  • run the Vysis script.
To modify the path find the line:

path='/home/simon/MFISH/'+'ASI'
Modify it as:
 
path='/fit/to/your/path/MFISH/'+'ASI'
 
The pygmentized ASI script is shown:

# -*- coding: utf-8 -*-
"""
Created on Thu Apr  5 14:43:39 2012

@author: jean-pat

3   532
5   Cy 5.5
6   568
A   S. Aqua
C   Cy 5
D   DAPI
E   DEAC
F   Far Red
G   S. Green
I   FITC
O   S. Orange
R   S. Red
T   Texas Red
Y   S. Gold

"""

import sys,os
from shutil import move,rmtree
import re

#Choix répertoire ex Vysis ou ASI ou PSI/Cytocell
path='/home/simon/MFISH/'+'ASI'
slidelist=os.listdir(path)

fluoASI={'5':'Cy5-5','C':'Cy5','D':'DAPI','G':'SpGreen','O':'SpOrange','T':'TexasRed','K':'Kar'}
fluoPSI={'C':'Cy5','D':'DAPI','E':'DEAC','I':'FITC','3':'532','6':'568','K':'Kar'}
fluoVysis={'A':'SpAqua','D':'DAPI','F':'FarRed','G':'SpGreen','R':'SpRed','Y':'SpGold','K':'Kar'}
print "slidelist[0]",slidelist[0]
for apv_slide in slidelist:
    #listfield=os.listdir(os.path.join(path,slidelist[0]))
    listfield=os.listdir(os.path.join(path,apv_slide))
    print listfield
    #cherchons le numero des images dans le nom des fichiers
    #contient V au début et .tif à la fin
    slidecode=apv_slide[0:1]#V for vysis, A for ASI, P for PSI
    motif=re.compile('.tif$')
    imagelist=[]
    for image in listfield:
        if not(motif.search(image)==None):
            imagelist.append(image)
    #print imagelist
    #obtenir le numero des images
    imagenumberlist=[]
    for image in imagelist:
        number=image[3:5]
        if number not in imagenumberlist:
            imagenumberlist.append(number)
    #print imagenumberlist
    #créer un répertoire correspondant au numéro de l'image(métaphase)
    for imNb in imagenumberlist:
        metaphasepath=os.path.join(path,apv_slide,imNb)
        os.makedirs(metaphasepath)
        #puis déplacer les images imNb dedans
        imagesToMove=[item for item in imagelist if item[3:5]==imNb]
        #print imagesToMove
        #print 'MOVING ',imNb, '************'
        for fluo in imagesToMove:
            sourceimage=os.path.join(path,apv_slide,fluo)
            destimage=os.path.join(path,apv_slide,imNb,fluo)
            #os.renames(sourceimage,destimage)
            #print fluo
            #print fluo[7:8],'component:',fluoVysis[fluo[7:8]]
            #make fluorochrome directories
            os.makedirs(os.path.join(path,apv_slide,fluoASI[fluo[7:8]]))
            #print 'Source',sourceimage,os.path.exists(sourceimage)
            #print 'Dest',destimage,os.path.exists(destimage)
            #move component image into component directory
            destComponentImage=os.path.join(path,apv_slide,fluoASI[fluo[7:8]],fluo)
            #print ' **** move compenent images into comp dirs'
            move(sourceimage,destComponentImage)
            #print ' **** ----move compenent dir into metaphase dirs'
            sourceCompDir=os.path.join(path,apv_slide,fluoASI[fluo[7:8]])
            destMetaphaseDir=metaphasepath
            move(sourceCompDir,destMetaphaseDir)
    #fusionner les dossiers correspondant à une même lame
    ## ex V29_62 et V29_63 dans V29

mfishSlidesList=os.listdir(path)
print mfishSlidesList
slidecode='A'#apv_slide[0:1]#V for vysis, A for ASI, P for PSI
    #store the number of each slide V29_62 et V29_63 -> 29
#==============================================================================
#     #For a given slide ex V29_62
#==============================================================================
dirtomake=[]
for slide in mfishSlidesList:
    slidenumber=slide[1:3]# ex 13, 14...29 for vysis
    #print slidenumber
    if slidenumber not in dirtomake:
        dirtomake.append(slidenumber)
        
for d in dirtomake:
    slidename=slidecode+d
    print "makedir " , os.path.join(path,slidename)
    os.makedirs(os.path.join(path,slidename))
    
for newdir in dirtomake:
    slidename=slidecode+newdir
    #find all slides starting by slidename
    slides_content_to_move=[] #ex all V29_xx
    #print "moving slides starting by " , slidename
    for movingslide in mfishSlidesList:        
        
        if movingslide[0:3]==slidename:
            slides_content_to_move.append(movingslide)
        
        for moving in slides_content_to_move:
            current_content=os.listdir(os.path.join(path,moving))
            #print "current content of" , movingslide , "is =" , current_content
            
            for cc in current_content:
                source=os.path.join(path , movingslide , cc)
                dest=os.path.join(path , slidename , cc)
                print "move ", source, "-> dest" , dest
                move(source,dest)
    #print "end",mfishSlidesList
    
#supprimer les lames vides
for empty_slide in mfishSlidesList:
    curpath=os.path.join(path,empty_slide)
    print empty_slide
    print "remove(",os.path.join(path,empty_slide),")"
    print 'dir?',os.path.isdir(curpath),'vide?',os.listdir(curpath)
    os.removedirs(curpath)