Skip to content

Instantly share code, notes, and snippets.



Last active Dec 20, 2015
What would you like to do?
Single molecule tracking using PTAj. - input: line 12-13, image stack. - output: track file (CSV) per track, MSD file (CSV) per track
from ij import IJ
from ij.gui import Roi
from pta import PTA
from pta.gui import ShowPdata
from pta.track import DetectParticle
from import PtaParam
from java.util import ArrayList
from ij.plugin.frame import ThresholdAdjuster
from ij.process import ImageProcessor
import os, csv
from ij.plugin.filter import BackgroundSubtracter
#datafilepath = "/Users/miura/Dropbox/20130805_Osaka/data/transferrin-movement/tconc1_3.tif"
datafilepath = "/Users/araiyoshiyuki/Desktop/spool-tub_2-2.tif"
# load data as an ImagePlus object
imp = IJ.openImage(datafilepath)
# default ROI = full frame
scanAreaRoi = Roi(0,0,imp.getWidth(),imp.getHeight()),"Subtract Background...", "rolling=10 stack")
#bs = BackgroundSubtracter()
#for frame in range(imp.getStackSize()):
# ip = imp.getStack().getProcessor(frame + 1)
# bs.rollingBallBackground(ip, 10.0, False, False, False, False, False)
ip = imp.getProcessor()
ht = ip.getAutoThreshold()
ip.setThreshold(0, ht, ImageProcessor.RED_LUT)
# set no GUI mode
# set Detection Parameters
ptap = PtaParam.Builder(12,12, False).build()
### paramter customizations
# instantiate a DetectParticle object
dp = DetectParticle(ptap,imp,scanAreaRoi, True)
# set range of frames to analyze
dp.setStackRange(1, imp.getStackSize())
# start threads.
# wait till the processing finishes
# stop the thread.
# show detection results
# Particle lists
#ll = dp.getalldplist()
#print ll
def CSVwrite(srcpath, msddata):
parentdirectory = os.path.dirname(srcpath)
filename = os.path.basename(srcpath)
basename = os.path.splitext(filename)[0]
for msd in msddata:
thisid = msd.getID()
coordsPath = os.path.join(parentdirectory, basename + "_" + str(int(thisid)) +"_coords.csv")
msdPath = os.path.join(parentdirectory, basename + "_" + str(int(thisid)) + "_msd.csv")
print coordsPath
print msdPath
f1 = open(coordsPath, 'wb')
writer = csv.writer(f1)
track = msd.getTrack()
for node in track:
writer.writerow([node.getCx(), node.getCy(), node.getParam()[1],node.getParam()[2]])
f2 = open(msdPath, 'wb')
writer = csv.writer(f2)
dtA = msd.getFullDFrames()
msdA = msd.getFullMSD()
for i in range(len(dtA)):
writer.writerow([dtA[i], msdA[i]])
# track lists
tracks = dp.getLinkedPointList()
for t in tracks:
print t
# MSD analysis
msds = dp.getMSDres(5.0, True)
print "msd data size", msds.size()
for msd in msds:
print msd.getID(), msd.getA(), msd.getB(), msd.getR()
# TODO: write MSD values in a csv file.
CSVwrite(datafilepath, msds)
# discard the DetextParticle Object
dp = None
##Back to the GUI mode: without this line, PTA GUI will not showup from next time.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.