I initially created a method (function) to parse out Sequences from a Fasta File but like so many other things Fasta files can be in so many formats that some form of setup will need to be done to parse one type from another type. Your Fasta files are no exception, since in most Fasta sequences I don't believe the hyphen or minus character (-
) is allowed within a sequence unless it is contained within the sequence Header but, I can see that in your Fasta files, hyphens are contained within sequences. Of course, I am most likely wrong about that. ;)
With this now known I've added some additional variables that can be manually set so as to provide more flexibility towards different Fasta file formats (I hope). In reality this should actually be a class instead of a method right from the beginning but I'm going to let you convert it to a workable class youself.
Now, this is a large method (and I don't feel good about that) with a lot of comments but I wanted to give you something relatively quick. I recommend you read all the provided comments within the code.
After you try this method...do make a class of it:
/**
* Returns a {@code List<String>} Interface object of all the Sequence Clusters
* detected within the supplied Fasta Data File.<br>
*
* @param sourceFilePath (String) The full path and file name to the source
* Fasta data file to parse.<br>
*
* @param destinationFileFolderPath (String) The full path to the
* destination directory (folder) where the two split Fasta files are to be
* created. If null or Null String ("") is supplied then the two split Fasta
* files will be created within the source file directory. Destination split
* file names are auto-generated!<br>
*
* @param splitRatio (String) The ratio of sequence clusters to be applied to
* each created split Fasta files. Ratio is considered percentages and the
* highest percentage is first followed by the lower percentage delimited with
* a colon (:), for example: "60:40". The two values provided <b>must</b> sum
* to 100.<br>
*
* @param newSequenceDesignator (String) By default an inner blank line is
* considered the end of a sequence cluster and the possible start of a new
* one. If a designator is supplied here then blank lines are ignored within
* the source file while parsing (if they exist). If there are no blank lines
* within the source file separating the sequence clusters then a designator
* <b>must</b> be supplied, usually the Sequence Cluster Header designator (>)
* is used. The New Sequence Designator can be the same as the Sequence Cluster
* Header Designator.<br>
*
* @return ({@code List<String>}) The List of parsed Sequence Clusters from
* the supplied source Fasta data file.
*/
public static java.util.List<String> splitFastaFile(String sourceFilePath, String destinationFileFolderPath,
String splitRatio, String newSequenceDesignator) {
/* System newline character(s) to use for console
display when required. */
String ls = System.lineSeparator();
// Valid characters allowed to be contained within a Fasta sequence line.
String allowableCharactersInSequences = "ABCDEFGHIJKLMNOPQRSTUVWXYZ-*";
/* By default, any sequence cluster line (other than the Header) which
contains an invalid character (a character not stipulated within the
'allowableCharactersInSequences' variable) is ignored and not added
to the Sequence Cluster. If however, you provide a string character
or phrase to the 'replaceInvalidSequenceCharactersWith' variable then
all invalid characters will be replace by what is held within that
variable. Keep in mind that you should use a character or case phrase
that is not considered valid (where as it is not also contained within
the 'allowableCharactersInSequences' variable). You want to always
maintain an invalid sequence line as INVALID unless the replacement is
indeed a VALID responce (repair) to the sequence which makes up the
sequence line or Sequence Cluster. */
String replaceInvalidSequenceCharactersWith = null; // Default is null.
/* The String or string character that will denote
the start of a new Sequence cluster. */
String sequenceDesignatorString = ">";
/* The String or string character that will denote
the start of a Sequence Header line. Can be the
same as the Sequence Designator String. */
String sequenceHeaderDesignatorString = ">";
// Escape RegEx meta characters in allowableCharactersInSequences (if any).
allowableCharactersInSequences = allowableCharactersInSequences.replaceAll("[\W]", "\\$0");
// Add the sequence Header (if any)
boolean keepSequenceHeader = true;
// Add a blank line between sequence clusters in created Split Fasta files.
boolean blankLineBetweenSequenceClusters = false;
/* If a comment is supplied to this variable then it MUST start with a
semicolon (;). If it doesn't then it will be nulled. This comment
will be applied as the fist line of any Split File created. The comment
provided can utilize one or all three of the method tags available.
These tags are:
%H The High percentabe Fasta sequences split value.
%L The Low percentabe Fasta sequences split value.
%SV The Split Value currently being processed.
%SFN The Source File Name
An example might be as what was provided below: */
String splitFileComment = ";SPLIT FASTA FILE - Source File: %SFN - Percent of source: %SV%";
// See if the supplied source Fasta file exists.
File f = new File(sourceFilePath);
if (!f.exists() || !f.canRead()) {
System.err.println("splitFastaFile() method error! Either the "
+ "specified source file can not be found or permission "
+ "to read the file does not exist!");
return null;
}
/* Get the supplied Fasta file name. The destination
files will derive from this name. */
String sourceFileName = f.getName();
/* If null or null string ("") is passed as the destination
directory then the Source file directory will also become
the destination for the two created Split Files. */
if (destinationFileFolderPath == null || destinationFileFolderPath.isEmpty()) {
String absPath = new File(sourceFilePath).getAbsolutePath();
destinationFileFolderPath = absPath.substring(0, absPath.lastIndexOf(File.separator));
}
/* Make sure the supplied destination file folder path
contains a system file separator character ( or /). */
if (!destinationFileFolderPath.endsWith(File.separator)) {
destinationFileFolderPath = destinationFileFolderPath + File.separator;
}
/* Make sure a proper Split Ratio is supplied! The Split Ratio is
supplied as a colon (:) delimited string containg the desired
percentage of sequences to be saved within the first text file
and the desired percentage of sequences to be saved to the second
text file. If you want 80% of the sequences within the Fasta source
file to be written to the first text file and you want the remaining
20% to be written to the second text file then you would supply to
the 'splitRatio' parameter: "80:20". Whatever is supplied, the sum
of the two supplied values MUST equal 100 (100%). The higher of the
two values MUST be first (this is enforced). */
if (!splitRatio.matches("\d{1,3}:\d{1,3}")) {
System.err.println("splitFastaFile() method Error! An invalid Split "
+ "Ratio string was supplied! (" + splitRatio + ") The format "
+ "must be: "80:20".");
return null;
}
/* Split the ratio string provided within the 'splitRatio' parameter.
Convert the string numerical values to Integer and check validity. */
String[] ratioParts = splitRatio.split("\s*:\s*");
int high = Integer.valueOf(ratioParts[0]);
int low = Integer.valueOf(ratioParts[1]);
if (high + low != 100) {
System.err.println("splitFastaFile() method Error! An invalid Split "
+ "Ratio string was supplied! (" + splitRatio + ") The percentage "
+ "values supplied must sum to 100. What was supplied sums to: "
+ (high + low) + ".");
return null;
}
else if (high < low) {
System.err.println("splitFastaFile() method Error! An invalid Split "
+ "Ratio string was supplied! (" + splitRatio + ") The higher "
+ "percentage value must be on the left and the lower percentage "
+ "value on the right! Swapping values to make it valid ("
+ low + ":" + high + ")!");
int tmp = high;
high = low;
low = tmp;
}
// Load source file Sequences into a List Interface object.
List<String> sequenceList = new ArrayList<>();
int fileLineCount = 0;
int validDataLineCount = 0;
int clusterLineCount = 0;
try (BufferedReader reader = new BufferedReader(new FileReader(sourceFilePath))) {
StringBuilder sb = new StringBuilder("");
String line;
while((line = reader.readLine()) != null) {
/* Increment the File Lines Count (tracks the
total number of lines in Fasta source file). */
fileLineCount++;
/* Trim off leading and trailing whitespaces, tabs,
etc from the read in data file line. */
line = line.trim();
// Ignore lines that START with a simicolon (;). They are considered comment lines.
if (line.startsWith(";")) { continue; }
// Ignore first line of file if it's blank.
if (line.isEmpty() && validDataLineCount == 0) { continue; }
// See if this is a Sequence Header Line and whether or not we are to keep it.
if (line.startsWith(sequenceHeaderDesignatorString) && !keepSequenceHeader) {
// Increment the Sequence Cluster Line Count.
clusterLineCount++;
continue; // Ignore this Header...loop again.
}
if (validDataLineCount > 0 && ((sequenceDesignatorString != null && !sequenceDesignatorString.isEmpty()) ? line.startsWith(sequenceDesignatorString) : line.isEmpty())) {
String tmpLine = "";
if (line.startsWith(sequenceDesignatorString)) {
tmpLine = line;
}
sequenceList.add(sb.toString());
sb.delete(0, sb.length());
clusterLineCount = 0;
if (!tmpLine.isEmpty()) {
sb.append(tmpLine).append(ls);
clusterLineCount++;
validDataLineCount++;
}
}
els
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…