Introduction

In everyday life we have to generate dexterous movements of limb joints to purposefully interact with variable environments. The mammalian primary motor cortex (M1) is known to contribute to control of dexterous limb movements1,2,3,4,5,6,7,8 and gait modifications9,10,11. Moreover, M1 has recently been shown to have a pivotal function in learning non-dexterous movement sequences in rats12, and has been suggested to be necessary for progressing through the steps of learned skilled forelimb movements in mice7. Still, the principles of its operation—such as the representation of movements in the M1 microcircuit—remain poorly understood13,14,15,16,17,18,19. Various studies linked changes of movement parameters to changes of neuronal M1 activity during ongoing motor actions. For example, neuronal activity in M1 was found to control various movement variables including direction, force, speed, end-posture, and individual joint angles as well as muscle activity14,15,17,18,20,21,22,23,24,25,26,27. However, in addition to representing features of the ongoing movement itself, neuronal activity in M1 may also represent general demands of the environmental setting, within which the movement is executed. These demands could be regarded as meta-variables that adjust activity of neuronal circuits in M1 in addition to the movement variables that characterize the ongoing motor action itself. The representation of a specific limb movement may thus be flexibly modulated according to certain principles when the same motor action is executed in different environmental contexts. Such context-dependent modulation of M1 neuronal encoding according to environment-characterizing meta-variables has been scarcely investigated so far.

Accurate motor control in a given environmental context requires integration of contextual information with processing of specific sensory stimuli28. Layer 2/3 (L2/3) neurons in M1 receive inputs from sensory areas29,30 as well as from the secondary motor cortex (M2)31,32,33, which is thought to convey context information for motor processing34,35,36. Given this pattern of afferent inputs and their excitatory output to L5 neurons in M137,38, L2/3 neurons are well positioned to optimize motor commands by integrating contextual and sensory information, consistent with a pivotal role in the refinement of motor actions39,40,41.

Here we hypothesized that the degree of flexibility that is demanded from an individual limb joint in a specific environment is a key contextual parameter that affects joint movement representation in L2/3 of M1. We applied two-photon calcium imaging to record L2/3 neuronal population activity in M1 while mice performed distinct forelimb grasping actions in order to move on either regularly or irregularly spaced ladder rungs. Compared with the regular pattern, the irregular pattern represents a different environmental context that demands more flexible use of several limb joints, for example of the proximal shoulder joint. Lesion studies demonstrated that the motor cortex is required for accurate forelimb movements on the regular and in particular on the irregular pattern in mice as well as rats3,42,43. We show that joint movements are differentially encoded in L2/3 neuronal networks of M1 in the regular and irregular context, respectively, even for motor actions with matching kinematic profile, and that encoding strength increases if a higher flexibility is demanded and vice versa. Furthermore, we demonstrate that this context-dependent coding of flexibility demands emerges when the animal is familiarized with the distinct contexts and learns to interact with the respective situations. Finally, using chemogenetic silencing we show that context-dependent modulation of L2/3 neuronal representation in M1 entails more precise limb movements and requires input from M2.

Results

Grasping behavior on regular and irregular ladder wheels

To enable calcium imaging of cortical neurons in awake, head-fixed mice under conditions of different environmental demands for flexible limb movements, we built ladder wheels to emulate the rung ladder test for rodents3,42,43 (Fig. 1a). This test requires sequential grasping actions to perform skilled locomotion on rungs with constant spacing (regular ladder) or with spacing that varies unpredictably (irregular ladder). We trained seven mice on the regular and irregular ladder wheels, all reaching saturating forelimb performance scores3,42 for both conditions within 8 training days (Supplementary Fig. 1; see the Methods section). Hence, days 9–12 were defined as expert phase, which we focused on first. Using high-speed videography of the animals’ right side we tracked kinematic changes of the shoulder, elbow, wrist, and finger-base joints for the right forelimb of trained mice during each run (Fig. 1a, Supplementary Movie 1). During a given run mice covered a predefined distance with a continuous sequence of forelimb grasps and with comparable running speed (Fig. 1b, c; 11.1 ± 3.5 grasps per run [24.7 ± 6.4 runs] and 8.8 ± 2.4 grasps per run [25.7 ± 6.1 runs] for regular and irregular wheel, respectively; mean ± s.d.; pooled across all seven mice during the expert phase). The area under the curve (AUC) of the receiver operating characteristic (ROC) revealed no significant difference in the distributions of running speed across all runs for the regular and irregular condition (ROC-AUC = 0.63; Methods).

Fig. 1
figure 1

Analysis of forelimb grasps and running behavior on ladder wheels. a Setup with head-fixed mouse on top of a ladder wheel and below a two-photon microscope. Joint angle changes in shoulder (S), elbow (E), wrist (W), and finger base (F), as well as the reaching distance (RD), were quantified by tracking tattoos on the right forelimb (red dots). Nine neuronal networks in M1 L2/3 (n1–n9) were recorded from seven mice (m1–m7) while they moved across rungs with regular (cyan) or irregular (magenta) spacing. b Running speed histograms, pooled for all mice on the regular (cyan) and irregular (magenta) wheel. c Time course of forelimb joint angles and RD during two runs on regular (top) and irregular (bottom) rungs. Three prototypical grasps are highlighted: Standard (gray), corrective (black), and digit-tip grasp (dark turquois). d Histograms of maximal RD during each grasp, grasp duration, mean grasping speed during paw reaching/retraction (positive/negative values), and mean grasping acceleration during reaching/retraction (positive/negative values) for both conditions, pooled across all seven mice. e Kinematic profile of joint angles and RD for standard (gray), corrective (black), and digit-tip (dark turquois) grasps during both conditions (grasps marked in (c) with duration normalized). Dots on RD traces indicate the number of reaching cycles during the grasp; black horizontal dashed lines mark 170° threshold for mean finger extension, which is exceeded only in digit-tip grasps as indicated by the dark turquois line and arrow). Time scale is normalized from start (0) to end (1) of grasps. f Representative stick-figure plots of limb kinematics for the three principal grasp types. g Grasp-similarity matrices of one mouse for the regular and irregular condition as well as for the difference between both conditions, sorted according to the classification in standard (gray), corrective (black), and digit-tip (dark turquois) grasps and sub-sorted according to similarity values. h Fraction of grasps types on the regular and irregular wheel for all nine neuronal networks. i Grasp-to-grasp variability (GGV) of each joint on the regular and irregular pattern for all nine neuronal networks; h, i: Asterisks indicate P < 0.05 (paired t-test, P-value adjusted according to Holm–Bonferroni)

Rung settings were designed to impose different demands for flexible joint adjustment in the regular and irregular condition while sustaining similar reaching distances, grasping duration, grasping speed and forelimb force. These prerequisites were satisfied by two ladder wheels of equal size (23-cm diameter) and weight (116.6 g) with constant 1-cm spacing for the regular wheel and spacing between 1 and 3 cm (average 1.68 ± 0.56 cm, mean ± s.d.) for the irregular wheel. With these rung settings, reaching distances varied mainly between 1 and 3 cm for both wheels with similar means (Fig. 1d; 2.26 ± 0.46 cm and 2.13 ± 0.64 cm for regular and irregular, respectively; mean ± s.d.; n = 7 mice). The distributions of reaching distance, grasp duration, grasping speed, and grasping acceleration (as proxy for forelimb force, see Methods) did not significantly differ for both types of wheels (Fig. 1d; ROC-AUC = 0.55, 0.64, 0.53, and 0.53, respectively; pooled across all seven animals during expert phase).

Across mice and conditions we identified three salient grasp types based on the temporal profile of the reaching movement and the mean finger extension during each grasp (Fig. 1e, f; Methods; Supplementary Movie 2): ʻStandard graspsʼ consisted of a single motion cycle including reaching phase, correct placement of the forepaw on the rung, closure of the paw, and a terminal pulling phase; ʻcorrective graspsʼ were characterized by one or multiple corrective movements after the initial reaching action until the forepaw optimally hit the targeted rung with its palm and the subsequent pull occurred; and finally, ʻdigit-tip graspsʼ, during which the targeted rung was hit with the digit tips rather than with the palm of the hand, causing pronounced extension of the finger-base joint and necessitating dexterous finger control to avoid a slip and to finish the pull. This classification was also reflected in similarity matrices for grasp pairs, which we calculated using the mean Euclidian distance of grasp trajectories in the 12-dimensional space consisting of the four joint angles along with their velocity and acceleration trajectories (see Methods). This similarity analysis also revealed some diversity within each cluster (Fig. 1g). The fraction of corrective grasps was significantly higher for the irregular compared with the regular wheel (Fig. 1h; P = 0.006, t = −5.17; paired t-test with P adjusted according to Holm–Bonferroni (HB); n = 7).

To quantify the flexibility demands for each forelimb joint in the regular and irregular context, respectively, we computed the grasp-to-grasp variability (GGV) of joint motion by calculating the mean amplitude variation from one grasp to the next (see Methods). In the expert phase, GGV for shoulder, elbow and wrist was significantly higher for the irregular compared with the regular rung pattern (Fig. 1i; P = 0.0003, t = −9.3056, P = 0.0121, t = −4.5131, and P = 0.0222, t = −3.6196, respectively; paired t-test with HB adjustment; n = 7). In contrast, GGV for the finger joint was not significantly different between conditions (P = 0.4881, t = −0.7385). For expert mice the irregular wheel thus imposed higher flexibility demands for shoulder, elbow and wrist movements compared with the regular wheel while flexibility demands of finger-base movements were comparable in both conditions.

Calcium imaging in the M1 forelimb area during grasping

To consistently localize the forelimb region in M1 for subsequent calcium imaging we used transgenic mice that express channelrhodopsin-2 (ChR2) under the thy1 promoter in cortical L5 neurons44,45 and in addition virally expressed in M1 the genetically encoded calcium indicator yellow-cameleon Nano140 (YC-Nano140)46,47,48. To identify the M1 forelimb area, we performed optogenetic motor mapping44 by scanning a blue laser spot across M1 to stimulate localized networks of L5 neurons and concurrently measuring light-evoked joint movements of the contralateral forelimb using video monitoring (Fig. 2a, b; Methods). Light stimulation caused forelimb muscle contractions and induced flexion or extension of individual joints within confined but overlapping regions (Fig. 2b). Within this forelimb region, all mice featured a central spot, in which a combined movement consisting of shoulder retroversion, elbow and wrist flexion as well as finger extension was maximal (Fig. 2b, c; Methods). These movements resembled the initial phase of a grasp, and the respective spots within the forelimb region were selected for two-photon calcium imaging of L2/3 neuronal populations. In control experiments using cell-attached recordings from ChR2-expressing L5 neurons, we verified that laser scanning for two-photon imaging in L2/3 did not cause spurious spiking activity in L5 (Fig. 2d, e; Methods).

Fig. 2
figure 2

Optogenetic mapping and electrophysiology during two-photon laser scanning. a Motor mapping setup with an anesthetized mouse hanging in a hammock, limbs dangling free. Blue laser light randomly scanned across M1 evoked forelimb joint angle movements that were monitored with a camera. b Maps of evoked joint angle changes for an example mouse. Negative values correspond to flexion (orange, retroversion in the shoulder joint), positive values to extension (blue, anteversion in the shoulder joint). Laser-stimulation elicited responses in shoulder (area 1.12 ± 0.19 mm2), elbow (1.29 ± 0.27 mm2), wrist (1.16 ± 0.32 mm2), and finger-base joints (1.03 ± 0.13 mm2; thresholded at 50% of maximal response in each joint, black contour; mean ± s.e.m. in each case). The superimposed white rectangles indicate the selected forelimb focus area for subsequent calcium imaging (purple cross = bregma, dashed lines indicate the affiliation to the respective map; a = anterior; p = posterior; m = medial; l = lateral). Scale bar 1 mm. c Movement amplitude of angle changes in shoulder (S), elbow (E), wrist (W) and finger-base joints (F) in the seven mice when optogenetic stimulation is applied in the forelimb focus. d Upper part: Cell-attached recording of a ChR2-expressing L5 neuron in M1 during repetitive application of blue 488-nm light. The evoked spiking pattern during one stimulation period (shaded area) is shown on expanded time scale below. Lower part: Cell-attached recording of the same L5 neuron during two-photon excitation laser scanning with near-infrared (NIR) light in L2/3, equivalent to the conditions used for L2/3 calcium imaging. The expanded view of one stimulation period below demonstrates the lack of two-photon excited spikes and extracellular voltage changes. e Pooled data for similar recordings in eight L5 neurons, indicated by black dots (neurons were not recorded from the same ChR2 mice that are shown in (c)). Whereas blue light stimulation induced strong spiking of L5 neurons, laser scanning in L2/3 with two-photon (2P) excitation light of 820-nm wavelength did not induce any detectable changes in the spiking rate of L5 neurons. Asterisks indicate significant differences with P < 0.05, n.s. (non-significant) means P > 0.05 (paired t-test; P-values HB-adjusted)

We then measured fluorescence signals of YC-Nano140-expressing L2/3 neurons while expert mice engaged in skilled locomotion on the regular and irregular wheel, respectively (9 imaging areas with the same neuronal network recorded in both contexts; 486 cells in total; 7 mice; 54.0 ± 12.1 neurons per area; 195–220 µm below the pia; Supplementary Movie 3). For running periods on both types of wheel, we extracted neuronal calcium transients from somatic regions of interests (ROIs). For further analysis we deconvolved calcium transients46 to infer the time course of instantaneous spiking rate changes (SR) (Fig. 3a–c; Methods). During runs, L2/3 activity was heterogeneous across the sampled neuronal subsets and temporally sparse, with occasional large calcium transients (>15% ΔR/R; >10 Hz SR) indicating bursts of action potentials. Some cells showed calcium transients that were strongly linked to salient finger movements as they occurred during digit-tip grasps in both conditions or to large shoulder movements on the irregular wheel (Fig. 3b, c; see Supplementary Fig. 2 for further cell examples).

Fig. 3
figure 3

Neuronal activity in M1 L2/3 and simultaneous forelimb joint movements. a Example two-photon image of L2/3 neuronal population (upper panel) imaged during skilled locomotion with the genetically encoded calcium indicator YC-Nano140 and schematic of ROIs (lower panel) with two neurons marked in orange; Scale bar 50 µm. b Raw YC-Nano140 traces (ΔR/R, black line) and deconvolved instantaneous spiking rates (SR, gray line) for the two example cells marked in (a) for the regular condition along with simultaneously recorded joint angles and classified grasp types. Three salient finger movements (light blue shaded areas) that correlate with neuronal activity are highlighted. c Same conventions as in (b) for the irregular condition. Three salient finger movements (light blue shaded areas) and three strong shoulder movements (light red shaded areas) as well as the simultaneous neuronal responses are highlighted. b, c: S = shoulder; E = elbow; W = wrist; F = finger base

Encoding of limb movements according to flexibility demands

First, we computed the mean spiking rate of each neuron in a given neuronal network. Averaged across all neurons for each network the mean spiking rate was not significantly different for the regular and irregular wheel (2.82 ± 0.72 Hz and 2.65 ± 0.49 Hz, respectively; mean ± s.d.; P = 0.3, t = 1.1; paired t-test; n = 9). We next evaluated how the activity of individual neurons related to specific grasp types by averaging grasp-related calcium transients separately for each grasp type (Fig. 4a). A small but significant fraction of neurons exhibited grasp-related activity for digit-tip grasps (Fig. 4b; 9% of all cells on the regular and 10% of all cells on the irregular wheel; above chance level of 2.5%; Methods). Thus, M1 L2/3 neurons show pronounced activity mainly during digit-tip grasps, whose salient feature is the extensive movement of finger-base joints.

Fig. 4
figure 4

Responses of single neurons to grasp types and movements in individual joints. a Raw calcium (ΔR/R) traces for cell 2 (left) and cell 12 (right) from Fig. 3 during the three different grasp types and for both conditions. Corresponding average traces below for raw calcium traces (upper panel) and deconvolved spiking rate (SR) traces. b Percentages of the 486 recorded cells showing significant activity related to specific grasp types under both conditions (pooled across all nine recorded neuronal networks). c Percentages of the 486 recorded neurons showing significant prediction of specific forelimb joint angle changes under both conditions (two pies on the left, pooled across all nine recorded neuronal networks). In Figs. 3 and 4a, cell 2 is classified as finger-predictive and significantly active during digit-tip grasps; cell 12 is classified as shoulder-predictive. Pie on the right: Context specificity of significant joint-angle predictive cells from regular to irregular. Neurons significantly predicted a particular joint movement either only in the regular condition (Reg, cyan), or only in the irregular condition (Irreg, magenta), or in both conditions (Both, purple), or they swapped significant encoding of the respective joint from regular to irregular (J-Swap, orange)

We next asked to what extent single neuron activity predicts the time course of finger movements, along with the time course of shoulder, elbow and wrist movements. We trained a random forest algorithm to predict each of the four joint angles based on the deconvolved calcium signals of individual neurons (see Methods). On the regular wheel, no neuron predicted elbow movements and very few individual neurons predicted shoulder (0.6%) and wrist (0.4%) movement (ROC-AUC between true and shuffled prediction ≥0.95; Methods). On the irregular rung pattern, however, 3% of neurons significantly predicted shoulder, 0.6% elbow and 3% wrist movements (Fig. 4c, two pies on the left). Under both conditions, about a tenth of neurons significantly predicted finger movements, with a slightly higher fraction for the regular pattern (9% compared with 7% on the irregular pattern; Fig. 4c, two pies on the left). Thus, when mice have learned to step on the irregular ladder, the number of M1 L2/3 neurons encoding shoulder, elbow and wrist motion increases compared with the regular ladder, at the expense of neurons encoding finger motion. This finding suggests environment-specific neuronal configurations in M1 L2/3, which did not arise as a mere consequence of correlation changes among joints between conditions (Δr, irregular minus regular) because these were minimal and not significantly different from zero (ΔrSE = −0.08 ± 0.11, ΔrSW = −0.01 ± 0.1, ΔrSF = −0.06 ± 0.12, ΔrEW = 0.06 ± 0.09, ΔrEF = 0.02 ± 0.17, ΔrWF = −0.05 ± 0.19, mean ± s.d.; S—shoulder, E—elbow, W—wrist, F—finger; P > 0.63 in all six cases; one-sample t-test with P-value adjusted according to HB, n = 7). As expected from the grasp-type-related analysis, a large fraction of the neurons showing significant activity during digit-tip grasps also significantly predicted finger-base movements in the regular (63%) and irregular condition (45%). Of the 486 neurons recorded, 5.8% significantly predicted a particular joint movement only in the regular and 9.25% only in the irregular context, also indicating context-specific reconfiguration of neuronal circuits in M1 L2/3. 2.9% of the 486 neurons predicted a particular joint movement in both conditions and only 1.2% switched the prediction of a particular joint movement for the regular and irregular context (Fig. 4c, pie on the right).

