My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package fr.inserm.umr915.core.bio.fasta;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.lindenb.io.IOUtils;
import org.lindenb.lang.InvalidXMLException;
import org.lindenb.sw.vocabulary.DC;
import org.lindenb.sw.vocabulary.RDF;
import org.lindenb.util.StringUtils;
import org.lindenb.xml.XMLUtilities;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class FastaIndexer
{
public static final String NS="uri:fr.inserm.umr915:fastaidx:1.0:";

@SuppressWarnings("unused")
private static final Logger LOG=Logger.getLogger("fr.inserm.umr915");


private FastaIndexer()
{

}


protected void echo(XMLStreamWriter w,FastaIndex fi) throws XMLStreamException
{
w.writeStartElement("fi","Index",FastaIndexer.NS);

w.writeCharacters("\n\t");

w.writeEmptyElement("fi","source",FastaIndexer.NS);
w.writeAttribute("rdf", RDF.NS, "resource","file://"+fi.getFile().getAbsolutePath());

w.writeCharacters("\n\t");

for(String name: fi.getNames())
{
w.writeStartElement("dc","title",DC.NS);
w.writeCharacters(name);
w.writeEndElement();
}
w.writeCharacters("\n\t");

w.writeStartElement("fi","line-size",FastaIndexer.NS);
w.writeAttribute("rdf", RDF.NS, "datatype", "http://www.w3.org/2001/XMLSchema#int");
w.writeCharacters(String.valueOf(fi.lineSize));
w.writeEndElement();

w.writeCharacters("\n\t");

w.writeStartElement("fi","start",FastaIndexer.NS);
w.writeAttribute("rdf", RDF.NS, "datatype", "http://www.w3.org/2001/XMLSchema#unsignedLong");
w.writeCharacters(String.valueOf(fi.getSeqStart()));
w.writeEndElement();

w.writeCharacters("\n\t");

w.writeStartElement("fi","end",FastaIndexer.NS);
w.writeAttribute("rdf", RDF.NS, "datatype", "http://www.w3.org/2001/XMLSchema#unsignedLong");
w.writeCharacters(String.valueOf(fi.getSeqEnd()));
w.writeEndElement();

w.writeCharacters("\n\t");

w.writeStartElement("fi","length",FastaIndexer.NS);
w.writeAttribute("rdf", RDF.NS, "datatype", "http://www.w3.org/2001/XMLSchema#int");
w.writeCharacters(String.valueOf(fi.getLength()));
w.writeEndElement();

w.writeCharacters("\n");

w.writeEndElement();

w.writeCharacters("\n");
}

private void build(XMLStreamWriter out,File fastaFile) throws XMLStreamException,IOException
{
if(!fastaFile.isAbsolute())
{
throw new IOException("Not an absolute file path:"+fastaFile);
}
int curr_line_size=0;
InputStream in=new BufferedInputStream(new FileInputStream(fastaFile));
FastaIndex currIndex=null;
long offset=-1L;

while(true)
{
int c=in.read();
if(c!=-1) offset++;
//end of line or end of file
if(currIndex!=null && (c==-1 || (c=='\n')))
{
currIndex.lineSize=Math.max(currIndex.lineSize,curr_line_size);
curr_line_size=0;
}

if(c==-1 || c=='>')
{
if(currIndex!=null) echo(out,currIndex);

if(c==-1) break;
StringBuilder name=new StringBuilder();
currIndex=new FastaIndex();
currIndex.source=fastaFile;
currIndex.nameStart= offset;
while(true)
{
c=in.read();
if(c==-1) throw new IOException("unfinished name at offset "+offset);
offset++;
if(c=='\n') break;
name.append((char)c);
}

currIndex.nameEnd=offset;
String seqName=name.toString().trim();
currIndex.names.add(seqName);
if(seqName.toLowerCase().startsWith("chrom"))
{
seqName=seqName.substring(5).trim();
currIndex.names.add(seqName);
}
else if(seqName.toLowerCase().startsWith("chr"))
{
seqName=seqName.substring(3).trim();
currIndex.names.add(seqName);
}
currIndex.seqStart=offset+1;
currIndex.seqEnd=offset+1;
currIndex.sequence_length=0;
curr_line_size=0;
}
else if(currIndex!=null)
{
if(c=='\n')
{
//already processed ignore
}
else if(Character.isLetter(c))
{
currIndex.seqEnd= offset+1L;
currIndex.sequence_length++;
curr_line_size++;
}
else
{
throw new IOException("Illegal character "+(char)c+" at "+offset);
}
}
}
in.close();
}



public static List<FastaIndex> readIndex(File file) throws IOException
{
Set<String> titles= new HashSet<String>();
List<FastaIndex> indexes=new ArrayList<FastaIndex>();
try {
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
factory.setCoalescing(true);
factory.setExpandEntityReferences(true);
factory.setValidating(false);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(true);
factory.setNamespaceAware(true);
DocumentBuilder builder=factory.newDocumentBuilder();

Document dom=builder.parse(file);
Element root=dom.getDocumentElement();
if(!XMLUtilities.isA(root, RDF.NS,"RDF"))
{
throw new InvalidXMLException(root, "not a rdf:RDF");
}
for(Element m:XMLUtilities.elements(root, FastaIndexer.NS, "Index"))
{

FastaIndex fi=new FastaIndex();
Element e= XMLUtilities.one(m, FastaIndexer.NS,"line-size");
fi.lineSize=Integer.parseInt(e.getTextContent());

e= XMLUtilities.one(m, FastaIndexer.NS,"start");
fi.seqStart=Long.parseLong(e.getTextContent());

e= XMLUtilities.one(m, FastaIndexer.NS,"end");
fi.seqEnd=Long.parseLong(e.getTextContent());


e= XMLUtilities.one(m, FastaIndexer.NS,"length");
fi.sequence_length=Integer.parseInt(e.getTextContent());

e= XMLUtilities.one(m, FastaIndexer.NS,"source");
String src= e.getAttributeNS(RDF.NS,"resource");
if(!src.startsWith("file://")) throw new InvalidXMLException(e,""+src+" should start with file://");
src=src.substring(7);

if(StringUtils.isBlank(src)) throw new InvalidXMLException(e,"not @rdf:resource");
fi.source=new File(src);
if(!fi.source.isAbsolute()) throw new InvalidXMLException(e,"not absolute path in @rdf:resource="+fi.source);

List<Element> names= XMLUtilities.elements(m, DC.NS,"title");
if(names.isEmpty()) throw new InvalidXMLException(m,"Names missing in "+file);
for(Element e2: names)
{
String name=e2.getTextContent();
if(titles.contains(name))
{
throw new InvalidXMLException(m,"name defined twice:"+name);
}
}
for(Element e2: names)
{
String name=e2.getTextContent();
titles.add(name);
fi.names.add(name);
}



indexes.add(fi);
}
return indexes;
}
catch (IOException e)
{
throw e;
}
catch (Throwable e)
{
throw new IOException(e);
}
}

public static void main(String[] args)
{
int optind=0;

try {
FastaIndexer app=new FastaIndexer();
Set<File> files=new HashSet<File>();
while(optind<args.length)
{
if(args[optind].equals("-h"))
{
System.err.println("Creates an index (XML format) for some FASTA sequences.\n");
System.err.println(" -i <file> (optional) read a file containing some absolute path to FASTA");
System.err.println(" <stdin with file path|files>");
return;
}
else if(args[optind].equals("-i"))
{
BufferedReader in=IOUtils.openReader(args[++optind]);
String line;
while((line=in.readLine())!=null)
{
if(line.startsWith("#") || StringUtils.isBlank(line)) continue;
files.add(new File(line.trim()));
}
in.close();
}
else if(args[optind].equals("--"))
{
optind++;
break;
}
else if(args[optind].startsWith("-"))
{
System.err.println("Unnown option: "+args[optind]);
return;
}
else
{
break;
}
++optind;
}
if(optind==args.length && files.isEmpty())
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String line;
while((line=in.readLine())!=null)
{
if(line.startsWith("#") || StringUtils.isBlank(line)) continue;
files.add(new File(line.trim()));
}
}

while(optind< args.length)
{
String inputName=args[optind++];
File file=new File(inputName);
if(!file.isAbsolute())
{
System.err.println("Not an absolute file path:"+file);
return;
}
files.add(file);
}

if(files.isEmpty())
{
System.err.println("Files missing");
return;
}
XMLOutputFactory xmlfactory= XMLOutputFactory.newInstance();
XMLStreamWriter w= xmlfactory.createXMLStreamWriter(System.out,"UTF-8");
w.writeStartDocument("UTF-8","1.0");
w.writeStartElement("rdf","RDF",RDF.NS);
w.writeAttribute("xmlns", XMLConstants.XML_NS_URI, "rdf", RDF.NS);
w.writeAttribute("xmlns", XMLConstants.XML_NS_URI, "fi", FastaIndexer.NS);
w.writeAttribute("xmlns", XMLConstants.XML_NS_URI, "dc", DC.NS);
w.writeCharacters("\n");
for(File file:files)
{
app.build(w,file);
}
w.writeEndElement();
w.writeCharacters("\n");
w.flush();
w.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

Change log

r74 by plindenbaum.u915 on May 18, 2010   Diff
cont
Go to: 
Project members, sign in to write a code review

Older revisions

r71 by plindenbaum.u915 on May 17, 2010   Diff
continue
r70 by plindenbaum.u915 on May 17, 2010   Diff
sequence indexing
r68 by plindenbaum.u915 on May 14, 2010   Diff
cont
All revisions of this file

File info

Size: 9540 bytes, 351 lines
Powered by Google Project Hosting