|
| 1 | +Class { |
| 2 | + #name : 'BioPhylipSmaCCParser', |
| 3 | + #superclass : 'SmaCCParser', |
| 4 | + #category : 'BioParsers-PHYLIP', |
| 5 | + #package : 'BioParsers', |
| 6 | + #tag : 'PHYLIP' |
| 7 | +} |
| 8 | + |
| 9 | +{ #category : 'instance creation' } |
| 10 | +BioPhylipSmaCCParser class >> on: aStream [ |
| 11 | + ^ self new scanner: (BioPhylipScanner on: aStream); yourself |
| 12 | +] |
| 13 | + |
| 14 | +{ #category : 'accessing' } |
| 15 | +BioPhylipSmaCCParser >> firstLineTokenizer [ |
| 16 | + |
| 17 | + ^ BioPhylipTokenizerWrapper new block: [ :input | |
| 18 | + | stream numTaxa numChars | |
| 19 | + stream := input readStream. |
| 20 | + stream skipSeparators. |
| 21 | + numTaxa := Integer readFrom: stream. |
| 22 | + stream skipSeparators. |
| 23 | + numChars := Integer readFrom: stream. |
| 24 | + Array with: numTaxa printString with: numChars printString ] |
| 25 | +] |
| 26 | + |
| 27 | +{ #category : 'accessing' } |
| 28 | +BioPhylipSmaCCParser >> parse [ |
| 29 | + |
| 30 | + | numTaxa numChars taxaNames sequences line | |
| 31 | + numTaxa := self parseNumber. |
| 32 | + numChars := self parseNumber. |
| 33 | + taxaNames := OrderedCollection new. |
| 34 | + sequences := OrderedCollection new. |
| 35 | + |
| 36 | + [ scanner stream atEnd ] whileFalse: [ |
| 37 | + line := scanner stream nextLine. |
| 38 | + line ifNotNil: [ |
| 39 | + line := line trimBoth. |
| 40 | + line ifNotEmpty: [ |
| 41 | + | name seq | |
| 42 | + name := line size <= 10 |
| 43 | + ifTrue: [ line ] |
| 44 | + ifFalse: [ (line copyFrom: 1 to: 10) trimRight ]. |
| 45 | + seq := line size <= 10 |
| 46 | + ifTrue: [ String empty ] |
| 47 | + ifFalse: [ |
| 48 | + (line copyFrom: 11 to: line size) trimBoth reject: [ |
| 49 | + :c | c = Character space ] ]. |
| 50 | + taxaNames add: name. |
| 51 | + seq ifNotEmpty: [ sequences add: seq ] ] ] ]. |
| 52 | + |
| 53 | + ^ Array |
| 54 | + with: numTaxa |
| 55 | + with: numChars |
| 56 | + with: taxaNames |
| 57 | + with: sequences |
| 58 | +] |
| 59 | + |
| 60 | +{ #category : 'parsing' } |
| 61 | +BioPhylipSmaCCParser >> parseInterleaved [ |
| 62 | + |
| 63 | + | numTaxa numChars taxaNames sequences line name seq padding | |
| 64 | + numTaxa := self parseNumber. |
| 65 | + numChars := self parseNumber. |
| 66 | + taxaNames := OrderedCollection new: numTaxa. |
| 67 | + sequences := OrderedCollection new: numTaxa. |
| 68 | + padding := String new: 10 withAll: Character space. |
| 69 | + |
| 70 | + "Read first block with species names" |
| 71 | + 1 to: numTaxa do: [ :i | |
| 72 | + line := scanner stream nextLine trimBoth. |
| 73 | + name := line size <= 10 |
| 74 | + ifTrue: [ line , (padding copyFrom: 1 to: 10 - line size) ] |
| 75 | + ifFalse: [ line copyFrom: 1 to: 10 ]. |
| 76 | + seq := line size <= 10 |
| 77 | + ifTrue: [ '' ] |
| 78 | + ifFalse: [ line copyFrom: 11 to: line size ]. |
| 79 | + taxaNames add: name. |
| 80 | + sequences add: (seq reject: [ :c | c = Character space ]) ]. |
| 81 | + |
| 82 | + "Read subsequent blocks" |
| 83 | + [ scanner stream atEnd ] whileFalse: [ |
| 84 | + line := scanner stream nextLine. |
| 85 | + line ifNotNil: [ |
| 86 | + line := line trimBoth. |
| 87 | + line ifNotEmpty: [ |
| 88 | + 1 to: numTaxa do: [ :i | |
| 89 | + scanner stream atEnd ifFalse: [ |
| 90 | + line := scanner stream nextLine trimBoth. |
| 91 | + line ifNotEmpty: [ |
| 92 | + seq := line reject: [ :c | c = Character space ]. |
| 93 | + sequences at: i put: (sequences at: i) , seq ] ] ] ] ] ]. |
| 94 | + |
| 95 | + ^ Array |
| 96 | + with: numTaxa |
| 97 | + with: numChars |
| 98 | + with: taxaNames |
| 99 | + with: sequences |
| 100 | +] |
| 101 | + |
| 102 | +{ #category : 'parsing' } |
| 103 | +BioPhylipSmaCCParser >> parseNameAndSequence: aLine into: nameBlock and: seqBlock [ |
| 104 | + |
| 105 | + | name seq | |
| 106 | + aLine size <= 10 |
| 107 | + ifTrue: [ |
| 108 | + name := aLine trimRight. |
| 109 | + seq := '' ] |
| 110 | + ifFalse: [ |
| 111 | + name := (aLine copyFrom: 1 to: 10) trimRight. |
| 112 | + seq := (aLine copyFrom: 11 to: aLine size) trimBoth. |
| 113 | + seq := seq copyReplaceAll: ' ' with: '' ]. |
| 114 | + nameBlock value: name. |
| 115 | + seqBlock value: seq |
| 116 | +] |
| 117 | + |
| 118 | +{ #category : 'parsing' } |
| 119 | +BioPhylipSmaCCParser >> parseNumber [ |
| 120 | + |
| 121 | + | token | |
| 122 | + scanner stream skipSeparators. |
| 123 | + token := '' writeStream. |
| 124 | + [ scanner stream atEnd not and: [ scanner stream peek isDigit ] ] |
| 125 | + whileTrue: [ token nextPut: scanner stream next ]. |
| 126 | + ^ token contents asInteger |
| 127 | +] |
| 128 | + |
| 129 | +{ #category : 'parsing' } |
| 130 | +BioPhylipSmaCCParser >> parseNumberFrom: stream [ |
| 131 | + |
| 132 | + | token | |
| 133 | + stream skipSeparators. |
| 134 | + token := String new writeStream. |
| 135 | + [ stream atEnd not and: [ stream peek isDigit ] ] whileTrue: [ |
| 136 | + token nextPut: stream next ]. |
| 137 | + ^ token contents asInteger |
| 138 | +] |
| 139 | + |
| 140 | +{ #category : 'parsing' } |
| 141 | +BioPhylipSmaCCParser >> parseSpeciesBlocksFrom: stream [ |
| 142 | + |
| 143 | + | results line name seq padding | |
| 144 | + results := OrderedCollection new. |
| 145 | + padding := String new: 10 withAll: Character space. |
| 146 | + [ stream atEnd ] whileFalse: [ |
| 147 | + line := stream nextLine. |
| 148 | + line ifNotNil: [ |
| 149 | + line := line trimBoth. |
| 150 | + line ifNotEmpty: [ |
| 151 | + name := line size <= 10 |
| 152 | + ifTrue: [ |
| 153 | + line , (padding copyFrom: 1 to: 10 - line size) ] |
| 154 | + ifFalse: [ line copyFrom: 1 to: 10 ]. |
| 155 | + seq := line size <= 10 |
| 156 | + ifTrue: [ '' ] |
| 157 | + ifFalse: [ line copyFrom: 11 to: line size ]. |
| 158 | + results add: (Array with: name with: seq with: nil) ] ] ]. |
| 159 | + ^ results |
| 160 | +] |
| 161 | + |
| 162 | +{ #category : 'parsing' } |
| 163 | +BioPhylipSmaCCParser >> parseSpeciesLineFrom: aString [ |
| 164 | + |
| 165 | + | line name seq padding | |
| 166 | + line := aString. |
| 167 | + padding := String new: 10 withAll: Character space. |
| 168 | + name := line size <= 10 |
| 169 | + ifTrue: [ line , (padding copyFrom: 1 to: 10 - line size) ] |
| 170 | + ifFalse: [ line copyFrom: 1 to: 10 ]. |
| 171 | + seq := line size <= 10 |
| 172 | + ifTrue: [ '' ] |
| 173 | + ifFalse: [ |
| 174 | + (line copyFrom: 11 to: line size) trimBoth reject: [ :c | |
| 175 | + c = Character space ] ]. |
| 176 | + ^ Array with: name with: seq |
| 177 | +] |
| 178 | + |
| 179 | +{ #category : 'accessing-dna' } |
| 180 | +BioPhylipSmaCCParser >> speciesDNALineTokenizer [ |
| 181 | + |
| 182 | + ^ BioPhylipTokenizerWrapper new block: [ :input | |
| 183 | + self parseSpeciesLineFrom: input ] |
| 184 | +] |
| 185 | + |
| 186 | +{ #category : 'accessing-dna' } |
| 187 | +BioPhylipSmaCCParser >> speciesDNANamedBlockTokenizer [ |
| 188 | + |
| 189 | + ^ BioPhylipTokenizerWrapper new block: [ :input | |
| 190 | + self parseSpeciesBlocksFrom: input readStream ] |
| 191 | +] |
0 commit comments