User:Lindenb/Notebook/UMR915/20110104

From OpenWetWare

Jump to: navigation, search

20110103        Top        20110105       


Contents

Eclipse!

too cloudy...

hg19

  cat chr1.fa chr2.fa chr3.fa chr4.fa chr5.fa chr6.fa  chr7.fa  chr8.fa chr9.fa chr10.fa chr11.fa\
  chr12.fa chr13.fa chr14.fa chr15.fa chr16.fa chr17.fa chr18.fa chr19.fa chr20.fa chr21.fa chr22.fa chrX.fa chrY.fa chrM.fa > hg19.fa
  
  bwa-0.5.9rc1/bwa index hg19.fa
  (....)
  Segmentation fault
  
  bwa-0.5.9rc1/bwa  index   -a bwtsw hg19.fa

Axis

source

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;



public class HelloAxisService
	{
	private BufferedImage bamImage(
			String filePath,
			String refSeq,
			int chromStart,
			int chromEnd,
			int highlight
			) throws Exception
			{
			BufferedImage img= new BufferedImage(100,100, BufferedImage.TYPE_INT_ARGB);
			Graphics2D g=img.createGraphics();
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING	, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, img.getWidth()-1, img.getHeight()-1);
			g.setColor(Color.BLACK);
			g.drawLine(0, 0, 50, 50);
			g.drawRect(0, 0, img.getWidth()-1, img.getHeight()-1);
			g.dispose();
			return img;
			}
	
	public byte[] getBamImage(
		String filePath,
		String refSeq,
		int chromStart,
		int chromEnd,
		int highlight
		) throws IOException
		{
		try
		{
		BufferedImage img= bamImage(filePath, refSeq, chromStart, chromEnd, highlight);
		ByteArrayOutputStream baos=new ByteArrayOutputStream();
		ImageIO.write(img, "PNG", baos);
		return baos.toByteArray();
		} catch(Throwable err)
		{
			throw new IOException(err);
		}
		}
	
	
	}
import java.awt.image.BufferedImage;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import generated.HelloAxisServiceStub;


