Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package slidingwindow;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class SlidingWindowMultiThread {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- SlidingWindowMultiThread measureSlope = new SlidingWindowMultiThread();
- String predictedFile = args[0];
- String statsFile = args[1];
- String windowString = args[2];
- String stepString = args[3];
- String maxReads = args[4];
- String simulationNumber = args[5];
- String threadCount = args[6];
- Integer threads = Integer.parseInt(threadCount);
- Integer simulationCount = Integer.parseInt(simulationNumber);
- Integer maxInteger = Integer.parseInt(maxReads);
- Integer windowInteger = Integer.parseInt(windowString);
- Integer stepInteger = Integer.parseInt(stepString);
- ArrayList<String> predictionsUnformattedArray = new ArrayList<String>();
- ArrayList<String> statsUnformattedArray = new ArrayList<String>();
- try {
- predictionsUnformattedArray = measureSlope.loadFile(predictedFile);
- statsUnformattedArray = measureSlope.loadFile(statsFile);
- } catch(Exception e) {
- if(e instanceof IOException) {
- System.out.println("YOU GOOFED");
- }
- }
- ArrayList<String> predictionsArray = new ArrayList<String>();
- ArrayList<String> windowArray = new ArrayList<String>();
- double predPercent = 0;
- predictionsArray = measureSlope.FormatPredictions(predictionsUnformattedArray);
- predPercent = measureSlope.FormatStats(statsUnformattedArray);
- windowArray = measureSlope.MakeWindows(windowInteger, stepInteger, maxInteger);
- measureSlope.MakeThreads(threads, windowArray, predictionsArray, predPercent, simulationCount); // This is the part of the code where I try to multithread. See right below.
- }
- public void MakeThreads(Integer threads, ArrayList<String> windows, ArrayList<String> predictions, double stat, Integer simulations) { // This is the subroutine that tries to multithread. Every other subroutine works
- ArrayList<String> windowSegment = new ArrayList<String>();
- Integer startValue = 0;
- Integer endValue = 0;
- Integer windowsSize = windows.size();
- Integer increment = windowsSize/threads;
- ExecutorService executor = Executors.newFixedThreadPool(threads);
- for(Integer j = 0; j < threads; j++) {
- endValue = startValue + increment;
- windowSegment.clear();
- for(Integer k = startValue; k < endValue; k++) {
- windowSegment.add(windows.get(k));
- }
- Runnable worker = new RunThreads(windowSegment, predictions, stat, simulations);
- executor.execute(worker);
- }
- executor.shutdown();
- }
- public double FormatStats(ArrayList<String> unformattedStats) {
- double predictedFraction = 0;
- String statLine = unformattedStats.get(0);
- String[] statLineParts = statLine.split("= ");
- String value = statLineParts[1];
- predictedFraction = Double.parseDouble(value);
- return predictedFraction;
- }
- public ArrayList<String> FormatPredictions(ArrayList<String> unformattedPredictions) {
- ArrayList<String> formattedPredictions = new ArrayList<String>();
- Integer lineNumber = 0;
- for(String predictionLine:unformattedPredictions) {
- lineNumber++;
- if(lineNumber > 1){
- String[] predictionLineParts = predictionLine.split("\t");
- String chrom = predictionLineParts[1];
- String start = predictionLineParts[2];
- String end = predictionLineParts[3];
- String countString = predictionLineParts[8];
- String siteLine = chrom + "\t" + start + "\t" + end;
- Integer count = Integer.parseInt(countString);
- if(count > 0){
- for(Integer j = 0; j < count; j++) {
- formattedPredictions.add(siteLine);
- }
- }
- }
- }
- return formattedPredictions;
- }
- public ArrayList<String> MakeWindows(Integer window, Integer step, Integer max) {
- ArrayList<String> windows = new ArrayList<String>();
- Integer start = 0;
- Integer end = window;
- String startString = Integer.toString(start);
- String endString = Integer.toString(end);
- String windowString = startString + "\t" + endString;
- windows.add(windowString);
- while(end <= max) {
- start += step;
- end += step;
- startString = Integer.toString(start);
- endString = Integer.toString(end);
- windowString = startString + "\t" + endString;
- windows.add(windowString);
- }
- return windows;
- }
- public ArrayList<String> loadFile(String fileName) throws IOException { // this subroutine loads the files
- FileReader fileReader = new FileReader(fileName);
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- String line = null;
- ArrayList<String> fileLines = new ArrayList<String>();
- try {
- while ((line = bufferedReader.readLine()) != null) {
- fileLines.add(line);
- }
- } finally {
- bufferedReader.close();
- }
- return fileLines;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement