nanomd 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
nanomd/__init__.py ADDED
File without changes
nanomd/main.py ADDED
@@ -0,0 +1,24 @@
1
+ import typer
2
+ from .modules.gene import gene
3
+ from .modules.isoform import isoform
4
+ from .modules.detectMod import detectMod
5
+ from .modules.isoformAS import isoformAS
6
+ from .modules.nascentRNA import nascentRNA
7
+
8
+ app = typer.Typer(add_completion=False)
9
+
10
+ @app.callback()
11
+ def callback():
12
+ """
13
+ nanoMD(Nanopore direct RNA sequencing Multi-dimensional analysis) was developed to synchronously analyze the changes in m6A, m5C, psi, AtoI modification sites, genes, isoforms, alternative splicing events, and nascent RNA in direct RNA sequencing data.
14
+ """
15
+
16
+ app.command(name="gene")(gene)
17
+ app.command(name="isoform")(isoform)
18
+ app.command(name="isoformAS")(isoformAS)
19
+ app.command(name="detectMod")(detectMod)
20
+ app.command(name="nascentRNA")(nascentRNA)
21
+
22
+
23
+ if __name__ == "__main__":
24
+ app()
File without changes
@@ -0,0 +1,58 @@
1
+ import time
2
+ import typer
3
+ from typing_extensions import Annotated
4
+ from rich.progress import Progress, SpinnerColumn, TextColumn
5
+ from ..utils.modtools import split_mod
6
+ from ..utils.basetools import check_path_exists
7
+ from ..utils.abs_position import gene_feature_distance_calculator
8
+ from ..utils.modifications import form_reads_get_modifications
9
+
10
+ app = typer.Typer()
11
+
12
+ @app.command()
13
+ def detectMod(
14
+ input: Annotated[str, typer.Option("--input", "-i", help="Input fastq files.")],
15
+ sam: Annotated[str, typer.Option("--sam", "-s", help="mapping sam/bam file.")],
16
+ bed: Annotated[str, typer.Option("--bed", "-b", help="bed file for transcripts sites.")],
17
+ regions: Annotated[str, typer.Option("--regions", "-r", help="regions file for gene feature distance calculation.")],
18
+ output: Annotated[str, typer.Option("--output", "-o", help="Output file path.")]=".",
19
+ prefix: Annotated[str, typer.Option("--prefix", "-p", help="Prefix for output files.")]="prefix",
20
+ pvalue: Annotated[float, typer.Option("--pvalue", help="pvalue cutoff for modification sites.")]=0.98,
21
+ ):
22
+ """
23
+ detect modification sites in input fastq files.
24
+ """
25
+
26
+ with Progress(
27
+ SpinnerColumn(),
28
+ TextColumn("[progress.description]{task.description}"),
29
+ transient=True,
30
+ ) as progress:
31
+ progress.add_task(description="Detecting modification start...", total=None)
32
+ start=time.time()
33
+
34
+ output_file=f"{output}/{prefix}.bed"
35
+ progress.add_task(description="Getting modification from fq files...", total=None)
36
+ if not check_path_exists(output_file):
37
+ mod = form_reads_get_modifications(input, sam, bed, output_file, pvalue)
38
+ mod.get_mod_position_with_sam()
39
+ progress.add_task(description=f"Getting modification from fq files Done", total=None)
40
+
41
+ progress.add_task(description="Splitting modification sites...", total=None)
42
+ if not check_path_exists(f"{output}/{prefix}_m6A.bed"):
43
+ split_mod(output_file, prefix)
44
+ progress.add_task(description="Splitting modification sites Done", total=None)
45
+
46
+ progress.add_task(description="Calculating absolute distance...", total=None)
47
+ bed_file_list = [f"{output}/{prefix}_m6A.bed", f"{output}/{prefix}_m5C.bed", f"{output}/{prefix}_psi.bed", f"{output}/{prefix}_AtoI.bed"]
48
+ for bed_file in bed_file_list:
49
+ gfc_output = bed_file.replace(".bed", "_abs_dist.txt")
50
+ if not check_path_exists(gfc_output):
51
+ gfc = gene_feature_distance_calculator(bed_file, regions, gfc_output)
52
+ gfc.process_bed_file()
53
+ progress.add_task(description="Calculating absolute distance Done", total=None)
54
+
55
+ end=time.time()
56
+ time_cost=f"{(end - start) // 3600}h{((end - start) % 3600) // 60}m{(end - start) % 60:.2f}s"
57
+ print(f"Detecting modification sites Done, time cost: {time_cost}")
58
+ progress.add_task(description=f"Detecting modification sites Done, time cost: {time_cost}", total=None)
nanomd/modules/gene.py ADDED
@@ -0,0 +1,39 @@
1
+ import time
2
+ import typer
3
+ from typing_extensions import Annotated
4
+ from rich.progress import Progress, SpinnerColumn, TextColumn
5
+ from ..utils.map import minimap2map
6
+
7
+ app = typer.Typer()
8
+
9
+ @app.command()
10
+ def gene(
11
+ input: Annotated[str, typer.Option("--input", "-i", help="Input fastq files.")],
12
+ reference: Annotated[str, typer.Option("--reference", "-r", help="reference genome path.")],
13
+ output: Annotated[str, typer.Option("--output", "-o", help="output for output sam/bam files.")],
14
+ tool: Annotated[str, typer.Option("--tool", help="minimap2.")]="minimap2",
15
+ parms: Annotated[str, typer.Option("--parms", help="minimap2 parameters for mapping.")]="--secondary=no --cs -a",
16
+ threads: Annotated[int, typer.Option("--threads", "-t", help="Number of threads.")]=4,
17
+ ):
18
+ """
19
+ Mapping of nanopore reads to a reference genome.
20
+ """
21
+
22
+ with Progress(
23
+ SpinnerColumn(),
24
+ TextColumn("[progress.description]{task.description}"),
25
+ transient=True,
26
+ ) as progress:
27
+ try:
28
+ progress.add_task(description="map reference...", total=None)
29
+ start=time.time()
30
+ minimap2map(input, reference, output, tool, parms, threads)
31
+ end=time.time()
32
+ time_cost=f"{(end - start) // 3600}h{((end - start) % 3600) // 60}m{(end - start) % 60:.2f}s"
33
+ print(f"map reference Done, time cost: {time_cost}")
34
+ progress.add_task(description=f"map reference Done, time cost: {time_cost}", total=None)
35
+ except Exception as e:
36
+ print(f"Error: {e}")
37
+ progress.add_task(description="map reference Failed", total=None)
38
+ exit(1)
39
+
@@ -0,0 +1,39 @@
1
+ import time
2
+ import typer
3
+ from typing_extensions import Annotated
4
+ from rich.progress import Progress, SpinnerColumn, TextColumn
5
+ from ..utils.map import minimap2map
6
+
7
+ app = typer.Typer()
8
+
9
+ @app.command()
10
+ def isoform(
11
+ input: Annotated[str, typer.Option("--input", "-i", help="Input fastq files.")],
12
+ reference: Annotated[str, typer.Option("--reference", "-r", help="reference transcripts path.")],
13
+ output: Annotated[str, typer.Option("--output", "-o", help="output for output sam/bam files.")],
14
+ tool: Annotated[str, typer.Option("--tool", help="minimap2.")]="minimap2",
15
+ parms: Annotated[str, typer.Option("--parms", help="minimap2 parameters for mapping.")]="--secondary=no --cs -a",
16
+ threads: Annotated[int, typer.Option("--threads", "-t", help="Number of threads.")]=4,
17
+ ):
18
+ """
19
+ Mapping of nanopore reads to a reference transcripts.
20
+ """
21
+
22
+ with Progress(
23
+ SpinnerColumn(),
24
+ TextColumn("[progress.description]{task.description}"),
25
+ transient=True,
26
+ ) as progress:
27
+ try:
28
+ progress.add_task(description="map reference...", total=None)
29
+ start=time.time()
30
+ minimap2map(input, reference, output, tool, parms, threads)
31
+ end=time.time()
32
+ time_cost=f"{(end - start) // 3600}h{((end - start) % 3600) // 60}m{(end - start) % 60:.2f}s"
33
+ print(f"map reference Done, time cost: {time_cost}")
34
+ progress.add_task(description=f"map reference Done, time cost: {time_cost}", total=None)
35
+ except Exception as e:
36
+ print(f"Error: {e}")
37
+ progress.add_task(description="map reference Failed", total=None)
38
+ exit(1)
39
+
@@ -0,0 +1,22 @@
1
+ import typer
2
+ import subprocess
3
+ from pathlib import Path
4
+ from rich.progress import track
5
+ from typing_extensions import Annotated
6
+ from ..utils.map import minimap2map
7
+
8
+ app = typer.Typer()
9
+
10
+ @app.command()
11
+ def isoformAS(
12
+ input: Annotated[str, typer.Option("--input", "-i", help="Input fastq files.")],
13
+ reference: Annotated[str, typer.Option("--reference", "-r", help="reference genome path.")],
14
+ prefix: Annotated[str, typer.Option("--prefix", "-p", help="Prefix for output files.")],
15
+ tool: Annotated[str, typer.Option("--tool", help="minimap2.")]="minimap2",
16
+ parms: Annotated[str, typer.Option("--parms", help="minimap2 parameters for mapping.")]="--secondary=no --cs -a",
17
+ threads: Annotated[int, typer.Option(help="Number of threads.")]=4,
18
+ ):
19
+ """
20
+ alternative splicing analysis.
21
+ """
22
+ minimap2map(input, reference, prefix, tool, parms, threads)
@@ -0,0 +1,22 @@
1
+ import typer
2
+ import subprocess
3
+ from pathlib import Path
4
+ from rich.progress import track
5
+ from typing_extensions import Annotated
6
+ from ..utils.map import minimap2map
7
+
8
+ app = typer.Typer()
9
+
10
+ @app.command()
11
+ def nascentRNA(
12
+ input: Annotated[str, typer.Option("--input", "-i", help="Input fastq files.")],
13
+ reference: Annotated[str, typer.Option("--reference", "-r", help="reference genome path.")],
14
+ prefix: Annotated[str, typer.Option("--prefix", "-p", help="Prefix for output files.")],
15
+ tool: Annotated[str, typer.Option("--tool", help="minimap2.")]="minimap2",
16
+ parms: Annotated[str, typer.Option("--parms", help="minimap2 parameters for mapping.")]="--secondary=no --cs -a",
17
+ threads: Annotated[int, typer.Option(help="Number of threads.")]=4,
18
+ ):
19
+ """
20
+ Detect nascent RNA of nanopore reads.
21
+ """
22
+ minimap2map(input, reference, prefix, tool, parms, threads)
nanomd/scripts/gene.R ADDED
File without changes
File without changes
nanomd/scripts/m6a.sh ADDED
@@ -0,0 +1,23 @@
1
+
2
+ conda activate mines
3
+
4
+ cat m6A.bed|sort -k1,1 -k2,2n > m6a.sorted.bed
5
+
6
+ perl ~/soft/metaPlotR/annotate_bed_file.pl --bed ./m6a.sorted.bed --bed2 ~/Refernce/human_ensembl_genome/91/hg38_annot.sorted.bed > annot_m6a.sorted.bed
7
+
8
+ perl ~/soft/metaPlotR/rel_and_abs_dist_calc.pl --bed ./annot_m6a.sorted.bed --regions ~/Refernce/human_ensembl_genome/91/region_sizes.txt > m6a.dist.measures.txt
9
+
10
+ cat Treat.mod.bed | awk -F "\t" '$4 > 0.95 {file="Treat/" $7 ".bed"; print $0 > file}'
11
+
12
+ cat Control.mod.bed | awk -F "\t" '$4 > 0.95 {file="Control/" $7 ".bed"; print $0 > file}'
13
+
14
+ cat Treat.mod.bed | awk -F "\t" '$4 >= 0.9 {file="treat_" $7 ".bed"; print $0 > file}'
15
+
16
+ cat Control.mod.bed | awk -F "\t" '$4 >= 0.9 {file="control_" $7 ".bed"; print $0 > file}'
17
+
18
+ awk '!seen[$4]++ {first[$4]=$0} {count[$4]++} END {for (key in count) print first[key], count[key]}' treat_m6A.bed > treat_m6A.uniq.bed
19
+
20
+ nohup python ./split.py -i Treat.mod.bed -p treat -v 0.6 &
21
+ nohup python ./split.py -i WT.mod.bed -p wt -v 0.6 &
22
+
23
+ R CMD INSTALL legendBaseModel_0.0.6.tar.gz
@@ -0,0 +1,133 @@
1
+ #!/opt/conda/bin/Rscript
2
+ options(repos=structure(c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")))
3
+ if (!requireNamespace("optparse", quietly = TRUE))
4
+ install.packages("optparse")
5
+ library(optparse)
6
+ option_list <- list(
7
+ make_option(c("-i", "--input"), type = "character", default = FALSE,
8
+ help = "input file path"),
9
+ make_option(c("-o", "--output"), type = "character", default = FALSE,
10
+ help = "output file path"),
11
+ make_option(c("-p", "--prefix"), type = "character", default = FALSE,
12
+ help = "prefix of output file"),
13
+ make_option(c("-t", "--type"), type = "character", default = FALSE,
14
+ help = "type of mod (m6A, m5C, AtoI, psi, etc.)")
15
+ )
16
+ opt_parser <- OptionParser(option_list = option_list)
17
+ opt <- parse_args(opt_parser)
18
+
19
+ input <- opt$input
20
+ output <- opt$output
21
+ prefix <- opt$prefix
22
+ type <- opt$type
23
+
24
+ # library packages
25
+ library(dplyr)
26
+ library(purrr)
27
+ library(scales)
28
+ library(ggplot2)
29
+ library(magrittr)
30
+ library(legendBaseModel)
31
+
32
+ plot_metagene_Rd <- function(input_data, output_path, prefix, mod_type = "m6A") {
33
+ ifelse(dir.exists(output_path), "Dir exist alreadly!", dir.create(output_path, recursive = TRUE))
34
+ tryCatch({
35
+ # START
36
+ cat(crayon::green(paste0(prefix, " Start ploting Metagene\n")))
37
+ colour_fill <- c("black", "red")
38
+ names(colour_fill) <- input_data$group %>% unique()
39
+ p1 <- ggplot(input_data, aes(x=value))+
40
+ geom_line(aes(colour = group), stat = "density", adjust = 2) +
41
+ scale_colour_manual(values=colour_fill) +
42
+ geom_vline(xintercept = 1:2, col = "red", linetype="dashed") +
43
+ annotate("text", x = 0.5, y = -0.14, label = "5'UTR") +
44
+ annotate("text", x = 1.5, y = -0.14, label = "CDS") +
45
+ annotate("text", x = 2.5, y = -0.14, label = "3'UTR") +
46
+ annotate("rect", xmin = 0, xmax = 1, ymin = -0.08, ymax = -0.04, alpha = .99, colour = "black")+
47
+ annotate("rect", xmin = 2, xmax = 3, ymin = -0.08, ymax = -0.04, alpha = .99, colour = "black")+
48
+ annotate("rect", xmin = 1, xmax = 2, ymin = -0.12, ymax = 0, alpha = .2, colour = "black") +
49
+ xlab(paste0(mod_type, " metagene")) +
50
+ ylab("Frequency") +
51
+ guides(colour = guide_legend(title = NULL)) +
52
+ theme_bw() +
53
+ theme(legend.position = c(1, 1), legend.justification = c(1, 1)) +
54
+ theme(legend.background = element_blank())
55
+ ggsave(paste0(output_path, "/", prefix, "_", mod_type, "_metagene.pdf"), p1, width = 8, height = 6)
56
+ ggsave(paste0(output_path, "/", prefix, "_", mod_type, "_metagene.png"), p1, width = 8, height = 6)
57
+ ggsave(paste0(output_path, "/", prefix, "_", mod_type, "_metagene.tiff"), p1, width = 8, height = 6)
58
+
59
+ cat(crayon::green(paste0(prefix, " Metagene analysis done\n")))
60
+ # END
61
+ return("sucess")},
62
+ error = function(e){print(e$message);message(return("failled"))})
63
+ }
64
+ restructure_coord <- function(input_path, group, type) {
65
+ if (type == "m6A") {
66
+ m6a.dist <- read.delim(input_path, header = T) %>%
67
+ dplyr::filter(utr5_size > utr3_size) %>%
68
+ dplyr::mutate(
69
+ trx_len = utr5_size + cds_size + utr3_size)
70
+ } else if (type == "m5C") {
71
+ m6a.dist <- read.delim(input_path, header = T) %>%
72
+ dplyr::filter(cds_size > utr3_size & utr3_size > utr5_size) %>%
73
+ dplyr::mutate(
74
+ trx_len = utr5_size + cds_size + utr3_size)
75
+ } else if (type == "psi") {
76
+ m6a.dist <- read.delim(input_path, header = T) %>%
77
+ dplyr::filter(cds_size > utr3_size & utr3_size > utr5_size) %>%
78
+ dplyr::mutate(
79
+ trx_len = utr5_size + cds_size + utr3_size)
80
+ } else if (type == "AtoI") {
81
+ m6a.dist <- read.delim(input_path, header = T) %>%
82
+ dplyr::filter(cds_size > utr3_size & utr3_size > utr5_size) %>%
83
+ dplyr::mutate(
84
+ trx_len = utr5_size + cds_size + utr3_size)
85
+ }
86
+
87
+ temp.df <- m6a.dist %>%
88
+ dplyr::select(c("gene_name", "refseqID", "trx_len")) %>%
89
+ dplyr::rename(setNames(colnames(.), c("gene_name", "gid", "trx_len"))) %$%
90
+ .[order(.$gene_name, .$gid, -.$trx_len), ] %$%
91
+ .[!duplicated(.$gene_name), ]
92
+
93
+ m6a.dist <- m6a.dist[m6a.dist$refseqID %in% temp.df$gid, ]
94
+ utr5.SF <- median(m6a.dist$utr5_size, na.rm = T)/median(m6a.dist$cds_size, na.rm = T)
95
+ utr3.SF <- median(m6a.dist$utr3_size, na.rm = T)/median(m6a.dist$cds_size, na.rm = T)
96
+ # print(paste0("utr5.SF: ", utr5.SF, " utr3.SF: ", utr3.SF))
97
+ utr5.m6a.dist <- m6a.dist[m6a.dist$rel_location < 1, ]
98
+ cds.m6a.dist <- m6a.dist[m6a.dist$rel_location < 2 & m6a.dist$rel_location >= 1, ]
99
+ utr3.m6a.dist <- m6a.dist[m6a.dist$rel_location >= 2 & m6a.dist$rel_location < 3, ]
100
+ utr5.m6a.dist$rel_location <- scales::rescale(utr5.m6a.dist$rel_location, to = c(1-utr5.SF, 1), from = c(0, 1))
101
+ utr3.m6a.dist$rel_location <- scales::rescale(utr3.m6a.dist$rel_location, to = c(2, 2+utr3.SF), from = c(2, 3))
102
+ m6a.metagene.coord <- c(utr5.m6a.dist$rel_location, cds.m6a.dist$rel_location, utr3.m6a.dist$rel_location)
103
+
104
+ m6a_data <- data.frame("value"= m6a.metagene.coord) %>%
105
+ dplyr::mutate("group"= group)
106
+ return(m6a_data)
107
+ }
108
+ auto_meta_plot <- function(input_path, output_path, prefix, mod_type = "m6A") {
109
+ # test
110
+ # 2. check output path
111
+ options(warn = -1)
112
+ ifelse(dir.exists(output_path), "Dir exist alreadly!", dir.create(output_path, recursive = TRUE))
113
+ tryCatch({
114
+ # START
115
+ sample_all <- legendBaseModel::make_sample_sheet(input_path, paste0("_", mod_type, "_abs_dist.txt")) %>%
116
+ dplyr::mutate(
117
+ group = sample,
118
+ type = mod_type,
119
+ dist_measures = purrr::pmap(list(sample_path, group, type), restructure_coord)
120
+ ) %>%
121
+ dplyr::mutate(
122
+ output_plot = output_path,
123
+ prefix_plot = paste0(prefix, group),
124
+ plot_meta <- purrr::pmap(list(dist_measures, output_plot, prefix_plot, type), plot_metagene_Rd)
125
+ )
126
+ # END
127
+ return("sucess")},
128
+ error = function(e){print(e$message);message(return("failled"))})
129
+ }
130
+
131
+
132
+ # main run
133
+ auto_meta_plot(input, output, prefix, type)
@@ -0,0 +1,65 @@
1
+ #!/user/bin/Rscript
2
+ options(repos=structure(c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")))
3
+ if (!requireNamespace("optparse", quietly = TRUE))
4
+ # install.packages("optparse",repos="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")
5
+ install.packages("optparse")
6
+ library(optparse)
7
+ option_list <- list(
8
+ make_option(c("-i", "--input"), type = "character", default = FALSE,
9
+ help = "You input file "),
10
+ make_option(c("-s", "--species"), type = "character", default = FALSE,
11
+ help = "You species in [human,mouse] "),
12
+ make_option(c("-o", "--outputDir"), type = "character", default = FALSE,
13
+ help = "Your outputDir ")
14
+ )
15
+ opt_parser = OptionParser(option_list = option_list);
16
+ opt = parse_args(opt_parser);
17
+
18
+ # 1.library package
19
+ library(modelr)
20
+ library(magrittr)
21
+ library(tidyverse)
22
+ library(rstudioapi)
23
+ options(na.action = na.warn)
24
+ # vignette("dplyr" ,package = "dplyr")
25
+ # 2.glob function
26
+ # 1.add analysis function
27
+ #get the file path
28
+ fun_path_ <- dirname(getSourceEditorContext()$path)
29
+ source(paste0(fun_path_, "/fun/basis_fun.R"), encoding = "UTF-8")
30
+ source(paste0(fun_path_, "/fun/unique_fun.R"), encoding = "UTF-8")
31
+
32
+ # 2.global variables
33
+
34
+ # input_ <- path_clean(opt$input)
35
+ # species <- choose_species(opt$species)
36
+ # output_ <- path_clean(opt$outputDir)
37
+
38
+ ## run chenyiping's m6a DEA
39
+ input_ <- path_clean("./1-output/20221027_newRNA/input/")
40
+ species <- choose_species("human")
41
+ output_ <- path_clean("./1-output/20221027_newRNA/all_newRNA_results3")
42
+
43
+ run_data <- data.frame(
44
+ group=c("newRNA"),
45
+ input = input_,
46
+ output = output_) %>%
47
+ mutate(
48
+ ipath = paste0(input, "/newRNA_counts_matrix.tsv"),
49
+ newRNA_DE = pmap(list(ipath, output, group), newRNA_DE_Rd))
50
+
51
+ run_data <- data.frame(
52
+ group=c("allRNA"),
53
+ input = input_,
54
+ output = output_) %>%
55
+ mutate(
56
+ ipath = paste0(input, "/newRNA_counts_matrix.tsv"),
57
+ newRNA_DE = pmap(list(ipath, output, group), newRNA_DE_Rd))
58
+
59
+ run_data <- data.frame(
60
+ group=c("allRNA"),
61
+ input = input_,
62
+ output = output_) %>%
63
+ mutate(
64
+ ipath = paste0(input, "/newRNA_counts_matrix.tsv"),
65
+ newRNA_DE = pmap(list(ipath, output, group), newRNA_DE_Rd))