gpt4 book ai didi

Java 检查列表中的元素是否出现在所有事件中

转载 作者:行者123 更新时间:2023-12-02 02:25:30 27 4
gpt4 key购买 nike

我有一个方法,它接受字符串的 ArrayList,列表中的每个元素等于以下内容的变体:

>AX018718 Equine influenza virus H3N8 // 4 (HA)
CAAAAGCAGGGTGACAAAAACATGATGGATTCCAACACTGTGTCAAGCTTTCAGGTAGACTGTTTTCTTT
GGCATGTCCGCAAACGATTTGCAGACCAAGAACTGGGTGATGCCCCATTCCTTGACCGGCTTCGCCGAGA

此方法分为 Acc(本例中为 AX018718)和 seq(Acc 后面的两行)

然后由另一个名为 pal 的字符串 ArrayList 检查子字符串是否匹配 [AAAATTTT, AAACGTTT, AATATATTT]

我能够获得第一个列表中不同元素的所有匹配,输出为:

AATATATT in organism: AX225014 Was found in position: 15 and at 15
AATATT in organism: AX225014 Was found in position: 1432 and at 1432
AATATT in organism: AX225016 Was found in position: 1404 and at 1404
AATT in organism: AX225016 Was found in position: 169 and at 2205

是否可以检查所有输出的信息是否所有 Acc 都匹配一个 friend ?

在上面的例子中,想要的输出是:

AATATT was found in all of the Acc.

我的工作代码:

public static ArrayList<String> PB2Scan(ArrayList<String> Pal) throws FileNotFoundException, IOException
{
ArrayList<String> PalindromesSpotted = new ArrayList<String>();

File file = new File("IAV_PB2_32640.txt");
Scanner sc = new Scanner(file);
sc.useDelimiter(">");
//initializes the ArrayList
ArrayList<String> Gene1 = new ArrayList<String>();
//initializes the writer
FileWriter fileWriter = new FileWriter("PB2out");
PrintWriter printwriter = new PrintWriter(fileWriter);
//Loads the Array List
while(sc.hasNext()) Gene1.add(sc.next());
for(int i = 0; i < Gene1.size(); i++)
{
//Acc breaks down the title so the element:
//>AX225014 Equine influenza virus H3N8 // 1 (PB2)
//ATGAAGACAACCATTATTTTGATACTACTGACCCATTGGGTCTACAGTCAAAACCCAACCAGTGGCAACA
//GGCATGTCCGCAAACGATTTGCAGACCAAGAACTGGGTGATGCCCCATTCCTTGACCGGCTTCGCCGAGA
//comes out as AX225014
String Acc = Accession(Gene1.get(i));
//seq takes the same element as above and returns only
//ATGAAGACAACCATTATTTTGATACTACTGACCCATTGGGTCTACAGTCAAAACCCAACCAGTGGCAACA
//GGCATGTCCGCAAACGATTTGCAGACCAAGAACTGGGTGATGCCCCATTCCTTGACCGGCTTCGCCGAGA
String seq = trimHeader(Gene1.get(i));
for(int x = 0; x<Pal.size(); x++)
{
if(seq.contains(Pal.get(x))){
String match = (Pal.get(x) + " in organism: " + Acc + " Was found in position: "+ seq.indexOf(Pal.get(x)) + " and at " +seq.lastIndexOf(Pal.get(x)));
printwriter.println(match);
PalindromesSpotted.add(match);
}
}
}
Collections.sort(PalindromesSpotted);
return PalindromesSpotted;
}

最佳答案

首先,您的代码不会写入任何文件来记录结果,因为您没有关闭您的编写器或至少刷新 PrintWriter。事实上,您也不会关闭您的阅读器。您确实应该关闭您的读者和作者以获取免费资源。值得深思。

您可以使您的 PB2Scan() 方法返回一个简单的结果列表,就像现在一样,或者返回一个包含相同 Pal(s) 的 acc 的结果列表,或者可能两者都包含简单的结果列表被记录,并且在该列表的末尾有一个 acc 列表,其中包含也将被记录的相同 Pal(s)。

PB2Scan() 方法的一些附加代码和附加整数参数可以实现此目的。对于附加参数,您可能需要添加如下内容:

public static ArrayList<String> PB2Scan(ArrayList<String> Pal, int resultType) 
throws FileNotFoundException, IOException
{ .... }

其中整数 resultType 参数将采用 0 到 2 之间的三个整数值之一:

  • 0 - 简单的结果列表,就像代码当前所做的那样;
  • 1 - Acc 与 Pal 的相匹配;
  • 2 - 简单的结果列表以及与结果列表末尾的 Pal 匹配的 Acc。

您还应该真正将文件读取为 PB2Scan() 方法的参数,因为该文件在下次运行时很容易成为不同的名称。这使得该方法更加通用,而不是硬编码文件名。

public static ArrayList<String> PB2Scan(String filePath, ArrayList<String> Pal, int resultType) 
throws FileNotFoundException, IOException { .... }

该方法始终可以写入相同的输出文件,因为它最适合它来自的方法。

使用上述概念而不是写入输出文件(PB2Out.txt),因为 PalindromesSpotted ArrayList 正在创建,我认为最好在您的ArrayList 或 ArrayList 已完成。为此,另一种方法(writeListToFile())最适合执行该任务。要查明是否有任何相同的 Pal 与其他 Acc 相匹配,最好使用另一个方法 (getPalMatches()) 来完成该任务。

