1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.utgenome.shell;
26
27 import java.io.File;
28 import java.io.FileReader;
29 import java.io.InputStreamReader;
30 import java.sql.SQLException;
31
32 import org.utgenome.format.fasta.FASTA2Db;
33 import org.utgenome.format.silk.read.ReadDBBuilder;
34 import org.utgenome.shell.db.bed.BEDDatabaseGenerator;
35 import org.xerial.util.log.Logger;
36 import org.xerial.util.opt.Argument;
37 import org.xerial.util.opt.Option;
38 import org.xerial.util.opt.OptionParser;
39
40
41
42
43
44
45
46
47 public class Import extends UTGBShellSubCommandBase {
48
49 private static Logger _logger = Logger.getLogger(Import.class);
50 private OptionParser parser = null;
51
52 private enum FileType {
53 AUTO, READ, BED, FASTA
54 }
55
56 @Option(symbol = "t", longName = "type", description = "specify the input file type: (AUTO, FASTA, READ, BED)")
57 private FileType fileType = FileType.AUTO;
58
59 @Argument(index = 0, required = false)
60 private String inputFilePath = null;
61
62 @Option(symbol = "o", longName = "output", varName = "DB FILE NAME", description = "output SQLite DB file name")
63 private String outputFileName;
64
65 @Option(symbol = "w", longName = "overwrite", description = "overwrite existing DB files")
66 private boolean overwriteDB = false;
67
68 private OptionParser getOptionParser() {
69 if (parser == null)
70 parser = new OptionParser(this);
71
72 return parser;
73 }
74
75 @Override
76 public void execute(String[] args) throws UTGBShellException {
77
78 File input = null;
79 try {
80 getOptionParser().parse(args);
81
82 if (inputFilePath == null)
83 _logger.info("use STDIN for the input");
84 else {
85 _logger.info("input file: " + inputFilePath);
86 input = new File(inputFilePath);
87 if (!input.exists())
88 throw new UTGBShellException("file not found: " + inputFilePath);
89 }
90
91 _logger.info("file type: " + fileType);
92
93 if (outputFileName == null) {
94
95
96 outputFileName = String.format("%s.sqlite", inputFilePath);
97 int count = 1;
98 if (!overwriteDB) {
99 while (new File(outputFileName).exists()) {
100 outputFileName = String.format("%s.sqlite.%d", inputFilePath, count++);
101 }
102 }
103 }
104
105 _logger.info("output file: " + outputFileName);
106
107 switch (fileType) {
108 case READ: {
109 ReadDBBuilder builder = new ReadDBBuilder(outputFileName);
110 if (input != null)
111 builder.build(input.toURL());
112 else
113 builder.build(new InputStreamReader(System.in));
114 break;
115 }
116 case BED: {
117 if (input != null)
118 BEDDatabaseGenerator.toSQLiteDB(new FileReader(input), outputFileName);
119 else
120 BEDDatabaseGenerator.toSQLiteDB(new InputStreamReader(System.in), outputFileName);
121 break;
122 }
123 case FASTA:
124 if (input != null)
125 FASTA2Db.main(new String[] { inputFilePath, "-o", outputFileName });
126 else
127 FASTA2Db.main(new String[] { "-o", outputFileName });
128 break;
129 case AUTO:
130 default: {
131 _logger.warn("file type (-t) must be given");
132 break;
133 }
134 }
135
136 }
137 catch (SQLException e) {
138 e.printStackTrace();
139 }
140 catch (Exception e) {
141 _logger.error(e);
142 }
143
144 }
145
146 @Override
147 public String name() {
148 return "import";
149 }
150
151 public String getDetailedDescription() {
152 return loadUsage("help-import.txt");
153 }
154
155 public String getOneLinerDescription() {
156 return "import a file and create a new database";
157 }
158
159 public String getOptionList() {
160 return getOptionParser().getUsage();
161 }
162 }