Exemple #1
0
def writeSetOfVolumes(volSet, volXml, volDir):
    """ Convert a SetOfVolumes to a xml file used by PyTom.
    The volumes will be converted to .mrc format if not are '.em' or '.mrc' 
    Params:
        volSet: input SetOfVolumes.
        volXml: filename where to write the xml file.
        volDir: where to create links or copies (converted to mrc).
    """
    # Add to the path the root to pytom
    backupPath = list(sys.path)
    addPyTomPaths()
    
    from pytom.basic.structures import Particle, ParticleList, Wedge, SingleTiltWedge
    from pytom.score.score import Score, PeakPrior, xcfScore
    from pytom.frm.FRMAlignment import FRMScore
    
    w = SingleTiltWedge()
    #s = PeakPrior()
    pl = ParticleList()
    ih = em.convert.ImageHandler()
    
    for vol in volSet:
        index, fn = vol.getLocation()
        convert = True # by default, convert, which is the save way
        if index == em.NO_INDEX: # means single volumes
            volName = os.path.basename(fn)
            if fn.endswith('.em') or fn.endswith('.mrc'):
                convert = False # we can just create a link in this case
        else:
            volName = 'volume_%03d.mrc' % vol.getObjId()
        
        volFn = os.path.join(volDir, volName)
        if convert:
            ih.convert(vol, volFn)
        else:
            pwutils.createLink(fn, volFn)
        
        # Make the volumes names relative to the xml file
        # where the programs will be executed
        volRel = os.path.relpath(volFn, os.path.dirname(volXml))
        p = Particle()
        s = xcfScore()
        s.setValue(1.0)
        pytomInfo = getattr(vol, 'pytomInfo', None)
        if pytomInfo is None:
            p.setWedge(w)
        else:
            p.fromXML(pytomInfo.get()) # Get stored XML format from PyTom
        p.setFilename(volRel)
        p.setScore(s)
        pl.append(p)
        
    pl.toXMLFile(volXml)
    #pl.setWedgeAllParticles(w)
    sys.path = backupPath
Exemple #2
0
def extractClassesFromPL(pl_name, class_names, output):
    from pytom.basic.structures import ParticleList

    pl = ParticleList()
    pl.fromXMLFile(pl_name)

    pls = pl.splitByClass()
    class_labels = class_names.split(',')

    res = ParticleList()
    for pp in pls:
        if pp[0].getClass() in class_labels:
            res += pp

    if output: res.toXMLFile(output)

    return res
Exemple #3
0
def subTomoClust(particleListFilename,
                 classifiedParticleListFilename,
                 cccName,
                 neig,
                 nclass,
                 verbose=False):
    """
    subtomogram clustering using CCC and kmeans
    @param particleListFilename: particle list filename
    @type particleListFilename: str
    @param classifiedParticleListFilename: output particle list filename
    @type classifiedParticleListFilename: str
    @param cccName: Name of (Constrained) Correlation Coefficient (CCC) matrix
    @type cccName: str
    @param neig: number of eigenvectors
    @type neig: int
    @param nclass: number of classes
    @type nclass: int

    @author: FF Jan 2013
    """
    from pytom.basic.structures import ParticleList
    pl = ParticleList('.')
    pl.fromXMLFile(particleListFilename)
    if verbose:
        print("Particle List read in")
    ccc = readCCC(cccName)
    if verbose:
        print("CCC read in")
    coeffs, eigvalues = SVD_analysis(ccc)
    if verbose:
        print("Eigen analysis done")
    labels = kmeansCluster(coeff=coeffs, neig=neig, nclass=nclass)
    if verbose:
        print("kmeans clustering done")
    for (ipart, part) in enumerate(pl):
        part.setClass(className=str(labels[ipart]))
    if verbose:
        print("Class labels assigned")
    pl.toXMLFile(classifiedParticleListFilename)
    if verbose:
        print("File written")
Exemple #4
0
def mirrorParticleList(particleList, outname, directory='./'):

    sizes = get_size(particleList, directory)

    if sizes == 'Failed':
        print(
            'Mirroring particle coordinates did not succeed. Please ensure the paths to the origin tomogram are correct'
        )
        return
    dimx, dimy, dimz = sizes

    tempPL = ParticleList()
    tempPL.fromXMLFile(particleList)
    for particle in tempPL:
        pp = particle.getPickPosition()
        pp.setX(dimx - pp.getX())
        pp.setY(dimy - pp.getY())
        pp.setZ(dimz - pp.getZ())
        shift = particle.getShift()
        shift.invert()

    tempPL.toXMLFile(outname)
Exemple #5
0
def convertCoords2PL(coordinate_files,
                     particleList_file,
                     subtomoPrefix=None,
                     wedgeAngles=None,
                     angleList=False):
    pl = ParticleList()
    for n, coordinate_file in enumerate(coordinate_files):
        wedgeAngle = wedgeAngles[2 * n:2 * (n + 1)]
        sourceInfo = pl.loadCoordinateFileHeader(coordinate_file)
        l2 = len(pl)
        pl.loadCoordinateFile(filename=coordinate_file,
                              name_prefix=subtomoPrefix[n],
                              wedgeAngle=wedgeAngle,
                              sourceInfo=sourceInfo)

        try:
            cc = 180. / numpy.pi
            for i in range(len(pl) - l2):
                z1, z2, x = random.choice(angleList)
                pl[-i - 1].setRotation(rotation=Rotation(
                    z1=z1 * cc, z2=z2 * cc, x=x * cc, paradigm='ZXZ'))
        except:
            pass
    pl.toXMLFile(particleList_file)
Exemple #6
0
    def start(self, job, verbose=False):
        """
        start FRM job
        @param job: FRM job
        @type job: L{FRMJob}
        @param verbose: print stuff (default: False)
        @type verbose: C{bool}
        """
        if self.mpi_id == 0:
            from pytom.basic.structures import ParticleList, Reference
            from pytom.basic.resolution import bandToAngstrom
            from pytom.basic.filter import lowpassFilter
            from math import ceil
            self.destination = job.destination
            new_reference = job.reference
            old_freq = job.freq
            new_freq = job.freq
            #print(f"reference  = {job.reference}")
            #print(f"particlelist = {job.particleList}")
            print(f"iterations = {job.max_iter:d}")
            print(f"binning    = {job.binning:d}")
            #print(f"mask       = {job.mask}")
            #print(f"peak_offset= {job.peak_offset:f2.1}")
            print(f"destination= {job.destination:s}")
            print(f"freq cut   = {job.freq:d}")
            # main node
            for i in range(job.max_iter):
                if verbose:
                    print(self.node_name + ': starting iteration %d ...' % i)

                # construct a new job by updating the reference and the frequency
                new_job = FRMJob(job.particleList,
                                 new_reference,
                                 job.mask,
                                 job.peak_offset,
                                 job.sampleInformation,
                                 job.bw_range,
                                 new_freq,
                                 job.destination,
                                 job.max_iter - i,
                                 job.r_score,
                                 job.weighting,
                                 constraint=job.constraint,
                                 binning=job.binning)

                # distribute it
                self.distribute_job(new_job, verbose)

                # get the result back
                all_even_pre = None
                all_even_wedge = None
                all_odd_pre = None
                all_odd_wedge = None
                pl = ParticleList()
                for j in range(self.num_workers):
                    result = self.get_result()
                    pl += result.pl
                    even_pre, even_wedge, odd_pre, odd_wedge = self.retrieve_res_vols(
                        result.name)

                    if all_even_pre:
                        all_even_pre += even_pre
                        all_even_wedge += even_wedge
                        all_odd_pre += odd_pre
                        all_odd_wedge += odd_wedge
                    else:
                        all_even_pre = even_pre
                        all_even_wedge = even_wedge
                        all_odd_pre = odd_pre
                        all_odd_wedge = odd_wedge

                # write the new particle list to the disk
                pl.toXMLFile(
                    os.path.join(job.destination,
                                 'aligned_pl_iter' + str(i) + '.xml'))

                # create half sets
                even = self.create_average(all_even_pre, all_even_wedge)
                odd = self.create_average(all_odd_pre, all_odd_wedge)

                # apply symmetries before determine resolution
                even = job.symmetries.applyToParticle(even)
                odd = job.symmetries.applyToParticle(odd)
                resNyquist, resolutionBand, numberBands = self.determine_resolution(
                    even, odd, job.fsc_criterion, None, job.mask, verbose)

                # write the half set to the disk
                even.write(
                    os.path.join(self.destination,
                                 'fsc_' + str(i) + '_even.em'))
                odd.write(
                    os.path.join(self.destination,
                                 'fsc_' + str(i) + '_odd.em'))

                # determine the resolution
                if verbose:
                    print(self.node_name + ': determining the resolution ...')
                current_resolution = bandToAngstrom(
                    resolutionBand, job.sampleInformation.getPixelSize(),
                    numberBands, 1)
                if verbose:
                    print(
                        self.node_name + ': current resolution ' +
                        str(current_resolution), resNyquist)

                # create new average
                all_even_pre += all_odd_pre
                all_even_wedge += all_odd_wedge
                average = self.create_average(all_even_pre, all_even_wedge)

                # apply symmetries
                average = job.symmetries.applyToParticle(average)

                # filter average to resolution and update the new reference
                average_name = os.path.join(self.destination,
                                            'average_iter' + str(i) + '.em')
                #                pl.average(average_name, True)
                average.write(average_name)
                new_reference = Reference(average_name)

                # low pass filter the reference and write it to the disk
                filtered = lowpassFilter(average, ceil(resolutionBand),
                                         ceil(resolutionBand) / 10)
                filtered_ref_name = os.path.join(
                    self.destination, 'average_iter' + str(i) + '_res' +
                    str(current_resolution) + '.em')
                filtered[0].write(filtered_ref_name)

                # if the position/orientation is not improved, break it

                # change the frequency to a higher value
                new_freq = int(ceil(resolutionBand)) + 1
                if new_freq <= old_freq:
                    if job.adaptive_res is not False:  # two different strategies
                        print(
                            self.node_name +
                            ': Determined resolution gets worse. Include additional %f percent frequency to be aligned!'
                            % job.adaptive_res)
                        new_freq = int((1 + job.adaptive_res) * new_freq)
                        old_freq = new_freq
                    else:  # always increase by 1
                        print(
                            self.node_name +
                            ': Determined resolution gets worse. Increase the frequency to be aligned by 1!'
                        )
                        new_freq = old_freq + 1
                        old_freq = new_freq
                else:
                    old_freq = new_freq
                if new_freq >= numberBands:
                    print(self.node_name +
                          ': New frequency too high. Terminate!')
                    break

                if verbose:
                    print(self.node_name + ': change the frequency to ' +
                          str(new_freq))

            # send end signal to other nodes and terminate itself
            self.end(verbose)
        else:
            # other nodes
            self.run(verbose)
Exemple #7
0
def updatePL(fnames,
             outnames,
             directory='',
             suffix='',
             wedgeangles=[],
             multiplypickpos=1,
             multiplyshift=None,
             new_center=[],
             sizeSubtomo=64,
             move_shift=False,
             binSubtomo=1,
             binRecon=1,
             rotation=[],
             anglelist='',
             mirror=False,
             tomogram_dir='./',
             convention='zxz'):
    if type(fnames) == str:
        fnames = [fnames]
    if type(outnames) == str:
        outnames = [outnames]

    try:
        wedgelen = len(wedgeangles)
    except:
        wedgelen = 0

    for n, xmlfile in enumerate(fnames):
        tempPL = ParticleList()
        tempPL.fromXMLFile(xmlfile)

        for particle in tempPL:

            # Update directory to particle
            if directory:
                filename = os.path.join(
                    directory, os.path.basename(particle.getFilename()))
                particle.setFilename(filename)

            # Add suffix to directory name in which particle is stored
            # e.g. suffix = _bin3
            #  --> Subtomograms/tomogram_000/particle_1.em will become Subtomograms/tomogram_000_bin3/particle_1.em
            if suffix:
                filename = particle.getFilename()
                filename = os.path.join(
                    os.path.dirname(filename) + suffix,
                    os.path.basename(filename))
                particle.setFilename(filename)

            # Update wedge angles of angle1 and angle2
            if wedgelen > n + 1:
                w = particle.getWedge()
                w.setWedgeAngles(wedgeangles[n * 2:n * 2 + 2])

            # Multiply pick position
            if abs(multiplypickpos - 1) > 1E-3:
                pp = particle.getPickPosition()
                pp.scale(multiplypickpos)

            # Shift is multiply by the respective binning factor.
            if not (multiplyshift is None):
                shift = particle.getShift()
                shift.scale(multiplyshift)

            # Randomize the angles of all particles in particle list.
            if type(anglelist) == type(numpy.array([])):
                cc = 180. / numpy.pi
                import random
                z1, z2, x = random.choice(anglelist)
                particle.setRotation(rotation=Rotation(
                    z1=z1 * cc, z2=z2 * cc, x=x * cc, paradigm='ZXZ'))

            shift = particle.getShift()
            angles = particle.getRotation().toVector(convention=convention)
            rot_particleList = R.from_euler(convention, angles, degrees=True)

            if new_center:
                new_center_vector = numpy.array(new_center) - sizeSubtomo // 2
                new_center_vector_rotated = rot_particleList.apply(
                    new_center_vector)
                shift.addVector(new_center_vector_rotated)

            if move_shift == True:
                pp = particle.getPickPosition()
                shift.scale(binSubtomo / binRecon)
                ss = shift.toVector()
                pp.setX(pp.getX() + ss[0])
                pp.setY(pp.getY() + ss[1])
                pp.setZ(pp.getZ() + ss[2])
                particle.setPickPosition(pp)
                shift.scale(0.)
                #print(particle)

            # Combine rotations from particleList and rotation
            if rotation:
                rot_rotation = R.from_euler(convention, rotation, degrees=True)
                combined_rotation = rot_particleList * rot_rotation
                z1, x, z2 = combined_rotation.as_euler(convention,
                                                       degrees=True)
                particle.setRotation(
                    rotation=Rotation(z1=z1, z2=z2, x=x, paradigm='ZXZ'))

            if mirror:
                # Update shifts
                shift.scale(-1)
                particle.setShift(shift)

                # Update angles as well
                rotationT = particle.getRotation()
                rotationT.setZ1(-1 * rotationT.getZ1())
                rotationT.setZ2(-1 * rotationT.getZ2())
                rotationT.setX(-1 * rotationT.getX())
                particle.setRotation(rotationT)

        tempPL.toXMLFile(outnames[n])
        if mirror:
            mirrorParticleList(outnames[n],
                               outnames[n],
                               directory=tomogram_dir)
Exemple #8
0
    def start(self, job, verbose=False):
        if self.mpi_id == 0:
            from pytom.basic.structures import ParticleList, Reference
            from pytom.basic.resolution import bandToAngstrom
            from pytom.basic.filter import lowpassFilter
            from math import ceil

            # randomly split the particle list into 2 half sets
            if len(job.particleList.splitByClass()) != 2:
                import numpy as np
                n = len(job.particleList)
                labels = np.random.randint(2, size=(n, ))
                print(self.node_name + ': Number of 1st half set:',
                      n - np.sum(labels), 'Number of 2nd half set:',
                      np.sum(labels))
                for i in range(n):
                    p = job.particleList[i]
                    p.setClass(labels[i])

            self.destination = job.destination
            new_reference = job.reference
            old_freq = job.freq
            new_freq = job.freq
            # main node
            for i in range(job.max_iter):
                if verbose:
                    print(self.node_name + ': starting iteration %d ...' % i)

                # construct a new job by updating the reference and the frequency
                new_job = FRMJob(job.particleList, new_reference, job.mask,
                                 job.peak_offset, job.sampleInformation,
                                 job.bw_range, new_freq, job.destination,
                                 job.max_iter - i, job.r_score, job.weighting)

                # distribute it
                self.distribute_job(new_job, verbose)

                # get the result back
                all_even_pre = None  # the 1st set
                all_even_wedge = None
                all_odd_pre = None  # the 2nd set
                all_odd_wedge = None
                pl = ParticleList()
                for j in range(self.num_workers):
                    result = self.get_result()
                    pl += result.pl
                    pre, wedge = self.retrieve_res_vols(result.name)

                    if self.assignment[result.worker_id] == 0:
                        if all_even_pre:
                            all_even_pre += pre
                            all_even_wedge += wedge
                        else:
                            all_even_pre = pre
                            all_even_wedge = wedge
                    else:
                        if all_odd_pre:
                            all_odd_pre += pre
                            all_odd_wedge += wedge
                        else:
                            all_odd_pre = pre
                            all_odd_wedge = wedge

                # write the new particle list to the disk
                pl.toXMLFile('aligned_pl_iter' + str(i) + '.xml')

                # create the averages separately
                if verbose:
                    print(self.node_name + ': determining the resolution ...')
                even = self.create_average(all_even_pre, all_even_wedge)
                odd = self.create_average(all_odd_pre, all_odd_wedge)

                # apply symmetries if any
                even = job.symmetries.applyToParticle(even)
                odd = job.symmetries.applyToParticle(odd)

                # determine the transformation between even and odd
                # here we assume the wedge from both sets are fully sampled
                from sh_alignment.frm import frm_align
                pos, angle, score = frm_align(odd, None, even, None,
                                              job.bw_range, new_freq,
                                              job.peak_offset)
                print(self.node_name +
                      'Transform of even set to match the odd set - shift: ' +
                      str(pos) + ' rotation: ' + str(angle))

                # transform the odd set accordingly
                from pytom_volume import vol, transformSpline
                from pytom.basic.fourier import ftshift
                from pytom_volume import reducedToFull
                from pytom_freqweight import weight
                transformed_odd_pre = vol(odd.sizeX(), odd.sizeY(),
                                          odd.sizeZ())
                full_all_odd_wedge = reducedToFull(all_odd_wedge)
                ftshift(full_all_odd_wedge)
                odd_weight = weight(
                    full_all_odd_wedge)  # the funny part of pytom
                transformed_odd = vol(odd.sizeX(), odd.sizeY(), odd.sizeZ())

                transformSpline(all_odd_pre, transformed_odd_pre, -angle[1],
                                -angle[0], -angle[2],
                                odd.sizeX() / 2,
                                odd.sizeY() / 2,
                                odd.sizeZ() / 2, -(pos[0] - odd.sizeX() / 2),
                                -(pos[1] - odd.sizeY() / 2),
                                -(pos[2] - odd.sizeZ() / 2), 0, 0, 0)
                odd_weight.rotate(-angle[1], -angle[0], -angle[2])
                transformed_odd_wedge = odd_weight.getWeightVolume(True)
                transformSpline(odd, transformed_odd, -angle[1], -angle[0],
                                -angle[2],
                                odd.sizeX() / 2,
                                odd.sizeY() / 2,
                                odd.sizeZ() / 2, -(pos[0] - odd.sizeX() / 2),
                                -(pos[1] - odd.sizeY() / 2),
                                -(pos[2] - odd.sizeZ() / 2), 0, 0, 0)

                all_odd_pre = transformed_odd_pre
                all_odd_wedge = transformed_odd_wedge
                odd = transformed_odd

                # determine resolution
                resNyquist, resolutionBand, numberBands = self.determine_resolution(
                    even, odd, job.fsc_criterion, None, job.mask, verbose)

                # write the half set to the disk
                even.write(
                    os.path.join(self.destination,
                                 'fsc_' + str(i) + '_even.em'))
                odd.write(
                    os.path.join(self.destination,
                                 'fsc_' + str(i) + '_odd.em'))

                current_resolution = bandToAngstrom(
                    resolutionBand, job.sampleInformation.getPixelSize(),
                    numberBands, 1)
                if verbose:
                    print(
                        self.node_name + ': current resolution ' +
                        str(current_resolution), resNyquist)

                # create new average
                all_even_pre += all_odd_pre
                all_even_wedge += all_odd_wedge
                average = self.create_average(all_even_pre, all_even_wedge)

                # apply symmetries
                average = job.symmetries.applyToParticle(average)

                # filter average to resolution
                average_name = os.path.join(self.destination,
                                            'average_iter' + str(i) + '.em')
                average.write(average_name)

                # update the references
                new_reference = [
                    Reference(
                        os.path.join(self.destination,
                                     'fsc_' + str(i) + '_even.em')),
                    Reference(
                        os.path.join(self.destination,
                                     'fsc_' + str(i) + '_odd.em'))
                ]

                # low pass filter the reference and write it to the disk
                filtered = lowpassFilter(average, ceil(resolutionBand),
                                         ceil(resolutionBand) / 10)
                filtered_ref_name = os.path.join(
                    self.destination, 'average_iter' + str(i) + '_res' +
                    str(current_resolution) + '.em')
                filtered[0].write(filtered_ref_name)

                # if the position/orientation is not improved, break it

                # change the frequency to a higher value
                new_freq = int(ceil(resolutionBand)) + 1
                if new_freq <= old_freq:
                    if job.adaptive_res is not False:  # two different strategies
                        print(
                            self.node_name +
                            ': Determined resolution gets worse. Include additional %f percent frequency to be aligned!'
                            % job.adaptive_res)
                        new_freq = int((1 + job.adaptive_res) * old_freq)
                    else:  # always increase by 1
                        print(
                            self.node_name +
                            ': Determined resolution gets worse. Increase the frequency to be aligned by 1!'
                        )
                        new_freq = old_freq + 1
                        old_freq = new_freq
                else:
                    old_freq = new_freq
                if new_freq >= numberBands:
                    print(self.node_name +
                          ': New frequency too high. Terminate!')
                    break

                if verbose:
                    print(self.node_name + ': change the frequency to ' +
                          str(new_freq))

            # send end signal to other nodes and terminate itself
            self.end(verbose)
        else:
            # other nodes
            self.run(verbose)
Exemple #9
0
def create_RandomParticleList(reffile,
                              pl_filename='pl.xml',
                              pdir='./testparticles',
                              nparticles=10):
    """
    @param reffile: reference file
    @type reffile: C{str}
    @param nparticles: number of particles (default: 10)
    @type nparticles: C{int}
    @param pl_filename: particle list filename
    @type pl_filename: C{str}
    @param pdir: particle directory
    @type pdir: C{str}
    @return: particleList
    @rtype: L{pytom.basic.ParticleList}
    
    """
    from pytom.basic.structures import Particle, ParticleList, Rotation, Shift
    from pytom_volume import vol, rotate, shift, read
    from pytom.basic.transformations import general_transform_crop
    from pytom.simulation.whiteNoise import add as addNoise
    import random
    from os import mkdir
    from pytom.score.score import FLCFScore as score

    try:
        mkdir(pdir)
    except FileExistsError:
        print('directory ' + pdir + ' existed already - using this one')
    random.seed(0)

    pl = ParticleList(directory='./')
    ref = read(reffile)
    for ii in range(0, nparticles):
        rot = Rotation(random.uniform(0, 360), random.uniform(0, 360),
                       random.uniform(0, 180))
        shift = Shift(x=random.uniform(-5, 5),
                      y=random.uniform(-5, 5),
                      z=random.uniform(-5, 5))
        rotvol = general_transform_crop(v=ref,
                                        rot=rot,
                                        shift=shift,
                                        scale=None,
                                        order=[0, 1, 2])
        # add some noise
        noisy = addNoise(volume=rotvol, SNR=1)
        fname = pdir + '/particle_' + str(ii) + '.em'
        noisy.write(fname)
        p = Particle(filename=fname,
                     rotation=rot,
                     shift=shift,
                     wedge=None,
                     className=0,
                     pickPosition=None,
                     score=score,
                     sourceInfo=None)
        p.setScoreValue(0.0)
        pl.append(particle=p)

    pl.setFileName(filename=pl_filename)
    pl.toXMLFile(filename=pl_filename)
    return pl