To examine encoding of forelimb kinematics on the population level we next used the random forest algorithm to predict forelimb joint movements based on the estimated spiking rates of all M1 L2/3 neurons within each imaging area (Fig. 5a, b; Methods). As a measure of predictive power we used the correlation of predicted and real joint motion (Supplementary Figs. 3 and 4). In the expert phase, the motion of all joints was significantly encoded by the local L2/3 populations of neuronal networks 1, 2, 3, 4, 6, and 7 for both wheel conditions (based on ROC-AUC ≥0.9 for true and shuffled distribution; see Methods and Supplementary Fig. 4a). In neuronal network 5, prediction of elbow movements just missed significance and in neuronal network 9, only prediction of shoulder movements in the irregular context was significant. In neuronal network 8, none of the joint angles were predicted significantly even though shoulder in both conditions came close (ROC-AUC = 0.89 in each case). When the predictive power in both conditions was compared, the same neuronal networks displayed significantly increased mean prediction for shoulder and wrist motion when the animals moved in the irregular instead of the regular context (Fig. 5c; P = 0.0059, t = −4.7337, P = 0.0742, t = −2.497, P = 0.0399, t = −3.1653 for shoulder, elbow and wrist, respectively, paired t-test with P adjusted according to HB, n = 9). The predictive power for finger-base joint motion tended to surpass that of all other joints in the regular and irregular context but did not differ significantly between conditions (Fig. 5c, Supplementary Movie 4; P = 0.6074, t = −0.5348). For all joints and both conditions, saturating population coding was in most cases achieved after inclusion of 20–40% of the population size (Supplementary Fig. 5).

Fig. 5
figure 5

Prediction of forelimb variables from L2/3 activity. a Tracked shoulder (S), elbow (E), wrist (W), and finger-base (F) joint angles (gray) during running on the regular wheel together with angle changes predicted from the imaged L2/3 neuronal population activity using random forest regression (cyan). Bottom: Grasp classification based on kinematic criteria as shown in Fig. 1. b Example running period on the irregular wheel for the same neuronal population as shown in (a). Angle changes predicted from the imaged L2/3 neuronal population activity are depicted in magenta. c Forelimb joint movement prediction from activity of neuronal populations in M1 L2/3 in the regular (cyan) and irregular context (magenta), based on Pearson’s correlation coefficient (PCC) between real and predicted joint angle traces. Results are shown for all 9 recorded neuronal networks. d Between-condition differences in prediction (irregular-regular: ΔPCC I-R) versus between-condition differences in grasp-to-grasp-variability (irregular-regular: ΔGGV I-R). ΔGGV I-R (see also Fig. 1) was computed to quantify differences of joint flexibility demands between the regular and irregular context; asterisk indicates P < 0.05, linear regression with clustered standard error (robust), cluster variable = neuronal network. e Population coding of each joint angle when one of the three grasp types is removed from the dataset in the regular condition (St: Standard grasps, C: Corrective grasps, D: Digit-tip grasps). For each neuronal network, prediction changes (ΔPCC) are shown relative to the population coding when all grasps are included (zero line). f Same conventions as in (e), but for the irregular condition; c, e, f: Asterisks indicate P < 0.05 (paired t-test, P-value adjusted according to HB)

Because one salient difference between both conditions is captured by the different flexibility demands of joint angle amplitudes (see grasp-to-grasp variability, GGV, in Fig. 1i), we next analyzed to what extent differences in predictive power in the regular versus the irregular context can be explained by context-dependent differences in GGV. We found that between-condition differences in joint angle prediction by neuronal networks were significantly explained by corresponding between-condition differences in joint angle GGV (Fig. 5d; P < 0.0001, r2 = 0.49, n = 9 networks, four joint angles for each; linear regression with clustered standard error; cluster variable = neuronal network; Methods). Importantly, this dependence was true for all joints and not only for wrist and shoulder. Encoding for wrist and shoulder consistently increased in all neuronal networks from regular to irregular, as did the GGV, leading to respective significant differences. However, in neuronal networks 4, 5, and 9, for example, the encoding of finger movements decreased from regular to irregular, as did the GGV. Consequently, encoding and GGV of finger movements were not significantly different between conditions, but were positively correlated also in these networks.

Because of the large fraction of regular- and irregular-specific single-cell predictors of joint angles (see above), we next investigated to what extent the population coding of individual joint movements relies on context-specific neuronal configurations. When we trained the random forest algorithm in the irregular condition and predicted joint movements for the regular condition, the predictive power significantly decreased for finger-base movements (Supplementary Fig. 6a; P = 0.1969, t = 1.4077, P = 0.0532, t = 2.9752, P = 0.1540, t = 2.0286, P = 0.0003, t = 7.4768 for shoulder, elbow, wrist and finger, respectively; paired t-test with P-values adjusted according to HB, n = 9). Vice versa, when we trained the random forest algorithm/decoder in the regular condition and predicted joint movements for the irregular condition, the predictive power significantly decreased for all joints (Supplementary Fig. 6b, P = 0.0137, t = 3.8962, P = 0.0078, t = 4.5185, P = 0.0344, t = 2.9946, P = 0.0173, t = 2.992 for shoulder, elbow, wrist, and finger, respectively; paired t-test with P-values adjusted according to HB, n = 9). Together with the single-cell analysis, these results imply that neuronal circuits in M1 L2/3 reconfigure when the animal moves in different contexts, showing enhanced motion encoding for those joint angles that demand higher grasp-to-grasp flexibility in a given contextual setting.

We next asked how the encoding of individual joint movements by neuronal networks in M1 L2/3 relates to the three different grasp types. We analyzed how the prediction of joint movements changes after removing one of the three grasp types from the dataset (Methods). This analysis showed that digit-tip grasps mainly contribute to the pronounced prediction of finger-base joints in both conditions, which decreased markedly after removal of digit-tip grasps (Fig. 5e, f; P = 7.7690 × 10−6, t = 12.1564, P = 0.0006, t = 7.4013 for regular and irregular condition, respectively). This result is in line with the high average activity of some cells during digit-tip grasps and their frequent additional encoding of finger movements (see above). Both in the regular and irregular context, all three grasp types contributed to a similar extent to the prediction of shoulder, elbow and wrist movements, even though their encoding tended to depend more on digit-tip grasps in the irregular condition (Fig. 5e, f).

Context-dependent M1 encoding of equivalent kinematics

Between conditions, we found two significant differences regarding the population coding of joint movements in M1 L2/3: First, the encoding of shoulder and wrist movements significantly increased in the irregular compared with the regular context. Second, context-dependent encoding differences could be explained by the corresponding contextual differences in joint flexibility demands. We therefore next asked to what extent these between-condition effects might have been generated by differences in limb kinematics across conditions despite the occurrence of the same grasp types. Kinematic differences across conditions could possibly arise from the different fraction of grasps of each type in the regular and irregular context as well as from joint angle differences between grasps of the same type.

To probe a possible influence of kinematic differences between conditions, we considered only equivalent movements with matching joint angle kinematics on the regular and irregular pattern (‘twin grasps’). If preserved, at least part of the encoding differences must have emerged from the different contexts and cannot simply be explained by kinematic differences during locomotion on the two types of wheels. We therefore compiled the most similar grasp pairs across conditions, separately for standard, corrective and digit-tip grasp clusters, based on the Euclidean distance of 12-dimensional trajectory pairs consisting of the four joint angles, the four joint-angle speed traces and the four joint-angle acceleration traces (Fig. 6a; Methods). The acceleration values were included to approximate the applied forces in each joint (see Methods). The selection procedure generated twin standard, corrective and digit-tip grasp clusters that differed minimally across conditions (Fig. 6b, c; Supplementary Fig. 7a). Across neuronal networks and joints, twin grasp differences in joint angle, joint-angle speed, and joint-angle acceleration on average amounted to ~0.5 standard deviations of the respective joint-specific parameter, such as for instance shoulder joint-angle speed (0.5–0.6, 0.45–0.59, and 0.41–0.56 for mean differences in joint angle, joint-angle speed, and joint-angle acceleration across networks; red solid line in Supplementary Fig. 7a). This analysis showed that differences in joint angle, joint angle speed and joint angle acceleration were controlled to comparable degrees by the twin grasp procedure.

Fig. 6
figure 6

