<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Iterative vs fragment-based mapping
Iterative mapping first proposed by <a name="ref-1"/>(Imakaev et al., 2012), allows to map usually a high number of reads. However other methodologies, less "brute-force" can be used to take into account the chimeric nature of the Hi-C reads.
A simple alternative is to allow split mapping.
Another way consists in pre-truncating <a name="ref-1"/>(Ay and Noble, 2015) reads that contain a ligation site and map only the longest part of the read <a name="ref-2"/>(Wingett et al., 2015).
Finally, an intermediate approach, fragment-based, consists in mapping full length reads first, and than splitting unmapped reads at the ligation sites <a name="ref-1"/>(Serra et al. 2017).
Advantages of iterative mapping
It's the only solution when no restriction enzyme has been used (i.e. micro-C)
Can be faster when few windows (2 or 3) are used
Advantages of fragment-based mapping
Generally faster
Safer
Step1:  The full mapping function can be used to perform either iterative or fragment-based mapping, or a combination of both.
It is important to note that although the default mapping parameters used by TADbit are relatively strict, a non negligible proportion of the reads will be mis-mapped, and this applies at each iteration of the mapping.
Iterative mapping
Here an example of use as iterative mapping
Step2:  Note
Step3:  And for the second side of the read-end
Step4:  Fragment-based mapping
The fragment-based mapping strategy works in 2 steps
<ASSISTANT_TASK:>
Python Code:
from pytadbit.mapping.full_mapper import full_mapping
Explanation: Iterative vs fragment-based mapping
Iterative mapping first proposed by <a name="ref-1"/>(Imakaev et al., 2012), allows to map usually a high number of reads. However other methodologies, less "brute-force" can be used to take into account the chimeric nature of the Hi-C reads.
A simple alternative is to allow split mapping.
Another way consists in pre-truncating <a name="ref-1"/>(Ay and Noble, 2015) reads that contain a ligation site and map only the longest part of the read <a name="ref-2"/>(Wingett et al., 2015).
Finally, an intermediate approach, fragment-based, consists in mapping full length reads first, and than splitting unmapped reads at the ligation sites <a name="ref-1"/>(Serra et al. 2017).
Advantages of iterative mapping
It's the only solution when no restriction enzyme has been used (i.e. micro-C)
Can be faster when few windows (2 or 3) are used
Advantages of fragment-based mapping
Generally faster
Safer: mapped reads are generally larger than 25-30 nm (the largest window used in iterative mapping). Less reads are mapped, but the difference is usually canceled or reversed when looking for "valid-pairs".
Note: We use GEM2 <a name="ref-1"/>(Marco-Sola et al. 2012), performance are very similar to Bowtie2, and in some cases slightly better. 
For now TADbit is only compatible with GEM2.
Mapping
End of explanation
cell = 'mouse_B'  # or mouse_PSC
rep = 'rep1'  # or rep2
Explanation: The full mapping function can be used to perform either iterative or fragment-based mapping, or a combination of both.
It is important to note that although the default mapping parameters used by TADbit are relatively strict, a non negligible proportion of the reads will be mis-mapped, and this applies at each iteration of the mapping.
Iterative mapping
Here an example of use as iterative mapping:
(Estimated time 15h with 8 cores)
End of explanation
! mkdir -p results/iterativ/$cell\_$rep
! mkdir -p results/iterativ/$cell\_$rep/01_mapping
# for the first side of the reads
full_mapping(mapper_index_path='genome/Mus_musculus-GRCm38.p6/Mus_musculus-GRCm38.p6_contigs.gem',
             out_map_dir='results/iterativ/{0}_{1}/01_mapping/mapped_{0}_{1}_r1/'.format(cell, rep),
             fastq_path='FASTQs/%s_%s_1.fastq.dsrc' % (cell,rep),
             frag_map=False,  clean=True, nthreads=8,
             windows=((1,25),(1,35),(1,45),(1,55),(1,65),(1,75)),
             temp_dir='results/iterativ/{0}_{1}/01_mapping/mapped_{0}_{1}_r1_tmp/'.format(cell, rep))
Explanation: Note: the execution of this notebook should be repeated for each of the 4 replicates
End of explanation
# for the second side of the reads
full_mapping(mapper_index_path='genome/Mus_musculus-GRCm38.p6/Mus_musculus-GRCm38.p6_contigs.gem',
             out_map_dir='results/iterativ/{0}_{1}/01_mapping/mapped_{0}_{1}_r2/'.format(cell, rep),
             fastq_path='FASTQs/%s_%s_2.fastq.dsrc' % (cell,rep),
             frag_map=False,  clean=True, nthreads=8,
             windows=((1,25),(1,35),(1,45),(1,55),(1,65),(1,75)), 
             temp_dir='results/iterativ/{0}_{1}/01_mapping/mapped_{0}_{1}_r2_tmp/'.format(cell, rep))
Explanation: And for the second side of the read-end:
End of explanation
! mkdir -p results/fragment/$cell\_$rep
! mkdir -p results/fragment/$cell\_$rep/01_mapping
# for the first side of the reads 
full_mapping(mapper_index_path='genome/Mus_musculus-GRCm38.p6/Mus_musculus-GRCm38.p6_contigs.gem',
             out_map_dir='results/fragment/{0}_{1}/01_mapping/mapped_{0}_{1}_r1/'.format(cell, rep),
             fastq_path='FASTQs/%s_%s_1.fastq.dsrc' % (cell, rep),
             r_enz='MboI', frag_map=True, clean=True, nthreads=8, 
             temp_dir='results/fragment/{0}_{1}/01_mapping/mapped_{0}_{1}_r1_tmp/'.format(cell, rep))
# for the second side of the reads
full_mapping(mapper_index_path='genome/Mus_musculus-GRCm38.p6/Mus_musculus-GRCm38.p6_contigs.gem',
             out_map_dir='results/fragment/{0}_{1}/01_mapping/mapped_{0}_{1}_r2/'.format(cell, rep),
             fastq_path='FASTQs/%s_%s_2.fastq.dsrc' % (cell, rep),
             r_enz='MboI', frag_map=True, clean=True, nthreads=8, 
             temp_dir='results/fragment/{0}_{1}/01_mapping/mapped_{0}_{1}_r2_tmp/'.format(cell, rep))
Explanation: Fragment-based mapping
The fragment-based mapping strategy works in 2 steps: 
1. The read-ends are mapped entirely, assuming that no ligation occurred in them. 
2. For unmapped read-ends, the function searches for a ligation site (e.g. in the case of MboI this would correspond to GATCGATC and in the case of HindIII to AAGCTAGCTT). The read-end is split accordingly replacing the ligation site by two RE sites:
read-end-part-one---AAGCTAGCTT----read-end-part-two
     will be split in:
                        read-end-part-one---AAGCTT
     and:
                                                AAGCTT----read-end-part-two
Note: __if no ligation site is found__, step two will be repeated using digested RE site as split point (AAGCT in the case of HindIII). This is done in order to be protected against sequencing errors. When this path is followed the digested RE site is removed, but not replaced. If digested RE sites are not found either, the read will be classified as unmapped.
Note: __both mapping strategies can be combined__, for example defining the windows as previously (iterative mapping), but also give a RE name r_enz=MboI and setting frag_map=True like this if a read has not been mapped in any window, TADbit will also try to apply the fragment-based strategy.
End of explanation
<END_TASK>