Exemple #10
0
    if wedgeangles:
        wedgeangles = wedgeangles.split(',')
    else:
        wedgeangle = []

    if directory:
        fnames = [
            line for line in glob.glob(os.path.join(directory, '*.xml'))
            if line.endswith('.xml')
        ]

    if XMLfnames:
        print(XMLfnames)
        fnames = XMLfnames.split(',')

    pl = ParticleList()

    if wedgeangles: wedgelen = len(wedgeangles)
    else: wedgelen = 0

    for n, xmlfile in enumerate(fnames):
        tempPL = ParticleList()
        tempPL.fromXMLFile(xmlfile)
        for particle in tempPL:
            if not wedgelen > n + 1: continue
            w = particle.getWedge()
            w.setWedgeAngles(wedgeangles[n * 2:n * 2 + 2])
        pl += tempPL

    a = pl.toXMLFile(outname)
Exemple #11
0
 from pytom.tools.parse_script_options import parse_script_options
 from pytom.basic.structures import ParticleList, SingleTiltWedge
 
 helper = ScriptHelper(sys.argv[0].split('/')[-1], # script name
                       description='Set the same wedge to all the particles in the particle list.',
                       authors='Yuxiang Chen',
                       options=[ScriptOption(['-p'], 'Particle list', True, False),
                                ScriptOption(['-w'], 'Wedge Angle (degree)', True, False),
                                ScriptOption(['-o'], 'Output particle list', True, True),
                                ScriptOption(['-h', '--help'], 'Help.', False, True)])
 
 if len(sys.argv) == 1:
     print(helper)
     sys.exit()
 try:
     pl_name, wedge_angle, output, bHelp = parse_script_options(sys.argv[1:], helper)
 except:
     sys.exit()
 if bHelp is True:
     print(helper)
     sys.exit()
 
 pl = ParticleList()
 pl.fromXMLFile(pl_name)
 w = SingleTiltWedge(int(wedge_angle))
 pl.setWedgeAllParticles(w)
 
 if output is None:
     pl.toXMLFile(pl_name)
 else:
     pl.toXMLFile(output)
