Friday, July 1, 2011

Karyotyping with pygame?

In a cytogenetic application, it is necessary to interact with chromosomes in order to classify them that is to do a karyotype.The pygame library may be suitable for that. To do a karyotype, supposing that all the chromosomes of a metaphase are segmented, the following actions are required:
  • moving the chromosomes with the mouse (translations)
  • setting the chromosome orientation (rotations)
  • modifying the way the chromosomes are displayed (DAPI, inverse DAPI, color).
  • Setting the chromosome classification according to its location in the image.
The following code, directly adapted from a pygame tutorial, displays a moving chromosome (no mouse interaction) on a background image which may be used to classify the chromosomes:

Screenshot of the running script
# -*- coding: utf-8 -*-
"""
Éditeur de Spyder
"""
import scipy
import pygame as pyg
#============================================
class GameObject:
    def __init__(self, image, height, speed):
        self.speed = speed
        self.image = image
        self.pos = image.get_rect().move(0, height)
    def move(self):
        self.pos = self.pos.move(0, self.speed)
        if self.pos.right > 600:
            self.pos.left = 0
#==========================================
screen=pyg.display.set_mode((640,480))
chrom=pyg.image.load('/home/claire/Applications/
ImagesTest/jp/Jpp48/13/DAPI/particules/part3.png').convert()
backFile='/home/claire/Applications/ProjetPython/
sprite/backgroundClassifier.png'
background = pyg.image.load(backFile).convert()
screen.blit(background, (0, 0))
karyo=[]
chr1=GameObject(chrom, 1, 1)
karyo.append(chr1)
#===============================================
while 1:
    for event in pyg.event.get():
        if event.type in (pyg.QUIT, pyg.KEYDOWN):
            pyg.sys.exit()
        for chr1 in karyo:
            screen.blit(background, chr1.pos, chr1.pos)
        for chr1 in karyo:
            chr1.move()
            screen.blit(chr1.image, chr1.pos)
        pyg.display.update()
        pyg.time.delay(100)

No comments: