View Javadoc
1   //******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2025.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
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   * Fasta outputs a sub-sequence from a FASTA file.
59   * <p>
60   * Usage:
61   *   ffxc Fasta [options] &lt;filename.fasta&gt;
62   */
63  @Command(name = "Fasta", description = " Fasta outputs a sub-sequence from a FASTA file.")
64  public class Fasta extends PotentialCommand {
65  
66    /** Define the first Fasta residue to keep (index of the first residue is 1). */
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    /** Define the last Fasta residue to keep (index of the last residue is n). */
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    /** The final argument should be a Fasta file. */
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       // Use the current base directory, or update if necessary based on the given filename.
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 }