Mercurial > repos > ecology > aligned_to_consensus
annotate consensus_from_alignments.R @ 0:0ccbe1c20fc3 draft default tip
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
| author | ecology |
|---|---|
| date | Tue, 25 Apr 2023 10:05:29 +0000 |
| parents | |
| children |
| rev | line source |
|---|---|
|
0
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
1 #Rscript |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
2 |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
3 ################################################################################ |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
4 ## Extract consensus sequence from aligned forward and reverse fasta ## |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
5 ################################################################################ |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
6 |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
7 #####Packages |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
8 library(bioseq, quietly = TRUE) |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
9 |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
10 ##Load arguments |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
11 args <- commandArgs(trailingOnly = TRUE) |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
12 |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
13 if (length(args) == 0) { |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
14 stop("This tool needs at least one argument") |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
15 } else { |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
16 fasta_f <- args[1] |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
17 seq_type <- args[2] |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
18 meth_choice <- args[3] |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
19 gaps_tf <- as.logical(args[4]) |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
20 out_og <- as.logical(args[5]) |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
21 } |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
22 |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
23 ## Read input file |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
24 seq_l <- bioseq::read_fasta(fasta_f, type = seq_type) |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
25 |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
26 if(bioseq::seq_nseq(seq_l) < 2){ |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
27 stop("Only one sequence in the file, at least two aligned sequences are needed to compute a consensus") |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
28 }else{ |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
29 if(length(unique(bioseq::seq_nchar(seq_l))) > 1) {stop("Sequences have different lengths, please provide aligned sequences")} |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
30 } |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
31 |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
32 ##Consensus sequence |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
33 seq_con <- bioseq::seq_consensus(seq_l, method = meth_choice, gaps = gaps_tf) |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
34 |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
35 if(bioseq::seq_nseq(seq_con) > 1){stop("Consensus hasn't worked for an unknown reason, double-check your input file and the parameters you chose")} |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
36 |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
37 names(seq_con) <- paste0("consensus_", Reduce(PTXQC::LCS, names(seq_l))) |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
38 ##Create output |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
39 if(out_og){ |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
40 bioseq::write_fasta(c(seq_con, seq_l), file = "output.fasta", line_length = Inf, block_length = Inf) |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
41 }else{ |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
42 bioseq::write_fasta(seq_con, file = "output.fasta", line_length = Inf, block_length = Inf) |
|
0ccbe1c20fc3
planemo upload for repository https://github.com/ColineRoyaux/Galaxy_tool_projects/tree/main/consensus_from_alignments commit ecc21de8f368c6a95c57d4e6511ed42af9e72a66
ecology
parents:
diff
changeset
|
43 } |
