Printing to Excel File From Java Based on Columns - java

Apologies in advance for any formatting/other errors, I am still very much a newbie to Java.
I am conducting a gene expression analysis in which I have a program that prints ~ 6 million gene names and their expression values in 23,000 sets of 249 (there are 249 patients total and they each have 23,000 genes/gene expression values). Right now, I have this program looping through all the 249 individual patient files, obtaining the 23,000 gene values, and printing to a text file (with 6 million rows and 2 columns, one column for gene name and one for expression).
However, I would like this program to print to an excel file instead, so that there are 249 rows (for each patient) and 23,000 columns (for each gene). I have been trying for a couple of days to do this (with apache POI) and still am unable to. I found this example code: https://www.scientecheasy.com/2019/01/write-excel-file-in-java.html, which is what I have been trying to modify to fit my program, but nothing seems to be working. I have included my original program (that prints to the text file but also include the POI jars I downloaded). Any help would be MUCH appreciated!
import java.io.*;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateExcel {
public static final File folder = new File("C:/Users/menon/OneDrive/Documents/R/TARGET");
private static PrintStream output;
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet1 = wb.createSheet("values");
public static void main(String [] args) throws FileNotFoundException {
output = new PrintStream(new File("final_CSRSEF_data.txt"));
listFilesForFolder(folder);
}
public static double listFilesForFolder(final File folder) throws FileNotFoundException {
double value = 0.0;
//contains names of all the 23k genes in order to loop through the 249 files and collect the needed names each time
File list = new File("C:/Users/menon/OneDrive/Documents/NamesOfGenes.txt");
Scanner names = new Scanner(list);
String data;
while (names.hasNext()) {
String name = names.next();
//looping through all separate 249 patient files in folder and searching for gene name
for (final File fileEntry : folder.listFiles()) {
Scanner files = new Scanner(fileEntry);
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
while (files.hasNextLine()) {
final String lineFromFile = files.nextLine();
if(lineFromFile.contains(name)) {
//System.out.print(name+ " in file " + fileEntry.getName());
String[] thisOne = lineFromFile.split("\\s");
String res = thisOne[0];
//System.out.println(res);
if (res.equals(name)) {
print(lineFromFile);
print("\n");
}
}
}
}
}
print("----------------");
print("\n");
}
return 0.0;
}
//print to final_CSRSEF_data.txt
private static void print(String stat) {
output.print(stat);
}
}
So basically what I am printing before the "---------------" in each text file should instead be in a separate column (not row) in an excel sheet.
Once again, thank you in advance!

try look at this:
www.pela.it, in my home page there's a link to download "gene expression" tool
i did it in java two years ago and if it is what you are trying to do i'll be happy to help.
It elaborate an xml raw data output from pcr tool and finally print a paginated excel with all elaboration phases. There's a ppt too that explain in detail.

Related

Java.lang.NumberFormatException when trying to read in a file, so I can use the data with my objects

I keep getting this error when I'm trying to read in a file and add the data to my objects. Can someone please help me? Here is the error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "N64"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:668)
at java.base/java.lang.Integer.parseInt(Integer.java:786)
at FinalProjectTests.main(FinalProjectTests.java:34)
Here are my objects. Also at the bottom I have setters and getters below the code as well as a toString() method generated by Eclipse:
Here is my code that is trying to read in a file and add the data to my objects:
public class FinalProjectTests {
//create an arraylist so the objects can be used through them
public static ArrayList<FinalProjectRecord> record = new ArrayList<FinalProjectRecord>();
public static void main(String[] args) {
Scanner input = null;
try {
input = new Scanner(new File("Video Games Sales.csv"));
} catch (FileNotFoundException e) {
System.out.println("file not found");
e.printStackTrace();
}//end try
//skips the title columns in our data file
input.nextLine();
while(input.hasNext()) {
String [] columns = input.nextLine().split(",");
record.add(new FinalProjectRecord(Integer.parseInt(columns[0]),
columns[1],
columns[2],
Integer.parseInt(columns[3]),
columns[4],
columns[5],
Double.parseDouble(columns[6]),
Double.parseDouble(columns[7]),
Double.parseDouble(columns[8]),
Double.parseDouble(columns[9]),
Double.parseDouble(columns[10]),
Double.parseDouble(columns[11])));
}//end while
for(FinalProjectRecord r: record)
System.out.println(r);
}//end main
}//end class
Here is a snippet of the data I am trying to read in:
Edit: I got the data from data.world.
Exception in thread "main" java.lang.NumberFormatException: For input string: "N64"
Exception is thrown because of "N64".(Integer.ParseInt or Double.ParseDouble in your code) The image you have posted as data, appears its is from EXCEL Sheet, if so, please search for "N64" and check whether it is correct in that column?
You should really use a library for parsing csv files, this is an example using commons-csv
Here is my hastily made csv file.
'object','value'
'red', 255
'blue', 100
'green, yellow', 50
This simple program will parse it.
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVFormat;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
public class CsvReading{
public static void main(String[] args) throws Exception{
CSVParser parsing = CSVParser.parse(
Paths.get("junk.csv"),
StandardCharsets.UTF_8,
CSVFormat.DEFAULT
);
System.out.println(parsing.getHeaderNames());
System.out.println(parsing.getRecords());
}
}
That will give an output with no headers, and 4 records.
[]
[CSVRecord [comment='null', recordNumber=1, values=['object', 'value']], CSVRecord [comment='null', recordNumber=2, values=['red', 255]], CSVRecord [comment='null', recordNumber=3, values=['blue', 100]], CSVRecord [comment='null', recordNumber=4, values=['green, yellow', 50]]]
You can see the last record has two elements, 'green, yellow' and 50. If we were to use String.split(",") then yellow and green would be incorrectly split. This library is very easy to use, I just downloaded the jar file and run the program with.
java -cp commons-csv-1.9.0.jar CsvReading.java

