List of usage examples for org.apache.commons.io FileUtils writeLines
public static void writeLines(File file, Collection lines) throws IOException
toString()
value of each item in a collection to the specified File
line by line. From source file:specminers.evaluation.ValidSpecGenerator.java
private static void extractInvalidSequences(Map<String, String> options) throws IOException, ParseException { File mopFilesFolder = new File(options.get(INPUT_PATH_OPTION)); String[] extensions = new String[] { MopExtractor.MOP_FILES_EXTENSION }; List<File> files = FileUtils.listFiles(mopFilesFolder, extensions, true).stream() .collect(Collectors.toList()); List<String> forbiddenSequences = new LinkedList<>(); for (File f : files) { MopExtractor extractor = new MopExtractor(f); if (extractor.containsParseableSpec()) { forbiddenSequences.addAll(extractor.getForbiddenSequences()); }// www. j av a 2 s . c om } File outputDir = new File(options.get(OUTPUT_OPTION)); if (outputDir == null || !outputDir.exists()) { forbiddenSequences.forEach(l -> System.out.println(l)); } else { File forbiddenSeqsFile; forbiddenSeqsFile = java.nio.file.Paths.get(outputDir.getAbsolutePath(), "forbidden_sequences.txt") .toFile(); FileUtils.writeLines(forbiddenSeqsFile, forbiddenSequences); } }
From source file:trigeditor.editors.PrefixRefactor.java
/** * Function that refactors prefixes. For URIs that haven't been matched to a prefix yet * and appear in the file at least 5 times, this function adds the prefix name and URI * to the top of the list of prefixes in the file. In addition, all occurrences of the URI * are replaced by the prefix name. If there is additional text in the URI, the text is * separated by ':' from the prefix name. * //ww w . j ava 2 s . com * Additionally, the same URI should not appear multiple times in a triple statement. * * @param input String representation of the file whose prefixes are being refactored. * @param fileName String representation of the path of the file * @throws IOException */ public void refactorPrefixes(String input, String filePath) throws IOException { File f = new File(filePath); //prevent concurrency issues: using different data structures to avoid access/changing at same time List<String> lines = FileUtils.readLines(f); List<String> tempLines = new ArrayList<String>(); List<String> tempLinesRename = new ArrayList<String>(); Map<String, String> tempPrefixes = new HashMap<String, String>(); Map<String, String> tempURIRename = new HashMap<String, String>(); tempPrefixes = generatePrefixes(input); tempURIRename = generatePrefixes(input); //Add prefix name to list of prefixes int index = 1; for (String s : lines) { tempLines.add(s); if (s.contains("@prefix") && !tempPrefixes.isEmpty()) { for (String URI : tempPrefixes.keySet()) { tempLines.add(index - 1, "@prefix " + tempPrefixes.get(URI) + " : " + "<" + URI + "> ."); tempPrefixes.remove(URI); } } index++; } //case where there are no prefixes listed in the file if (!tempPrefixes.isEmpty()) { for (String URI : tempPrefixes.keySet()) { tempLines.add(0, "@prefix " + tempPrefixes.get(URI) + " : " + "<" + URI + "> ."); tempPrefixes.remove(URI); } } //Replace URIs with prefix name in document int index2 = 0; for (String s : tempLines) { tempLinesRename.add(s); for (String uri : tempURIRename.keySet()) { String strAtLine = tempLinesRename.get(index2); if (strAtLine.contains(uri) && !strAtLine.contains("@prefix")) { int start = strAtLine.indexOf(uri) + uri.length(); String strAtLineNew = ""; if (start > 0) { int i = start; String temp = strAtLine; if (strAtLine.charAt(start) == '#' || strAtLine.charAt(start) == '/') { temp = strAtLine.substring(0, start) + ":" + strAtLine.substring(start + 1); } while (temp.charAt(i) != '>') { i++; } if (i != temp.length() - 1) { strAtLineNew = temp.substring(0, i) + temp.substring(i + 1); } else { strAtLineNew = temp.substring(0, i); } } CharSequence target = "<" + uri; CharSequence literal = tempURIRename.get(uri); tempLinesRename.set(index2, strAtLineNew.replace(target, literal)); } } index2++; } FileUtils.writeLines(f, tempLinesRename); }
From source file:uk.ac.tgac.citadel.CitadelCLI.java
public void execute() throws InterruptedException, TaskExecutionException, IOException { // Run the pipeline as described by the user TaskResult result = super.execute(new GuestUser("citadel@tgac.ac.uk"), ConanTask.Priority.HIGH, this.executionContext); // Output the resource usage to file FileUtils.writeLines(new File(this.args.getOutputDir(), this.args.getJobPrefix() + ".summary"), result.getOutput());/*from w w w .ja v a 2 s. co m*/ }
From source file:uk.ac.tgac.conan.process.asm.tools.AllpathsLgV50.java
protected void createPloidyFile(int ploidy, File ploidyFile) throws IOException { List<String> lines = new ArrayList<>(); lines.add(Integer.toString(ploidy)); FileUtils.writeLines(ploidyFile, lines); }
From source file:uk.ac.tgac.conan.process.asm.tools.AllpathsLgV50.java
protected void createLibFile(File libFile, List<Library> libraries) throws IOException { List<String> lines = new ArrayList<String>(); lines.add(/*from w w w.j av a 2 s . c o m*/ "library_name, project_name, organism_name, type, paired, frag_size, frag_stddev, insert_size, insert_stddev, read_orientation, genomic_start, genomic_end"); for (Library lib : libraries) { String type = ""; String paired = ""; String fragSize = ""; String fragDev = ""; String insertSize = ""; String insertDev = ""; String readOrientation = ""; switch (lib.getType()) { case OPE: type = "fragment"; paired = "1"; fragSize = Integer.toString(lib.getAverageInsertSize()); fragDev = Integer.toString((int) (lib.getAverageInsertSize() * lib.getInsertErrorTolerance())); readOrientation = lib.getSeqOrientation() == Library.SeqOrientation.FR ? "inward" : "outward"; break; case PE: type = "jumping"; paired = "1"; insertSize = Integer.toString(lib.getAverageInsertSize()); insertDev = Integer.toString((int) (lib.getAverageInsertSize() * lib.getInsertErrorTolerance())); readOrientation = lib.getSeqOrientation() == Library.SeqOrientation.FR ? "inward" : "outward"; break; case MP: type = "long_jump"; paired = "1"; insertSize = Integer.toString(lib.getAverageInsertSize()); insertDev = Integer.toString((int) (lib.getAverageInsertSize() * lib.getInsertErrorTolerance())); readOrientation = lib.getSeqOrientation() == Library.SeqOrientation.FR ? "inward" : "outward"; break; case SE: type = "long"; paired = "0"; break; } List<String> parts = new ArrayList<String>(); parts.add(lib.getName()); parts.add("rampart"); parts.add(this.getArgs().getOrganism().getName().replaceAll(" ", "_")); parts.add(type); parts.add(paired); parts.add(fragSize); parts.add(fragDev); parts.add(insertSize); parts.add(insertDev); parts.add(readOrientation); parts.add(""); parts.add(""); lines.add(StringUtils.join(parts, ", ")); } FileUtils.writeLines(libFile, lines); }
From source file:uk.ac.tgac.conan.process.asm.tools.AllpathsLgV50.java
private GroupInfo createGroupFiles(List<Library> libraries) throws IOException { int countOpe = 0; int countPe = 0; int countMp = 0; int countLong = 0; List<String> phred33Lines = new ArrayList<>(); List<String> phred64Lines = new ArrayList<>(); List<String> groupNames = new ArrayList<>(); final String groupHeader = "group_name, library_name, file_name"; phred33Lines.add(groupHeader);/*from w ww .j a va 2s .com*/ phred64Lines.add(groupHeader); for (Library lib : libraries) { String groupName = lib.getType().toString(); switch (lib.getType()) { case OPE: countOpe++; groupName += countOpe; break; case PE: countPe++; groupName += countPe; break; case MP: countMp++; groupName += countMp; break; case SE: countLong++; groupName += countLong; break; default: } groupNames.add(groupName); List<String> parts = new ArrayList<String>(); parts.add(groupName); parts.add(lib.getName()); if (lib.getType() == Library.Type.SE) { parts.add(lib.getFile1().getAbsolutePath()); } else { parts.add(createGlobPattern(lib.getFile1(), lib.getFile2())); } String groupLine = StringUtils.join(parts, ", "); if (lib.getPhred() == null || lib.getPhred() == Library.Phred.PHRED_64) { phred64Lines.add(groupLine); } else { phred33Lines.add(groupLine); } } File p33File = null; File p64File = null; File outputDir = this.getArgs().getOutputDir(); if (phred33Lines.size() > 1) { p33File = new File(outputDir, "in_groups_phred33.csv"); FileUtils.writeLines(p33File, phred33Lines); } if (phred64Lines.size() > 1) { p64File = new File(outputDir, "in_groups_phred64.csv"); FileUtils.writeLines(p64File, phred64Lines); } return new GroupInfo(p33File, p64File, groupNames); }
From source file:uk.ac.tgac.conan.process.re.tools.QuakeV03.java
@Override public void setup() { String pwdFull = new File(".").getAbsolutePath(); String pwd = pwdFull.substring(0, pwdFull.length() - 2); this.addPreCommand("cd " + this.getArgs().getOutputDir().getAbsolutePath()); this.addPostCommand("cd " + pwd); Args args = this.getArgs(); if (args.getReadsListFile() == null || !args.getReadsListFile().exists()) { if (args.getInput() == null) { throw new IllegalArgumentException( "Input reads file does not exist and no library object provided. Cannot continue."); }/* www.j a v a 2 s . co m*/ final ConanProcessService cps = this.conanExecutorService.getConanProcessService(); boolean pairedEnd = args.getInput().isPairedEnd(); File in1 = new File(args.getOutputDir(), args.getInput().getFile1().getName()); File in2 = pairedEnd ? new File(args.getOutputDir(), args.getInput().getFile2().getName()) : null; try { cps.createLocalSymbolicLink(args.getInput().getFile1(), in1); if (pairedEnd) { cps.createLocalSymbolicLink(args.getInput().getFile2(), in2); } } catch (ProcessExecutionException | InterruptedException e) { throw new IllegalArgumentException("Could not create symbolic links for input files.", e); } String fp1 = in1.getAbsolutePath(); String fp2 = pairedEnd ? " " + in2.getAbsolutePath() : ""; String outLine = fp1 + fp2; List<String> outLines = new ArrayList<>(); outLines.add(outLine); File inputFileList = new File(args.getOutputDir(), "readsListFile.lst"); args.setReadsListFile(inputFileList); args.setQualityScale(args.getInput().getPhred() == null ? 0 : args.getInput().getPhred() == Library.Phred.PHRED_33 ? 33 : 64); try { FileUtils.writeLines(inputFileList, outLines); } catch (IOException e) { throw new IllegalArgumentException( "Could not output Quake reads configuration file: " + inputFileList, e); } } }
From source file:uk.ac.tgac.jellyswarm.StatsProcess.java
protected void createStatsFile(List<File> statsFiles, File outputFile) throws IOException { List<String> outputLines = new ArrayList<>(); // Add header outputLines.add("filename\tunique\tdistinct\ttotal\tmax_count"); for (File file : statsFiles) { List<String> statsLines = FileUtils.readLines(file); long unique = 0; long distinct = 0; long total = 0; long maxCount = 0; for (String line : statsLines) { if (line.startsWith("Unique")) { String[] parts = line.split("\\s+"); unique = Long.parseLong(parts[1]); }/*w w w . j ava 2 s.c o m*/ if (line.startsWith("Distinct")) { String[] parts = line.split("\\s+"); distinct = Long.parseLong(parts[1]); } if (line.startsWith("Total")) { String[] parts = line.split("\\s+"); total = Long.parseLong(parts[1]); } if (line.startsWith("Max_count")) { String[] parts = line.split("\\s+"); maxCount = Long.parseLong(parts[1]); } } StringBuilder sb = new StringBuilder(); sb.append(file.getName()).append("\t").append(unique).append("\t").append(distinct).append("\t") .append(total).append("\t").append(maxCount); outputLines.add(sb.toString()); } FileUtils.writeLines(outputFile, outputLines); }
From source file:uk.ac.tgac.jellyswarm.StatsProcess.java
private void createDistinctStatsFile(List<File> statsFiles, File outputFile) throws IOException { List<String> lines = new ArrayList<>(); lines.add("filename\tmin\tmax\tmean\tvariance\tstddev"); for (File file : statsFiles) { long min = Long.MAX_VALUE; long max = 0; long sum = 0; long sum2 = 0; List<String> statsLines = FileUtils.readLines(file); for (int i = 1; i < statsLines.size(); i++) { String line = statsLines.get(i); String[] parts = line.split("\t"); long distinctVal = Long.parseLong(parts[2]); min = distinctVal < min ? distinctVal : min; max = distinctVal > max ? distinctVal : max; sum += distinctVal;//ww w . j a v a 2 s . c o m sum2 += distinctVal * distinctVal; } int entries = statsLines.size() - 1; double mean = (double) sum / (double) entries; double var = ((sum * sum) - sum2) / (double) entries; double stddev = Math.sqrt(var); lines.add(file.getName() + "\t" + min + "\t" + max + "\t" + mean + "\t" + var + "\t" + stddev); } FileUtils.writeLines(outputFile, lines); }
From source file:uk.ac.tgac.rampart.RampartCLI.java
public void execute() throws InterruptedException, TaskExecutionException, IOException { // Run the pipeline as described by the user TaskResult result = super.execute(new GuestUser("rampart@tgac.ac.uk"), ConanTask.Priority.HIGH, this.args.getExecutionContext()); // Output the resource usage to file FileUtils.writeLines(new File(this.args.getOutputDir(), this.args.getJobPrefix() + ".summary"), result.getOutput());/* www. jav a2 s . c o m*/ }