comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:89ad0a9cca52
1 package middlewares;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.net.URL;
8 import java.util.ArrayList;
9 import java.util.Scanner;
10
11 import datastructures.ReducedReadLine;
12 import datastructures.TargetLine;
13 import exceptions.ChromosomeFormatException;
14 import exceptions.ChromosomeNotFoundException;
15 import exceptions.NullOrNegativeRangeException;
16 import exceptions.RangeFormatException;
17 import exceptions.RangeLimitNotFoundException;
18
19 /**
20 * Miscellaneous methods for different uses.
21 *
22 * @author Ali Abdallah
23 * @version 01.2011
24 * @since jdk 1.6.0
25 */
26 public class Misc {
27
28 /**
29 * Make the first letter big, if possible. nGSrich -> NGSrich.
30 *
31 * @param s the string
32 * @return the same string with the first letter big.
33 */
34 public static String bigFirst(String s) {
35 return s.substring(0, 1).toUpperCase() + s.substring(1);
36 }
37
38 /**
39 * Creates a shortcut for the file with a different file extension.
40 *
41 * @param fName the original file name.
42 * @param newExt the new extension of the file.
43 * @param dir the directory of the mad link.
44 * @return a link to the file fName with a different extension.
45 */
46 public static File rename(String fName, String newExt, String dir) {
47
48 String inDirShortCut = dir + Misc.slash(dir);
49 String fShortcutName = inDirShortCut + Misc.prefix(fName) + "."
50 + newExt;
51 Runtime r = Runtime.getRuntime();
52 try {
53 r.exec("ln -s " + fName + " " + fShortcutName);
54 } catch (IOException e) {
55 e.printStackTrace();
56 }
57 try {
58 Thread.sleep(100);
59 } catch (InterruptedException e) {
60 e.printStackTrace();
61 }
62 return new File(fShortcutName);
63 }
64
65 /**
66 * @return the path of the bin directory of this software.
67 */
68 public static String binDir() {
69 URL url = new Misc().getClass().getResource("Misc.class");
70 String path = url.toString().substring(url.toString().indexOf(":") + 1);
71 path = path.substring(0, path.lastIndexOf("/"));
72
73 return path.substring(0, path.lastIndexOf("/"));
74
75 }
76
77 /**
78 * @return the path of this software.
79 */
80 public static String scriptDir() {
81 URL url = new Misc().getClass().getResource("../NGSrich.class");
82 String path = url.toString().substring(url.toString().indexOf(":") + 1);
83 path = path.substring(0, path.lastIndexOf("/"));
84 return path.substring(0, path.lastIndexOf("/")+1);
85
86 }
87
88 public static void main(String[] args) {
89 System.out.println(scriptDir());
90 }
91
92 /**
93 * @param path
94 * @return extends the path by a slash at the end, in the case it don't
95 * exists.
96 */
97 public static String slash(String path) {
98 return (path.endsWith("/") ? "" : "/");
99 }
100
101 /**
102 * @return the name of the host.
103 */
104 public static String getHostName() {
105 Runtime rt = Runtime.getRuntime();
106 String erg = "";
107 try {
108 File f = new File("hostname.sh");
109 FileWriter fw = new FileWriter(f);
110
111 fw.write("#!/bin/bash\r\nproc=$(hostname -s).$$\r\necho $proc");
112 fw.close();
113 Process p = rt.exec("chmod 700 " + f.getAbsolutePath());
114 p = rt.exec("sh " + f.getAbsolutePath());
115
116 Scanner s = new Scanner(p.getInputStream());
117
118 while (s.hasNextLine()) {
119 erg += (s.nextLine());
120 }
121 s.close();
122
123 f.delete();
124 } catch (IOException e) {
125 e.printStackTrace();
126 }
127 return erg;
128 }
129
130 /**
131 * @param fileName
132 * @return the extension of the file.
133 */
134 public static String suffix(String fileName) {
135 return fileName.substring(fileName.lastIndexOf(".") + 1);
136 }
137
138 /**
139 * @param fileName
140 * @return the name of the file without the extension.
141 */
142 public static String prefix(String fileName) {
143 return fileName.substring(fileName.lastIndexOf("/") + 1,
144 fileName.lastIndexOf("."));
145 }
146
147 /**
148 * @param fileName
149 * @return the directory of the specified file.
150 */
151 public static String path(String fileName) {
152 String absolutePath =
153 new File(fileName).getParentFile().getAbsolutePath();
154 return absolutePath+slash(absolutePath);
155 }
156
157 /**
158 * Check if the chromosome names in the target file are a subset of the
159 * chromosome name in the alignment file.
160 *
161 * @param alignment the alignment file.
162 * @param target the target file.
163 * @return true if target chromosomes are a subset of alignment chromosome
164 * and false otherwise.
165 */
166 public static boolean areChromosomeCompatible(File alignment, File target){
167 Scanner aScan = null, tScan = null;
168 try {
169 aScan = new Scanner(alignment);
170 tScan = new Scanner(target);
171
172 ArrayList<String> alist = new ArrayList<String>();
173 ArrayList<String> tlist = new ArrayList<String>();
174 while(aScan.hasNextLine()){
175 ReducedReadLine rl = new ReducedReadLine(aScan.nextLine());
176 if(alist.indexOf(rl.chrom())==-1)
177 alist.add(rl.chrom());
178 }
179 while(tScan.hasNextLine()){
180 TargetLine tl = new TargetLine(tScan.nextLine());
181 if(tlist.indexOf(tl.chrom())==-1)
182 tlist.add(tl.chrom());
183 }
184 for(int i = 0; i < tlist.size(); i++){
185 if(alist.indexOf(tlist.get(i))!=-1)
186 return true;
187 }
188 } catch (FileNotFoundException e) {
189 e.printStackTrace();
190 } catch (ChromosomeFormatException e) {
191 e.printStackTrace();
192 } catch (ChromosomeNotFoundException e) {
193 e.printStackTrace();
194 } catch (RangeFormatException e) {
195 e.printStackTrace();
196 } catch (RangeLimitNotFoundException e) {
197 e.printStackTrace();
198 } catch (NullOrNegativeRangeException e) {
199 e.printStackTrace();
200 }
201
202
203 return false;
204 }
205
206 }