Advertisement
Guest User

Untitled

a guest
Sep 14th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. package slidingwindow;
  2.  
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.concurrent.ExecutorService;
  9. import java.util.concurrent.Executors;
  10.  
  11. public class SlidingWindowMultiThread {
  12. public static void main(String[] args) {
  13. // TODO Auto-generated method stub
  14.  
  15. SlidingWindowMultiThread measureSlope = new SlidingWindowMultiThread();
  16. String predictedFile = args[0];
  17. String statsFile = args[1];
  18. String windowString = args[2];
  19. String stepString = args[3];
  20. String maxReads = args[4];
  21. String simulationNumber = args[5];
  22. String threadCount = args[6];
  23. Integer threads = Integer.parseInt(threadCount);
  24. Integer simulationCount = Integer.parseInt(simulationNumber);
  25. Integer maxInteger = Integer.parseInt(maxReads);
  26. Integer windowInteger = Integer.parseInt(windowString);
  27. Integer stepInteger = Integer.parseInt(stepString);
  28. ArrayList<String> predictionsUnformattedArray = new ArrayList<String>();
  29. ArrayList<String> statsUnformattedArray = new ArrayList<String>();
  30. try {
  31. predictionsUnformattedArray = measureSlope.loadFile(predictedFile);
  32. statsUnformattedArray = measureSlope.loadFile(statsFile);
  33. } catch(Exception e) {
  34. if(e instanceof IOException) {
  35. System.out.println("YOU GOOFED");
  36. }
  37. }
  38.  
  39. ArrayList<String> predictionsArray = new ArrayList<String>();
  40. ArrayList<String> windowArray = new ArrayList<String>();
  41. double predPercent = 0;
  42. predictionsArray = measureSlope.FormatPredictions(predictionsUnformattedArray);
  43. predPercent = measureSlope.FormatStats(statsUnformattedArray);
  44. windowArray = measureSlope.MakeWindows(windowInteger, stepInteger, maxInteger);
  45. measureSlope.MakeThreads(threads, windowArray, predictionsArray, predPercent, simulationCount); // This is the part of the code where I try to multithread. See right below.
  46. }
  47.  
  48. 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
  49. ArrayList<String> windowSegment = new ArrayList<String>();
  50. Integer startValue = 0;
  51. Integer endValue = 0;
  52. Integer windowsSize = windows.size();
  53. Integer increment = windowsSize/threads;
  54. ExecutorService executor = Executors.newFixedThreadPool(threads);
  55. for(Integer j = 0; j < threads; j++) {
  56. endValue = startValue + increment;
  57. windowSegment.clear();
  58. for(Integer k = startValue; k < endValue; k++) {
  59. windowSegment.add(windows.get(k));
  60. }
  61. Runnable worker = new RunThreads(windowSegment, predictions, stat, simulations);
  62. executor.execute(worker);
  63. }
  64. executor.shutdown();
  65. }
  66.  
  67. public double FormatStats(ArrayList<String> unformattedStats) {
  68. double predictedFraction = 0;
  69. String statLine = unformattedStats.get(0);
  70. String[] statLineParts = statLine.split("= ");
  71. String value = statLineParts[1];
  72. predictedFraction = Double.parseDouble(value);
  73. return predictedFraction;
  74. }
  75.  
  76. public ArrayList<String> FormatPredictions(ArrayList<String> unformattedPredictions) {
  77. ArrayList<String> formattedPredictions = new ArrayList<String>();
  78. Integer lineNumber = 0;
  79. for(String predictionLine:unformattedPredictions) {
  80. lineNumber++;
  81. if(lineNumber > 1){
  82. String[] predictionLineParts = predictionLine.split("\t");
  83. String chrom = predictionLineParts[1];
  84. String start = predictionLineParts[2];
  85. String end = predictionLineParts[3];
  86. String countString = predictionLineParts[8];
  87. String siteLine = chrom + "\t" + start + "\t" + end;
  88. Integer count = Integer.parseInt(countString);
  89. if(count > 0){
  90. for(Integer j = 0; j < count; j++) {
  91. formattedPredictions.add(siteLine);
  92. }
  93. }
  94. }
  95. }
  96. return formattedPredictions;
  97. }
  98.  
  99. public ArrayList<String> MakeWindows(Integer window, Integer step, Integer max) {
  100. ArrayList<String> windows = new ArrayList<String>();
  101. Integer start = 0;
  102. Integer end = window;
  103. String startString = Integer.toString(start);
  104. String endString = Integer.toString(end);
  105. String windowString = startString + "\t" + endString;
  106. windows.add(windowString);
  107. while(end <= max) {
  108. start += step;
  109. end += step;
  110. startString = Integer.toString(start);
  111. endString = Integer.toString(end);
  112. windowString = startString + "\t" + endString;
  113. windows.add(windowString);
  114. }
  115. return windows;
  116. }
  117.  
  118. public ArrayList<String> loadFile(String fileName) throws IOException { // this subroutine loads the files
  119.  
  120. FileReader fileReader = new FileReader(fileName);
  121. BufferedReader bufferedReader = new BufferedReader(fileReader);
  122. String line = null;
  123. ArrayList<String> fileLines = new ArrayList<String>();
  124. try {
  125. while ((line = bufferedReader.readLine()) != null) {
  126. fileLines.add(line);
  127. }
  128. } finally {
  129. bufferedReader.close();
  130. }
  131. return fileLines;
  132. }
  133.  
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement