seq_length=samfile.lengths[samfile.references.index(seq)]#Permet d'obtenir la longueur de la sequence
forpos_startinrange(0,seq_length-window_size+1,step_size):#Itère sur la sequence par "fenêtre" en fonction de la taille
pos_end=pos_start+window_size#pos_start + window_size donne la position de début de la fenêtre, puis grâce à min() et seq_length je peux définir position de fin. le min me permet d'être certain que pos_end < seq_lenght. C-a-d que si la fenêtre dépasse la taille de la sequence, j'aurais quand même pos_end
window_depths=[]#liste pour stocker la profondeur de chaque fenêtre
forpileupcolumninsamfile.pileup(seq,start=pos_start,stop=pos_end):#itère sur chaque colone de pileup
window_depths.append(pileupcolumn.n)#ajoute le nb de lecture couvrant cette position à la liste window_depths
ifwindow_depths:#calcul la profondeur moyenne pour chaque sequence
sys.stderr.write("\t entering calcul_depth_seq : thread numero %s\n"%numero_de_thread)
samfile=pysam.AlignmentFile(bamfile,"rb")
seq_length=samfile.lengths[samfile.references.index(seq)]#Permet d'obtenir la longueur de la sequence
forpos_startinrange(0,seq_length-window_size+1,step_size):#Itère sur la sequence par "fenêtre" en fonction de la taille
pos_end=pos_start+window_size#pos_start + window_size donne la position de début de la fenêtre, puis grâce à min() et seq_length je peux définir position de fin. le min me permet d'être certain que pos_end < seq_lenght. C-a-d que si la fenêtre dépasse la taille de la sequence, j'aurais quand même pos_end
window_depths=[]#liste pour stocker la profondeur de chaque fenêtre
forpileupcolumninsamfile.pileup(seq,start=pos_start,stop=pos_end):#itère sur chaque colone de pileup
window_depths.append(pileupcolumn.n)#ajoute le nb de lecture couvrant cette position à la liste window_depths
ifwindow_depths:#calcul la profondeur moyenne pour chaque sequence
sys.stderr.write("\t\t entering wd thread numero %s\n"%numero_de_thread)
// Kernel pour calculer la mappabilite moyenne ponderee
__global__ void calcul_map_kernel(float *map_data, int seq_length, int window_size, int step_size, float *map_results) {
int idx = threadIdx.x + blockIdx.x * blockDim.x;
if (idx < seq_length - window_size + 1) {
int pos_start = (idx * step_size) + 1;
int pos_end = pos_start + window_size;
float weighted_sum = 0.0;
float total_weight = 0.0;
for (int i = pos_start; i <= pos_end; ++i) {
float weight = map_data[i];
weighted_sum += weight;
total_weight += 1;
}
float avg_map = weighted_sum / total_weight;
map_results[idx] = avg_map;
}
}
// Kernel pour calculer la lecture de profondeur corrigee par la mappabilitee
__global__ void calcul_depth_correction_kernel(float *depth_results, float *map_results, int seq_length, int window_size, int step_size, float *depth_correction_results) {
int idx = threadIdx.x + blockIdx.x * blockDim.x;
if (idx < seq_length - window_size + 1) {
float avg_depth = depth_results[idx];
float avg_map = map_results[idx];
// Verification si avg_map est egal a 0 pour eviter la division par 0
__global__ void normalize_depth_kernel(float *depth_correction, float *gc_results, float m, float *gc_to_median, int seq_length, int window_size, int step_size, float *depth_normalize) {
int idx = threadIdx.x + blockIdx.x * blockDim.x;
if (idx < seq_length - window_size + 1) {
float mGC = gc_to_median[(int)gc_results[idx]];
// Verification si mGC est egal a 0 pour eviter la division par 0
__global__ void ratio_par_window_kernel(float *depth_normalize_val, float mean_chr, int seq_length, int window_size, int step_size, float *ratio_par_window_results) {
int idx = threadIdx.x + blockIdx.x * blockDim.x;
if (idx < seq_length - window_size + 1) {
float ratio = depth_normalize_val[idx] / mean_chr;
ratio_par_window_results[idx] = ratio;
}
}
// Kernel pour calculer le z_score par window sur le ratio
__global__ void z_score_kernel(float *ratio, float mean_ratio, float std_ratio, int seq_length, int window_size, int step_size, float *z_score_results) {
// Kernel pour calculer le ratio divise par le ratio moyen
__global__ void ratio_par_mean_ratio_kernel(float *ratio, float mean_ratio, int seq_length, int window_size, int step_size, float *ratio_par_mean_ratio_results) {
Compute the mean, standard deviation, and median of non-zero ratio results per window.
This function filters out zero and -1 values from the ratio results per window and computes the mean, standard deviation, and median of the remaining values. Values greater than or equal to 5 are capped at 5.
Parameters
----------
ratio_par_window_results : list or numpy.ndarray
A list or array of ratio values per window.
Returns
-------
tuple
A tuple containing the mean, standard deviation, and median of the filtered ratio values.
"""
logging.info(f"Entering compute_mean_std_med for {chr}")
Create a signal dictionary for a specific chromosome based on z-score results.
This function populates a signal dictionary with positions and corresponding z-score results for a given chromosome.
Parameters
----------
signal : dict
A dictionary to store the signal data.
chr : str
The chromosome for which the signal is created.
z_score_results : list or numpy.ndarray
A list or array of z-score results.
step_size : int
The step size used to calculate the positions.
Returns
-------
None
The function modifies the signal dictionary in place.
"""
logging.info(f"Entering create_signal for {chr} (include copy_number_level)")
start_time=time.time()
ifchrnotinsignal:
signal[chr]={}
foriinrange(len(z_score_results)):
pos=(i*step_size)+1
signal[chr][pos]=z_score_results[i]
#sys.stderr.write("\t signal %s\n" % signal[chr])
end_time=time.time()
elapsed_time=end_time-start_time
logging.info(f"Leaving create_signal for {chr} (include copy_number_level) (Time taken: {elapsed_time:.4f} seconds)")
defdetect_events(
z_score_results,
zscore_threshold,
events,
med_ratio,
ratio_par_mean_ratio_results,
chr,
):
"""
Detect genomic events based on z-score results and a z-score threshold.
This function identifies significant genomic events where z-scores exceed the given threshold. Events are recorded in the `events` dictionary for the specified chromosome.
Parameters
----------
z_score_results : list or numpy.ndarray
A list or array of z-score values.
zscore_threshold : float
The threshold for detecting significant z-score events.
events : dict
A dictionary to store detected events.
med_ratio : float
The median ratio used for copy number level calculations.
ratio_par_mean_ratio_results : list or numpy.ndarray
A list or array of ratio values compared to the mean ratio.
chr : str
The chromosome for which events are detected.
Returns
-------
None
The function modifies the events dictionary in place.
Generate a VCF file containing structural variant calls based on segmented regions and signal data.
This function creates a VCF (Variant Call Format) file containing structural variant calls derived from segmented regions and signal data. The structural variant type (DEL for deletion or DUP for duplication) is determined based on copy number levels and signal values. The resulting VCF file includes information about the chromosome, position, type of structural variant, copy number, and other relevant information.
Parameters
----------
sample : str
The sample name to be included in the VCF file header.
segment : dict
A dictionary containing segmented regions with copy number information.
signal : dict
A dictionary containing signal data for each chromosome.
lengthFilter : int
The minimum length threshold for including variants in the VCF file.
output_file : str
The path to the output VCF file.
Returns
-------
None
This function writes the structural variant calls to the specified output file in VCF format.
"""
globalheader_written
logging.info(f"Entering display_results_vcf for {chr}")
start_time=time.time()
withopen(output_file,"a")asf:
ifnotheader_written:
f.write("##fileformat=VCFv4.2\n")
f.write("##source=cnvcaller\n")
f.write(
'##INFO=<ID=SVTYPE,Number=1,Type=String,Description="Type of structural variant">\n'
)
f.write(
'##INFO=<ID=SVLEN,Number=.,Type=Integer,Description="Difference in length between REF and ALT alleles">\n'
)
f.write(
'##INFO=<ID=END,Number=1,Type=Integer,Description="End position of the variant described in this record">\n'