PageRenderTime 4307ms CodeModel.GetById 29ms RepoModel.GetById 3ms app.codeStats 1ms

/curtain-pipeline/src/main/java/uk/ac/ebi/curtain/AbstractPipelineBootstrapper.java

https://code.google.com/p/curtain/
Java | 306 lines | 236 code | 48 blank | 22 comment | 20 complexity | 34374e94e74b9ec3789148699b933163 MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * File: StartBatchProcessing.java
  3. * Created by: mhaimel
  4. * Created on: 16 Jul 2009
  5. * CVS: $Id: AbstractPipelineBootstrapper.java,v 1.3 2009/12/07 16:35:38 mhaimel Exp $
  6. */
  7. package uk.ac.ebi.curtain;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.util.List;
  11. import org.apache.commons.io.FileUtils;
  12. import org.apache.commons.logging.Log;
  13. import org.apache.commons.logging.LogFactory;
  14. import uk.ac.ebi.curtain.configuration.CurtainConfig;
  15. import uk.ac.ebi.curtain.configuration.CurtainContextModule;
  16. import uk.ac.ebi.curtain.configuration.CurtainInputArguments;
  17. import uk.ac.ebi.curtain.configuration.DefaultModule;
  18. import uk.ac.ebi.curtain.management.CurtainControllerFactory;
  19. import uk.ac.ebi.curtain.management.TypeInfo;
  20. import uk.ac.ebi.curtain.pipeline.CurtainPipelineManager;
  21. import uk.ac.ebi.curtain.service.IdentifierFilepositionIndex;
  22. import uk.ac.ebi.curtain.util.FileIndexReader;
  23. import uk.ac.ebi.curtain.util.FileIndexWriter;
  24. import uk.ac.ebi.curtain.util.FileIndexUtil.FileIndexEvent;
  25. import uk.ac.ebi.curtain.utils.CurtainUncheckedException;
  26. import uk.ac.ebi.curtain.utils.data.FileType;
  27. import uk.ac.ebi.curtain.utils.data.ReadType;
  28. import uk.ac.ebi.curtain.utils.file.FileInfo;
  29. import uk.ac.ebi.curtain.utils.io.impl.FileIO;
  30. import uk.ac.ebi.velvet.model.Strand;
  31. import com.google.inject.Guice;
  32. import com.google.inject.Inject;
  33. import com.google.inject.Injector;
  34. /**
  35. * @author mhaimel
  36. *
  37. */
  38. public abstract class AbstractPipelineBootstrapper {
  39. private Log log= null;
  40. private CurtainConfig config;
  41. private CurtainContext context;
  42. private CurtainPipelineManager manager;
  43. public AbstractPipelineBootstrapper() {
  44. // do nothing;
  45. }
  46. protected abstract CurtainInputArguments parseArguments(String[] args);
  47. public String printHelp(){
  48. Injector inj = getInjector(new CurtainContextImpl(-1));
  49. // CurtainControllerFactory
  50. CurtainControllerFactory ctr = inj.getInstance(CurtainControllerFactory.class);
  51. List<TypeInfo> types = ctr.getTypes();
  52. StringBuilder sb = new StringBuilder();
  53. sb.append("Curtain pipelines:\n-----------------------------------\n");
  54. for(TypeInfo ty : types){
  55. if(ty.getMain()){
  56. sb.append(" ")
  57. .append(ty.getName())
  58. .append("\t : ")
  59. .append(ty.getDescription())
  60. .append("\n");
  61. }
  62. }
  63. return sb.toString();
  64. }
  65. public void boot(String[] args){
  66. CurtainInputArguments arguments = parseArguments(args);
  67. CurtainContextImpl impl = new CurtainContextImpl(arguments.getCurtainIteration());
  68. impl.setArguments(arguments);
  69. Injector injector = getInjector(impl);
  70. impl.setInjector(injector);
  71. injector.injectMembers(this);
  72. this.run();
  73. }
  74. private Injector getInjector(CurtainContextImpl impl) {
  75. return Guice.createInjector(
  76. new CurtainContextModule(impl),
  77. new DefaultModule(impl));
  78. }
  79. protected void run() {
  80. try{
  81. String contr = getControllerType();
  82. getLog().info("Check and load configuration for " + contr + " ... ");
  83. _clearConfiguration();
  84. _checkWorkingDir();
  85. _checkFilePositionIndex();
  86. registerSequenceFiles();
  87. getLog().info("Connect pipeline " + contr + " ... ");
  88. connectPipeline(contr);
  89. getLog().info("Boot pipeline " + contr + " ... ");
  90. bootPipeline(contr);
  91. getLog().info("Process pipeline " + contr + " ... ");
  92. processPipeline(contr);
  93. getLog().info("Wait pipeline to finish " + contr + " ... ");
  94. getManager().shutdownAndWait(false);
  95. List<Object> resSet = getManager().getResult(contr);
  96. getLog().info("Finished Pipeline with " + resSet.size() + " results");
  97. } catch (Exception e) {
  98. getManager().shutdown(true);
  99. getLog().error("Problems with the pipeline", e);
  100. exit(99);
  101. }
  102. }
  103. protected void bootPipeline(String contr) {
  104. getManager().startPipeline(contr);
  105. }
  106. protected void connectPipeline(String contr) {
  107. getManager().connectPipeline(contr);
  108. }
  109. protected void processPipeline(String type) {
  110. getManager().handle(type, getContext().currentFiles().getBucketDir());
  111. }
  112. protected abstract String getControllerType();
  113. protected void _checkWorkingDir() {
  114. {
  115. File dir = getContext().currentFiles().getWorkingDir();
  116. if(!dir.exists() && !dir.mkdirs()){
  117. throw new CurtainUncheckedException("Not able to create working directory " + dir);
  118. }
  119. }
  120. // {
  121. // File dir = getContext().currentFiles().getContigDir();
  122. // if(!dir.exists() && !dir.mkdir()){
  123. // throw new CurtainUncheckedException("Not able to create working directory " + dir);
  124. // }
  125. // }
  126. }
  127. protected void _clearConfiguration() {
  128. if(deleteWorkingDirecotry()){
  129. for(ReadType rt : ReadType.values()){
  130. deleteFile(getContext().currentFiles().getEntryFilePosition(rt));
  131. deleteFile(getContext().currentFiles().getEntryFilePosition(rt));
  132. }
  133. deleteDir(getContext().currentFiles().getWorkingDir());
  134. // deleteFile(getContext().currentFiles().getConfigurationInformation());
  135. // deleteFile(getContext().currentFiles().getEntryFilePosition());
  136. // deleteFile(getContext().currentFiles().getUnmappedFilePosition());
  137. // deleteDir(getContext().currentFiles().getContigDir());
  138. }
  139. }
  140. protected abstract boolean deleteWorkingDirecotry();
  141. protected Log getLog() {
  142. if(null == log){
  143. log = LogFactory.getLog(this.getClass());
  144. }
  145. return log;
  146. }
  147. protected void deleteFile(File file){
  148. if(file.exists()){
  149. getLog().info("Delete file " + file);
  150. if(!file.delete()){
  151. throw new CurtainUncheckedException("Failed deleting File " + file);
  152. }
  153. }
  154. }
  155. protected void deleteDir(File dir){
  156. if(dir.exists()){
  157. if(dir.isDirectory()){
  158. getLog().info("Delete directory " + dir);
  159. try {
  160. FileUtils.deleteDirectory(dir);
  161. } catch (IOException e) {
  162. throw new CurtainUncheckedException("Failed deleting File " + dir,e);
  163. }
  164. } else {
  165. throw new CurtainUncheckedException("Expected directory: " + dir);
  166. }
  167. }
  168. }
  169. protected void _checkFilePositionIndex() {
  170. File cFile = getContext().currentFiles().getConfigurationInformation();
  171. IdentifierFilepositionIndex idx = getContext().getPositionIndex(cFile);
  172. if(cFile.exists()){
  173. idx.synchronize();
  174. }
  175. if(!idx.isWriteable()){
  176. idx.setWriteOnly();
  177. }
  178. FileIndexWriter out = null;
  179. try{
  180. out = idx.getWriter();
  181. for(FileInfo f : getContext().getArguments().getFiles(FileType.values())){
  182. Integer cat = getContext().getArguments().getCategory(f);
  183. out.addFile(f, cat);
  184. Integer insLen = getContext().getArguments().getInsertLength(f);
  185. Integer insSd = getContext().getArguments().getInsertLengthSd(f);
  186. if(null != insLen){
  187. out.addInsertLength(cat, insLen);
  188. }
  189. if(null != insSd){
  190. out.addInsertLengthSd(cat, insSd);
  191. }
  192. }
  193. } finally{
  194. FileIO.closeQuietly(out);
  195. FileIO.closeQuietly(idx);
  196. }
  197. }
  198. protected void registerSequenceFiles() {
  199. File cFile = getContext().currentFiles().getConfigurationInformation();
  200. IdentifierFilepositionIndex idx = getContext().getPositionIndex(cFile);
  201. idx.setReadOnly();
  202. FileIndexReader reader = null;
  203. try{
  204. reader = idx.getReader();
  205. reader.setEventHandler(new FileIndexEvent(){
  206. public void addContig(FileInfo fi, String key, Long pos) {/* empty*/}
  207. public void addContigPosition(FileInfo fi, Long pos) {/* empty*/}
  208. public void addFile(FileInfo fi, Integer category) {
  209. getContext().getArguments().addFile2Category(category,fi);
  210. }
  211. public void addInsertLength(Integer category, Integer insLen) {
  212. getContext().getArguments().addInsertLength2Category(category, insLen);
  213. }
  214. public void addInsertLengthSd(Integer category, Integer sd) {
  215. getContext().getArguments().addInsertLengthSd2Category(category, sd);
  216. }
  217. public void addRead(FileInfo fi, String key, Strand strand,Long pos) {/* empty*/}
  218. public void addReadPosition(FileInfo fi, Strand strand, Long pos) {/* empty*/}
  219. public void close() throws IOException {/* empty*/}
  220. public void addFileId(Integer fid, FileInfo fi) {
  221. getContext().getGlobalFileIndex().register(fid, fi);
  222. }
  223. @Override
  224. public void addCategory(Integer category, String id) {
  225. getContext().getArguments().addCategory(category,id);
  226. }});
  227. reader.readAll();
  228. } catch (IOException e) {
  229. throw new CurtainUncheckedException("Problems reading initialization file!",e);
  230. }finally{
  231. FileIO.closeQuietly(reader);
  232. }
  233. }
  234. protected void exit(Integer id){
  235. System.exit(id);
  236. }
  237. protected CurtainConfig getConfig() {
  238. return config;
  239. }
  240. @Inject
  241. public void setConfig(CurtainConfig config) {
  242. this.config = config;
  243. }
  244. protected CurtainPipelineManager getManager() {
  245. return manager;
  246. }
  247. @Inject
  248. public void setManager(CurtainPipelineManager manager) {
  249. this.manager = manager;
  250. }
  251. @Inject
  252. public void setContext(CurtainContext context) {
  253. this.context = context;
  254. }
  255. public CurtainContext getContext() {
  256. return context;
  257. }
  258. }