Extract data from one ArrayList in one java file and show it in different java file in an ArrayList

I have one java file named Employee.java.i have to read a CSV file where the employee details are stored i.e;
(Employee_ID, Employee_Name, Employee_Designation, Employee_Mobile, Employee_Year_Of_Joining).
i have to convert it into an ArrayList.
Another file Project.java where i have to read another CSV file where the project details are stored i.e;
(Project_ID, Project_Name, Project_Description, Project_Start_Date, Project_End_Date, Employee_ID).
i have to convert this also into an ArrayList.
Now i have to send the Employee_ID and extract the Employee_Name and Employee_Designation and the output is-
OutPut
Project_Name
Project_Description
Employee_Name
Employee_Designation
I presume you are learning Java, do writing all the code for you would be a disservice. And Stack Overflow is not intended to be a “write my code for me” service.
So here is a narrative to walk you through the steps to take in writing your code. Tip: Learn to use these four steps to programming: write requirements as you did in the Question, write a plain language description to walk through the logic, as I did here, then write pseudo-code without worrying about the exact syntax of any particular programming language so you can work out the nuances and details, and finally write your code.
Write classes (or not)
If you will be doing further work with this data, write a class for each type, an Employee class and a Project class. Then read each input file, parsing each row to instantiate an object of the class you just wrote.
If not doing further work, you could skip writing the classes. In my description of looping below, substitute a row of input data for where I say Employee or Project object.
Tip: Use any of the several libraries for reading and parsing the CSV files. For example, Apache Commons CSV.
Nested loop
How would you do this if you were a clerk rather than a programmer, with two written lists and a typewriter in front of you? The same logic applies here in programming.
Write a loop, going through the Project list.
For each Project element, get the project’s employee ID number.
Write a second loop, going through the Employee list. Extract from each Employee object the employee ID number. Compare this employee ID number to the employee ID number we have in hand for the project. If they match, write your result. And if they match, you can break out of this inner loop as there is no reason to continue examining further employee objects. If you exhaust the employee list without a match, you have an error condition to resolve however you see fit.
Maybe I have a code to help you. it reads a excel and save an arraylist. you should only duplicate to code to read the second csv
read csv java
the code is the next, maybe you should add the neccesary library to run its. You find the library that i used at pom
package com.examen.excel;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
/**
*
* #author wilso
*/
public class Excel {
public static void main(String[] args) {
try {
ArrayList<String> dta = new ArrayList<>();
try (Workbook workbook = WorkbookFactory.create(new File("C:\\Users\\wilso\\OneDrive\\Documents\\NetBeansProjects\\Excel\\src\\resources/Employment.xlsx"))) {
Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();
StringBuilder result;
while (iterator.hasNext()) {
result = new StringBuilder();
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
result.append(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
result.append(cell.getNumericCellValue());
break;
}
result.append(":");
}
dta.add(result.toString());
}
System.out.println("Resultado");
System.out.println(dta);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
The second option is the next
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.generic;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
/**
*
* #author wilso
*/
public class prueba {
public static void main(String[] args) {
try {
ArrayList<String> data = new ArrayList<>();
BufferedReader bufferedReader = new BufferedReader(new FileReader(new File("C:\\Users\\wilso\\Desktop/PRUEBA.csv")));
String line;
while ((line = bufferedReader.readLine()) != null) {
data.add(line);
}
//Read the second file and do matching
} catch (Exception e) {
e.printStackTrace();
}
}
}

Reading a CSV file into a 2d Array

I've been trying for days to learn how to create this code. It's a homework example. Beginner Java Final Project. I'm about to rip out my hair, if you could guide me a little, I'd appreciate it. I can't seem to figure out how to parse the csv file into a proper 2d array. The delimiter is a ",". I need to maniuplate one column of data (such as the year), but ignore the first (0,0), (0,1), (0,2) row as it only carries the labels I believe. I'm so lost. What I have prints out the first column, but how would I ignore the label at (0,0), and how would I store this information so I could manipulate it in a method? I don't need help on most of the assignment except how to read the values properly and then be able to manipulate them. Thank you.
import java.util.*;
import java.io.*;
public class BasicFileIO{
public static void main (String[] args) {
String fileName = "Crime.csv";
File file = new File(fileName);
try {
Scanner input = new Scanner(file);
while (input.hasNext()) {
String data = input.nextLine();
String[] values = data.split(",");
System.out.println(values[0]);
}
}catch (Exception e) {
System.out.printf("Error");
}
}
}
HERE IS AN IMAGE OF THE CSV FILE. I couldn't upload it. This is how it looks in google docs, but if I open it in atom it's just a file with commas and values (not in cells).
CSV screenshot
You can read and parse scv files with apache commons-csv. Here's an example for reading the columns with this library:
Reader in = new FileReader("path/to/file.csv");
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
for (CSVRecord record : records) {
String lastName = record.get("Last Name"); // or you can pass the index of column
String firstName = record.get("First Name");
}

Apache Library to Standard Java

I have a problem. I wrote this code that reads a string from a txt file and I exported with the first method a int while the second one particular string. This method is already running but I have used the apache library, now I wanted to rewrite it in Java standard libraries. I have tried this, but I have had problems. Could someone help me? Thank you very much.
package ausiliare;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.*;
public class Read {
public static int getInt() throws IOException {
String content = null;
File folder = new File("C:\\Solution.txt");
content = FileUtils.readFileToString(folder) + "\n";
int outside = Integer.parseInt(content.substring(0,
content.indexOf("[")).trim());
return outside;
}
public static String getString() throws IOException {
String content = null;
File folder = new File("C:\\Solution.txt");
content = FileUtils.readFileToString(folder) + "\n";
String remainingString = content.substring(content.indexOf(" ["),
content.lastIndexOf("]") + 1);
// System.out.println(remainingString);
return remainingString;
}
public static String[] arg() throws IOException {
String[] strArray = getString().split(" ");
// System.out.println(Arrays.toString(strArray));
return strArray;
}
}
Ps: The input file is txt (for example):
50 [8,24,-22] [-8,34,12] [19,14,47] [-49,32,44] [-41,16,-6] [-49,-11,43]
Where the first method extracts the int 50 and the second extraction method extracts the remaining
content = new String(Files.readAllBytes(folder.toPath()),
StandardCharsets.UTF_8);
The missing part is the knowledge of the Files class.
There is a List<String> readAllLines too.
The character set parameter is optional and defaults to the current operating system's encoding - not very portable to other computers.

'Un'-externalize strings from Eclipse or Intellij

I have a bunch of strings in a properties file which i want to 'un-externalize', ie inline into my code.
I see that both Eclipse and Intellij have great support to 'externalize' strings from within code, however do any of them support inlining strings from a properties file back into code?
For example if I have code like -
My.java
System.out.println(myResourceBundle.getString("key"));
My.properties
key=a whole bunch of text
I want my java code to be replaced as -
My.java
System.out.println("a whole bunch of text");
I wrote a simple java program that you can use to do this.
Dexternalize.java
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Deexternalize {
public static final Logger logger = Logger.getLogger(Deexternalize.class.toString());
public static void main(String[] args) throws IOException {
if(args.length != 2) {
System.out.println("Deexternalize props_file java_file_to_create");
return;
}
Properties defaultProps = new Properties();
FileInputStream in = new FileInputStream(args[0]);
defaultProps.load(in);
in.close();
File javaFile = new File(args[1]);
List<String> data = process(defaultProps,javaFile);
buildFile(javaFile,data);
}
public static List<String> process(Properties propsFile, File javaFile) {
List<String> data = new ArrayList<String>();
Set<Entry<Object,Object>> setOfProps = propsFile.entrySet();
int indexOf = javaFile.getName().indexOf(".");
String javaClassName = javaFile.getName().substring(0,indexOf);
data.add("public class " + javaClassName + " {\n");
StringBuilder sb = null;
// for some reason it's adding them in reverse order so putting htem on a stack
Stack<String> aStack = new Stack<String>();
for(Entry<Object,Object> anEntry : setOfProps) {
sb = new StringBuilder("\tpublic static final String ");
sb.append(anEntry.getKey().toString());
sb.append(" = \"");
sb.append(anEntry.getValue().toString());
sb.append("\";\n");
aStack.push(sb.toString());
}
while(!aStack.empty()) {
data.add(aStack.pop());
}
if(sb != null) {
data.add("}");
}
return data;
}
public static final void buildFile(File fileToBuild, List<String> lines) {
BufferedWriter theWriter = null;
try {
// Check to make sure if the file exists already.
if(!fileToBuild.exists()) {
fileToBuild.createNewFile();
}
theWriter = new BufferedWriter(new FileWriter(fileToBuild));
// Write the lines to the file.
for(String theLine : lines) {
// DO NOT ADD windows carriage return.
if(theLine.endsWith("\r\n")){
theWriter.write(theLine.substring(0, theLine.length()-2));
theWriter.write("\n");
} else if(theLine.endsWith("\n")) {
// This case is UNIX format already since we checked for
// the carriage return already.
theWriter.write(theLine);
} else {
theWriter.write(theLine);
theWriter.write("\n");
}
}
} catch(IOException ex) {
logger.log(Level.SEVERE, null, ex);
} finally {
try {
theWriter.close();
} catch(IOException ex) {
logger.log(Level.SEVERE, null, ex);
}
}
}
}
Basically, all you need to do is call this java program with the location of the property file and the name of the java file you want to create that will contain the properties.
For instance this property file:
test.properties
TEST_1=test test test
TEST_2=test 2456
TEST_3=123456
will become:
java_test.java
public class java_test {
public static final String TEST_1 = "test test test";
public static final String TEST_2 = "test 2456";
public static final String TEST_3 = "123456";
}
Hope this is what you need!
EDIT:
I understand what you requested now. You can use my code to do what you want if you sprinkle a bit of regex magic. Lets say you have the java_test file from above. Copy the inlined properties into the file you want to replace the myResourceBundle code with.
For example,
TestFile.java
public class TestFile {
public static final String TEST_1 = "test test test";
public static final String TEST_2 = "test 2456";
public static final String TEST_3 = "123456";
public static void regexTest() {
System.out.println(myResourceBundle.getString("TEST_1"));
System.out.println(myResourceBundle.getString("TEST_1"));
System.out.println(myResourceBundle.getString("TEST_3"));
}
}
Ok, now if you are using eclipse (any modern IDE should be able to do this) go to the Edit Menu -> Find/Replace. In the window, you should see a "Regular Expressions" checkbox, check that. Now input the following text into the Find text area:
myResourceBundle\.getString\(\"(.+)\"\)
And the back reference
\1
into the replace.
Now click "Replace all" and voila! The code should have been inlined to your needs.
Now TestFile.java will become:
TestFile.java
public class TestFile {
public static final String TEST_1 = "test test test";
public static final String TEST_2 = "test 2456";
public static final String TEST_3 = "123456";
public static void regexTest() {
System.out.println(TEST_1);
System.out.println(TEST_1);
System.out.println(TEST_3);
}
}
You may use Eclipse "Externalize Strings" widget. It can also be used for un-externalization. Select required string(s) and press "Internalize" button. If the string was externalized before, it'll be put back and removed from messages.properties file.
May be if you can explain on how you need to do this, then you could get the correct answer.
The Short answer to your question is no, especially in Intellij (I do not know enough about eclipse). Of course the slightly longer but still not very useful answer is to write a plugin. ( That will take a list of property files and read the key and values in a map and then does a regular expression replace of ResourceBundle.getValue("Key") with the value from Map (for the key). I will write this plugin myself, if you can convince me that, there are more people like you, who have this requirement.)
The more elaborate answer is this.
1_ First I will re-factor all the code that performs property file reading to a single class (or module called PropertyFileReader).
2_ I will create a property file reader module, that iterates across all the keys in property file(s) and then stores those information in a map.
4_ I can either create a static map objects with the populated values or create a constant class out of it. Then I will replace the logic in the property file reader module to use a get on the map or static class rather than the property file reading.
5_ Once I am sure that the application performs ok.(By checking if all my Unit Testing passes), then I will remove my property files.
Note: If you are using spring, then there is a easy way to split out all property key-value pairs from a list of property files. Let me know if you use spring.
I would recommend something else: split externalized strings into localizable and non-localizable properties files. It would be probably easier to move some strings to another file than moving it back to source code (which will hurt maintainability by the way).
Of course you can write simple (to some extent) Perl (or whatever) script which will search for calls to resource bundles and introduce constant in this place...
In other words, I haven't heard about de-externalizing mechanism, you need to do it by hand (or write some automated script yourself).
An awesome oneliner from #potong sed 's|^\([^=]*\)=\(.*\)|s#Messages.getString("\1")#"\2"#g|;s/\\/\\\\/g' messages.properties |
sed -i -f - *.java run this inside your src dir, and see the magic.

Categories

Resources