\(\(\( (*\ 
      Two - conformation\ Model\ *) \)\(
\)\(
\)\(\(SeedRandom[];\)

    \(step = 0.01;\)
 (*\ 
      parameter\ step\ determines\ the\ time\ resolution\ and\ is\ regarded\ as\ the\ unit\ time\ in\ the\ simulation . \ step\  = \ 
        0.01\ means\ the\ program\ generates\ data\ every\ 0.01\ \(\(second\)\(.\)\)\ *) 


    \(Prob = {{0.013, 0.013}, {0.053, 
            0.053}};\)
 (*Prob\ of\ leaving\ the\ \(state : \ \((k + ck)\)*step\)\ *) 
 (*\ 
      parameter\ Prob\ defines\ the\ probability\ of\ leaving\ a\ state . \ 
            In\ a\ two - conformation\ model, \ 
      there\ are\ 4\ possible\ microscopic\ states, \ 
      including\ off + conformation\ 1, \ on + conformation\ 1, \ 
      off + conformation\ 2, \ and\ on + conformation\ 2. \ Therefore, \ 
      Prob[i, 1] = \ 
        1 - exp \((\(-\((ki1 + ri1)\)\)*
                step)\)\ is\ the\ probability\ of\ leaving\ the\ state\ of\ off + conformation\ i, \ 
      where\ ki1\ is\ the\ backward\ reaction\ rate\ \((from\ off - 
            state\ to\ on - 
            state)\)\ in\ conformation\ i\ and\ ri1\ is\ the\ conformation\ interconvrsion\ rate\ in\ this\ state; \ 
      Prob[i, 2] = \ 
        1 - exp \((\(-\((ki2 + ri2)\)\)*
                step)\)\ is\ the\ probability\ of\ leaving\ the\ state\ of\ on + conformation\ i, \ 
      where\ ki2\ is\ the\ forward\ reaction\ rate\ \((from\ on - 
            state\ to\ off - 
            state)\)\ in\ conformation\ i\ and\ ri2\ is\ the\ conformation\ interconvrsion\ rate\ in\ this\ state . \ 
          When\ the\ step\ size\ is\ very\ small, \ 
      Prob = \ \((k + r)\)*
          step\ for\ all\ 4\ \(\(states\)\(.\)\)\ *) 
 (*\ 
      For\ example, \ for\ k11 = \ \(k21\  = \ 1\ per\ second\), \ 
      k12 = \ \(k22 = \ 5\ per\ second\), \ 
      r11 = \ \(r21 = \ \(r21 = \ \(r22 = \ 0.3\ per\ second\)\)\), \ 
      Prob[1, 1] = \ 0.013; \ Prob[1, 2] = \ 0.013, \ Prob[2, 1] = \ 0.053, \ 
      and\ Prob[2, 
            2] = \ \(\(0.053\)\(.\)\)\ *) 


    \(cProb = {{0.2308, 0.2308}, {0.0566, 0.0566}};\)
 (*\ 
      parameter\ cProb\ defines\ the\ probability\ that\ when\ a\ molecule\ is\ leaving\ a\ state, \ 
      it' s\ leaving\ for\ a\ different\ conformation\ state . \ \ cProb[i, 
              j] = \ rij/\((kij + rij)\)\ *) 
 (*\ 
      For\ example, \ for\ k11 = \ \(k21\  = \ 1\ per\ second\), \ 
      k12 = \ \(k22 = \ 5\ per\ second\), \ 
      r11 = \ \(r21 = \ \(r21 = \ \(r22 = \ 0.3\ per\ second\)\)\), \ 
      cProb[1, 1] = \ 0.2308; \ Prob[1, 2] = \ 0.2308, \ 
      Prob[2, 1] = \ 0.0566, \ 
      and\ Prob[2, 
            2] = \ \(\(0.0566\)\(.\)\)\ *) 


    \(Time = 2800000;\)
 (*\ 
      parameter\ Time\ defines\ the\ total\ number\ of\ data\ points . \ 
            If\ Time = \ 10000, \ 
      the\ total\ length\ of\ the\ single\ molecule\ trace\ is\ 100\ seconds\ \((Time\ *\ step)\)\ *) 


    \(Turnover = 40000;\)
 (*\ 
      parameter\ Turnover\ is\ a\ rough\ estimation\ of\ the\ total\ number\ of\ turnover\ from\ the\ forward\ and\ backward\ reaction\ rates . \ 
            The\ number\ of\ turnovers\ is\ also\ the\ number\ of\ on - 
        times\ and\ off - 
        times\ generated\ from\ the\ \(\(program\)\(.\)\)\ *) 


    \(signal = 1;\)

    \(t = 1;\)

    \(j = 1;\)

    \(k = 1;\)

    \(trace = Table[0, {i, Time}];\)

    \(on = Table[0, {i, Turnover}];\)

    \(off = Table[0, {i, Turnover}];\)


    \(c = 1;\)

    \(ct = 1;\)

    \(m = 1;\)

    \(n = 1;\)

    \(conformation = Table[0, {i, Time}];\)

    \(c1 = Table[0, {i, Time}];\)

    \(c2 = Table[0, {i, Time}];\)


    \(Do[{
trace[\([i]\)] = 
            signal; 
conformation[\([i]\)] = 
            c; 

If[
            Random[] > 
              Prob[\([c, signal + 1]\)], 
{t = t + 1; 
              ct = ct + 1}, 
{If[
                Random[] >= 
                  cProb[\([c, signal + 1]\)], 
{ct = 
                    ct + 1; 
If[
                    signal == 1, {on[\([j]\)] = t; 
                      j = j + 1}, {off[\([k]\)] = t; 
                      k = k + 1}]; 
signal = 
                    Mod[signal + 1, 2]; 
t = 
                    1}, 
{t = t + 1; 
If[
                    c == 1, {c1[\([m]\)] = ct; m = m + 1}, {c2[\([n]\)] = ct; 
                      n = n + 1}]; 
c = 
                    Mod[c, 2] + 1; 
ct = 
                    1}]
}]
}, 
{i, \ Time}];\)


    \(v = 
        Table[{on[\([i]\)]*step, off[\([i]\)]*step}, {i, 
            Turnover}];\)

    \(cv = 
        Table[{c1[\([i]\)]*step, c2[\([i]\)]*step}, {i, 
            Time}];\)


    \(Export["C:\\simulation\data\\trace.dat", 
        trace, "Table"];\)
 (*\ 
      the\ generated\ single - molecule\ on - 
        off\ trace\ is\ output\ to\ the\ above\ \(\(address\)\(.\)\)\ *) 


    \(Export["C:\\simulation\data\\on-off.dat", 
        v, "Table"];\)
 (*\ 
      the\ generated\ on - times\ and\ off - 
        times\ are\ output\ to\ the\ above\ \(\(address\)\(.\)\)\ *) 


    \(Export["C:\simulation\data\conf-trace.dat", 
        conformation, "Table"];\)
 (*\ 
      time\ trace\ of\ the\ conformation\ state\ of\ the\ molecule\ is\ output\ to\ the\ above\ \(\(address\)\(.\)\)\ *) 


    \(Export["C:\simulation\data\c1-c2.dat", 
        cv, "Table"];\)
 (*\ 
      the\ time\ durations\ that\ the\ molecule\ reside\ in\ the\ two\ distinct\ conformation\ states\ are\ output\ to\ the\ above\ \(\(address\)\(.\)\)\ *) 


    \)\)\)