java - 使用滑动窗口算法的 CPG Island Finder : String Index Out of Bound Exception intermittently

标签 java algorithm bioinformatics sliding-window

在阅读下面的评论并按照建议进行单元测试后,我正在编辑这篇文章。以下是我的程序的简要说明:

  1. 给定一个仅包含字母 A、G、C、T 的输入字符串。字符串的长度通常为 80-100K。
  2. 我必须确定满足特定条件的区域(长度至少为 200)。我正在使用滑动窗口算法。 (示例:输入字符串:abcdef,输入宽度=3,滑动窗口字符串为 abc、bcd、cde、def、ef。在我的例子中,输入宽度=200)。我为此创建了一个函数,并将字符串的开始和结束间隔保存在一个整数列表中。 因此,假设我的列表类似于 (30,230, 40, 240, 60, 260, 300,500, 450,650),其中 30,40,60,300,450 是满足特定条件的开始间隔,其余数字是结束间隔。
  3. 下一步是确定那些邻近的间隔(距离为 100)并将它们组合在一起。我已经做到了。现在我的列表是 (30,260, 300,500, 450,650)。
  4. 我的最后一步是在这些时间间隔内重新运行标准,以确保它们仍然符合要求。这是我遇到问题的地方。这是我的代码:

    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;
    

    }

在此函数中,输入参数是包含开始和结束间隔的整数列表、我的输入字符串和整数差,即开始和结束间隔之间的最小差。当我尝试在以下行中创建一个子字符串时,出现了一个超出范围的字符串:-9 异常:

String testSeq = iSeq.substring(iList.get(i),
                        iList.get(i + 1) + 1);

此外,此异常只是间歇性出现。我有一个大约 95K 个字符的输入文件,但不会发生此异常。我认为通过在我检查以确保字符串长度大于输入列表值的地方放置一个 IF 语句,我涵盖了这个异常。 另外,-9 表示什么?这是否表明字符串中的第 9 个字符无效?即使我通过删除所有/r 和/n 事件来清理字符串,是否有可能是任何不需要的字符导致此问题。抱歉过于冗长,但我想给出这个问题的背景。根本原因似乎仍然只是创建子字符串时的字符串索引超出范围异常。

最佳答案

通过添加以下行解决了字符串 OOB 异常。这涵盖了创建子字符串时可能发生的所有可能的错误情况。
if (str != null && from >= 0 && to >= from && to <= str.length()) { }

关于java - 使用滑动窗口算法的 CPG Island Finder : String Index Out of Bound Exception intermittently,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19608780/

相关文章:

java - InnoDB 数据库锁定会阻塞线程

arrays - 和大于给定值的子数组的最小和

python - 如何以有效的方式获取长度为 2 或更长的数组的所有子数组?

python - 为什么我的 for 循环(python)在 4 次迭代后会改变行为?

Java Applet 在浏览器中运行时不会显示图像

java - 如何为 Netbeans 和 Eclipse 使用不同的 jdk

r - 在 R 中使用 for 循环进行冒泡排序

Python for 循环过早停止

python - Snakemake 无法将多个文件识别为输入

java - 如何在java中获取所有网络接口(interface)并将信息存储在Java Map中