Exemple #12
0
def create_RandomParticleList(reffile,
                              pl_filename='pl.xml',
                              pdir='./testparticles',
                              nparticles=10):
    """
    @param reffile: reference file
    @type reffile: C{str}
    @param nparticles: number of particles (default: 10)
    @type nparticles: C{int}
    @param pl_filename: particle list filename
    @type pl_filename: C{str}
    @param pdir: particle directory
    @type pdir: C{str}
    @return: particleList
    @rtype: L{pytom.basic.ParticleList}
    
    """
    from pytom.basic.structures import Particle, ParticleList, Rotation, Shift, Wedge
    from pytom_volume import vol, rotate, shift, read
    from pytom.basic.transformations import general_transform_crop
    from pytom.basic.functions import initSphere
    from pytom.simulation.whiteNoise import add as addNoise
    import random
    from os import mkdir
    from pytom.score.score import FLCFScore as score

    try:
        mkdir(pdir)
    except FileExistsError:
        print('directory ' + pdir + ' existed already - using this one')
    random.seed(0)

    a = 0

    wedge = Wedge(wedgeAngles=[30.0, 30.0], cutoffRadius=50.0)

    pl = ParticleList(directory='./')
    ref1 = read(reffile)
    ref2 = initSphere(sizeX=ref1.sizeX(),
                      sizeY=ref1.sizeY(),
                      sizeZ=ref1.sizeZ(),
                      radius=45)
    #ref2.write('testData/mask_45.em')
    parts = {0: ref1, 1: ref2}

    for ii in range(0, nparticles):
        if not ii % 2:
            ref = read(reffile)
        else:
            ref = initSphere(sizeX=ref1.sizeX(),
                             sizeY=ref1.sizeY(),
                             sizeZ=ref1.sizeZ(),
                             radius=30,
                             smooth=30 / 10)

        rot = Rotation(random.uniform(0, 360), random.uniform(0, 360),
                       random.uniform(0, 180))
        shift = Shift(x=a * random.uniform(-5, 5),
                      y=a * random.uniform(-5, 5),
                      z=a * random.uniform(-5, 5))
        rotvol = general_transform_crop(v=ref,
                                        rot=rot,
                                        shift=shift,
                                        scale=None,
                                        order=[0, 1, 2])
        # add some noise
        noisy = addNoise(volume=rotvol, SNR=1)
        fname = pdir + '/particle_' + str(ii) + '.em'

        #noisy.write( fname)
        p = Particle(filename=fname,
                     rotation=rot,
                     shift=shift,
                     wedge=wedge,
                     className=0,
                     pickPosition=None,
                     score=score,
                     sourceInfo=None)
        p.setScoreValue(0.0)

        wg = p.getWedge().getWedgeObject()
        wg.apply(noisy, Rotation(0, 0, 0)).write(fname)

        pl.append(particle=p)

    pl.setFileName(filename=pl_filename)
    pl.toXMLFile(filename=pl_filename)
    return pl
Exemple #13
0
from pytom.basic.structures import ParticleList

