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