public class HelloAxisClient {
public static void main(String[] args)
	{
	try {
		HelloAxisServiceStub app=new HelloAxisServiceStub("http://localhost:8080/axis2/services/HelloAxisService");
		HelloAxisServiceStub.GetBamImage param=new HelloAxisServiceStub.GetBamImage();
		param.setArgs0("");
		param.setArgs1("");
		param.setArgs2(1);
		param.setArgs3(1);
		param.setArgs4(1);
		HelloAxisServiceStub.GetBamImageResponse res=app.getBamImage(param);
		//System.err.println(res.getClass()+" "+res.get_return()+" "+res);
		InputStream in=res.get_return().getInputStream();
		BufferedImage img=ImageIO.read(in);
		in.close();
		JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(img)));
		}
	catch (Exception e)
		{
		e.printStackTrace();
		}
	}
}
axis.lib=/home/lindenb/package/axis2-1.5.1/lib
axis.jar=$(axis.lib)/*

axis:
	rm -rf myaxisservice myaxisclient
	mkdir myaxisservice myaxisclient
	mkdir myaxisservice/META-INF
	cp services.xml myaxisservice/META-INF
	javac -d myaxisservice -sourcepath src src/HelloAxisService.java
	jar cvf /home/lindenb/srv/apache-tomcat-7.0.4/webapps/axis2/WEB-INF/services/myaxisservice.aar -C myaxisservice .
	rm -rf myaxisservice
	sleep 1
	 ~/package/axis2-1.5.1/bin/wsdl2java.sh -uri "http://localhost:8080/axis2/services/HelloAxisService?wsdl" -o . -d adb -p generated
	javac  -cp '/home/lindenb/package/axis2-1.5.1/lib/*' -d myaxisclient  -sourcepath src src/HelloAxisClient.java
	java -cp '/home/lindenb/package/axis2-1.5.1/lib/*':myaxisclient HelloAxisClient
	rm -rf myaxisclient
	

Later, transformed it to display a BAM file:

Image:AxiServiceBam.png

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

import net.sf.samtools.Cigar;
import net.sf.samtools.CigarElement;
import net.sf.samtools.SAMFileReader;
import net.sf.samtools.SAMRecord;
import net.sf.samtools.SAMFileReader.ValidationStringency;
import net.sf.samtools.util.CloseableIterator;



public class HelloAxisService
	{
	
	
	private BufferedImage bamImage(
			String filePath,
			String refSeq,
			int chromStart,
			int chromEnd,
			Integer highlight
			) throws Exception
			{
			if(filePath==null  || filePath.trim().isEmpty())
				{
				throw new IllegalArgumentException("bad file");
				}
			if(refSeq==null  || refSeq.trim().isEmpty())
				{
				throw new IllegalArgumentException("bad chromosome");
				}
			if(chromStart<0 || chromStart>=chromEnd )
				{
				throw new IllegalArgumentException("bad chromStart:"+chromStart);
				}
			
			
			
			File file= new File(filePath);
			
			if( file==null ||
				!file.exists() ||
				!file.isFile() || 
				!file.canRead() ||
				!file.getName().toLowerCase().endsWith(".bam"))
				{
				throw new IllegalArgumentException("bad file:"+file);
				}

			SAMFileReader inputSam=null;
			List<SAMRecord> records=new ArrayList<SAMRecord>();
			CloseableIterator<SAMRecord> iter=null;
			
			
			try {
				inputSam=new SAMFileReader(file);
				inputSam.setValidationStringency(ValidationStringency.SILENT);
				

				iter= inputSam.queryOverlapping(
					refSeq,
					chromStart,
					chromEnd
					);			
				
				while (iter.hasNext())
			     	{
					
					final SAMRecord samRecord =iter.next();
					if(samRecord.getReadUnmappedFlag()) continue;
					//if(samRecord.getCigarString().equals("54M")) continue;
					records.add(samRecord);
			     	}
				final int fontHeight=14;
				final String fontFamily="Courier";
				Font font=new Font(fontFamily, Font.PLAIN, fontHeight);
				BufferedImage img=new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
				Graphics g1=img.getGraphics();
				FontMetrics fm=g1.getFontMetrics(font);
				g1.dispose();
				
				final int marginy=2;
				int cx=fm.getMaxAdvance()+2;
				int cy=fm.getHeight()+marginy;
				
				img=new BufferedImage(
						cx*(chromEnd - chromStart),
						Math.max(cy,cy*(records.size())),
						BufferedImage.TYPE_INT_ARGB
						);
				
				int y=0;
				Graphics2D g=(Graphics2D)img.getGraphics();
				g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				if(highlight!=null)
					{
					int x= (highlight-chromStart)*cx;
					g.setColor(Color.LIGHT_GRAY);
					g.fillRect(x, 1, cx, img.getHeight()-1);
					g.setColor(Color.RED);
					g.drawRect(x, 1, cx, img.getHeight()-1);
					}
				g.setColor(Color.BLACK);
				for(SAMRecord rec:records)
					{
					Cigar cigar=rec.getCigar();
					
					byte cigarBytes[]=rec.getReadBases();
					int byte_index=0;
					
					int pos=rec.getUnclippedStart()-chromStart;
					
					for(CigarElement elt:cigar.getCigarElements())
						{
						int cl=elt.getLength();
						switch(elt.getOperator())
							{
							case M:
								{
								while(cl>0)
								    {
									g.drawString(String.valueOf((char)cigarBytes[byte_index]), pos*cx,y+ fm.getHeight());
									
									byte_index++;
								    cl--;
								    ++pos;
								    }
								break;
								}
							case H:break; /* ignore */
						    case S:break; /* ignore */
						    case P: //...
						    case N://....
						    case D:
								{
								/* Spans positions, No Coverage */
									while(cl>0)
								    {
									pos++;
									cl--;
								    }
								break;
								}
						    case I: /* cursor not moved on reference */ ;
						    	{
						    	g.setColor(Color.GREEN);
						    	g.drawRect(pos*cx-2, y, 2,cy);
						    	g.setColor(Color.BLACK);
						    	while(cl>0)
								    {
						    		byte_index++;
									cl--;
								    }
						    	break;
						    	}
						    default: throw new IllegalArgumentException(elt.getOperator().toString());

							}
						}
					System.err.println( " "+rec.getCigarString()+" "+rec.getAlignmentStart()+" "+chromStart+" "+y);
					y+=cy;
					}
				
				g.setColor(Color.BLACK);
				g.drawRect(0, 0, img.getWidth()-1, img.getHeight()-1);
				g.dispose();
				
				return img;
				}
			catch (Exception err)
				{
				throw err;
				}
			finally
				{
				if(iter!=null) try {iter.close();} catch(Exception err){}
			    if(inputSam!=null) try {inputSam.close();} catch(Exception err){}
			    }
			}
	
	public byte[] getBamImage(
		String filePath,
		String refSeq,
		int chromStart,
		int chromEnd,
		int highlight
		) throws IOException
		{
		try
		{
		BufferedImage img= bamImage(filePath, refSeq, chromStart, chromEnd, highlight);
		ByteArrayOutputStream baos=new ByteArrayOutputStream();
		ImageIO.write(img, "PNG", baos);
		return baos.toByteArray();
		} catch(Throwable err)
		{
			throw new IOException(err);
		}
		}
	
	public static void main(String[] args)
		{
		try
		{
		BufferedImage img=ImageIO.read(new ByteArrayInputStream(new HelloAxisService().getBamImage(
				"/home/lindenb/tmp/DATASANGER/chr1.sorted.bam", "chr1", 100, 200,150)));
		JScrollPane scroll=new JScrollPane(new JLabel(new ImageIcon(img)));
		scroll.setPreferredSize(new Dimension(300,300));
		JOptionPane.showMessageDialog(null, scroll);
		} catch(Exception err)
		{
			err.printStackTrace();
		}
		}
	}
Personal tools