我正在尝试计算 MAF 文件中的突变数量。我最初用 python 编写了这段代码,它工作得很好,但是当我将它翻译成 Java 时,它停止工作了。在输出文件中,突变数始终为 1。我在这里做错了什么?
package dev.suns.bioinformatics;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
public class Main {
static String filePath = "C:/Users/Matthew/Bioinformatics/Data Files/DLBC.maf";
static String fileName = "DLBC_Info.txt";
public static void main(String[] args){
createFile(filePath, fileName);
}
public static void createFile(String filePath, String fileName){
BufferedReader br = null;
String line = "";
String delimiter = "\t";
String geneSymbol = "";
String newGene = "";
int count;
try {
PrintWriter writer = new PrintWriter(fileName);
br = new BufferedReader(new FileReader(filePath));
writer.println("Gene" + "\t" + "Mutations" + "\n");
br.readLine();
while ((line = br.readLine()) != null){
String[] splitFile = line.split(delimiter);
newGene = splitFile[0];
if(geneSymbol == ""){
geneSymbol = newGene;
}
else if(newGene == geneSymbol){
#This is here I am having trouble. I have this if-statement to check if the gene appears more than once in the .maf file, but nothing is ever entering this.
count++;
}
else{
count++;
writer.println(geneSymbol + "\t" + count + "\n");
geneSymbol = newGene;
count=0;
}
}
writer.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
这是文件的前几行内容
基因突变
A1CF 1
A2M 1
A2M 1
A2ML1 1
A4GALT 1
AADAC 1
AADACL3 1
AAED1 1
AAGAB 1
AAGAB 1
AARD 1
AARS2 1
AARS2 1
AARS2 1
最佳答案
在java中,您需要使用equals函数来比较字符串。这应该有效-
else if(newGene.equals(geneSymbol)){
#This is here I am having trouble. I have this if-statement to check if the gene appears more than once in the .maf file, but nothing is ever entering this.
count++;
}
“==”检查引用。它们是否是相同的字符串对象。为了比较字符串的值,您需要使用 equals() 函数。
关于java - 计算 .maf 文件中发生突变的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37770736/