4212 |
10 Apr 08 |
nicklas |
1 |
/* |
4212 |
10 Apr 08 |
nicklas |
$Id$ |
4212 |
10 Apr 08 |
nicklas |
3 |
|
4212 |
10 Apr 08 |
nicklas |
Copyright (C) Nicklas Nordborg |
4212 |
10 Apr 08 |
nicklas |
5 |
|
4212 |
10 Apr 08 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
4212 |
10 Apr 08 |
nicklas |
Available at http://base.thep.lu.se/ |
4212 |
10 Apr 08 |
nicklas |
8 |
|
4212 |
10 Apr 08 |
nicklas |
BASE is free software; you can redistribute it and/or modify it |
4212 |
10 Apr 08 |
nicklas |
under the terms of the GNU General Public License as published by |
4480 |
05 Sep 08 |
jari |
the Free Software Foundation; either version 3 of the License, or |
4212 |
10 Apr 08 |
nicklas |
(at your option) any later version. |
4212 |
10 Apr 08 |
nicklas |
13 |
|
4212 |
10 Apr 08 |
nicklas |
BASE is distributed in the hope that it will be useful, but |
4212 |
10 Apr 08 |
nicklas |
WITHOUT ANY WARRANTY; without even the implied warranty of |
4212 |
10 Apr 08 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
4212 |
10 Apr 08 |
nicklas |
General Public License for more details. |
4212 |
10 Apr 08 |
nicklas |
18 |
|
4212 |
10 Apr 08 |
nicklas |
You should have received a copy of the GNU General Public License |
4514 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
4212 |
10 Apr 08 |
nicklas |
21 |
*/ |
4720 |
08 Jan 09 |
nicklas |
22 |
import java.io.File; |
4212 |
10 Apr 08 |
nicklas |
23 |
import java.io.IOException; |
4212 |
10 Apr 08 |
nicklas |
24 |
import java.io.InputStream; |
4212 |
10 Apr 08 |
nicklas |
25 |
import java.util.ArrayList; |
4212 |
10 Apr 08 |
nicklas |
26 |
import java.util.List; |
4212 |
10 Apr 08 |
nicklas |
27 |
import java.util.regex.Pattern; |
4212 |
10 Apr 08 |
nicklas |
28 |
|
4212 |
10 Apr 08 |
nicklas |
29 |
import net.sf.basedb.core.Config; |
4212 |
10 Apr 08 |
nicklas |
30 |
import net.sf.basedb.util.Values; |
4212 |
10 Apr 08 |
nicklas |
31 |
import net.sf.basedb.util.parser.FlatFileParser; |
4212 |
10 Apr 08 |
nicklas |
32 |
|
4212 |
10 Apr 08 |
nicklas |
33 |
|
4212 |
10 Apr 08 |
nicklas |
34 |
|
4212 |
10 Apr 08 |
nicklas |
35 |
public class Base1TestPlugin |
4212 |
10 Apr 08 |
nicklas |
36 |
{ |
4212 |
10 Apr 08 |
nicklas |
37 |
|
4212 |
10 Apr 08 |
nicklas |
38 |
/** |
4212 |
10 Apr 08 |
nicklas |
* @param args |
4212 |
10 Apr 08 |
nicklas |
* @throws IOException |
4212 |
10 Apr 08 |
nicklas |
41 |
*/ |
4212 |
10 Apr 08 |
nicklas |
42 |
public static void main(String[] args) throws IOException |
4212 |
10 Apr 08 |
nicklas |
43 |
{ |
4212 |
10 Apr 08 |
nicklas |
44 |
|
4212 |
10 Apr 08 |
nicklas |
45 |
FlatFileParser ffp = getInitializedFlatFileParser(System.in); |
4212 |
10 Apr 08 |
nicklas |
46 |
System.out.println("BASEFile"); |
4212 |
10 Apr 08 |
nicklas |
47 |
|
4212 |
10 Apr 08 |
nicklas |
48 |
boolean mergeAssays = false; |
4212 |
10 Apr 08 |
nicklas |
49 |
boolean offsetPositions = false; |
4266 |
05 May 08 |
nicklas |
50 |
boolean reporterList = false; |
4720 |
08 Jan 09 |
nicklas |
51 |
boolean createSubdir = false; |
4212 |
10 Apr 08 |
nicklas |
52 |
int numSpotSections = 0; |
4212 |
10 Apr 08 |
nicklas |
53 |
while (ffp.hasMoreSections()) |
4212 |
10 Apr 08 |
nicklas |
54 |
{ |
4212 |
10 Apr 08 |
nicklas |
55 |
FlatFileParser.Line section = ffp.nextSection(); |
4212 |
10 Apr 08 |
nicklas |
56 |
ffp.parseHeaders(); |
4212 |
10 Apr 08 |
nicklas |
57 |
|
4212 |
10 Apr 08 |
nicklas |
58 |
if ("settings".equals(section.name())) |
4212 |
10 Apr 08 |
nicklas |
59 |
{ |
4212 |
10 Apr 08 |
nicklas |
60 |
mergeAssays = Values.getBoolean(ffp.getHeader("mergeAssays")); |
4212 |
10 Apr 08 |
nicklas |
61 |
offsetPositions = Values.getBoolean(ffp.getHeader("offsetPositions")); |
4266 |
05 May 08 |
nicklas |
62 |
reporterList = Values.getBoolean(ffp.getHeader("reporterList")); |
4720 |
08 Jan 09 |
nicklas |
63 |
createSubdir = Values.getBoolean(ffp.getHeader("createSubdir")); |
4212 |
10 Apr 08 |
nicklas |
64 |
} |
4266 |
05 May 08 |
nicklas |
65 |
else if ("assays".equals(section.name()) && !reporterList) |
4212 |
10 Apr 08 |
nicklas |
66 |
{ |
4212 |
10 Apr 08 |
nicklas |
67 |
System.out.println("section assays"); |
4212 |
10 Apr 08 |
nicklas |
68 |
if (mergeAssays) |
4212 |
10 Apr 08 |
nicklas |
69 |
{ |
4212 |
10 Apr 08 |
nicklas |
70 |
System.out.println("count\t1"); |
4212 |
10 Apr 08 |
nicklas |
71 |
System.out.println("columns\tid\tname\tparents"); |
4212 |
10 Apr 08 |
nicklas |
72 |
System.out.println("%"); |
4212 |
10 Apr 08 |
nicklas |
73 |
List<String> parents = new ArrayList<String>(); |
4212 |
10 Apr 08 |
nicklas |
74 |
while (ffp.hasMoreData()) |
4212 |
10 Apr 08 |
nicklas |
75 |
{ |
4212 |
10 Apr 08 |
nicklas |
76 |
FlatFileParser.Data data = ffp.nextData(); |
7665 |
20 Mar 19 |
nicklas |
77 |
parents.add(data.getString(0)); |
4212 |
10 Apr 08 |
nicklas |
78 |
} |
4212 |
10 Apr 08 |
nicklas |
79 |
System.out.println("1\tA merged assay\t" + Values.getString(parents, "/", true)); |
4212 |
10 Apr 08 |
nicklas |
80 |
System.out.println(); |
4212 |
10 Apr 08 |
nicklas |
81 |
} |
4212 |
10 Apr 08 |
nicklas |
82 |
else |
4212 |
10 Apr 08 |
nicklas |
83 |
{ |
4212 |
10 Apr 08 |
nicklas |
// Just copy all headers... |
4212 |
10 Apr 08 |
nicklas |
85 |
for (FlatFileParser.Line line : ffp.getLines()) |
4212 |
10 Apr 08 |
nicklas |
86 |
{ |
4212 |
10 Apr 08 |
nicklas |
87 |
if (line.type() == FlatFileParser.LineType.HEADER) |
4212 |
10 Apr 08 |
nicklas |
88 |
{ |
4212 |
10 Apr 08 |
nicklas |
89 |
System.out.println(line.line()); |
4212 |
10 Apr 08 |
nicklas |
90 |
} |
4212 |
10 Apr 08 |
nicklas |
91 |
} |
4212 |
10 Apr 08 |
nicklas |
92 |
System.out.println("%"); |
4212 |
10 Apr 08 |
nicklas |
// ...and the data |
4212 |
10 Apr 08 |
nicklas |
94 |
while (ffp.hasMoreData()) |
4212 |
10 Apr 08 |
nicklas |
95 |
{ |
4212 |
10 Apr 08 |
nicklas |
96 |
FlatFileParser.Data data = ffp.nextData(); |
4212 |
10 Apr 08 |
nicklas |
97 |
System.out.println(data.line()); |
4212 |
10 Apr 08 |
nicklas |
98 |
} |
4212 |
10 Apr 08 |
nicklas |
99 |
System.out.println(); |
4212 |
10 Apr 08 |
nicklas |
100 |
} |
4212 |
10 Apr 08 |
nicklas |
101 |
} |
4212 |
10 Apr 08 |
nicklas |
102 |
else if ("spots".equals(section.name())) |
4212 |
10 Apr 08 |
nicklas |
103 |
{ |
4212 |
10 Apr 08 |
nicklas |
104 |
numSpotSections++; |
4212 |
10 Apr 08 |
nicklas |
105 |
|
4266 |
05 May 08 |
nicklas |
106 |
if (reporterList) |
4212 |
10 Apr 08 |
nicklas |
107 |
{ |
4266 |
05 May 08 |
nicklas |
108 |
if (numSpotSections == 1) |
4266 |
05 May 08 |
nicklas |
109 |
{ |
4266 |
05 May 08 |
nicklas |
110 |
System.out.println("section\treporterlist"); |
4266 |
05 May 08 |
nicklas |
111 |
System.out.println("name\tA reporter list"); |
4266 |
05 May 08 |
nicklas |
112 |
System.out.println("columns\treporterId\tscore"); |
4266 |
05 May 08 |
nicklas |
113 |
System.out.println("%"); |
4266 |
05 May 08 |
nicklas |
114 |
while (ffp.hasMoreData()) |
4266 |
05 May 08 |
nicklas |
115 |
{ |
4266 |
05 May 08 |
nicklas |
116 |
FlatFileParser.Data data = ffp.nextData(); |
7665 |
20 Mar 19 |
nicklas |
117 |
System.out.println(data.getString(2) + "\t" + Math.random()); |
4266 |
05 May 08 |
nicklas |
118 |
} |
4266 |
05 May 08 |
nicklas |
119 |
} |
4266 |
05 May 08 |
nicklas |
120 |
} |
4266 |
05 May 08 |
nicklas |
121 |
else if (!mergeAssays || numSpotSections == 1) |
4266 |
05 May 08 |
nicklas |
122 |
{ |
4212 |
10 Apr 08 |
nicklas |
123 |
System.out.println("section\tspots"); |
4212 |
10 Apr 08 |
nicklas |
124 |
System.out.println("channels\t" + ffp.getHeader("channels")); |
4212 |
10 Apr 08 |
nicklas |
125 |
System.out.println("assayFields\t" + ffp.getHeader("assayFields")); |
4212 |
10 Apr 08 |
nicklas |
126 |
System.out.println("columns\t" + ffp.getHeader("columns")); |
4212 |
10 Apr 08 |
nicklas |
127 |
System.out.println("count\t" + ffp.getHeader("count")); |
4212 |
10 Apr 08 |
nicklas |
128 |
System.out.println("assays\t" + (mergeAssays ? "1" : ffp.getHeader("assays"))); |
4212 |
10 Apr 08 |
nicklas |
129 |
System.out.println("%"); |
4212 |
10 Apr 08 |
nicklas |
130 |
String offset = offsetPositions ? "1000" : ""; |
4212 |
10 Apr 08 |
nicklas |
131 |
while (ffp.hasMoreData()) |
4212 |
10 Apr 08 |
nicklas |
132 |
{ |
4212 |
10 Apr 08 |
nicklas |
133 |
FlatFileParser.Data data = ffp.nextData(); |
4631 |
07 Nov 08 |
nicklas |
134 |
System.out.println(offset + data.line().replaceFirst("\\t\\t", "\t0\t")); |
4212 |
10 Apr 08 |
nicklas |
135 |
} |
4212 |
10 Apr 08 |
nicklas |
136 |
System.out.println(); |
4212 |
10 Apr 08 |
nicklas |
137 |
} |
4720 |
08 Jan 09 |
nicklas |
138 |
} |
4212 |
10 Apr 08 |
nicklas |
139 |
} |
4720 |
08 Jan 09 |
nicklas |
140 |
if (createSubdir) |
4720 |
08 Jan 09 |
nicklas |
141 |
{ |
4720 |
08 Jan 09 |
nicklas |
142 |
File tmpDir = new File(System.getProperty("java.io.tmpdir")); |
4720 |
08 Jan 09 |
nicklas |
143 |
File subdir = new File(tmpDir, "subdir"); |
4720 |
08 Jan 09 |
nicklas |
144 |
subdir.mkdirs(); |
4720 |
08 Jan 09 |
nicklas |
145 |
File subFile = new File(subdir, "file.txt"); |
4720 |
08 Jan 09 |
nicklas |
146 |
subFile.createNewFile(); |
4720 |
08 Jan 09 |
nicklas |
147 |
} |
4212 |
10 Apr 08 |
nicklas |
148 |
} |
4212 |
10 Apr 08 |
nicklas |
149 |
|
4212 |
10 Apr 08 |
nicklas |
150 |
private static FlatFileParser getInitializedFlatFileParser(InputStream stream) |
4212 |
10 Apr 08 |
nicklas |
151 |
{ |
4212 |
10 Apr 08 |
nicklas |
152 |
FlatFileParser ffp = new FlatFileParser(); |
4212 |
10 Apr 08 |
nicklas |
153 |
ffp.setSectionRegexp(Pattern.compile("section[\\t ](.*)")); |
4212 |
10 Apr 08 |
nicklas |
154 |
ffp.setHeaderRegexp(Pattern.compile("(.*?)\\t(.*)")); |
4212 |
10 Apr 08 |
nicklas |
155 |
ffp.setDataHeaderRegexp(Pattern.compile("%")); |
4212 |
10 Apr 08 |
nicklas |
156 |
ffp.setDataFooterRegexp(Pattern.compile("^$")); |
4212 |
10 Apr 08 |
nicklas |
157 |
ffp.setDataSplitterRegexp(Pattern.compile("\\t")); |
4212 |
10 Apr 08 |
nicklas |
158 |
ffp.setUseNullIfEmpty(false); |
4212 |
10 Apr 08 |
nicklas |
159 |
ffp.setInputStream(stream, Config.getCharset()); |
4212 |
10 Apr 08 |
nicklas |
160 |
return ffp; |
4212 |
10 Apr 08 |
nicklas |
161 |
} |
4212 |
10 Apr 08 |
nicklas |
162 |
|
4212 |
10 Apr 08 |
nicklas |
163 |
|
4212 |
10 Apr 08 |
nicklas |
164 |
|
4212 |
10 Apr 08 |
nicklas |
165 |
} |