def savePkl(db,f):
    pklPath = os.path.join(os.path.split(outputDir)[0],'pklFiles')
    if not os.path.exists(pklPath): makeDir(pklPath)
    fpath = os.path.join(pklPath,f)
    ofile = open(fpath,'w')
    pickle.dump(db,ofile)
    ofile.close()
    return True
def tempFunction():
    sourceDir = '/home/jaggu/marcotte_project/boulgakov/microscope/2016-July/2016-07-21t/before_after'
    destDir = os.path.join(sourceDir,'convertedImages')
    makeDir(destDir)

    all_subDir = [d for d in os.walk(sourceDir).next()[1] if not d.endswith('trace') ]

    for subDir in all_subDir:
        convertDir(subDir)
    return True
def colocalizeImages(dateStamp, pathDir, destDir):
    print dateStamp
    print pathDir
    print destDir
    processed_dir = os.path.join(destDir, "colocalizePKL")
    makeDir(processed_dir)

    pklFname = os.path.join(processed_dir, dateStamp + '_colocalize.pkl')
    ofile = open(pklFname, 'w')
    subDir_colocal_dict = collections.defaultdict(list)
    allSubDir = [
        os.path.join(pathDir, d) for d in os.walk(pathDir).next()[1]
        if d.startswith('16')
    ]  #Dirty exception
    for subDir in allSubDir:
        print "Processing directory %s ..." % (subDir)
        f = str()
        allFile_pairs = list()
        f = os.path.split(subDir)[1]
        allPkl = [
            os.path.join(subDir, f) for f in os.listdir(subDir)
            if f.endswith('.pkl') and 'c1.tif.png' in f
        ]
        for pklF in allPkl:
            file_list = list()
            suf = ".".join(pklF.split('.')[1:])
            pre = pklF.split('.')[0]
            if pre.endswith('c1'):
                for nbr in range(1, 6):
                    f = pre[:-2] + 'c' + str(nbr) + '.' + suf
                    if os.path.isfile(f): file_list.append(f)
                    else:
                        file_couple = list(itertools.combinations(
                            file_list, 2))
                        allFile_pairs.append(file_couple)
                        break
            else:
                raise SystemExit("Need atleast two files to compare")
        for subDir_list in allFile_pairs:
            for f1, f2 in subDir_list:
                colocal = Colocalize(f1, f2)
                overlapPeaks = colocal.checkPosOverlap()
                subDir_colocal_dict[subDir].append([
                    f1, f2, colocal.f1NbrPeaks, colocal.f2NbrPeaks,
                    len(overlapPeaks)
                ])

    pickle.dump(subDir_colocal_dict, ofile)

    return subDir_colocal_dict
Beispiel #4
0
 def saveProjected(self,img,offsetDict,type='max',dest_dir=None,fname=None,level=1):
     if not dest_dir:
         dest_dir = os.path.split(self.imagesList[self.IDX])[0]
     if not fname:
         fname = '.'.join([self.imagesList[self.IDX][:-4],type+'_projection'])
     
     target_dir = os.path.join(dest_dir,'projectionImages')
     cfunc.makeDir(target_dir)
     f_img_name = os.path.join(target_dir,fname+'.level_'+str(level)+'.image.tif')
     f_offset_name = os.path.join(target_dir,fname+'.level_'+str(level)+'.offset_dict.pkl')
 
     cv2.imwrite(f_img_name,img)
     with open(f_offset_name,'wb') as ofile:
         pickle.dump(offsetDict,ofile)
     
     return f_img_name, f_offset_name
