コード例 #1
0
def pick_starting_configuration(traj_file, max_bound):
    """
        Pick a random conf out of the trajectory file to use as the reference structure.
        
        We assume that that is optimal to align against.  Based on experience, the choice of reference configuration has very little impact on the mean structure

        Parameters: 
            traj_file (string): The name of the trajectory file
            max_bound (int): The reference configuration will be chosen at random from the first max_bound configurations in the trajectory file

        Returns:
            stop_at (int): The configuration ID of the reference configuration
            initial_structure (base.System): The oxDNA system representing the reference configuration.
    """
    with ErikReader(traj_file) as reader:
        if args.align:
            stop_at = int(args.align[0])
        else:
            stop_at = randint(0, max_bound-1)
        print("INFO: We chose {} as reference".format(stop_at), file=stderr)
        initial_structure = reader.read(n_skip=stop_at)
        if not initial_structure:
            print("ERROR: Couldn't read structure at conf num {0}.".format(stop_at), file=stderr)
            exit(1)
        print("INFO: reference structure loaded", file=stderr)
        initial_structure.inbox()
    return stop_at, initial_structure
コード例 #2
0
def split_trajectory(traj_file, num_confs, n_cpus, confs_per_processor):
    """
    Splits a trajectory file into temporary files and attaches a reader to each file.

    Parameters:
        traj_file (str): Name of the trajectory file to split.  
        top_file (str): Name of the topology file associated with the trajectory. 
        num_confs (int): The number of configurations in the trajectory.  
        n_cpus (int): The number of chunks to split the trajectory into.  
        conf_per_processor (int): The number of configurations per chunk (equivalent to floor(num_confs/n_cpus))  

    Returns:
        readers (list of LorenzoReader2s): A list of readers with each one on a unique chunk of the file.
    """
    n_files = 0
    readers = []
    files = []
    rem = num_confs % n_cpus

    with open(traj_file, "rb") as f:
        it = blocks(f)
        chunk = next(it)  # iterator producing 1 MB chunks of the trajectory
        last_conf_byte = 0

        #create a number of temporary file equal to the number of CPUs
        while n_files < n_cpus:
            out = NamedTemporaryFile(mode='w+b', delete=False)
            conf_count = 0

            #If there is a remainder after dividing the number of configurations by the number of CPUs
            #Add one extra configuration to the first rem files
            if n_files < rem:
                a = 1
            else:
                a = 0

            #Find successive configuration start points and write them out to the tempfiles
            while conf_count < confs_per_processor + a:
                next_conf_byte = chunk.find(b"t", last_conf_byte + 1)
                if next_conf_byte == -1:
                    out.write(chunk[last_conf_byte:])
                    try:
                        chunk = next(it)
                    except:  #next() throws an error if there isn't another chunk
                        break
                    last_conf_byte = 0
                else:
                    out.write(chunk[last_conf_byte:next_conf_byte])
                    conf_count += 1
                    last_conf_byte = next_conf_byte

            #create a reader from the newly created trajectory chunk
            readers.append(ErikReader(out.name))
            files.append(out)
            n_files += 1

    return (readers, files)
コード例 #3
0
def fire_multiprocess(traj_file, function, num_confs, n_cpus, *args, **kwargs):
    confs_per_processor = int(np.floor(num_confs / n_cpus))

    reader_pool = []
    processor_pool = pp.Pool(n_cpus)

    #for calculations on symmetric matricies (eRMSD)
    #can't just hand each line to the parallelizer
    if ("matrix", True) in kwargs.items():
        total_calculations = sum([(num_confs - i)
                                  for i in range(1, num_confs)])
        calcs_per_cpus = total_calculations / n_cpus
        split_ends = []
        i = 0
        while i < num_confs:
            e = 0
            calcs = 0
            while calcs < calcs_per_cpus:
                calcs += num_confs - i
                e += 1
                i += 1
                if i >= num_confs: break
            split_ends.append(e)

    #define sizes of trajectory chunks
    else:
        split_ends = [confs_per_processor for _ in range(n_cpus)]
        split_ends[
            -1] += num_confs % n_cpus  #last chunk gets all the leftovers

    #now figure out which configuration each chunk starts on
    split_starts = [0]
    for i in range(n_cpus):
        reader_pool.append(ErikReader(traj_file))
        #rint(split_starts[i-1], split_ends[i-1])
        if i != 0:
            split_starts.append(split_starts[i - 1] + split_ends[i - 1])

    #staple everything together, send it out to the workers, and collect the results as a list
    results = []
    lst = [(r, *args, num_confs, s, e)
           for r, s, e in zip(reader_pool, split_starts, split_ends)]
    results = processor_pool.starmap_async(function, lst).get()
    processor_pool.close()

    return (results)
コード例 #4
0
    parser.add_argument('outfile', type=str, nargs=1, help='minified file')

    parser.add_argument('-a', action='store_true', help='Discard a vectors.')
    parser.add_argument('-p',
                        action='store_true',
                        help='Round positions to 7 digits.')

    args = parser.parse_args()

    traj_file = args.trajectory[0]
    out = args.outfile[0]
    # get the number of configurations
    n_confs = cal_confs(traj_file)

    try:  # make sure there is no out file
        remove(out)
    except:
        pass

    with ErikReader(traj_file) as reader:
        for i in range(n_confs):
            print(i + 1, ":", n_confs)
            # Erik reader ignores velocities
            system = reader.read()
            if args.p:  # round positions
                system.positions = round(system.positions, 7)
            if args.a:  # discard a vectors
                system.a1s -= system.a1s
                system.a3s -= system.a3s
            # output conf
            system.write_append(out)
コード例 #5
0
    outfile = args.outfile[0]
    parallel = args.parallel
    if parallel:
        n_cpus = args.parallel[0]
    #-c makes it run the clusterer on the output
    cluster = args.cluster

    num_confs = cal_confs(traj_file)

    if mean_file.split(".")[-1] == "json":
        with open(mean_file) as file:
            align_conf = load(file)['g_mean']

    elif mean_file.split(".")[-1] == "dat" or mean_file.split(
            ".")[-1] == "conf" or mean_file.split(".")[-1] == "oxdna":
        with ErikReader(mean_file) as reader:
            align_conf = reader.read().positions
    else:
        print(
            "ERROR: {} is an unrecognized file type. \nThe mean structure must either be provided as an oxDNA configuration file with the extension .dat, .conf or .oxdna or as the .json file produced by compute_mean.py.",
            file=stderr)
        exit(1)

    cms = np.mean(align_conf,
                  axis=0)  #all structures must have the same center of mass
    align_conf -= cms

    #Compute the deviations
    if not parallel:
        r = ErikReader(traj_file)
        covariation_matrix = get_cov(r, align_conf, num_confs)
コード例 #6
0
outfile = args.outfile[0]

#-i will make it only run on a subset of nucleotides.
#The index file is a space-separated list of particle IDs
if args.index_file:
    index_file = args.index_file[0]
    with open(index_file, 'r') as f:
        indexes = f.readline().split()
        try:
            indexes = [int(i) for i in indexes]
        except:
            print(
                "ERROR: The index file must be a space-seperated list of particles.  These can be generated using oxView by clicking the \"Download Selected Base List\" button"
            )
else:
    with ErikReader(traj_file) as r:
        indexes = list(range(len(r.read().positions)))

#read the first configuration and use it as the reference configuration for the rest
r = ErikReader(traj_file)
ref = r.read()
ref.inbox()
ref_conf = ref.positions[indexes]
sup = SVDSuperimposer()

#The topology remains the same so we only write the configuration
ref.write_new(outfile)
mysystem = r.read()

#Read the trajectory one configuration at a time and perform the alignment
while mysystem != False:
コード例 #7
0
traj_file = args.traj[0]
outfile = args.outfile[0]
sup = SVDSuperimposer()

#-i will make it only run on a subset of nucleotides.
#The index file is a space-separated list of particle IDs
if args.index_file:
    index_file = args.index_file[0]
    with open(index_file, 'r') as f:
        indexes = f.readline().split()
        try:
            indexes = [int(i) for i in indexes]
        except:
            print("ERROR: The index file must be a space-seperated list of particles.  These can be generated using oxView by clicking the \"Download Selected Base List\" button")
else: 
    with ErikReader(traj_file) as r:
        indexes = list(range(len(r.read().positions)))

#-r will make it align to a provided .dat file instead of the first configuration
if args.reference_structure:
    #read reference configuration
    r = ErikReader(args.reference_structure[0])
    ref = r.read()
    ref.inbox()
    r = ErikReader(traj_file)
    ref_conf = ref.positions[indexes]
    
    mysystem = align_frame(ref_conf, sup, r.read())

else:
    #read the first configuration and use it as the reference configuration for the rest
コード例 #8
0
    dev_file = None
    if args.deviations:
        dev_file = args.deviations[0]

    #-i will make it only run on a subset of nucleotides.
    #The index file is a space-separated list of particle IDs
    if args.index_file:
        index_file = args.index_file[0]
        with open(index_file, 'r') as f:
            indexes = f.readline().split()
            try:
                indexes = [int(i) for i in indexes]
            except:
                print("ERROR: The index file must be a space-seperated list of particles.  These can be generated using oxView by clicking the \"Download Selected Base List\" button")
    else: 
        with ErikReader(traj_file) as r:
            indexes = list(range(len(r.read().positions)))

    # helper to prepare a configuration of np.array coordinates
    # into smth json is able to serialize
    prep_pos_for_json = lambda conf: list(
                            list(p) for p in conf
                        )


    # The reference configuration which is used to define alignment
    align_conf = []

    #calculate the number of configurations in the trajectory 
    num_confs = cal_confs(traj_file)
コード例 #9
0
ref_dat = args.reference[0]

#-i will make it only run on a subset of nucleotides.
#The index file is a space-separated list of particle IDs
if args.index_file:
    index_file = args.index_file[0]
    with open(index_file, 'r') as f:
        indexes = f.readline().split()
        try:
            indexes = [int(i) for i in indexes]
        except:
            print(
                "ERROR: The index file must be a space-seperated list of particles.  These can be generated using oxView by clicking the \"Download Selected Base List\" button"
            )
else:
    with ErikReader(ref_dat) as r:
        indexes = list(range(len(r.read().positions)))

#Create list of configurations to superimpose
to_sup = []
r = ErikReader(ref_dat)
ref = r.read()
ref.inbox()
ref_conf = ref.positions[indexes]
for i in args.victims:
    r = ErikReader(i)
    sys = r.read()
    sys.inbox()
    to_sup.append(sys)

sup = SVDSuperimposer()
コード例 #10
0
args = parser.parse_args()

#if not "out_file" in args.format:
#    exit(1)
particles_array = []
# import index file
with open(args.index_file) as file:
    index_lines = file.readlines()
# parse the file
for line in index_lines:
    particles_array.append(np.array(line.split(" "), dtype=np.int))
# parsed index file
particles_array = np.array(particles_array, dtype=object)

# import mean configuration
r = ErikReader(args.mean_file)
ref = r.read()
ref.inbox()

# calculate reference conf particle positions
reference_configuration = get_mean_positions(ref)

# to collect the distance data of the superparticles
trajectory_devs = []
# go over the trajectory to compute the distances array
reader = ErikReader(args.trajectory)
system = reader.read()
i = 0
while system:
    if (i % 10 == 0):
        print(i)