| 1 |
package driver; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.FileInputStream; |
| 5 |
import java.io.FileNotFoundException; |
| 6 |
import java.io.FileOutputStream; |
| 7 |
import java.io.IOException; |
| 8 |
import java.io.InputStream; |
| 9 |
import java.io.OutputStream; |
| 10 |
|
| 11 |
import shared.Tools; |
| 12 |
|
| 13 |
|
| 14 |
public class MoveFiles { |
| 15 |
|
| 16 |
|
| 17 |
public static void main(String[] args){ |
| 18 |
|
| 19 |
String root=args[0].replace('\\', '/'); |
| 20 |
|
| 21 |
File dir=new File(root); |
| 22 |
assert(dir.exists()); |
| 23 |
assert(dir.isDirectory()); |
| 24 |
|
| 25 |
|
| 26 |
File[] files=dir.listFiles(); |
| 27 |
|
| 28 |
for(int chrom=1; chrom<=22; chrom++){ |
| 29 |
|
| 30 |
String key="chr"+chrom; |
| 31 |
|
| 32 |
File dest=new File(root+"/"+key); |
| 33 |
if(!dest.exists()){ |
| 34 |
dest.mkdir(); |
| 35 |
} |
| 36 |
|
| 37 |
for(File f : files){ |
| 38 |
String name=f.getName(); |
| 39 |
if(name.contains("/")){ |
| 40 |
name=name.substring(name.lastIndexOf("/")+1); |
| 41 |
} |
| 42 |
final String name2=name; |
| 43 |
|
| 44 |
if(name.contains(".")){ |
| 45 |
name=name.substring(0,name.lastIndexOf(".")); |
| 46 |
} |
| 47 |
|
| 48 |
while(name.length()>1 && !Tools.isDigit(name.charAt(name.length()-1))){ |
| 49 |
name=name.substring(0, name.length()-1); |
| 50 |
} |
| 51 |
name=name.toLowerCase(); |
| 52 |
|
| 53 |
if(f.isFile() && name.endsWith("chr"+chrom)){ |
| 54 |
copyFile(f.getAbsolutePath(), dest.getAbsolutePath()+"/"+name2); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
private static void copyFile(String src, String dst){ |
| 68 |
|
| 69 |
try{ |
| 70 |
File f1 = new File(src); |
| 71 |
File f2 = new File(dst); |
| 72 |
InputStream in = new FileInputStream(f1); |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
OutputStream out = new FileOutputStream(f2); |
| 78 |
|
| 79 |
byte[] buf = new byte[16384]; |
| 80 |
int len; |
| 81 |
while ((len = in.read(buf)) > 0){ |
| 82 |
out.write(buf, 0, len); |
| 83 |
} |
| 84 |
in.close(); |
| 85 |
out.close(); |
| 86 |
}catch(FileNotFoundException e){ |
| 87 |
throw new RuntimeException(e); |
| 88 |
}catch(IOException e){ |
| 89 |
throw new RuntimeException(e); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
} |