由于任何给定Seq多个给定Pal的索引位置未正确报告,所以我提供了另一种方法(findSubstringIndexes()) 快速完成该任务。

需要注意的是,下面的代码假设从 trimHeader() 方法获取的 Seq 都是一个字符串,其中没有换行符。

修改后的PB2Scan()方法和上述其他方法如下:

The PB2Scan() Method:

public static ArrayList<String> PB2Scan(String filePath, ArrayList<String> Pal, int resultType) 
throws FileNotFoundException, IOException {
// Make sure the supplied result type is either
// 0, 1, or 2. If not then default to 0.
if (resultType < 0 || resultType > 2) {
resultType = 0;
}
ArrayList<String> PalindromesSpotted = new ArrayList<>();

File file = new File(filePath);
Scanner sc = new Scanner(file);
sc.useDelimiter(">");
//initializes the ArrayList
ArrayList<String> Gene1 = new ArrayList<>();
//Loads the Array List
while (sc.hasNext()) {
Gene1.add(sc.next());
}
sc.close(); // Close the read in text file.

for (int i = 0; i < Gene1.size(); i++) {
//Acc breaks down the title so the element:
//>AX225014 Equine influenza virus H3N8 // 1 (PB2)
//ATGAAGACAACCATTATTTTGATACTACTGACCCATTGGGTCTACAGTCAAAACCCAACCAGTGGCAACA
//GGCATGTCCGCAAACGATTTGCAGACCAAGAACTGGGTGATGCCCCATTCCTTGACCGGCTTCGCCGAGA
//comes out as AX225014
String Acc = Accession(Gene1.get(i));

//seq takes the same element as above and returns only
//ATGAAGACAACCATTATTTTGATACTACTGACCCATTGGGTCTACAGTCAAAACCCAACCAGTGGCAACA
//GGCATGTCCGCAAACGATTTGCAGACCAAGAACTGGGTGATGCCCCATTCCTTGACCGGCTTCGCCGAGA
String seq = trimHeader(Gene1.get(i));
for (int x = 0; x < Pal.size(); x++) {
if (seq.contains(Pal.get(x))) {
String match = Pal.get(x) + " in organism: " + Acc +
" Was found in position(s): " +
findSubstringIndexes(seq, Pal.get(x));
PalindromesSpotted.add(match);
}
}
}

// If there is nothing to work with get outta here.
if (PalindromesSpotted.isEmpty()) {
return PalindromesSpotted;
}

// Sort the ArrayList
Collections.sort(PalindromesSpotted);
// Another ArrayList for matching Pal's to Acc's
ArrayList<String> accMatchingPal = new ArrayList<>();
switch (resultType) {
case 0: // if resultType is 0 is supplied
writeListToFile("PB2Out.txt", PalindromesSpotted);
return PalindromesSpotted;

case 1: // if resultType is 1 is supplied
accMatchingPal = getPalMatches(PalindromesSpotted);
writeListToFile("PB2Out.txt", accMatchingPal);
return accMatchingPal;

default: // if resultType is 2 is supplied
accMatchingPal = getPalMatches(PalindromesSpotted);
ArrayList<String> fullList = new ArrayList<>();
fullList.addAll(PalindromesSpotted);
// Create a Underline made of = signs in the list.
fullList.add(String.join("", Collections.nCopies(70, "=")));
fullList.addAll(accMatchingPal);
writeListToFile("PB2Out.txt", fullList);
return fullList;
}
}

The findSubstringIndexes() Method:

private static String findSubstringIndexes(String inputString, String stringToFind){
String indexes = "";
int index = inputString.indexOf(stringToFind);
while (index >= 0){
indexes+= (indexes.equals("")) ? String.valueOf(index) : ", " + String.valueOf(index);
index = inputString.indexOf(stringToFind, index + stringToFind.length()) ;
}
return indexes;
}

The getPalMatches() Method:

private static ArrayList<String> getPalMatches(ArrayList<String> Palindromes) {
ArrayList<String> accMatching = new ArrayList<>();
for (int i = 0; i < Palindromes.size(); i++) {
String matches = "";
String[] split1 = Palindromes.get(i).split("\\s+");
String pal1 = split1[0];
// Make sure the current Pal hasn't already been listed.
boolean alreadyListed = false;
for (int there = 0; there < accMatching.size(); there++) {
String[] th = accMatching.get(there).split("\\s+");
if (th[0].equals(pal1)) {
alreadyListed = true;
break;
}
}
if (alreadyListed) { continue; }
for (int j = 0; j < Palindromes.size(); j++) {
String[] split2 = Palindromes.get(j).split("\\s+");
String pal2 = split2[0];
if (pal1.equals(pal2)) {
// Using Ternary Operator to build the matches string
matches+= (matches.equals("")) ? pal1 + " was found in the following Accessions: "
+ split2[3] : ", " + split2[3];
}
}
if (!matches.equals("")) {
accMatching.add(matches);
}
}
return accMatching;
}

The writeListToFile() Method:

private static void writeListToFile(String filePath, ArrayList<String> list, boolean... appendToFile) {
boolean appendFile = false;
if (appendToFile.length > 0) { appendFile = appendToFile[0]; }

try {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath, appendFile))) {
for (int i = 0; i < list.size(); i++) {
bw.append(list.get(i) + System.lineSeparator());
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}

关于Java 检查列表中的元素是否出现在所有事件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47849811/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com