alignmentList = 'AlignmentList-1.xml'
particleList = 'alignedParticles.xml'
pl = ParticleList()
pl.fromAlignmentList(alignmentList)
pl.toXMLFile(particleList)
Exemple #14
0
     dest_dir = '.'
 
 from pytom_volume import read, subvolume
 v = read(vol_filename)
 
 from pytom.basic.structures import ParticleList, Particle, WedgeInfo
 pl = ParticleList("./")
 pl.fromXMLFile(pl_filename)
 
 def regulaize(xx, dim):
     if xx*binning-radius < 0:
         if 2*radius > dim:
             raise Exception("Volume size to be cut is too big!")
         return 0
     if xx*binning+radius > dim:
         if dim-2*radius < 0:
             raise Exception("Volume size to be cut is too big!")
         return dim-2*radius
     return xx*binning-radius
 
 res = ParticleList(dest_dir)
 for p in pl:
     x,y,z = p.getPickPosition().toVector()
     x = regulaize(int(x), v.sizeX()); y = regulaize(int(y), v.sizeY()); z = regulaize(int(z), v.sizeZ())
     new_vol = subvolume(v, x, y, z, 2*radius, 2*radius, 2*radius)
     name = dest_dir+'/'+p.getFilename()
     new_vol.write(name) # write the subtomograms to the disk
     res.append(Particle(name, p.getRotation(), None, WedgeInfo(w), 1, p.getPickPosition(), p.getScore())) # append it to the particle list for alignment
 
 res.toXMLFile(dest_dir+'/'+res_name)
        dir_name, name_prefix, wedge_angle, output, bHelp = parse_script_options(
            sys.argv[1:], helper)
    except:
        sys.exit()
    if bHelp is True:
        print(helper)
        sys.exit()

    if name_prefix is None:
        name_prefix = ''
    wedge_angle = float(wedge_angle)
    w = SingleTiltWedge(wedge_angle)

    pl = ParticleList()
    all_files = os.listdir(dir_name)
    for fname in all_files:
        p = None
        name_suffix = fname.split('.')[-1]
        if len(name_prefix):
            if name_prefix in fname and name_suffix in ['em', 'mrc']:
                p = Particle(dir_name + '/' + fname)
        else:
            if name_suffix in ['em', 'mrc']:
                p = Particle(dir_name + '/' + fname)
        if p is not None:
            pl.append(p)

    pl.setWedgeAllParticles(w)

    pl.toXMLFile(output)
Exemple #16
0
        for particle in res:
            newParticle = particle.toParticle()
            newParticle.setWedge(wedge)
            newParticle.setFilename(particlePath + newParticle.getFilename())

            if scale != 1.0:
                pi = newParticle.getPickPosition()
                pi.setX(pi.getX() * scale)
                pi.setY(pi.getY() * scale)
                pi.setZ(pi.getZ() * scale)
                newParticle.setPickPosition(pi)

            pl.append(newParticle)

        pl.toXMLFile(plFilename)

    if motlFilename:
        from pytom.basic.structures import ParticleList

        pl = ParticleList()
        for newParticle in res:

            if scale != 1.0:
                pi = newParticle.getPickPosition()
                pi.setX(pi.getX() * scale)
                pi.setY(pi.getY() * scale)
                pi.setZ(pi.getZ() * scale)
                newParticle.setPickPosition(pi)

            pl.append(newParticle.toParticle())
Exemple #17
0
                print('Coordinate out of bounds (', x, y, z, ') for ')
                print(particle)
                print('Particle could not be cut out from origin volume!')
                print('')
                continue

            v = subvolume(vol, x, y, z, cubeSize, cubeSize,
                          cubeSize)  # faster this way

            newParticleList.append(
                particle)  # this part should be inside the try block
            v.write(particle.getFilename())
        except:
            print('Error for')
            print(particle)
            print('Particle could not be cut out from origin volume!')
            print('')
            continue

    if len(particleList) != len(newParticleList):
        new_plFilename = None
        if '/' in plFilename:
            new_plFilename = plFilename[:-4] + 'New.xml'
        else:
            new_plFilename = 'new_' + plFilename

        print(
            'Length of particle list has been changed. The new particle list is saved as',
            new_plFilename)
        newParticleList.toXMLFile(new_plFilename)