1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package ffx.potential.commands;
39
40 import ffx.potential.cli.PotentialCommand;
41 import ffx.utilities.FFXBinding;
42 import org.biojava.nbio.core.sequence.ProteinSequence;
43 import org.biojava.nbio.core.sequence.io.FastaReaderHelper;
44 import org.biojava.nbio.core.sequence.io.FastaWriterHelper;
45 import picocli.CommandLine.Command;
46 import picocli.CommandLine.Option;
47 import picocli.CommandLine.Parameters;
48
49 import java.io.File;
50 import java.util.ArrayList;
51 import java.util.Collection;
52 import java.util.Map;
53
54 import static java.lang.String.format;
55 import static org.apache.commons.io.FilenameUtils.getName;
56
57
58
59
60
61
62
63 @Command(name = "Fasta", description = " Fasta outputs a sub-sequence from a FASTA file.")
64 public class Fasta extends PotentialCommand {
65
66
67 @Option(names = {"-f", "--firstResidue"}, paramLabel = "1", defaultValue = "1",
68 description = "Define the first Fasta residue to keep (index of the first residue is 1).")
69 private int firstResidue = 1;
70
71
72 @Option(names = {"-l", "--lastResidue"}, paramLabel = "-1", defaultValue = "-1",
73 description = "Define the last Fasta residue to keep (index of the last residue is n).")
74 private int lastResidue = -1;
75
76
77 @Parameters(arity = "1", paramLabel = "file",
78 description = "A file in FASTA format.")
79 private String fastaName = null;
80
81 public Fasta() {
82 super();
83 }
84
85 public Fasta(FFXBinding binding) {
86 super(binding);
87 }
88
89 public Fasta(String[] args) {
90 super(args);
91 }
92
93 @Override
94 public Fasta run() {
95 if (!init()) {
96 return this;
97 }
98
99 if (fastaName == null) {
100 logger.info(helpString());
101 return this;
102 }
103
104 logger.info("\n Opening FASTA " + fastaName);
105
106 try {
107 Map<String, ProteinSequence> fastaData =
108 FastaReaderHelper.readFastaProteinSequence(new File(fastaName));
109 if (fastaData == null || fastaData.isEmpty()) {
110 logger.warning(" No sequences found in FASTA file: " + fastaName);
111 return this;
112 }
113 ProteinSequence sequence = fastaData.values().iterator().next();
114 String seq = sequence.getSequenceAsString();
115 int length = seq.length();
116 logger.info(format("\n %s of length: %d\n %s", sequence.getOriginalHeader(), length, seq));
117
118 if (firstResidue < 1 || firstResidue > length) {
119 firstResidue = 1;
120 }
121 if (lastResidue < firstResidue || lastResidue > length) {
122 lastResidue = length;
123 }
124
125 ProteinSequence proteinSequence = new ProteinSequence(seq.substring(firstResidue - 1, lastResidue));
126 proteinSequence.setOriginalHeader(sequence.getOriginalHeader());
127 length = proteinSequence.getLength();
128 logger.info(format("\n New sequence from residue %d to residue %d is of length %d: \n %s",
129 firstResidue, lastResidue, length, proteinSequence));
130
131 Collection<ProteinSequence> proteinSequenceCollection = new ArrayList<>();
132 proteinSequenceCollection.add(proteinSequence);
133
134
135 String dirString = getBaseDirString(fastaName);
136 File saveFile = potentialFunctions.versionFile(new File(dirString + getName(fastaName)));
137
138 logger.info(format("\n Saving new Fasta file to: %s", saveFile.getAbsolutePath()));
139 FastaWriterHelper.writeProteinSequence(saveFile, proteinSequenceCollection);
140 } catch (Exception e) {
141 logger.warning(" Exception processing FASTA file: " + e.getMessage());
142 }
143
144 return this;
145 }
146 }