annotate annotate_contigs.R @ 3:e955b40ad3a4 draft default tip

Uploaded
author petr-novak
date Tue, 12 Oct 2021 07:43:54 +0000
parents cf3cea0a3039
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
1 #!/usr/bin/env Rscript
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
2 ## input 1 - fasta with CLXContig names
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
3 ## input 2 - annotation
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
4 ## output 3 - annotated fasta
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
5 suppressPackageStartupMessages(library(Biostrings))
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
6
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
7 clean_contigs = function(s){
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
8 ## remove all N
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
9 sc = as.character(s)
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
10 sc_trimmed = gsub("N+$", "", gsub("^N+","",s))
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
11 ## remove blank and short sequences:
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
12 sc_trimmed_not_empty = sc_trimmed[nchar(sc_trimmed) !=0]
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
13 sc_trimmed_short = sc_trimmed_not_empty[nchar(sc_trimmed_not_empty) <=20]
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
14 sc_trimmed_long = sc_trimmed_not_empty[nchar(sc_trimmed_not_empty) >20]
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
15 sc_trimmed_short_tarean = sc_trimmed_short[grep("sc_", names(sc_trimmed_short), fixed=TRUE)]
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
16 sc_out = DNAStringSet(c(sc_trimmed_long, sc_trimmed_short_tarean))
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
17 }
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
18
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
19 ## annotate_rm_fasta.R input.fasta annot.csv output.fasta
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
20 ## input 1 - input.fasta - contigs from clustering
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
21 ## input 2 - annot.csv of clusters, firts column is CL number, seciond is annotation
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
22 ##
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
23 ## output - clean conntigs with appended annotation
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
24
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
25 ## find header row of annotation table
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
26 x = readLines(commandArgs(T)[2])
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
27
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
28 ## TODO - check mandatory names!!!
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
29 hl = intersect(grep("cluster", tolower(x)), grep("automatic_annotation", tolower(x)))
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
30 message("using line ", hl, " as header")
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
31
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
32 annotation_table=read.table(commandArgs(T)[2], sep="\t", header=TRUE, skip = hl - 1)
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
33 colnames(annotation_table) = tolower(colnames(annotation_table))
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
34
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
35 contigs = readDNAStringSet(commandArgs(T)[1])
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
36 if("final_annotation" %in% colnames(annotation) & all(!is.na(annotation_table$final_annotation))){
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
37 annot_dict = annotation_table$final_annotation
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
38 message("using final annotation column")
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
39 }else{
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
40 message("using automatic annotation column")
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
41 annot_dict = annotation_table$automatic_annotation
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
42 }
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
43
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
44
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
45
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
46 names(annot_dict) = paste0("CL",annotation_table$cluster)
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
47 print(annot_dict)
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
48
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
49 contigs_ok = clean_contigs(contigs)
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
50 contig_name = gsub("Contig.+","",names(contigs_ok))
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
51
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
52 ## keep only contigs which are in annot table
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
53 include = contig_name %in% names(annot_dict)
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
54
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
55 contig_name_inc = contig_name[include]
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
56 contig_ok_include = contigs_ok[include]
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
57
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
58 new_name_with_annot = paste0(names(contig_ok_include),"#",annot_dict[contig_name_inc])
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
59 names(contig_ok_include) = new_name_with_annot
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
60
cf3cea0a3039 Uploaded
petr-novak
parents:
diff changeset
61 writeXStringSet(contig_ok_include, filepath = commandArgs(T)[3])