def colocalizeImages(dateStamp,pathDir,destDir):
    print dateStamp
    print pathDir
    print destDir
    processed_dir = os.path.join(destDir,"colocalizePKL")
    makeDir(processed_dir)

    pklFname = os.path.join(processed_dir,dateStamp + '_colocalize.pkl')
    ofile = open(pklFname,'w')
    subDir_colocal_dict = collections.defaultdict(list)
    allSubDir = [os.path.join(pathDir,d) for d in os.walk(pathDir).next()[1] if d.startswith('16')] #Dirty exception
    for subDir in allSubDir:
        print "Processing directory %s ..."%(subDir)
        f = str()
        allFile_pairs = list() 
        f = os.path.split(subDir)[1]
        allPkl = [os.path.join(subDir,f) for f in os.listdir(subDir) if
                  f.endswith('.pkl') and 'c1.tif.png' in f]
        for pklF in allPkl:
            file_list = list()
            suf = ".".join(pklF.split('.')[1:])
            pre = pklF.split('.')[0]
            if pre.endswith('c1'):
                for nbr in range( 1,6):
                    f = pre[:-2]+'c'+str(nbr)+'.'+suf
                    if os.path.isfile(f): file_list.append(f)
                    else:
                        file_couple = list(itertools.combinations(file_list,2))
                        allFile_pairs.append(file_couple)
                        break
            else: raise SystemExit("Need atleast two files to compare")
        for subDir_list in allFile_pairs:
            for f1,f2 in subDir_list:
                colocal = Colocalize(f1,f2)
                overlapPeaks = colocal.checkPosOverlap()
                subDir_colocal_dict[subDir].append([f1,f2,colocal.f1NbrPeaks,colocal.f2NbrPeaks,len(overlapPeaks)])

    pickle.dump(subDir_colocal_dict,ofile)

    return subDir_colocal_dict
        destDir = os.path.join(
            "/project/jagannath/projectfiles/singleMoleculeMicroscopy/dataAnalysis/microscope1",
            monthStamp, dateStamp)
    elif microscope is 2:
        sourceDir = "/project/boulgakov/microscope2/jagannath/rawFiles"
        pathDir = os.path.join(sourceDir, monthStamp, dateStamp)
        destDir = os.path.join(
            "/project/current/project2/jaggu/dataAnalysis/microscope2",
            monthStamp, dateStamp)

    elif microscope is 3:
        sourceDir = "/project/boulgakov/microscope3/rawFiles/jagannath"
        pathDir = os.path.join(sourceDir, monthStamp, dateStamp)
        destDir = os.path.join(
            "/project/current/project2/jaggu/dataAnalysis/microscope3",
            monthStamp, dateStamp)
    else:
        raise SystemExit("Incorrect microscope number. Quiting ..")

    makeDir(destDir)
    t0 = time.clock()

    if ARG == 'COLOCALIZE':
        colocal_dict = colocalizeImages(dateStamp, pathDir, destDir)
        writeToFile(colocal_dict, dateStamp, destDir)

    t1 = time.clock()
    print "Script - %s \t Completed in %s secs \t %s" % (
        sys.argv, t1 - t0, time.strftime("%d %b %Y  %H:%M:%S",
                                         time.localtime()))
#    pathDir = os.path.join(sourceDir,subDir)

    microscope = 1 
    if microscope is 1:                                                                                                                     
        sourceDir = "/project/boulgakov/microscope"                                                                                         
        pathDir = os.path.join(sourceDir,monthStamp,dateStamp)                                                                                  
        destDir = os.path.join("/project/jagannath/projectfiles/singleMoleculeMicroscopy/dataAnalysis/microscope1",monthStamp,dateStamp)     
    elif microscope is 2:                                                                                                                   
        sourceDir = "/project/boulgakov/microscope2/jagannath/rawFiles"                                                                     
        pathDir = os.path.join(sourceDir,monthStamp,dateStamp)                                                                              
        destDir = os.path.join("/project/current/project2/jaggu/dataAnalysis/microscope2",monthStamp,dateStamp)     
    
    elif microscope is 3:
        sourceDir = "/project/boulgakov/microscope3/rawFiles/jagannath"
        pathDir = os.path.join(sourceDir,monthStamp,dateStamp)
        destDir = os.path.join("/project/current/project2/jaggu/dataAnalysis/microscope3",monthStamp,dateStamp)
    else:                                                                                                                                   
        raise SystemExit("Incorrect microscope number. Quiting ..")                                                                         
    
    makeDir(destDir)
    t0 = time.clock()   

    if ARG == 'COLOCALIZE':
        colocal_dict = colocalizeImages(dateStamp,pathDir,destDir)
        writeToFile(colocal_dict,dateStamp,destDir)

    
    t1 = time.clock()
    print "Script - %s \t Completed in %s secs \t %s"%(sys.argv, t1-t0,
                                                       time.strftime("%d %b %Y  %H:%M:%S",time.localtime()))