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

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

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.

Related

Move First Word to end of String Java

move keywords flagstop, wb, nb, sb, eb from start of the names to the end of the names
of the stops when reading the file
eg “WB HASTINGS ST FS HOLDOM AVE” becomes “HASTINGS ST FS HOLDOM AVE WB”.
So far I have gotten the code to read in the file as shown below, but im unsure how to move keyword of this string in array to end of string given the criteria shown above.
I need to be able to move first word in stops[2] if it is any of the above postcodes. Is there a way in JAVA to do this? Im thinking I need to make separate a function (moveFirstToLast) and call this in the main, but im having difficulty with this.
'''
String line = "";
try {
#SuppressWarnings("resource")
BufferedReader x = new BufferedReader(new FileReader("stops.txt"));
while((line = x.readLine()) != null)
{
String[] stops = line.split(",");
System.out.println("Stop name:" + stops[2]);
}
}
'''
Using a regex replacement:
String input = "WB HASTINGS ST FS HOLDOM AVE";
String output = input.replaceAll("(\\S+) (.*)$", "$2 $1");
System.out.println(output); // HASTINGS ST FS HOLDOM AVE WB
You can use String#split() to separate the first word from the rest of the string.
public moveFirstToLast(String stopName){
String [] temp = stopName.split(" ", 2);
return temp[1] + " " + temp[0];
}
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Sample_Two {
public static void main(String[] args) {
String line = "";
try {
#SuppressWarnings("resource")
BufferedReader x = new BufferedReader(new FileReader("stops.txt"));
while ((line = x.readLine()) != null) {
System.out.println("Actual line===>" + line);
var arr = line.split("\\s");
if (arr != null && arr.length > 0) {
System.out.println("Updated line===>" + line.substring(line.indexOf(" ")) +" "+ arr[0]);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
You can try above code.
Why you are splitting through comma ",".
You need to split through space.
This will help you.

Using formulas from a text/writing to a text

So, my lecture powerpoint slides and even my book is not really doing a good job (for my understanding that is) of explaining how to use formulas from a text document, then when the code runs/compiles successfully it will create a "Results.txt" in the same folder.
These are the formulas in a notepad doc. Nothing to crazy, just a proof of concept
4 * 5 ..
3 / 4...
3 - 1..
2 + 3..
import java.io.*;
import java.util.*;
public class ReadFileLineByLine {
public static void main(String[] args) throws FileNotFoundException {
String line;
int numberOfLines = 3;
String[] textData = new String[numberOfLines];
int i;
for(i = 0; i < numberOfLines; i++){
textData[i] = textReader.readLine();
}
text.Reader.close();
return textData;
try {
File inputfile = new File(args[0]); //new File("formulas.txt")
Scanner input = new Scanner(new File("C:\Users\Frost\Documents\Question4"));
BuffredReader br = new BufferedReader(new FileReader("C:\Users\Frost\Documents\Question4"));
PrintWriter output = new PrintWriter("Results.txt");
while (input.hasNextLine()) {
line = input.nextLine();
System.out.println("read <" + line + ">"); // Display message to commandline
// Declare ArrayList of for storing tokenized formula from String line
double result = 0; // The variable to store result of the operation
// Determine the operator and calculate value of the result
System.out.println(formula.get(0) + ' ' + formula.get(1) + ' ' +
formula.get(2) + " = " + result); // Display result to command line
// Write result to file
}
// Need to close input and output files
}
catch (FileNotFoundException e) {
System.out.println("Error reading file named " + Formulas.txt);
}
}
}
Here's something to get you started. The //TODO: comments are where you need to build your logic. Be sure to change the file paths back to what you need. I changed them to a Temp location. Also change the messages printed as I just put something there as proof of concept. I tried to comment thoroughly but don't hesitate to ask questions.
import java.io.*;
import java.util.*;
public class ReadFileLineByLine {
public static void main(String[] args) throws FileNotFoundException {
String line = "";
//Declare Scanner and PrintWriter outside of try clause so they can be closed in finally clause
Scanner input = null;
PrintWriter output = null;
try {
//Instantiate input and output file
input = new Scanner(new File("C:\\Temp\\test.txt"));
output = new PrintWriter(new File("C:\\Temp\\Results.txt"));
//Loop through lines in input file
while (input.hasNextLine()) {
line = input.nextLine();
// Display message to commandline
System.out.println("read <" + line + ">");
// Populate ArrayList of tokenized formula from String line
//TODO:
// The variable to store result of the operation
double result = 0;
// Determine the operator and calculate value of the result
//TODO:
// Write result to file
output.println("Print result of " + line + " to Results.txt");
}
} catch (FileNotFoundException e) {
//Exception thrown, print message to console
System.out.println("File Not Found: " + e.getMessage());
} finally {
//close files in finally clause so it happens even if exception is thrown
//I also set to null as extra precaution
input.close();
input = null;
output.close();
output = null;
}
}
}

Java - Not able to print all the Output in a text file and getting array out of bound exception

package Test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
public class TestCode {
public static void main( String[] args ) throws IOException {
//Getting files from the folder
File dir = new File("C:\\Users\\Dell\\Desktop\\testing\\");
for (File file : dir.listFiles()) {
HashMap<String, String> map = new HashMap<>();
String line;
BufferedReader reader = new BufferedReader(new FileReader(file));
while ((line = reader.readLine()) != null) {
if(line.length() > 0) {
String[] parts = line.split(",", 28);
//System.out.println("LENGTH:" +parts.length);
if (parts.length >=0) {
//Concatenating FirstName and LastName
String key = parts[2] + "_" + parts[4];
map.put(key, line);
String a= args[0];
if (key.equals(a)) {
System.out.println(key + ":" + map.get(key));
String b = key + "," + map.get(key);
String text = b;
BufferedWriter output = null;
// Trying to print all the outputs to a different file
try {
File file1 = new File("C:\\Users\\Dell\\Desktop\\Output\\output.txt");
output = new BufferedWriter(new FileWriter(file1));
output.write(text);
} catch ( IOException e ) {
e.printStackTrace();
} finally {
if ( output != null ) {
output.close();
}
}
}
}
}
}
reader.close();
}
}
}
My requirement is to read files from a folder and search for a particular user (Firstname_LastName) in all the files and print the Output to .txt file.
In the above code am getting array out of bound exception in line String key = parts[2] + "_" + parts[4]; when i use a file containing very large number of users and it works fine for less number of users and also am missing something because only one file output is stored at the output file. Please help me out with this. Thank you in advance.
String key = parts[2] + "_" + parts[4];
This means, you are expecting the parts array length will be always at least 5. Make sure the length is not less than 5.
if (parts.length >= 5) {
String key = parts[2] + "_" + parts[4];
...
Check the parts array size greater than or not.
String[] parts = line.split(",", 28);
Because if one line does't have 4 comma separated values it will break the loop.

Unidentified symbol

Given the following:
import java.io.*;
public class WriteRead {
public void writeToFile(String filename) throws IOException {
FileWriter fw = new FileWriter(filename);
fw.write("testing");
fw.close();
}
public String readFromFile(String filename) throws IOException {
String str = "";
int characterInt = 0;
FileReader fr = new FileReader(filename);
while (characterInt != -1) {
characterInt = fr.read();
str += "" + (char) characterInt;
}
fr.close();
return str;
}
public static void main(String[] args) throws IOException {
WriteRead wR = new WriteRead();
wR.writeToFile("test.java");
System.out.println(wR.readFromFile("test.java"));
}
}
when I run the program it prints the following:
What is the symbol at the end of "testing" and what part of the program causes it to be there?
Use the following code-
while ((characterInt = fr.read()) != -1) {
str += "" + (char) characterInt;
}
since in last iteration when this reads -1 this appends ? in str so please check before adding.and as far as your display of testing0 is concern give me hexcode of this first.
Make the changes as below:
while ((characterInt = fr.read()) != -1) {
//characterInt = fr.read();
str += "" + (char) characterInt;
}
Read the char and compare it with -1 as EOF. You were appending the end of file char in string and then while loop condition was getting failed.
You are setting characterInt = fr.read(); inside your whileloop. You should set this either outside whileloop or in while()condition.
So as per your code, it will run for second iteration also because in second iteration, your characterInt is not equal to -1 (it is holding earlier value i.e. testing).

Error while counting number of character,lines and words in java

i have written the following code to count the number of character excluding white spaces,count number of words,count number of lines.But my code is not showing proper output.
import java.io.*;
class FileCount
{
public static void main(String args[]) throws Exception
{
FileInputStream file=new FileInputStream("sample.txt");
BufferedReader br=new BufferedReader(new InputStreamReader(file));
int i;
int countw=0,countl=0,countc=0;
do
{
i=br.read();
if((char)i==(' '))
countw++;
else if((char)i==('\n'))
countl++;
else
countc++;
}while(i!=-1);
System.out.println("Number of words:"+countw);
System.out.println("Number of lines:"+countl);
System.out.println("Number of characters:"+countc);
}
}
my file sample.txt has
hi my name is john
hey whts up
and my out put is
Number of words:6
Number of lines:2
Number of characters:26
You need to discard other whitespace characters as well including repeats, if any. A split around \\s+ gives you words separated by not only all whitespace characters but also any appearance of those characters in succession.
Having got a list of all words in the line it gets easier to update the count of words and characters using length methods of array and String.
Something like this will give you the result:
String line = null;
String[] words = null;
while ((line = br.readLine()) != null) {
countl++;
words = line.split("\\s+");
countw += words.length;
for (String word : words) {
countc += word.length();
}
}
A new line means also that the words ends.
=> There is not always a ' ' after each word.
do
{
i=br.read();
if((char)i==(' '))
countw++;
else if((char)i==('\n')){
countl++;
countw++; // new line means also end of word
}
else
countc++;
}while(i!=-1);
End of file should also increase the number of words (if no ' ' of '\n' was the last character.
Also handling of more than one space between words is still not handled correctly.
=> You should think about more changes in your approach to handle this.
import java.io.*;
class FileCount {
public static void main(String args[]) throws Exception {
FileInputStream file = new FileInputStream("sample.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(file));
int i;
int countw = 0, countl = 0, countc = 0;
do {
i = br.read();
if ((char) i == (' ')) { // You should also check for other delimiters, such as tabs, etc.
countw++;
}
if ((char) i == ('\n')) { // This is for linux Windows should be different
countw++; // Newlines also delimit words
countl++;
} // Removed else. Newlines and spaces are also characters
if (i != -1) {
countc++; // Don't count EOF as character
}
} while (i != -1);
System.out.println("Number of words " + countw);
System.out.println("Number of lines " + countl); // Print lines instead of words
System.out.println("Number of characters " + countc);
}
}
Ouput:
Number of words 8
Number of lines 2
Number of characters 31
Validation
$ wc sample.txt
2 8 31 sample.txt
Try this:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class FileCount {
/**
*
* #param filename
* #return three-dimensional int array. Index 0 is number of lines
* index 1 is number of words, index 2 is number of characters
* (excluding newlines)
*/
public static int[] getStats(String filename) throws IOException {
FileInputStream file = new FileInputStream(filename);
BufferedReader br = new BufferedReader(new InputStreamReader(file));
int[] stats = new int[3];
String line;
while ((line = br.readLine()) != null) {
stats[0]++;
stats[1] += line.split(" ").length;
stats[2] += line.length();
}
return stats;
}
public static void main(String[] args) {
int[] stats = new int[3];
try {
stats = getStats("sample.txt");
} catch (IOException e) {
System.err.println(e.toString());
}
System.out.println("Number of words:" + stats[1]);
System.out.println("Number of lines:" + stats[0]);
System.out.println("Number of characters:" + stats[2]);
}
}

Categories