import java.io.*;
import java.util.*;
import org.biojava.bio.program.sax.*;
import org.biojava.bio.program.ssbind.*;
import org.biojava.bio.search.*;
import org.biojava.bio.seq.db.*;
import org.xml.sax.*;
import org.biojava.bio.*;
public class BlastParser {
// args[0] 为Blast输出文件
public static void main(String[] args){
try{
// 以流的方式得到Blast输出
InputStream is = new FileInputStream(args[0]);
// 创建一个Blast类型的SAX解析器
BlastLikeSAXParser parser = new BlastLikeSAXParser();
// 创建一个可以将事件传送给句柄的SAX事件适配器
SeqSimilarityAdapter adapter = new SeqSimilarityAdapter();
// 给parser设置SAX事件适配器
parser.setContentHandler(adapter);
// 保存序列相似性搜索结果(SeqSimilaritySearchResults)的链表
List results = new ArrayList();
// 创建在结果链表(Results List)中生成序列相似性搜索结果对象的搜索内容句柄(SearchContentHandler)
SearchContentHandler builder = new BlastLikeSearchBuilder(results,
new DummySequenceDB("queries"), new DummySequenceDBInstallation());
// 解析文件,序列相似性搜索结果填充到结果链表
parser.parse(new InputSource(is));
// 输出一些Blast细节
for (Iterator i = results.iterator(); i.hasNext(); ){
SeqSimilaritySearchResult result = (SeqSimilaritySearchResult)i.next();
Annotation anno = result.getAnnotation();
for(Iterator j = anno.keys().iterator(); j.hasNext(); ){
Object key = j.next();
Object property = anno.getProperty(key);
System.out.println(key+" : "+property);
}
System.out.println("Hits: ");
// 列出匹配
for (Iterator k = result.getHits().iterator(); k.hasNext(); ){
SeqSimilaritySearchHit hit = (SeqSimilaritySearchHit)k.next();
System.out.println("\tmatch: "+hit.getSubjectID());
System.out.println("\te score: "+hit.getEValue());
}
System.out.println("\n");
}
}
catch (SAXException ex) {
//XML 出问题
ex.printStackTrace();
}catch (IOException ex) {
//IO 出问题,例如文件找不到
ex.printStackTrace();
}
}
}