Java Code Examples for com.opencsv.CSVReader#close()
The following examples show how to use
com.opencsv.CSVReader#close() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CsvReaderExamples.java From tutorials with MIT License | 6 votes |
public static List<String[]> readAll(Reader reader) { CSVParser parser = new CSVParserBuilder() .withSeparator(',') .withIgnoreQuotations(true) .build(); CSVReader csvReader = new CSVReaderBuilder(reader) .withSkipLines(0) .withCSVParser(parser) .build(); List<String[]> list = new ArrayList<>(); try { list = csvReader.readAll(); reader.close(); csvReader.close(); } catch (Exception ex) { Helpers.err(ex); } return list; }
Example 2
Source File: CsvDataProvider.java From NoraUi with GNU Affero General Public License v3.0 | 6 votes |
private void initColumns() throws EmptyDataFileContentException, WrongDataFileFormatException, IOException { columns = new ArrayList<>(); final CSVReader reader = openInputData(); final String[] headers = reader.readNext(); for (final String header : headers) { if (!"".equals(header)) { columns.add(header); } } reader.close(); if (columns.size() < 2) { throw new EmptyDataFileContentException(Messages.getMessage(EmptyDataFileContentException.EMPTY_DATA_FILE_CONTENT_ERROR_MESSAGE)); } resultColumnName = columns.get(columns.size() - 1); if (!isResultColumnNameAuthorized(resultColumnName)) { throw new WrongDataFileFormatException(String.format(Messages.getMessage(WrongDataFileFormatException.WRONG_RESULT_COLUMN_NAME_ERROR_MESSAGE), ResultColumnNames.getAuthorizedNames())); } }
Example 3
Source File: CSVConnector.java From TAcharting with GNU Lesser General Public License v2.1 | 6 votes |
/** * Reads a csv file with structure of yahoo api: No info line with name and timeFormatId, just header line and * {@link TimeFormatType timeFormat YAHOO} * @param name the name of this symbol * @param file the csv file with financial data in yahoo format * @return the corresponding TimeSeries object * @throws IOException IOException */ public TaBarSeries getSeriesFromYahooFile(String name, File file) throws IOException{ CSVReader reader = new CSVReaderBuilder(new FileReader(file)).withCSVParser(new CSVParser()).build(); String line[]; line = reader.readNext(); Map<Parameter.Columns, Integer> headers = FormatUtils.getHeaderMap(Arrays.asList(line)); List<Bar> Bars = new ArrayList<>(); while((line = reader.readNext()) != null) { Bars.add(FormatUtils.extractOHLCData( headers, DateTimeFormatter.ofPattern(TimeFormatType.YAHOO.pattern),line,false)); } reader.close(); if(Bars.get(Bars.size()-1).getEndTime().isBefore(Bars.get(0).getEndTime())){ Collections.reverse(Bars); } String yahooIntervall = YahooSettingsManager.getProperties().getProperty(Parameter.PROPERTY_YAHOO_INTERVAL); GeneralTimePeriod timePeriod = YahooTimePeriod.of(yahooIntervall).generalTimePeriod; return new TaBarSeries(name==null?"unnamed":name.toUpperCase(),Bars,Currency.getInstance("USD"),timePeriod); }
Example 4
Source File: CsvDecoder.java From metafacture-core with Apache License 2.0 | 5 votes |
private String[] parseCsv(final String string) { String[] parts = new String[0]; try { final CSVReader reader = new CSVReader(new StringReader(string), separator); final List<String[]> lines = reader.readAll(); if (lines.size() > 0) { parts = lines.get(0); } reader.close(); } catch (IOException e) { e.printStackTrace(); } return parts; }
Example 5
Source File: SearchTestWithCSVDataProvider.java From Selenium-WebDriver-3-Practical-Guide-Second-Edition with MIT License | 5 votes |
@DataProvider(name = "searchWords") public Iterator<Object[]> provider() throws Exception { CSVReader reader = new CSVReader( new FileReader("./src/test/resources/data/data.csv") , ',', '\'', 1); List<Object[]> myEntries = new ArrayList<Object[]>(); String[] nextLine; while ((nextLine = reader.readNext()) != null) { myEntries.add(nextLine); } reader.close(); return myEntries.iterator(); }
Example 6
Source File: CSVFileParser.java From development with Apache License 2.0 | 5 votes |
public CSVFileParser(InputStream stream, String encoding, char delimiter, final String mandatory_columns[], final String mandatory_column_values[]) throws Exception { this.lines = new HashSet<String>(); this.mandatory_cols = mandatory_column_values; this.data = new LinkedHashMap<String, String>(); reader = new CSVReader(new InputStreamReader(stream, encoding), delimiter); try { mappings = reader.readNext(); if (mappings == null) { throw new IOException("Empty CSV file given."); } line = 1; List<String> checkCols = Arrays.asList(mappings); for (String reqCol : mandatory_columns) { if (!checkCols.contains(reqCol)) { throw new Exception( "Missing mandatory column '" + reqCol + "'."); } } } catch (Exception e) { reader.close(); throw e; } }
Example 7
Source File: CsvParser.java From openmrs-module-initializer with MIT License | 5 votes |
/** * @param is The input stream for the CSV data. * @return The header line. * @throws IOException */ public static String[] getHeaderLine(InputStream is) throws IOException { CSVReader reader = new CSVReader(new InputStreamReader(is, StandardCharsets.UTF_8)); String[] headerLine = reader.readNext(); reader.close(); return headerLine; }
Example 8
Source File: FileUtils.java From tutorials with MIT License | 5 votes |
public void closeReader() { try { CSVReader.close(); fileReader.close(); } catch (IOException e) { logger.error("Error while closing reader."); } }
Example 9
Source File: SparseDataSet.java From hlta with GNU General Public License v3.0 | 5 votes |
/** * Converts the csv input data , where rows represent datacase and columns represent variables, to * the form of tuples. For Example in the original dataset if each datacase is a documnet, then we would * have tuples like (doc,word1),(doc(word2)... * Here word1 represents the name of the corresponding variable in the .csv file * We then write this converted dataset to a file for future use. * @param DataSetNameCsv .csv datafile name * @param OutputDataSetPath the path where the converted input data format will be saved * @throws IOException */ public static void convertCSVtoTuples(String DataSetNameCsv, String OutputDataSetPath) throws IOException{ PrintWriter out = new PrintWriter(OutputDataSetPath+File.separator+"SparseDataInputFormat.txt"); // get the reader, split char = , and quotation char = " // We start from line 0 CSVReader reader = new CSVReader(new FileReader(DataSetNameCsv), ',', '"', 0); Iterator<String[]> iter = reader.iterator(); //Line 0 should contain the variable names so read them first String[] varName = iter.next(); int row_id = 1; while(iter.hasNext()){ String[] datacase = iter.next(); for(int i = 0 ; i < datacase.length ; i++){ // For each datacase get the variables which are 1 if(Integer.parseInt(datacase[i]) == 1){ out.println(Integer.toString(row_id)+","+varName[i]);// write a (doc,varName) tuple } } row_id++; } out.close(); reader.close(); }
Example 10
Source File: DataCollectorGui.java From burp_data_collector with Apache License 2.0 | 5 votes |
public String getFileHead(String fileName) throws IOException, CsvValidationException { String head = ""; FileReader fileReader = new FileReader(new File(fileName)); CSVReader csvReader = new CSVReader(fileReader); String[] line = csvReader.readNext(); if (line != null) { head = line[0]; } csvReader.close(); return head; }
Example 11
Source File: OpenCSVTest.java From tarql with BSD 2-Clause "Simplified" License | 5 votes |
@Test public void test() throws IOException { CSVReader r = new CSVReader(new StringReader("a,b\n1,2\n3,4")); try { assertArrayEquals(new String[]{"a","b"}, r.readNext()); assertArrayEquals(new String[]{"1","2"}, r.readNext()); assertArrayEquals(new String[]{"3","4"}, r.readNext()); assertNull(r.readNext()); } finally { r.close(); } }
Example 12
Source File: GtfsFeedImpl.java From pt2matsim with GNU General Public License v2.0 | 5 votes |
/** * Loads shapes (if available) and puts them in {@link #shapes}. A shape is a sequence of points, i.e. a line. * <p/> * <br/><br/> * shapes.txt <i>[https://developers.google.com/transit/gtfs/reference]</i><br/> * Rules for drawing lines on a map to represent a transit organization's routes. */ protected void loadShapes() { // shapes are optional log.info("Looking for shapes.txt"); int l = 1; try { CSVReader reader = createCSVReader(root + GtfsDefinitions.Files.SHAPES.fileName); String[] header = reader.readNext(); Map<String, Integer> col = getIndices(header, GtfsDefinitions.Files.SHAPES.columns, GtfsDefinitions.Files.SHAPES.optionalColumns); String[] line = reader.readNext(); while(line != null) { l++; usesShapes = true; // shape file might exists but could be empty Id<RouteShape> shapeId = Id.create(line[col.get(GtfsDefinitions.SHAPE_ID)], RouteShape.class); RouteShape currentShape = shapes.get(shapeId); if(currentShape == null) { currentShape = new GtfsShape(line[col.get(GtfsDefinitions.SHAPE_ID)]); shapes.put(shapeId, currentShape); } Coord point = new Coord(Double.parseDouble(line[col.get(GtfsDefinitions.SHAPE_PT_LON)]), Double.parseDouble(line[col.get(GtfsDefinitions.SHAPE_PT_LAT)])); currentShape.addPoint(point, Integer.parseInt(line[col.get(GtfsDefinitions.SHAPE_PT_SEQUENCE)])); line = reader.readNext(); } reader.close(); log.info("... shapes.txt loaded"); } catch (IOException e) { log.info("... no shapes file found."); } catch (ArrayIndexOutOfBoundsException i) { throw new RuntimeException("Line " + l + " in shapes.txt is empty or malformed."); } }
Example 13
Source File: GtfsFeedImpl.java From pt2matsim with GNU General Public License v2.0 | 5 votes |
/** * Basically just reads all routeIds and their corresponding names and types and puts them in {@link #routes}. * <p/> * <br/><br/> * routes.txt <i>[https://developers.google.com/transit/gtfs/reference]</i><br/> * Transit routes. A route is a group of trips that are displayed to riders as a single service. * * @throws IOException */ protected void loadRoutes() throws IOException { log.info("Loading routes.txt"); int l = 1; try { CSVReader reader = createCSVReader(root + GtfsDefinitions.Files.ROUTES.fileName); String[] header = reader.readNext(); Map<String, Integer> col = getIndices(header, GtfsDefinitions.Files.ROUTES.columns, GtfsDefinitions.Files.ROUTES.optionalColumns); String[] line = reader.readNext(); while(line != null) { l++; int routeTypeNr = Integer.parseInt(line[col.get(GtfsDefinitions.ROUTE_TYPE)]); ExtendedRouteType extendedRouteType = RouteType.getExtendedRouteType(routeTypeNr); if(extendedRouteType == null) { log.warn("Route " + line[col.get(GtfsDefinitions.ROUTE_ID)] + " of type " + routeTypeNr + " will be ignored"); ignoredRoutes.add(line[col.get(GtfsDefinitions.ROUTE_ID)]); } else { String routeId = line[col.get(GtfsDefinitions.ROUTE_ID)]; String shortName = line[col.get(GtfsDefinitions.ROUTE_SHORT_NAME)]; String longName = line[col.get(GtfsDefinitions.ROUTE_LONG_NAME)]; Route newGtfsRoute = new RouteImpl(routeId, shortName, longName, extendedRouteType); routes.put(line[col.get(GtfsDefinitions.ROUTE_ID)], newGtfsRoute); } line = reader.readNext(); } reader.close(); } catch (ArrayIndexOutOfBoundsException i) { throw new RuntimeException("Line " + l + " in routes.txt is empty or malformed."); } log.info("... routes.txt loaded"); }
Example 14
Source File: BasicScheduleEditor.java From pt2matsim with GNU General Public License v2.0 | 5 votes |
/** * Parses a command file (csv) and runs the commands specified */ @Override public void parseCommandCsv(String filePath) throws IOException { CSVReader reader = new CSVReader(new FileReader(filePath), ';'); String[] line = reader.readNext(); while(line != null) { log.info(CollectionUtils.arrayToString(line)); executeCmdLine(line); line = reader.readNext(); } reader.close(); }
Example 15
Source File: FilterPrioBasedOnMutatedGenes2.java From systemsgenetics with GNU General Public License v3.0 | 4 votes |
/** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException, IOException { // final File sampleFile = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\Prioritisations\\samplesWithGeno.txt"); // final File genoFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\Prioritisations\\gavinRes\\"); // final File prioFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\Prioritisations"); // final File resultFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\Prioritisations\\rankingCandidateGenes"); // final File sampleFile = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\PrioritizeRequests\\Prioritisations\\samples.txt"); // final File genoFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\PrioritizeRequests\\CandidateGenes\\"); // final File prioFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\PrioritizeRequests\\Prioritisations"); // final File resultFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\PrioritizeRequests\\rankingCandidateGenes"); final File sampleFile = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\New5gpm\\hpo5gpm.txt"); final File genoFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\New5gpm\\Genes\\"); final File prioFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\New5gpm\\Prioritisations\\"); final File resultFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\New5gpm\\RankingCandidateGenes\\"); final CSVParser parser = new CSVParserBuilder().withSeparator('\t').withIgnoreQuotations(true).build(); final CSVReader sampleFileReader = new CSVReaderBuilder(new BufferedReader(new FileReader(sampleFile))).withSkipLines(0).withCSVParser(parser).build(); String[] nextLine; while ((nextLine = sampleFileReader.readNext()) != null) { String sample = nextLine[0]; String genoSampleName = sample + ".txt"; File genoFile = new File(genoFolder, genoSampleName); File prioFile = new File(prioFolder, sample + ".txt"); File rankingFile = new File(resultFolder, sample + ".txt"); System.out.println("------------------------------------------------------------------"); System.out.println("Sample: " + sample); System.out.println("Geno: " + genoFile.getAbsolutePath()); System.out.println("Prio: " + prioFile.getAbsolutePath()); System.out.println("Ranking: " + rankingFile.getAbsolutePath()); HashSet<String> genesWithMutation = getMutatedGenes(genoFile, 0, 0); final CSVReader prioFileReader = new CSVReaderBuilder(new BufferedReader(new FileReader(prioFile))).withSkipLines(0).withCSVParser(parser).build(); CSVWriter writer = new CSVWriter(new FileWriter(rankingFile), '\t', '\0', '\0', "\n"); String[] outputLine = prioFileReader.readNext(); writer.writeNext(outputLine); while ((outputLine = prioFileReader.readNext()) != null) { if (genesWithMutation.contains(outputLine[1])) { writer.writeNext(outputLine); } } writer.close(); prioFileReader.close(); } }
Example 16
Source File: FilterPrioBasedOnMutatedGenes.java From systemsgenetics with GNU General Public License v3.0 | 4 votes |
/** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException, IOException { // // final File sampleFile = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\Prioritisations\\samplesWithGeno.txt"); // final File genoFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\Prioritisations\\gavinRes\\"); // final File prioFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\Prioritisations"); // final File resultFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\Prioritisations\\rankingCandidateGenes"); // final File sampleFile = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\Prioritisations3\\samplesWithGeno.txt"); // final File genoFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\Prioritisations\\gavinRes\\"); // final File prioFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\Prioritisations3"); // final File resultFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\Prioritisations3\\rankingCandidateGenes"); final File sampleFile = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\extraUnsolved\\samplesWithGeno.txt"); final File genoFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\extraUnsolved\\gavinRes\\"); final File prioFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\extraUnsolved\\Prioritisations"); final File resultFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\extraUnsolved\\rankingCandidateGenes"); // final File sampleFile = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\PrioritisationsDcm\\samplesWithGeno.txt"); // final File genoFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\PrioritisationsDcm\\gavinRes\\"); // final File prioFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\PrioritisationsDcm"); // final File resultFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\PrioritisationsDcm\\rankingCandidateGenes"); // // final File sampleFile = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\Prioritisations3\\samplesWithGeno.txt"); // final File genoFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\Prioritisations\\gavinRes\\"); // final File prioFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\PrioritisationsSpiked"); // final File resultFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\PrioritisationsSpiked\\rankingCandidateGenes"); // final File sampleFile = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\PrioritisationsCardioMieke\\samplesWithGeno.txt"); // final File genoFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\PrioritisationsCardioMieke\\Gavin\\"); // final File prioFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\PrioritisationsCardioMieke\\Prioritisations"); // final File resultFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\PrioritisationsCardioMieke\\"); // // final File sampleFile = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\PrioritisationsCardioEdgar\\Prioritisations\\samples.txt"); // final File genoFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\PrioritisationsCardioEdgar\\CandidateGenes\\"); // final File prioFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\PrioritisationsCardioEdgar\\Prioritisations"); // final File resultFolder = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\BenchmarkSamples\\PrioritisationsCardioEdgar\\"); // // resultFolder.mkdirs(); final CSVParser parser = new CSVParserBuilder().withSeparator('\t').withIgnoreQuotations(true).build(); final CSVReader sampleFileReader = new CSVReaderBuilder(new BufferedReader(new FileReader(sampleFile))).withSkipLines(0).withCSVParser(parser).build(); String[] nextLine; while ((nextLine = sampleFileReader.readNext()) != null) { String sample = nextLine[0]; String genoSampleName = new File(nextLine[1]).getName(); if (!genoSampleName.endsWith(".txt")) { genoSampleName += ".txt"; } File genoFile = new File(genoFolder, genoSampleName); File prioFile = new File(prioFolder, sample + ".txt"); File rankingFile = new File(resultFolder, sample + ".txt"); System.out.println("------------------------------------------------------------------"); System.out.println("Sample: " + sample); System.out.println("Geno: " + genoFile.getAbsolutePath()); System.out.println("Prio: " + prioFile.getAbsolutePath()); System.out.println("Ranking: " + rankingFile.getAbsolutePath()); HashSet<String> genesWithMutation = getMutatedGenes(genoFile); final CSVReader prioFileReader = new CSVReaderBuilder(new BufferedReader(new FileReader(prioFile))).withSkipLines(0).withCSVParser(parser).build(); CSVWriter writer = new CSVWriter(new FileWriter(rankingFile), '\t', '\0', '\0', "\n"); String[] outputLine = prioFileReader.readNext(); writer.writeNext(outputLine); while ((outputLine = prioFileReader.readNext()) != null) { if (genesWithMutation.contains(outputLine[1])) { writer.writeNext(outputLine); } } writer.close(); prioFileReader.close(); } }
Example 17
Source File: EffectOfRandom10Percent.java From systemsgenetics with GNU General Public License v3.0 | 4 votes |
public static TObjectDoubleMap<String> readPredictedHpoTermFile(File predictedHpoTermFile) throws FileNotFoundException, IOException { final CSVParser parser = new CSVParserBuilder().withSeparator('\t').withIgnoreQuotations(true).build(); final CSVReader reader = new CSVReaderBuilder(new BufferedReader(new FileReader(predictedHpoTermFile))).withSkipLines(1).withCSVParser(parser).build(); TObjectDoubleMap<String> hpos = new TObjectDoubleHashMap<>(); String[] nextLine; while ((nextLine = reader.readNext()) != null) { hpos.put(nextLine[0], Double.parseDouble(nextLine[3])); } reader.close(); return hpos; }
Example 18
Source File: InvestigateAucChildParent.java From systemsgenetics with GNU General Public License v3.0 | 4 votes |
public static TObjectDoubleMap<String> readSignificantPredictedHpoTermFile(File predictedHpoTermFile) throws FileNotFoundException, IOException { final CSVParser parser = new CSVParserBuilder().withSeparator('\t').withIgnoreQuotations(true).build(); final CSVReader reader = new CSVReaderBuilder(new BufferedReader(new FileReader(predictedHpoTermFile))).withSkipLines(1).withCSVParser(parser).build(); TObjectDoubleMap<String> hpos = new TObjectDoubleHashMap<>(); String[] nextLine; while ((nextLine = reader.readNext()) != null) { if(Double.parseDouble(nextLine[4]) <= 0.05){ hpos.put(nextLine[0], Double.parseDouble(nextLine[3])); } } reader.close(); return hpos; }
Example 19
Source File: InstExpCsv.java From CQL with GNU Affero General Public License v3.0 | 4 votes |
/** * Expects filenames in the map */ public static Map<En, List<String[]>> start2(Map<String, Reader> map, AqlOptions op, Schema<Ty, En, Sym, Fk, Att> sch, boolean omitCheck) throws Exception { Character sepChar = (Character) op.getOrDefault(AqlOption.csv_field_delim_char); Character quoteChar = (Character) op.getOrDefault(AqlOption.csv_quote_char); Character escapeChar = (Character) op.getOrDefault(AqlOption.csv_escape_char); final CSVParser parser = new CSVParserBuilder().withSeparator(sepChar).withQuoteChar(quoteChar) .withEscapeChar(escapeChar).withFieldAsNull(CSVReaderNullFieldIndicator.EMPTY_SEPARATORS).build(); Map<En, List<String[]>> ret = new THashMap<>(); for (String k : map.keySet()) { if (!omitCheck) { if (!sch.ens.contains(En.En(k))) { throw new RuntimeException("Not an entity: " + k); } } Reader r = map.get(k); // File file = new File(map.get(k)); // BufferedReader fileReader = new BufferedReader(r); // String s; /// while ((s = fileReader.readLine()) != null) { // System.out.println(s); // } final CSVReader reader = new CSVReaderBuilder(r).withCSVParser(parser) .withFieldAsNull(CSVReaderNullFieldIndicator.EMPTY_SEPARATORS).build(); List<String[]> rows = reader.readAll(); ret.put(En.En(k), rows); reader.close(); r.close(); } if (!omitCheck) { for (En en : sch.ens) { if (!ret.containsKey(en)) { ret.put(en, new LinkedList<>(Collections.singletonList(Util .union(sch.attsFrom(en).stream().map(Object::toString).collect(Collectors.toList()), sch.fksFrom(en).stream().map(Object::toString).collect(Collectors.toList())) .toArray(new String[0])))); } } } return ret; }
Example 20
Source File: ParseLog.java From systemsgenetics with GNU General Public License v3.0 | 2 votes |
/** * @param args the command line arguments * @throws java.io.FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException, IOException { final File logFile = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\Data31995Genes05-12-2017\\PCA_01_02_2018\\predictions\\hpo_predictions.log"); final int pcCount = 1588; final File outputMatrixFile = new File("C:\\UMCG\\Genetica\\Projects\\GeneNetwork\\Data31995Genes05-12-2017\\PCA_01_02_2018\\predictions\\hpo_compPathwayZscoreMatrix.txt.gz"); final CSVParser parser = new CSVParserBuilder().withSeparator('\t').withIgnoreQuotations(true).build(); CSVReader reader = new CSVReaderBuilder(new BufferedReader(new FileReader(logFile))).withSkipLines(0).withCSVParser(parser).build(); LinkedHashSet<String> pathways = new LinkedHashSet<>(); String[] nextLine; while ((nextLine = reader.readNext()) != null) { if (nextLine[0].equals("Processing geneset:")) { if (!pathways.add(nextLine[1])) { System.err.println("Duplicate pathway: " + nextLine[1]); } } } System.out.println("Number of pathways: " + pathways.size()); LinkedHashSet<String> pcs = new LinkedHashSet<>(); for (int pc = 1; pc <= pcCount; pc++) { pcs.add("PC" + pc); } DoubleMatrixDataset zscoreMatrix = new DoubleMatrixDataset(pathways, pcs); reader.close(); reader = new CSVReaderBuilder(new BufferedReader(new FileReader(logFile))).withSkipLines(0).withCSVParser(parser).build(); while ((nextLine = reader.readNext()) != null) { if (nextLine[0].equals("Processing geneset:")) { String pathway = nextLine[1]; reader.readNext();//skip header for (int pc = 1; pc <= pcCount; pc++) { nextLine = reader.readNext(); if(!nextLine[0].equals("PC" + pc)){ System.err.println("Error parsing z-scores for: " + pathway); } zscoreMatrix.setElement(pathway, nextLine[0], Double.valueOf(nextLine[4])); } } } reader.close(); zscoreMatrix.save(outputMatrixFile); }