Encoding of forelimb variables for twin grasps in different contexts. a Representative examples of twin grasp pairs for the regular and irregular condition. b Similarity matrices for the regular and irregular condition after the twin-movement pruning for the same example animal as in Fig. 1g: For each movement on the regular wheel, a near-identical “twin”-movement on the irregular wheel exists that belongs to the same grasp type and features minimal differences in the sample-point-wise Euclidean distance calculated for the 12-dimensional space of joint angles (4) as well as their speed (4), and acceleration values (4). c Similarity matrix for the difference between regular and irregular after the twin-pruning. d Between the regular and irregular condition, the significant encoding differences for shoulder and wrist are preserved when only twin grasps are regarded. Same conventions as in Fig. 5c. Asterisks indicate P < 0.05 (paired t-test for shoulder and Wilcoxon signed rank test for wrist, P-values adjusted according to HB). e Differences in the encoding of individual joint angles when grasps from each twin grasp cluster are pooled per condition versus differences in their total grasp-to-grasp variability from the regular to the irregular condition. Same conventions as in Fig. 5d. Asterisk indicates P < 0.05; linear regression with clustered standard error (robust); cluster variable = neuronal network

Re-analyzing only these twin grasps, we could confirm the differences found for joint motion encoding: Population encoding of shoulder and wrist motion was still significantly increased for the irregular compared with the regular wheel (Fig. 6d, P = 0.0472, t = −2.7884, P = 0.0382, z = −2.0732, for shoulder and wrist, respectively; paired t-test and Wilcoxon signed rank test with P adjusted according to HB in each case, n = 9). In addition, between-condition differences in the encoding of all joints were still significantly explained by between-condition differences in their GGV (Fig. 6e; P < 0.0001, r2 = 0.47, four joint angles for each of nine neuronal networks; linear regression with clustered standard error; cluster variable = neuronal network). The observed encoding differences between twin grasps are neither explained by the minor between-condition differences in joint angle, joint angle speed and joint angle acceleration (as proxy for force) within the twin grasp pools, nor by the supposably higher difficulty of the irregular task (Supplementary Fig. 7b, c; see Methods for detailed explanations). Altogether, the results of the twin grasp analysis support the notion that the observed encoding differences between twin grasps reflect distinct contextual needs for joint flexibility.

Emergence of context-dependent coding in the M2-M1-network

We next asked how the observed M1 encoding of motion in individual joints according to their flexibility demands in the environmental context relates to motor learning, precision of limb movements, and interaction with the secondary motor cortex (M2). To probe the impact of M2 on contextual encoding in M1 L2/3 we conducted a subset of experiments, in which we injected a Cre-dependent hM4D(Gi)-construct into M2 and retrograde AAV-6 Cre virus into M1 of two additional mice, along with YC-Nano140 for calcium imaging (Fig. 7a; Methods). This allowed us to silence neurons projecting from M2 to M1 by injection of the otherwise pharmacologically inert synthetic ligand clozapine-N-oxide (CNO)49,50,51. We recorded the activity of the neuronal networks 6 and 7 (mouse 6) as well as 8 and 9 (mouse 7) across the naive, learning, and expert training phase as well as during the expert phase while silencing M2-M1-projections (242 cells across four imaging areas, 60.5 ± 15.6 neurons per area; mean ± s.d.).

Fig. 7
figure 7

Context-dependent encoding during learning and dependence on M2 input. a Expression of the DREADD system in M2 L2/3 and L5 (hM4Di receptor and mCherry). Temporal development of grasp type frequency (b), joint flexibility demands (c), population coding (d), and population coding for twin grasps (e) during the naive (N), learning (L), expert (E), and expert M2-M1-silenced phase (S), and separately for the regular (cyan) and irregular (magenta) condition; mean ± s.d., n = 4. f Population coding according to joint flexibility demands during the naive, learning, expert and expert M2-M1-silenced phase, respectively (only twin grasps are considered). Note that context-dependent encoding according to joint flexibility demands emerges during learning and is disrupted after silencing of M2-M1 projections; asterisk indicates P < 0.05, linear regression with clustered standard error (robust); cluster variable = neuronal network

Across experimental phases and conditions, the distributions of reaching distance, grasp duration, grasping speed, grasping acceleration as well as running speed were similar (Supplementary Fig. 8; ROC-AUC = 0.5–0.71 in all cases, data pooled across mice 6 and 7). In the regular and irregular condition, standard grasps tended to occur more frequently and corrective grasps less frequently during the learning and expert phase when compared with the naive and M2-M1-silenced phase (Fig. 7b). For the regular condition, joint flexibility demands tended to be lower during the learning and expert phase than during the naive and M2-M1-silenced phase, while no trend was apparent for the irregular condition (Fig. 7c). The encoding level of joints in M1 L2/3 showed some diversity across neuronal networks and no clear trend was observed across phases for the regular or irregular condition, both when considering all grasps or only twin grasps (Fig. 7d, e). However, context-dependent encoding of flexibility demands consistently increased across mice from the naive to the learning to the expert phase, with significance reached in the latter (Fig. 7f; r2 = 0.02; P = 0.69, r2 = 0.06; P = 0.11, r2 = 0.43, P = 0.002 for naive, learning and expert training phase, respectively; four joint angles for each of the four neuronal networks; linear regression with clustered standard error, cluster variable = neuronal network). During the expert phase, silencing of M2-M1-projections by injection of CNO considerably decreased context-dependent encoding when compared with vehicle injections (Fig. 7f). In fact, the amount of context-dependent encoding of flexibility demands subsided approximately to a level between the naive and moderate training phase (r2 = 0.04, P = 0.53; four joint angles for each of the four neuronal networks; linear regression with clustered standard error, cluster variable = neuronal network).

A single-cell coding analysis of all 242 recorded cells revealed that the fraction of neurons that predicted a particular joint in the regular or irregular context correlated inversely with the level of context-dependent encoding, decreasing from the naive (37%) over the learning (15%) to the expert training phase (12%) while increasing again during silencing of M2-M1-projections (39%; Fig. 8a). These results indicate that M2 increasingly suppresses redundant neuronal joint movement representation when the interaction with a new environment is learned. Finally, in line with the increased context-dependence of twin grasp encoding during learning and its decrease upon silencing of M2-M1-projections, the forelimb performance score increased from naive to learning and to expert phase but substantially decreased following the disruption of M2-M1-input. In fact, the forelimb performance score for each neuronal network significantly correlated with the level of context-dependent encoding (Fig. 8b; r2 = 0.63, P = 0.007; four flexibility encoding indices for each of the four recorded neuronal networks; one for naive, learning, expert, and M2-M1-silenced phase, respectively; linear regression with clustered standard error, cluster variable = neuronal network). We conclude that context-dependent encoding of joint movements emerges during learning, entailing more accurate limb movements in the respective context, and that it requires intact information streams from the higher motor area M2.

Fig. 8
figure 8

Phase-dependent neuronal features and context-dependent coding vs. performance. a Total number of neurons that significantly predicted a joint during either condition (black) decreases from naive over learning to expert level and increases again in the expert M2-M1-silenced phase. b Context-dependent encoding of individual twin joint movements correlates positively with the precision of grasping actions, quantified by the forelimb performance score as shown in Supplementary Fig. 1; asterisk indicates P < 0.05, linear regression with clustered standard error (robust); cluster variable = neuronal network. Data are shown for neuronal networks n6–n9

Discussion

Our study extends the current concept of movement processing in motor cortex networks. We found that M1 L2/3 learns to encode joint movements according to their flexibility demands in the given environmental context. The context-dependent modulation of encoding occurs for equivalent movements, is conveyed by the higher motor area M2, and is paralleled by more precise limb movements. Encoding of joints according to contextual flexibility demands is accompanied by a low number of predictive neurons, thereby requiring low computational resources, and features context-specific neuronal configurations.

So far, neuronal activity in M1 during skilled locomotion on ladder paradigms has been mainly investigated in the cat motor cortex. These studies showed that M1 L5 neurons modify their firing rates during locomotion on ladders with regular or vertically displaced rungs52, during forelimb movement phases when evading an obstacle9,10,53, during expected and unexpected gait perturbations on a regular ladder54, and when accuracy demands are varied by comparing locomotion on flat surface, a regular ladder and regular ladders with different crosspiece widths55,56. However, none of these studies addressed the question how the required flexibility of individual limb joints in the given context impacts their coding in neuronal motor cortex networks. While two studies55,56 demonstrated different firing of M1 L5 neurons by comparing different accuracy demands, their tasks included a flat surface and regular ladders thus featuring virtually equivalent joint flexibility demands. Another study52 compared paradigms with different joint flexibility demands but neuronal activity was not related to motor output parameters and no context-variable such as the required flexibility of joint movements was quantified.

Further studies on monkeys probed the relationship between movement variables such as limb joints and neuronal activity in the motor cortex. In restricted, simplified movement sets, such as a set of reach directions, neuronal activity has been proposed to relate to direction, force, speed, joint angle, movement trajectories, and muscle activity, among other variables17,20,21,22,25,26,57,58. In contrast, the representation of these variables was minor during naturalistic movements and much of the neuronal variance remained unexplained, even though neurons showed partial tuning to individual joint angles of preferred arm-postures23,59. One possibility may be that the difference of context-characterizing variables in natural behavior and restricted tasks contributed to the observed encoding differences between the different experimental settings. Indeed, the contexts of natural behavior and restricted tasks are likely to differ in demands such as the average required flexibility of limb joints and presumably many others. So far, the activity of neuronal networks in M1 has been predominantly related to kinematics or force of ongoing movements, and context-characterizing meta-variables may not have been acknowledged sufficiently. Context-dependent encoding of equivalent limb movements, as found here, suggests that integrating contextual features into models of M1 encoding leads to a more coherent motor cortex conception. We presume that demand for flexible joint adjustment represents only one of many meta-variables that span together a multi-dimensional space and define the modulation of M1 encoding in arbitrary environmental contexts.

Due to its anatomical position and connectivity, M1 L2/3 is a suitable candidate to integrate features of the current environmental context with motor commands. M1 L2/3 receives input from other cortical areas such as the somatosensory cortex29,30 and the secondary motor area M231,32,33, which is thought to organize flexible motor behavior and to link relevant context information to motor processing, similar to the primate supplementary complex34,35,36,60,61. Neuronal populations in M1 L2/3 also send excitatory projections to M1 output neurons in layer 537,38 whereby they can couple sensory information to motor output39,41,62. Context-dependent encoding may therefore reflect how M1 L2/3 exploits context information from M2 to selectively route sensory input from those joints into corticospinal circuits, which require flexible re-adjustments in the given environment. Our finding that silencing of M2-M1-projections disintegrates context-dependent encoding is in line with a contextual information flow from M2 to M1. Purposeful coordination of contextual and sensory information to generate accurate movements in varying environmental conditions is postulated by models of sensorimotor control28, and may refine the flexible recruitment of muscle synergies by corticospinal neurons18,27,53,63. We also observed that the increase of grasp precision during motor learning and the decrease during silencing of M2-M1-projections correlated positively with the level of context-dependent encoding. Possibly, the decrease in grasp precision during silencing of M2-M1-projections is directly related to the disintegration of context-dependent encoding. Alternatively, the reduced grasp precision during disruption of M2-M1-projections could result from impairment of other essential M2 functions, such as coordinating the activity of adjacent cortical areas including M164. Besides, top-down input from M2 to S1 has been shown to support accurate somatosensory perception65. If the silenced M2-M1-pathway sent collaterals to S1, disruption of M2-M1-projections could have also impaired the fidelity of sensory feedback and as a consequence grasp precision.

The concept that M1 tailors the representation of movement variables to contextual demands would have important implications in clinical neuroscience. Considering context-dependent representations may for instance help to improve the performance of brain machine interfaces for the control of limb prosthetics in people with paralysis which remains challenging particularly with regard to environmental context changes66. Furthermore, this concept may contribute to the understanding of movement deficits in motor disorders as rodents with neurological M1 dysfunction due to a stroke or Parkinson lesion are known to lack the joint movement flexibility that is required during skilled locomotion especially across irregular rungs3,42. In this regard, our experimental paradigm also provides a novel approach that should be helpful for investigating cortical pathophysiology in rodent models of neurologic diseases with M1 dysfunction.

In summary, we suggest that representation of limb movements in M1 significantly depends on the environmental context and not only on features that characterize the ongoing motor action. Learning which joint movements need to be varied with high flexibility in a given context and reinforcing their sparse representation might be a specific function of M1 L2/3 to focus control on the most relevant degrees of freedom. Context-dependent encoding of limb movements in M1 L2/3 presumably emerges under the impact of higher motor areas during motor learning, entails precise limb movements and may therefore reflect a fundamental cortical processing strategy for adaptive motor behavior.

Methods

Animal surgery and viral constructs

All experimental procedures were carried out according to the guidelines of the Veterinary Office of Switzerland and approved by the Cantonal Veterinary Office in Zurich. In 7 young adult (5–6 weeks) male transgenic ChR2 mice (Thy1-COP4/EYFP)67, we injected AAV2/1-EF1α-YC-Nano140 (300 nl, ~1 × 109 vg μl−1) into L2/3 of M1 (0.1 mm anterior, 1.9 mm lateral from Bregma, 300 μm below pial surface). Mice 1–5 were part of the first experimental series and mice 6–7 of the second experimental series. Mice 6 and 7 were additionally injected with AAV-6 Cre-dependent hM4D(Gi)-mCherry virus into lower layer 2/3 as well as upper layer 5 of M2 (1.5 mm anterior, 0.5 mm lateral to Bregma, 500 μm below pial surface) and with AAV-6 Cre virus into M1 L2/3 (0.1 mm anterior, 1.9 mm lateral from Bregma, 300 μm below pial surface). hM4D(Gi) is a DREADD (“designer receptor exclusively activated by designer drug”)68 that we used in the second experimental series to chemogenetically silence M2-neurons with axonal projections to M1 L2/3, found mainly in lower L2/3 and upper L532. hM4D(Gi)-DREADDs are activated by the otherwise pharmacologically inert synthetic ligand clozapine-N-oxide (CNO), resulting in membrane hyperpolarization and silencing of the infected neurons49,50,51.

It should be noted that recent studies pointed out potential caveats regarding the application of DREADD systems. While one group reported no evidence that CNO crosses the blood brain barrier69, this result is in contrast to the findings from another group70. One suggested possibility is that activation of DREADDs in vivo is likely to be mediated by metabolism of CNO to clozapine69, which readily crosses the blood brain barrier, but has also affinity for serotonergic and dopaminergic receptors71. However, the affinity of clozapine for muscarinic-based DREADDs is substantially higher than for native receptors68. The observed decline of forelimb performance in the expert M2-M1-silenced phase therefore is unlikely to have been generated by effects of low metabolized clozapine doses on native receptors, especially since clozapine is known as anti-psychotic drug with minimal motor side effects71. Still, the possibility that the observed disruption of contextual flexibility encoding and the impairment of forelimb performance in the expert M2-M1-silenced phase was generated specifically by M2-M1-projections, must be regarded with the above mentioned reservations.

Twenty-four hours after virus injections, a circular cranial window (4-mm diameter in animals 1–5, 5-mm diameter in animals 6 and 7) was implanted over M1 around the injection coordinates72. Contralateral to the cranial window, an aluminum head post for head fixation (weight <1 g) was implanted on the skull using dental cement. During the surgeries, mice were anaesthetized with isoflurane (4% induction, 2% maintenance). After the surgery the animals were treated for analgesia with Rimadyl (Carprofen; 5 mg/kg body weight, s.c.) as well as the antibiotic Rocephin (40 mg/kg body weight, s.c.) and returned to their home cage for recovery. For the following 3 days, Rimadyl and Rocephin were injected once per day and the animal’s well-being was evaluated at least twice per day for the first 2 days after surgery and at least once per day during the following 5 days.

Behavioral setup and training

In a set of preparatory experiments, we first probed different rung settings on the regular and irregular wheel. We sought for rung settings that impose different demands for flexible joint adjustment in the regular and irregular condition while demands for reaching distance, reaching range, grasping duration, grasping speed and forelimb forces were supposed to be comparable. We could satisfy these prerequisites using two wheels with the following rung settings: The regular wheel featured rungs with constant 1 cm spacings and the rungs of the irregular wheel were placed at distances varying unpredictably between 1 and 3 cm in 0.5-cm steps (1.68 ± 0.56 cm spacing, mean ± s.d.; Fig. 1a). Both on the regular and on the irregular wheel, mice sometimes chose to apply reaching distances that spanned more than two rungs, thereby surpassing the maximal rung distance of the respective wheel. On the other hand, mice also rarely grasped shorter than the minimal rung distance of each wheel (1 cm) when they placed the paw back on the previous instead of the next rung. A slightly higher mean rung distance in the irregular wheel (1.68 cm vs. 1 cm for the regular wheel) was necessary to ensure equivalent distributions of the actually applied reaching distance in both conditions since mice consistently chose shorter reaching distances on the irregular wheel when the mean rung distance was equal between conditions (e.g., 1 cm). With our rung settings, mice chose reaching distances that had equivalent means (2.26 ± 0.46 cm and 2.13 ± 0.64 cm for regular and irregular, respectively, means ± s.d.) and that occurred with comparable frequency within the same range (0.5–4 cm, mostly within 1–3 cm) on both wheels. Equivalent distributions of reaching distance with comparable means in both conditions were a prerequisite to compare a large pool of equal grasping actions on the regular and irregular wheel. In addition to the reaching distance, our rung settings entailed equivalent distributions of running speed, grasp duration, grasping speed as well as grasping acceleration (Fig. 1b, d). During movements of equal mass, the underlying forces are proportional to the movement accelerations when isometric conditions are disregarded. Since the mass of the animals and of the wheels remained unaltered between conditions, and since the acceleration distributions during paw reaching and retraction (Fig. 1d) are comparable between conditions, the applied forelimb forces were presumably similar on the regular and irregular wheel.

During locomotion on both wheels, mice continuously localized rungs before reaching actions through whisking. The task was designed so that at least the next rung is in reach of the whiskers. When the animal placement on the wheels held the distance of nose and ground between 1 and 1.8 cm, localization of the next rung through whisking was possible on the one hand, and significant medio-lateral forepaw movement could be avoided on the other hand. Using this placement, we additionally monitored forepaw movements from below to quantify motion in the horizontal plane and thereby medio-lateral forepaw movement in three animals. Forepaw movements were significantly smaller in the medio-lateral (m-l) compared with the posterior-anterior (p-a) direction on both the regular and irregular wheel (Supplementary Fig. 9; m-l 0.05 ± 0.01 cm vs. p-a 0.64 ± 0.06 cm for regular; m-l 0.06 ± 0.01 cm vs. p-a 0.65 ± 0.09 cm for irregular; mean ± s.d.; p < 0.01, paired t-test, n = 3). We therefore simplified recording of forelimb kinematics by monitoring the animals’ right side only, allowing us to track the major forepaw movement components in the posterior-anterior and dorso-ventral directions. When the distance of nose and ground was significantly lower than 1 cm, the forepaw more frequently deviated laterally during reaching actions which is why such placement was avoided in our experimental setting.

All mice were handled, habituated to head fixation, and at first trained to locomote on top of a 23-cm diameter wheel with a flat surface. Before each trial, a brake blocked the wheel and prevented the animals to initiate locomotion. After an auditory start cue (12- and 16-kHz tones for regular and irregular pattern, respectively, or vice versa) and release of the brake, animals had to initiate skilled locomotion and cover a predefined distance of 15–30 cm (in one animal only 10–15 cm per run) until an auditory stop cue (8-kHz tone) indicated the end of the trial (run). In successful trials, 2 s after the stop tone, the brake was re-activated and the animal received a reward of sweet water (2 µl). In unsuccessful trials, in which the mouse did not traverse the predefined distance within the given time period, the animal was punished with a time-out (~ 20 s) before the next run. After 1 week of training on the wheel with the flat surface animals were placed for the first time on the 23-cm diameter regular or irregular ladder wheel which were custom-built from two acrylic glass rings as well as carbon rungs and which emulated the rung ladder test for rodents3,42,73,43.

Due to the training week on the flat surface, all animals successfully ran the predefined distance according to the tone cues in more than 80% of the trials on the regular and irregular wheel. On each day, we then evaluated the first 10 successful runs on the regular and irregular wheel with the forelimb performance score3,42 which was then averaged across the 10 runs of each condition. Based on the forelimb performance score, the first 4 days were regarded as naive training phase, days 5–8 were regarded as learning training phase and days 9–12 were regarded as expert training phase in which the forelimb performance score reached a plateau (Supplementary Fig. 1). In the first subset of experiments (animals 1–5), calcium imaging was performed in one M1 L2/3 neuronal network per mouse (networks 1–5) during the expert phase. In the second subset of experiments (animals 6 and 7), calcium imaging was performed in two M1 L2/3 neuronal networks per mouse (networks 6–9) during the naive, learning and expert phase as well as additionally during the expert phase after M1-projecting M2-neurons were silenced by injection of CNO (expert M2-M1-silenced phase, corresponding to days 13–16, Supplementary Fig. 1). To allow the comparison between the expert M2-M1-silenced phase and the three training phases, mice 6 and 7 received vehicle injections during the expert, learning and naive training phases. For all nine neuronal networks, the sequence of the regular and irregular condition was randomized.

Limb motion tracking

To allow the analysis of forelimb kinematics during light stimulation and calcium imaging of M1, the skin overlying defined anatomical landmarks of the right forelimb was shaved and tattooed with a commercially available tattooing kit (Hugo Sachs Elektronik, Harvard Apparatus GmbH). On the forelimb, we marked the vertebral border of the scapula along with shoulder, wrist, metacarpophalangeal joint (MCP; also referred as finger-base joint throughout the manuscript), and the tip of the third digit. Limb kinematics during the optogenetic light stimulation was tracked at 30-Hz frame rate with a video camera (Logitech B910 HD) monitoring the right side of the animal. To allow tracking of limb kinematics during calcium imaging, the right side of the animals was illuminated with two 940-nm infrared LED light sources and recorded at 90-Hz frame rate (1280 × 640 pixels) using a high-speed CMOS camera (A504k; Basler). For analysis, time series of kinematic variables were downsampled to the imaging frame rate (18 Hz or 21.768 Hz) using cubic spline interpolation as implemented in MATLAB.

The markers on the skin of the forelimb were semi-automatically tracked offline, frame-by-frame using the ClickJoint 6.0 software (ALEA solutions GmbH), extracting two-dimensional coordinates (x for horizontal, y for vertical) for every marker and time point43. Based on these coordinates, the software modeled limb segments as rigid straight lines between markers and calculated the angles in each joint for consecutive frames. To minimize artifacts caused by skin stretching over the elbow joint, the position of the elbow was deduced from the shoulder and wrist coordinates as well as from the upper (~1.1 cm) and lower (~1.2 cm) forelimb length43. For subsequent analyses, we considered the angle changes in the shoulder, elbow, wrist, and MCP joints. Hand movement was quantified by the x- and y-coordinates of the MCP joint in each video frame and the reaching distance was calculated from x and y by Pythagorean addition.

Optogenetic motor mapping

Two weeks after window implantation mice were anesthetized with ketamine–xylazine (100 mg kg–1 ketamine, 10 mg kg–1 xylazine) for optogenetic motor mapping44. Mice were placed in a hammock with all four limbs dangling freely (Fig. 2a). Using a stereoscope with a motorized scanning system, a 473-nm laser beam was directed to 100 spots in mice 1–5 and 225 spots in mice 6 and 7 on the left motor cortex. The spots were arranged in a 10 × 10 grid in mice 1–5 and in a 15 × 15 grid in mice 6 and 7 (square area of 9.66 ± 0.55 mm2, n = 7 mice). Each of the 100 or 225 spots was hit in random order and stimulated for 500 ms at 100 Hz (pulse duration 4 ms; laser intensity <100 mW mm−2). In all animals, the beam diameter was adjusted to 130 µm at the level of the motor cortex in the window center by using a reference micrometer grid. During stimulation of M1, the right side of the animal was monitored with a camera for subsequent offline-analysis of forelimb kinematics. The angle changes in the shoulder, elbow, wrist and finger-base joint during light stimulation of M1 were quantified using ClickJoint 6.0 software (ALEA Solutions GmbH). Spatial maps of joint angle changes were spline interpolated to 145 × 145 pixels in mice 1–5 and 225 × 225 pixels in mice 6 and 7. Subsequently, the half-maximal (50%) contours were calculated in MATLAB. The stimulation spot, which caused the maximal combined absolute angle changes of shoulder retroversion, elbow flexion, wrist flexion and finger joint extension, was selected as forelimb focus region for calcium imaging. The forelimb movement that was evoked by optogenetic stimulation of this M1 forelimb focus, resembled the initial phase of a grasping action.

Two-photon calcium imaging

In the first experimental series, calcium imaging was performed with a custom-built two-photon microscope controlled by HelioScan74, equipped with a Ti:sapphire laser system (100-fs laser pulses; Mai Tai HP; Newport Spectra Physics), a water-immersion objective (16 × CFI75 LWD, NA 0.80, Nikon), galvanometric scan mirrors (Cambridge Technology), and a Pockel’s cell (Conoptics) for laser intensity modulation. In the second experimental series, a custom-built two-photon resonant-scanning microscope controlled by Scope (http://rkscope.sourceforge.net/)48 was used in conjunction with a Mai Tai HP DeepSee laser (Spectra-Physics). For calcium imaging, YC-Nano140 was excited at 820 nm to avoid simultaneous activation of ChR2 in dendrites of L5 neurons (see below). Fluorescence was collected in epi-collection mode with 480/60 nm (CFP) and 542/50 nm (YFP) emission filters using photomultiplier tubes. Image series were acquired at 18 Hz with 128 × 64 pixel resolution (galvanometric system) and at 21.768 Hz with 942 × 362 pixel resolution (resonance system).

Calcium imaging data from YFP and CFP channels were imported into MATLAB for subsequent processing steps. Lateral motion in both data channels were corrected with the TurboReg algorithm75. Individual neurons were selected manually from the mean image of each single-trial time series as regions of interest (ROIs). The background-subtracted mean pixel value of each ROI was extracted for both channels and applied to express neuronal calcium signals as relative YFP/CFP ratio change ΔR/R = (R − R0)/R0. Baseline ratio R0 was determined by calculating mean values in a 2-s long sliding window and then taking the minimum value (assuming a 2-s period exists, in which a particular neuron is silent). To yield an estimate of instantaneous spiking rate (SR), calcium signals were deconvolved using a Wiener filter algorithm assuming an exponential kernel as single-action potential evoked ΔR/R transient (amplitude 4.54%, decay time constant 0.673 s, onset time constant 0.186 s)46. The smoothness parameter was set to 0.01. As an alternative spiking inference method we also applied the peeling algorithm76, which did not change the results and main conclusions of our study.

To identify single neurons that displayed activity related to particular grasp types (Fig. 4b) we first calculated the mean SR traces across all grasps for each type (traces normalized in duration). A neuron was considered significantly responsive for a particular grasp type if the SR value of the mean trace, averaged over the entire grasp duration, surpassed the mean + 2 s.d. of the distribution of average SR values obtained from shuffled neuronal SR traces (corresponding to a chance level of 2.5%; 500 times shuffling of the grasp order). SR traces were also used for subsequent correlation and population coding analyses (see below).

Electrophysiology of L5 neurons

The use of transgenic ChR2 mice is advantageous for performing calcium imaging in motor cortex areas identified using optogenetic mapping, which does not require mechanical tissue perturbation with electrodes. To ensure that subsequent two-photon imaging in L2/3 did not affect the activity of ChR2-expressing L5 neurons through depolarization of their apical dendrites, we performed cell-attached recordings of L5 neurons in eight additional transgenic ChR2 mice (Fig. 2d, e). Blind juxtacellular voltage recordings were obtained from putative L5 neurons using glass pipettes (4–7 MΩ resistance) filled with control extracellular solution (in mM: 145 NaCl, 5.4 KCl, 10 HEPES, 1 MgCl2, and 1.8 CaCl2) and an Axoclamp 2B amplifier (Molecular Devices), preamplified, and digitized at 20 kHz with an ITC-18 board (InstruTECH) controlled by custom-written IGOR Pro software (WaveMetrics). Positive pressure (20–30 mbar) was applied while navigating the pipette in the tissue with a micromanipulator (Luigs & Neumann) to approach neurons. ChR2-expressing L5 neurons were identified by the pronounced spiking rate increases induced by blue (488 nm) laser light stimulation through an optical fiber placed a few millimeter above motor cortex (fiber output power about 11 mW). The effect of two-photon excitation was assessed by imaging in L2/3 above the recorded L5 neuron using the same experimental settings as in our calcium imaging experiments (near-infrared [NIR] light of 820-nm wavelength; illumination power <45 mW). Following two-photon imaging in L2/3, blue light stimulation of M1 was repeated to confirm that the neuron was still spiking. We analyzed the number of spikes evoked for the four conditions (“Blue light on”, “Blue light off”, “NIR light on”, “NIR light off”) with spikes detected with a threshold routine: Spikes were assigned to those time points when the voltage difference crossed a threshold of 7 s.d. above mean baseline. Statistical significance was tested by paired t-tests between each of the four conditions with post hoc adjustment of P-values according to Holm–Bonferroni (HB). Paired t-tests were applied after the Anderson-Darling test was used on the paired differences between each of the four conditions to test for normality.

Analysis of grasping actions

Classification of grasping actions into the three grasp types was performed using custom-written functions in MATLAB (Version 7, MathWorks). We first defined single grasp cycles based on local minima found in the horizontal x-component of the reaching distance vector. To distinguish full grasp cycles from corrective grasping actions that typically occurred in front of the reach (creating subcycles with secondary maxima), only minima with values lower than the mean of all local minima and maxima were accepted. The number of subcycles during each grasp cycle was then defined as the number of local maxima of the Savitzky-Golay-filtered reaching distance vector (taking the larger number for x- and y-component). In addition, the mean finger extension was computed for each grasp cycle. Based on these measures we classified each grasp cycle into one of three types according to the following criteria:

  • standard grasp: one subcycle, mean finger extension <170°

  • corrective grasp: two or more subcycles, mean finger extension <170°

  • digit-tip grasp: one or several subcycles, mean finger extension >170°

Grasp amplitude A for each joint was calculated as the difference between the maximum and minimum of angle position. The GGV for a particular joint angle (JA) and condition C (regular or irregular wheel) was defined as the mean absolute amplitude difference between each grasp and its preceding grasp per run, averaged across all runs:

$${\mathrm{GGV}}_{{\mathrm{JA}},{\mathrm{C}}} = \frac{1}{{N_{\mathrm{C}}}}\mathop {\sum }\limits_{i = 1}^{N_{\mathrm{C}}} \left( {\frac{1}{{n_i - {\mathrm{1}}}}\mathop {\sum }\limits_{j = 2}^{n_i} |A_{i,j} - A_{i,j - 1}|_{\mathrm{JA}}} \right)$$
(1)

Here, Ai,j denotes the amplitude of the j-th grasp of the i-th run, ni the number of grasping cycles during the i-th run, and NC the number of runs for condition C. A high GGV value indicates that the movement amplitude of this particular joint was frequently substantially changed from one grasp to the next. In contrast, low GGV values indicate rare and little grasp-to-grasp adjustments of motion amplitude. The GGV of a grasp sequence thus reflects the requirement for grasp-to-grasp adjustments (flexibility demands) set by the specific movement context (here regular or irregular rung pattern).

To quantify the similarity of pairs of grasping actions we first normalized (z-scored) all kinematic JA variables (S—shoulder, E—elbow, W—wrist, F—finger base, SSp—shoulder speed, ESp—elbow speed, WSp—Wrist speed, FSp—finger-base speed, SA—shoulder acceleration, EA—elbow acceleration, WA—wrist acceleration, FA—finger-base acceleration). The acceleration values were used to approximate the applied forces in each joint, which is proportional to the acceleration when equal mass is moved (both wheels had exactly the same mass and the weight of the animals did not vary during the experimental series). We then resampled kinematic traces for all individual grasps via interpolation to a fixed number of sample points (Nfix = 160) in order to align all grasps with a normalized duration. As distance measure we calculated the sum of sample-point-wise Euclidean distance d for pairs of 12-dimensional resampled vectors p and q:

$${\!}d\left( {{\mathbf{p}},{\mathbf{q}}} \right) = \mathop {\sum }\limits_{i = 1}^{N_{{\mathrm{fix}}}} \left( {\begin{array}{*{20}{c}} {\left( {p_{{\mathrm{S}},{\mathrm{i}}} - q_{{\mathrm{S}},{\mathrm{i}}}} \right)^2 + \left( {p_{{\mathrm{E}},{\mathrm{i}}} - q_{{\mathrm{E}},{\mathrm{i}}}} \right)^2 + \left( {p_{{\mathrm{W}},{\mathrm{i}}} - q_{{\mathrm{W}},{\mathrm{i}}}} \right)^2 + \left( {p_{{\mathrm{F}},{\mathrm{i}}} - q_{{\mathrm{F}},{\mathrm{i}}}} \right)^2} \\ { {\!\!\!\!}+ \left( {p_{{\mathrm{SSp}},{\mathrm{i}}} - q_{{\mathrm{SSp}},{\mathrm{i}}}} \right)^2 + \left( {p_{{\mathrm{ESp}},{\mathrm{i}}} - q_{{\mathrm{ESp}},{\mathrm{i}}}} \right)^2 + \left( {p_{{\mathrm{WSp}},{\mathrm{i}}} - q_{{\mathrm{WSp}},{\mathrm{i}}}} \right)^2 + \left( {p_{{\mathrm{FSp}},{\mathrm{i}}} - q_{{\mathrm{FSp}},{\mathrm{i}}}} \right)^2} \\ { + \left( {p_{{\mathrm{SA}},{\mathrm{i}}} - q_{{\mathrm{SA}},{\mathrm{i}}}} \right)^2\left( {p_{{\mathrm{EA}},{\mathrm{i}}} - q_{{\mathrm{EA}},{\mathrm{i}}}} \right)^2 + \left( {p_{{\mathrm{WA}},{\mathrm{i}}} - q_{{\mathrm{WA}},{\mathrm{i}}}} \right)^2 + \left( {p_{{\mathrm{FA}},{\mathrm{i}}} - q_{{\mathrm{FA}},{\mathrm{i}}}} \right)^2} \end{array}} {\!\!\!}\right)^{1/2}$$
(2)

where i runs over all sample points. A problem with this definition is that two discrete grasping vectors featuring nearly the same time course of coordinated kinematic changes, but slightly temporally shifted would yield an artificially high Euclidean distance. To reduce this problem, we used dynamic time warping77 to allow temporal warps, coupled for all joint angles, speeds and accelerations, over a restricted time window. Maximally allowed time warps were 33% of the grasp duration, i.e., 53 sample points. Optimal time warping for each p and q vector pair was found by minimizing d(p,q). Finally, to bound similarity values between 1 (maximum similarity) and 0 (maximum dissimilarity) the similarity value S was calculated as

$$S = 1 - \frac{{d\left( {{\mathbf{p}},{\mathbf{q}}} \right)}}{{{\mathrm{max}}\left( {d\left( {{\mathbf{p}},{\mathbf{q}}} \right)} \right)}}$$
(3)

Grasp-similarity matrices were clustered according to the grasp-type classification and within each cluster sub-sorted according to similarity values with respect to the mean grasp for the respective grasp type.

Motion prediction using the random forest algorithm

We used the random forest algorithm (RFA)78 to predict limb motion, i.e., kinematics of shoulder, elbow, wrist and finger-base joints, and grasp types from the activity of either single neurons or neuronal populations. The RFA is a multivariate, non-parametric machine learning algorithm and utilizes bootstrap aggregation of regression trees. We adopted the Treebagger function implemented in MATLAB and specified 100 trees and default settings for minimum leaf size and number of variables to select at random for each decision split. These parameters were an appropriate trade-off between computation time and decoding accuracy. For the prediction of joint angle kinematics a regression RFA was used.

After concatenation of normalized (z-scored) joint angle traces of all grasps for one condition (regular or irregular wheel), the algorithm was trained to predict the real joint angle changes from the instantaneous SR traces of one (single neuron prediction) or all neurons of the recorded network (population coding) on a randomly selected subset of grasps, comprising 70% of the dataset (training set). For cross-validation, the trained algorithm was then evaluated on the remaining 30% of the dataset left out during training (test set). To quantify the predictive power, we computed the Pearson’s correlation coefficient (PCC) between joint angle changes predicted by RFA in the test set and the corresponding real joint angles. We repeated this procedure 500 times, thereby obtaining a distribution of 500 true predictions, which came from 500 randomly selected test sets of one true dataset. As shuffled control, the true assignment of calcium traces to motor output parameters in each trial was randomly shuffled between all trials for 500 times. For each shuffling, training (70%) and corresponding test set (30%) were randomly selected and the predictive power was quantified as for the true dataset. This process generated a second distribution of 500 shuffled predictions that came from 1 randomly selected test set of 500 shuffled data sets. This procedure allowed us to compare the lowest predictive power from all test sets in the true dataset to the highest predictive power from different test sets in different shufflings (high predictive power in the shuffled distribution could arise due to the random data-shuffling itself and additionally due to the random selection of a certain test set with high predictive power; therefore, comparison of these shuffled and true distributions is far stricter than for example comparing the mean prediction of 500 shufflings with the mean prediction of the true dataset). We then applied a ROC-analysis on the shuffled and true distribution and calculated the area under the ROC-curve (ROC-AUC). According to Hosmer and Lemeshow79, ROC = 0.5 indicates no discrimination, 0.7 ≤ ROC-AUC < 0.8 indicates acceptable discrimination, 0.8 ≤ ROC-AUC < 0.9 indicates excellent discrimination and ROC-AUC ≥ 0.9 indicates outstanding discrimination. We regarded the predictive power of a single neuronal network for a particular joint angle as significant if the AUC was ≥0.9. Since the analysis was more frequently applied in the single-cell analysis (486 neurons), the predictive power of a single cell for a particular joint angle was considered significant if the AUC was ≥0.95. The ROC-analysis does not require that the two distributions are normally distributed. Even though each neuron predicted a particular joint to the highest degree, a considerable amount of predictive power was sometimes also observed for further joints (Supplementary Fig. 10). If a single neuron displayed significant prediction for more than one joint, the identity of a neuron (e.g., finger-predictive cell) was determined by the joint that was predicted to the highest degree.

As single, summarizing value for the predictive power in the true and shuffled distributions, we defined the mean PCC. To test the significance of differences in predictive power between the regular and irregular condition with regard to all neuronal networks, paired t-test or Wilcoxon signed rank test with post hoc adjustment of P-values according to Holm–Bonferroni was used for the mean prediction values of the 9 neuronal networks (Regular vs. Irregular). The decision if a paired t-test or a Wilcoxon signed rank test was applied depended on the result of a previous Anderson-Darling test that tested normality of the paired differences between the nine neuronal networks. In the regular and irregular condition, neuronal population RFA prediction was applied for the whole dataset (Fig. 5c) as well as after removal of standard, corrective and digit-tip grasps (Fig. 5e, f). In this analysis, we randomly selected an equal number of grasps (corresponding to the number of grasps in the smallest cluster in each network and condition) from the standard, corrective and digit-tip grasp cluster to avoid training bias of the random forest algorithm. We then computed the population coding as described above for the combined grasps from the three clusters as well as after removal of one grasp type. The change in predictive power (ΔPCC) was then calculated by subtracting the predictive power of the pool with all three grasp types from the predictive power of the respective pools with two grasp types. To compare the underlying encoding rules between the regular and irregular condition, we also trained the RFA on a given neuronal network in one condition (e.g., regular) and used this training set to predict the motor output parameters in the other condition (e.g., irregular).

Relationship of encoding with grasp-to-grasp variability

We investigated whether between-condition-differences in prediction of individual joint angles relate to between-condition-differences of their GGV. We first calculated for each neuronal network and joint angle the differences in prediction (PCC) and GGV (irregular minus regular), yielding four ΔPCC and four ΔGGV values per neuronal network (one for each joint). We then z-scored the four values for ΔPCC and ΔGGV for each animal. Then, we calculated a linear regression with clustered standard error (regression in Stata, type clustered robust, animals as cluster variable) of ΔPCC differences versus ΔGGV differences pooled for all mice (Figs. 5d and 6e). A significant positive linear relationship here indicates that between-condition increases in GGV of individual joint angles are accompanied by between-condition enhancements of their encoding in M1 L2/3 neuronal networks and vice versa. For example, GGV for the shoulder angle increased from the regular to the irregular condition, as did its encoding in neuronal networks of M1 L2/3. Importantly, linear regression with clustered standard error regards the regression separately for each neuronal network and has therefore stricter requirements for significance than a simple linear regression with values from all neuronal networks pooled.

Twin grasp analysis

Similarity of grasp pairs across the two conditions (regular and irregular) was quantified as described above using Euclidean distance of normalized vectors in the 12-dimensional space (four joint angles, four joint angle speeds, four joint angle accelerations, Eqs. (2) and (3)). Starting with the standard grasp cluster, we first selected the most similar grasp pair across conditions. The respective standard grasps were then no longer available for further selections. From the remainder of grasps in the standard grasp clusters, we again selected the most similar pair and repeated this procedure until all grasps in the standard grasp cluster of one condition were consumed. The same procedure for twin grasp selection was performed on the corrective and digit-tip grasp clusters. After this selection procedure, each twin cluster in the regular condition featured the same number of grasps as its counterpart in the irregular condition. The mean deviation of all joint angles in twin grasp pairs over the grasp duration was 14.03° ± 1.37° across animals (mean ± s.d., see Supplementary Fig. 7a for values with regard to individual neuronal networks and individual joints). In the twin grasp analysis, the grasp-to-grasp variability of each joint angle was calculated as before for all grasps in the regular or irregular condition (not only for twin grasps) because we regarded this measure as a general feature of the entire movement sequence and its environmental context.

Investigation of potentially confounding variables

We applied additional analyses to check potential explanations for the observed encoding differences between the regular and irregular condition. We did not find any relationship between the minor kinematic joint dissimilarities within twin grasp pools and the observed encoding differences (Supplementary Fig. 7b, r2 = 0.007, P = 0.69, r2 = 0.012, P = 0.66, r2 = 0.02, P = 0.548; differences in joint angle, joint angle speed and joint angle acceleration as proxy for applied forces versus encoding differences, respectively). Since the irregular condition is the more difficult task, we also asked to what extent a single, non-joint-specific parameter such as the general difficulty of the task could have generated the observed encoding differences between the regular and irregular condition, for example by affecting the attention of the animals. We modeled encoding of a task more difficult than the regular wheel by multiplying the baseline encoding on the regular wheel with an arbitrary positive factor c in the range 0 < c < 10. This modeling approach assumes that a single, non-joint-specific parameter such as the general difficulty of a task would influence encoding similarly across joints and lead to similar relative encoding differences across joints. However, the resulting encoding differences explained almost none of the variance of the actual encoding differences, both for all grasps (r2 = 0.01, P = 0.68) and for twin grasps only (r2 = 0.07, P = 0.2616, Supplementary Fig. 7c). Note that this regression analysis is not dependent on the amount of the relative encoding increase from regular to irregular, the arbitrary constant c can have any value in the applied range 0 < c < 10. Thus, encoding differences across joints are non-uniform and therefore not well explained by a single parameter such as the general difficulty. In contrast, encoding differences across joints are well explained by the joint-specific parameter grasp-to-grasp variability which characterizes the flexibility demands in the current environmental context.

Statistics

All statistical analyses were computed in MATLAB R2017a and Stata 14. Data are shown as individual data points for each observational unit except in Fig. 7b–e in which we plotted data as mean ± s.d. to visualize trends during the different phases more distinctly. To compare two data sets (e.g., regular vs. irregular), paired t-tests (two-tailed) or Wilcoxon signed rank tests were used. The decision if a paired t-test or a Wilcoxon signed rank test was applied depended on the result of a previous Anderson-Darling test that tested normality of the paired differences between the neuronal networks or other investigated parameters. Linear regression with clustered standard error was calculated in Stata 14 (linear regression, type clustered robust). Linear regression with clustered standard error is sensitive to the regression in each observational unit (e.g., values of the respective neuronal network) and therefore stricter than a simple linear regression of values that have been pooled across neuronal networks.

For all statistical tests, the post hoc Holm–Bonferroni method for multiple comparisons was applied by adjusting the P-value correspondingly, and the respective exact P-value is given in the Results section. A significant difference between two data sets was assumed when the Holm–Bonferroni-corrected P-value was below 0.05 (indicated by one asterisk in figures).

Reporting summary

Further information on research design is available in the Nature Research Reporting Summary linked to this article.