-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathBioJava:CookBook3:Stockholm.html
More file actions
105 lines (87 loc) · 5.5 KB
/
BioJava:CookBook3:Stockholm.html
File metadata and controls
105 lines (87 loc) · 5.5 KB
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
<h2 id="how-to-read-multiple-sequence-alignment-files-in-stockholm-format">How to Read Multiple Sequence Alignment files in Stockholm format</h2>
<p>StockholmFileParser is used to read MSA files written in Stockholm file
format. This example demonstrates how you can read one or more
StockholmStructure object(s) from a Stockholm file.</p>
<p>The StockholmFileParser class can read a single structure object file, a
multiple structure objects file, or an InputStream.</p>
<h3 id="to-read-a-single-object-from-a-file-you-can-simply-write">To read a single object from a file, you can simply write</h3>
<java> public static void main(String[] args){
` try {`
` StockholmFileParser parser = new StockholmFileParser();`
` String pathName= "stockholmFilePathAndName";`
` StockholmStructure structure = parser.parse(pathName);`
` `
` //use read structures`
` `
` } catch (IOException e) {`
` e.printStackTrace();`
` } catch (Exception e) {`
` e.printStackTrace();`
` }`
} </java>
<h3 id="also-you-can-read-multiple-alignments-within-the-same-file-as-follows">Also you can read multiple alignments within the same file as follows</h3>
<java> public static void main(String[] args){
` try {`
` StockholmFileParser parser = new StockholmFileParser();`
` String sourcePath=settingsManager.getSourcePath();`
` String fileName= settingsManager.getFileName();`
` FileInputStream inStream = new FileInputStream(new File(sourcePath,fileName));`
` String outputPath=settingsManager.getOutputPath();`
` parser.parse(inStream,STRUCTURES_TO_SKIP);//if you don't want to start from first structure`
` do {`
` structures = parser.parse(inStream, MAX_PER_ITERATION);`
` for (int i = 0; i < structures.size(); i++) {`
` StockholmStructure structure = structures.get(i);`
` List`<AbstractSequence<? extends AbstractCompound>`> sequences = structure.getBioSequences(true);`
` final String accessionNumber = structure.getFileAnnotation().getAccessionNumber();`
` final String identification = structure.getFileAnnotation().getIdentification().toString();`
` manageRelatedSequences(accessionNumber, identification,sequences);`
` }`
` } while (structures.size()== MAX_PER_ITERATION);`
` } catch (FileNotFoundException e) {`
` e.printStackTrace();`
` } catch (IOException e) {`
` e.printStackTrace();`
` } catch (Exception e) {`
` e.printStackTrace();`
` }`
} </java>
<h3 id="some-times-you-dont-have-a-reference-to-the-file-or-input-stream">Some times you don’t have a reference to the file or input stream</h3>
<p>Some times you use the parser in a place other than where it was
created.</p>
<p>For example, you can create a StockholmFileParser in a function <java></java></p>
<p><code class="highlighter-rouge"> public StockholmFileParser getStockholmFileParser(String filePathName) {</code><br />
<code class="highlighter-rouge"> StockholmFileParser parser = new StockholmFileParser();</code><br />
<code class="highlighter-rouge"> try {</code><br />
<code class="highlighter-rouge"> parser.parse(filePathName, 0);</code><br />
<code class="highlighter-rouge"> } catch (ParserException e) {</code><br />
<code class="highlighter-rouge"> e.printStackTrace();</code><br />
<code class="highlighter-rouge"> } catch (IOException e) {</code><br />
<code class="highlighter-rouge"> e.printStackTrace();</code><br />
<code class="highlighter-rouge"> }</code><br />
<code class="highlighter-rouge"> return parser;</code><br />
<code class="highlighter-rouge"> }</code></p>
<p></java></p>
<p>Then you use the created parser in another function, where you don’t
have a reference to its underling data source <java></java></p>
<p><code class="highlighter-rouge"> public void useParser(StockholmFileParser parser) {</code><br />
<code class="highlighter-rouge"> final int MAX_PER_ITTERATION = 10;</code><br />
<code class="highlighter-rouge"> List</code><stockholmstructure>` structures;`
` long count= 0;`
` int successfullyRead = 0;`
` do {`
` try {`
` structures = parser.parseNext(MAX_PER_ITTERATION);`
` successfullyRead = structures.size();`
` } catch (IOException e) {`
` e.printStackTrace();`
` }`
` count += successfullyRead;`
` System.out.println("reached "+count);`
` `
` //use read structures`
` `
` } while (successfullyRead== MAX_PER_ITTERATION);`
` System.out.println("TOTAL COUNT = "+count);`
` }`</stockholmstructure></p>
<p></java></p>