Created
July 24, 2013 10:57
-
-
Save timyates/6069641 to your computer and use it in GitHub Desktop.
Groovy version of 'Low resolution sequence alignment visualization' by @lindenb: https://github.com/lindenb/jvarkit#biostar77288
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Insets | |
import groovy.xml.StreamingMarkupBuilder | |
import groovy.transform.* | |
class Biostar77288 { | |
private int ALN_WIDTH = 1000 | |
private Map<String,Seq> sequences = [:].withDefault { new Seq( name:it ) } | |
private int maxLength = 0 | |
private int maxName = 0 | |
private int featureHeight = 12 | |
private class Seq { | |
String name | |
StringBuffer sequence = new StringBuffer() | |
boolean isGap( int idx ) { | |
idx < 0 || idx >= sequence.length() || !Character.isLetter( sequence.charAt( idx ) ) | |
} | |
} | |
private double base2Pix( int n ) { | |
( n / maxLength ) * ALN_WIDTH + maxName * featureHeight | |
} | |
void doWork() { | |
System.in.eachLine { line -> | |
int ws = 0 | |
if( !line.empty && !line.startsWith( 'CLUSTAL W' ) && !Character.isWhitespace( line.charAt( 0 ) ) && line.indexOf( ' ' ) >= 1 ) { | |
line.split( ' ', 2 ).toList().with { key, sequence -> | |
sequences[ key ].with { s -> | |
s.sequence.append( sequence.trim() ) | |
maxName = Math.max( s.name.length() - 1, maxName ) | |
maxLength = Math.max( s.sequence.length(), maxLength ) | |
} | |
} | |
} | |
} | |
Insets svgInset=new Insets( 10, 10, 10, 10 ) | |
Insets rowInset=new Insets( 7, 7, 7, 7 ) | |
double halfFeatureHeight = featureHeight / 2.0 | |
// Print out svg | |
println new StreamingMarkupBuilder().bind { | |
mkp.xmlDeclaration() | |
svg( xmlns : 'http://www.w3.org/2000/svg', | |
width : maxName * featureHeight + 1000 + svgInset.top + svgInset.left, | |
height : sequences.size() * ( featureHeight + rowInset.top + rowInset.left ) + ( svgInset.top + svgInset.left ), | |
style : "stroke:none;stroke-width:1px;text-anchor:start;font-size:${featureHeight}px;font-family:Courier;", | |
version: '1.0' ) { | |
def y = svgInset.top | |
defs { | |
linearGradient( id:'grad', x1: 0, x2: 0, y1:0, y2: 1 ) { | |
stop( offset: '0%', 'stop-color': 'blue' ) | |
stop( offset: '50%', 'stop-color': 'white' ) | |
stop( offset: '100%', 'stop-color': 'blue' ) | |
} | |
} | |
sequences.each { name, seq -> | |
y += rowInset.top | |
g( transform: "translate( $svgInset.left,$y )" ) { | |
text( seq.name, x: 0, y: featureHeight ) | |
int prevK, k1 = 0 | |
while( k1 < maxLength ) { | |
if( seq.isGap( k1 ) ) { k1++ ; continue } | |
int k2 = k1 | |
while( k2 < maxLength && !seq.isGap( k2 ) ) { k2++ } | |
line( style: 'stroke:black;', x1: base2Pix( prevK ), y1: halfFeatureHeight, x2: base2Pix( k2 ), y2: halfFeatureHeight ) | |
rect( x: base2Pix( k1 ), y: 0, rx: halfFeatureHeight, ry: halfFeatureHeight, width: base2Pix( k2 ) - base2Pix( k1 ), height: featureHeight, style: 'stroke:black; fill: url(#grad)' ) | |
ellipse( cx: base2Pix( k2 ) - halfFeatureHeight, cy: halfFeatureHeight, rx: halfFeatureHeight, ry: halfFeatureHeight, style: 'fill:blue;stroke:black;' ) | |
k1 = k2 | |
prevK = k2 | |
} | |
line( style: 'stroke:black;', x1: base2pix( prevK ), y1: halfFeatureHeight, x2: base2pix( maxLength ), y2: halfFeatureHeight ) | |
y += rowInset.bottom | |
} | |
} | |
} | |
} | |
} | |
static main( args ) { | |
new Biostar77288().doWork() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'll put this here again so the links work:
Groovy version of 'Low resolution sequence alignment visualization' by @lindenb:
https://github.com/lindenb/jvarkit#biostar77288
To run it, type: