Objective. Respiration negatively affects the outcome of a radiation therapy treatment, with potentially severe effects especially in particle therapy (PT). If compensation strategies are not applied, accuracy cannot be achieved. To support the clinical practice based on 4D computed tomography (CT), 4D magnetic resonance imaging (MRI) acquisitions can be exploited. The purpose of this study was to validate a method for virtual 4DCT generation from 4DMRI data for lung cancers on a porcine lung phantom, and to apply it to lung cancer patients in PT. Approach. Deformable image registration was used to register each respiratory phase of the 4DMRI to a reference phase. Then, a static 3DCT was registered to this reference MR image set, and the virtual 4DCT was generated by warping the registered CT according to previously obtained deformation fields. The method was validated on a physical phantom for which a ground truth 4DCT was available and tested on lung tumor patients, treated with gated PT at end-exhale, by comparing the virtual 4DCT with a re-evaluation 4DCT. The geometric and dosimetric evaluation was performed for both proton and carbon ion treatment plans. Main results. The phantom validation exhibited a geometrical accuracy within the maximum resolution of the MRI and mean dose deviations, with respect to the prescription dose, up to 3.2% for target D 95%, with a mean gamma pass rate of 98%. For patients, the virtual and re-evaluation 4DCTs showed good correspondence, with errors on target D 95% up to 2% within the gating window. For one patient, dose variations up to 10% at end-exhale were observed due to relevant inter-fraction anatomo-pathological changes that occurred between the planning and re-evaluation CTs. Significance. Results obtained on phantom data showed that the virtual 4DCT method was accurate, allowing its application on patient data for testing within a clinical scenario.