Mercurial > repos > pfrommolt > ngsrich
view NGSrich_0.5.5/src/middlewares/Misc.java @ 0:89ad0a9cca52 default tip
Uploaded
author | pfrommolt |
---|---|
date | Mon, 21 Nov 2011 08:12:19 -0500 |
parents | |
children |
line wrap: on
line source
package middlewares; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Scanner; import datastructures.ReducedReadLine; import datastructures.TargetLine; import exceptions.ChromosomeFormatException; import exceptions.ChromosomeNotFoundException; import exceptions.NullOrNegativeRangeException; import exceptions.RangeFormatException; import exceptions.RangeLimitNotFoundException; /** * Miscellaneous methods for different uses. * * @author Ali Abdallah * @version 01.2011 * @since jdk 1.6.0 */ public class Misc { /** * Make the first letter big, if possible. nGSrich -> NGSrich. * * @param s the string * @return the same string with the first letter big. */ public static String bigFirst(String s) { return s.substring(0, 1).toUpperCase() + s.substring(1); } /** * Creates a shortcut for the file with a different file extension. * * @param fName the original file name. * @param newExt the new extension of the file. * @param dir the directory of the mad link. * @return a link to the file fName with a different extension. */ public static File rename(String fName, String newExt, String dir) { String inDirShortCut = dir + Misc.slash(dir); String fShortcutName = inDirShortCut + Misc.prefix(fName) + "." + newExt; Runtime r = Runtime.getRuntime(); try { r.exec("ln -s " + fName + " " + fShortcutName); } catch (IOException e) { e.printStackTrace(); } try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } return new File(fShortcutName); } /** * @return the path of the bin directory of this software. */ public static String binDir() { URL url = new Misc().getClass().getResource("Misc.class"); String path = url.toString().substring(url.toString().indexOf(":") + 1); path = path.substring(0, path.lastIndexOf("/")); return path.substring(0, path.lastIndexOf("/")); } /** * @return the path of this software. */ public static String scriptDir() { URL url = new Misc().getClass().getResource("../NGSrich.class"); String path = url.toString().substring(url.toString().indexOf(":") + 1); path = path.substring(0, path.lastIndexOf("/")); return path.substring(0, path.lastIndexOf("/")+1); } public static void main(String[] args) { System.out.println(scriptDir()); } /** * @param path * @return extends the path by a slash at the end, in the case it don't * exists. */ public static String slash(String path) { return (path.endsWith("/") ? "" : "/"); } /** * @return the name of the host. */ public static String getHostName() { Runtime rt = Runtime.getRuntime(); String erg = ""; try { File f = new File("hostname.sh"); FileWriter fw = new FileWriter(f); fw.write("#!/bin/bash\r\nproc=$(hostname -s).$$\r\necho $proc"); fw.close(); Process p = rt.exec("chmod 700 " + f.getAbsolutePath()); p = rt.exec("sh " + f.getAbsolutePath()); Scanner s = new Scanner(p.getInputStream()); while (s.hasNextLine()) { erg += (s.nextLine()); } s.close(); f.delete(); } catch (IOException e) { e.printStackTrace(); } return erg; } /** * @param fileName * @return the extension of the file. */ public static String suffix(String fileName) { return fileName.substring(fileName.lastIndexOf(".") + 1); } /** * @param fileName * @return the name of the file without the extension. */ public static String prefix(String fileName) { return fileName.substring(fileName.lastIndexOf("/") + 1, fileName.lastIndexOf(".")); } /** * @param fileName * @return the directory of the specified file. */ public static String path(String fileName) { String absolutePath = new File(fileName).getParentFile().getAbsolutePath(); return absolutePath+slash(absolutePath); } /** * Check if the chromosome names in the target file are a subset of the * chromosome name in the alignment file. * * @param alignment the alignment file. * @param target the target file. * @return true if target chromosomes are a subset of alignment chromosome * and false otherwise. */ public static boolean areChromosomeCompatible(File alignment, File target){ Scanner aScan = null, tScan = null; try { aScan = new Scanner(alignment); tScan = new Scanner(target); ArrayList<String> alist = new ArrayList<String>(); ArrayList<String> tlist = new ArrayList<String>(); while(aScan.hasNextLine()){ ReducedReadLine rl = new ReducedReadLine(aScan.nextLine()); if(alist.indexOf(rl.chrom())==-1) alist.add(rl.chrom()); } while(tScan.hasNextLine()){ TargetLine tl = new TargetLine(tScan.nextLine()); if(tlist.indexOf(tl.chrom())==-1) tlist.add(tl.chrom()); } for(int i = 0; i < tlist.size(); i++){ if(alist.indexOf(tlist.get(i))!=-1) return true; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (ChromosomeFormatException e) { e.printStackTrace(); } catch (ChromosomeNotFoundException e) { e.printStackTrace(); } catch (RangeFormatException e) { e.printStackTrace(); } catch (RangeLimitNotFoundException e) { e.printStackTrace(); } catch (NullOrNegativeRangeException e) { e.printStackTrace(); } return false; } }