001/*
002 * Copyright (c) 2005-2012 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2012-04-13 17:49:27 +0000 (Fri, 13 Apr 2012) $' 
007 * '$Revision: 29712 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029package org.kepler.bio.util;
030
031import java.io.BufferedReader;
032import java.io.FileNotFoundException;
033import java.io.FileReader;
034import java.io.IOException;
035import java.math.BigDecimal;
036import java.util.ArrayList;
037import java.util.Collections;
038import java.util.Iterator;
039import java.util.List;
040
041//////////////////////////////////////////////////////////////////////////
042////BlastTabularResult
043
044/**
045* This class is for the lines in blast tabular result. Each line will be one object. 
046* They are compared by E-Value and Bit.
047* 
048* @author Jianwu Wang
049* @version $Id: BlastTabularResult.java 29712 2012-04-13 17:49:27Z crawl $
050*/
051public class BlastTabularResult implements Comparable<BlastTabularResult> {
052        
053        String queryId, subjectId, eValue;
054        float identity, bitScore;
055        int alignmentLength, mismatches, gapOpenings, qStart, qEnd, sStart, sEnd;
056        BigDecimal eValueDec;
057        
058        public BlastTabularResult(String result){
059                String[] resElements = result.split("\t");
060                this.queryId = resElements[0];
061                this.subjectId = resElements[1];
062                this.identity = new Float(resElements[2]).floatValue();
063                this.alignmentLength = new Integer(resElements[3]).intValue();
064                this.mismatches = new Integer(resElements[4]).intValue();
065                this.gapOpenings = new Integer(resElements[5]).intValue();
066                this.qStart = new Integer(resElements[6]).intValue();
067                this.qEnd = new Integer(resElements[7]).intValue();
068                this.sStart = new Integer(resElements[8]).intValue();
069                this.sEnd = new Integer(resElements[9]).intValue();
070                this.eValue = resElements[10];
071                this.bitScore = new Float(resElements[11]).floatValue();
072        }
073
074        public String getQueryId() {
075                return queryId;
076        }
077
078        public void setQueryId(String queryId) {
079                this.queryId = queryId;
080        }
081
082        public String getSubjectId() {
083                return subjectId;
084        }
085
086        public void setSubjectId(String subjectId) {
087                this.subjectId = subjectId;
088        }
089
090        public String geteValue() {
091                return eValue;
092        }
093
094        public void seteValue(String eValue) {
095                this.eValue = eValue;
096        }
097
098        public Float getIdentity() {
099                return identity;
100        }
101
102        public void setIdentity(Float identity) {
103                this.identity = identity;
104        }
105
106        public int getAlignmentLength() {
107                return alignmentLength;
108        }
109
110        public void setAlignmentLength(int alignmentLength) {
111                this.alignmentLength = alignmentLength;
112        }
113
114        public int getMismatches() {
115                return mismatches;
116        }
117
118        public void setMismatches(int mismatches) {
119                this.mismatches = mismatches;
120        }
121
122        public int getGapOpenings() {
123                return gapOpenings;
124        }
125
126        public void setGapOpenings(int gapOpenings) {
127                this.gapOpenings = gapOpenings;
128        }
129
130        public int getqStart() {
131                return qStart;
132        }
133
134        public void setqStart(int qStart) {
135                this.qStart = qStart;
136        }
137
138        public int getqEnd() {
139                return qEnd;
140        }
141
142        public void setqEnd(int qEnd) {
143                this.qEnd = qEnd;
144        }
145
146        public int getsStart() {
147                return sStart;
148        }
149
150        public void setsStart(int sStart) {
151                this.sStart = sStart;
152        }
153
154        public int getsEnd() {
155                return sEnd;
156        }
157
158        public void setsEnd(int sEnd) {
159                this.sEnd = sEnd;
160        }
161
162        public float getBitScore() {
163                return bitScore;
164        }
165
166        public void setBitScore(float bitScore) {
167                this.bitScore = bitScore;
168        }
169        
170        public String toString(){
171                return queryId + "\t" + subjectId + "\t" + identity + "\t" + alignmentLength + "\t" + mismatches +  "\t" + gapOpenings + "\t" +qStart + "\t" +qEnd + "\t" +sStart + "\t" +sEnd + "\t" + eValue + "\t" + bitScore;               
172        }
173
174        @Override
175        public int compareTo(BlastTabularResult result) {
176                result.getQueryId();
177                //same query id
178                if (this.getQueryId().equals(result.getQueryId())){
179                        this.eValueDec = new BigDecimal(this.geteValue());
180                        BigDecimal resultEValueDec = new BigDecimal(result.geteValue());
181                        int evalueCompare = eValueDec.compareTo(resultEValueDec);
182                        // same e value, compare bit
183                        if (evalueCompare == 0) {
184                                float bitCom = this.getBitScore() - result.getBitScore();
185                                if (bitCom == 0)
186                                        return 0;
187                                else if (bitCom > 0)
188                                        return -1;
189                                else
190                                        return 1;
191                        } else{
192                                return evalueCompare;
193                        }
194                } else
195                        return this.getQueryId().compareTo(result.getQueryId());
196        }
197        
198        public static void main(String[] args){
199                BufferedReader reader;
200                StringBuffer lineBuffer = null;
201                BlastTabularResult blastResult = null;
202                List<BlastTabularResult> blastResList = new ArrayList<BlastTabularResult>();
203                try {
204                        reader = new BufferedReader(new FileReader("/Users/jianwu/Projects/bioinfo/testQueryFile/testdbOtiny-PACT-cross.out11"));
205                lineBuffer = new StringBuffer();
206                while (true) {
207                    String line = reader.readLine();
208                    if (line == null) {
209                        break;
210                    }
211                    blastResult = new BlastTabularResult(line);
212                    blastResList.add(blastResult);
213                }
214                } catch (FileNotFoundException e) {
215                        e.printStackTrace();
216                } catch (IOException e) {
217                        e.printStackTrace();
218                }
219
220//              String[] lines = lineBuffer.toString().split("\n");
221                Collections.sort(blastResList);
222                Iterator blastIt = blastResList.iterator();
223                while (blastIt.hasNext()){
224                        System.out.println(blastIt.next());
225                }
226        
227        }
228
229}