Matthew N. Matthew N. - 3 months ago 21
Java Question

Counting amount of times a mutation occurs in a .maf file

I'm trying to count the number of mutations in a MAF file. I originally wrote this code in python and it worked perfectly fine, but when I translated it to Java it stopped working. In the output file the number of mutations is always one. What am I doing wrong here?

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();
}
}
}


Here is what the first few lines of the file look like

Gene Mutations

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

Answer

In java you need to compare strings using equals function. This should work-

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++;
        }

"==" checks for the reference. whether they are same string objects. In order to compare values of string you need to use equals() function.