Creating Scripts

In this section, we explore Cytocalc by creating two simple scripts that uses some functions in built in Cytocalc. The code reference lists all functions available for usage and some example scripts can be found in the scripts directory.

General idea

  1. Import the specific class of Cytocalc you want to use and additional python modules.

  2. Define an object of that specific class

  3. Read over data stored in single or multiple files and add it to the object

  4. Call the specific analysis function you want to use

  5. Plot or print result

Example 1: MSD of beads

Simulation details: A cytosim simulation with N beads in a filament network. The report is then generated using report bead:position beads.txt.

  1. We start by importing the CSMParser for data analysis, and matplotlib for ploting.

    import matplotlib.pyplot as plt
    from cytocalc.csmparser import CSMParser
    
  2. We want to read the report file with the name <path-to-beads.txt>.

    filename = '<path-to-beads.txt>'
    
  3. We then use the parse_simFile() function of the CSMParser to read the report file. We use the parse_simFile() function because the data from every frame is contained in a single text file.

    parser = CSMParser()
    sim = parser.parse_simFile(filename)
    
  4. We can now use the compute_time_avg_msd() function to compute the Mean square displacement of the beads in the system.

    msd = sim.compute_time_avg_msd()
    
  5. We use matplotlib to plot the outputs of the compute_time_avg_msd() function, which shows time vs mean square displacement of the beads.

    plt.plot(msd.keys(),msd.values())
    plt.show()
    

Example 2: Contraction of network

Simulation details: Following the studies on effects of crosslinkers and motors on the contraction of cytoskeleton network [1], here we have a set of cytosim simulations of filament networks with motors and crosslinkers. A parameter scan over different number of motors is saved in the folders named motor_count####, and the position of each filament nodes for each frame of the simulation are reported using reportf, creating a directory structure as follows:

.
├── config.cym
├── motor_count1000
│   ├── report0001.txt
│   ├── ...
│   └── report0040.txt
├── motor_count2000
│   ├── report0001.txt
│   ├── ...
│   └── report0040.txt
├── motor_count3000
│   ├── report0001.txt
│   ...
...
  1. We import the glob module for finding certain files in a directory. From our package, we also import the CSMSimulation class.

    import glob
    from cytocalc.csmsimulation import CSMSimulation
    
  2. We get the name of the directory, dir containing a few report####.txt files corresponding to the frames of a simulation. The directory name is provided as the first argument when calling the python script. We then find the .txt files in this directory using the glob module.

    dir = sys.argv[1]
    frame_files = glob.glob(dir+'*.txt')
    
  3. We read each of the files corresponding to individual frames using the parse_frameFile() function and add it to a CSMSimulation.

    sim = CSMSimulation()
    for frame_file in frame_files:
            frame = CSMParser.parse_frameFile(frame_file)
            sim.add_frame(frame)
    
  4. We compute the radius of network in each frame using the compute_radius() function.

    times = [frame.csm.time for frame in sim.frames]
    radii = [frame.compute_radius() for frame in sim.frames]
    
  5. We can plot radius of network w.r.t time .

    plt.plot(times, radii)
    
    _images/radii_vs_time_750.png
  6. We can also compute the mean contraction rate of the network using the contraction_rate() function.

    print(sim.contraction_rate())
    

    This will return the contraction rate in Cytosim units (μm/s).

    $ ./contrate.py ../data/2022_01_10_22_17_56/
    -0.07988585749886662
    
  7. Looping over all directories motor_count####, we can also observe how contraction rate varies with different parameters, which is motor count in this particular case, along with the thoretical contraction rate [1] plotted using theoretical_cont_rate().