Java 带滑动窗口算法的CPG岛查找器:字符串索引间歇性越界异常
在阅读了下面的评论并按照建议进行单元测试后,我正在编辑这篇文章。以下是我的计划的简要说明:Java 带滑动窗口算法的CPG岛查找器:字符串索引间歇性越界异常,java,algorithm,bioinformatics,sliding-window,Java,Algorithm,Bioinformatics,Sliding Window,在阅读了下面的评论并按照建议进行单元测试后,我正在编辑这篇文章。以下是我的计划的简要说明: 给定一个只包含字母A、G、C、T的输入字符串。该字符串的长度通常为80-100K 我必须确定符合某些标准的区域(最小长度为200)。我使用的是滑动窗口算法。(例如:输入字符串:abcdef,输入宽度=3,滑动窗口字符串应该是abc,bcd,cde,def,ef。在我的例子中,输入宽度=200)。我创建了一个函数来执行此操作,并将字符串的开始和结束间隔保存在一个整数列表中。 所以,假设我的列表是(30230
public static List<Integer> finalCPGIslands(List<Integer> iList,
String iSeq, int width) {
// Declare output list that contains final list of start and end
// intervals
List<Integer> oList = new ArrayList<Integer>();
// Add the first two elements anyways
oList.add(iList.get(0));
oList.add(iList.get(1));
if (iList.size() > 2) {
for (int i = 2; i < iList.size(); i += 2) {
// The below IF is attempted to ensure that substring is always
// valid
if (iSeq.length() > iList.get(i + 1)) {
// While creating the substring in next line, I get String
// index out of range: -9
String testSeq = iSeq.substring(iList.get(i),
iList.get(i + 1) + 1);
boolean check = cpgCriteriaCheck(testSeq);
if (check) {
// If condition is met, add the indexes to the final
// list
oList.add(iList.get(i));
oList.add(iList.get(i + 1));
}
// If condition is not met, start removing one character at
// a time until condition is met
else {
int counter = 0;
int currentSequenceLength = testSeq.length();
String newTestSeq = null;
while (counter <= currentSequenceLength) {
counter++;
if (testSeq.length() > 2) {
newTestSeq = testSeq.substring(1,
testSeq.length() - 1);
testSeq = newTestSeq;
if (newTestSeq.length() < width) {
counter = currentSequenceLength + 1;
} else {
boolean checkAgain = cpgCriteriaCheck(newTestSeq);
// If condition met, add the item to list
// and exit
if (checkAgain) {
oList.add(iList.get(i) + counter);
oList.add(iList.get(i + 1) - counter);
counter = currentSequenceLength + 1;
}
} // End of Else
} // End of IF
} // End of While
} // End of Else
}
} // End of For
} // End of Else
return oList;
此外,此异常仅间歇性出现。我有一个大约95K个字符的输入文件,这个异常不会发生。我想,通过在检查字符串长度是否大于输入列表值的位置放置一个IF语句,我就解决了这个异常。
还有,-9表示什么?这是否表示字符串中的第9个字符无效?即使我正在通过删除所有出现的/r和/n来清理字符串,也可能有任何不需要的字符导致此问题。抱歉说得太冗长了,但我想给出这个问题的背景。根本原因似乎仍然只是创建子字符串时字符串索引越界异常。通过添加以下行解决了字符串OOB异常。这涵盖了创建子字符串时可能发生的所有错误情况。
if(str!=null&&from>=0&&to>=from&&to你真的希望这里有人会为你调试你的代码吗?我建议你花点时间,为你的函数编写单元测试,找出问题的根源,然后,如果找不到解决方案,就带着一个定义良好的问题回来。@zero323坦白地说,你实际上给了我一个答案这是一个非常好的建议。我正在学习Java,实际上我读过关于使用JUnit进行单元测试的文章。我以前从未使用过JUnit,但现在我将返回并开始为每个函数进行单元测试。谢谢。很高兴听到这一点。如果你要求我们阅读几百行代码并找出可能的错误,你真的想得到答案。即使你不知道ite测试总是试图找到问题的根源。顺便说一下,如果您的问题与表示层无关,则不欢迎截图。已解决此问题。通过添加以下行,字符串OOB异常已得到解决:if(str!=null&&from>=0&&to>=from&&to您可能应该将其作为答案发布:)
String testSeq = iSeq.substring(iList.get(i),
iList.get(i + 1) + 1);