517f361d679855c0c4887d5f2d431c342d58ccae
[jabaws.git] / runner / compbio / pipeline / _jpred / Pairwise.java
1 /* Copyright (c) 2011 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 package compbio.pipeline._jpred;\r
19 \r
20 import java.io.BufferedInputStream;\r
21 import java.io.File;\r
22 import java.io.FileInputStream;\r
23 import java.io.FileNotFoundException;\r
24 import java.io.IOException;\r
25 import java.util.HashSet;\r
26 import java.util.List;\r
27 import java.util.Set;\r
28 \r
29 import compbio.data.sequence.FastaSequence;\r
30 import compbio.data.sequence.SequenceUtil;\r
31 \r
32 public class Pairwise {\r
33 \r
34         private final List<FastaSequence> sequences; \r
35         final Set<PScore> pscores; \r
36         \r
37         public Pairwise(List<FastaSequence> sequences) {\r
38                 this.sequences = sequences;\r
39                 pscores = new HashSet<PScore>(); \r
40         }\r
41 \r
42         void compare() {\r
43                 for (int i = 0; i < sequences.size(); i++) {\r
44                         FastaSequence seq1 = sequences.get(i); \r
45                         System.out.println(seq1.getId());\r
46                         for (int j = i+1; j < sequences.size(); j++) {\r
47                                 FastaSequence seq2 = sequences.get(j);\r
48                                 PScore pscore = new PScore();\r
49                                 pscore.name = seq1.getId();\r
50                                 pscore.otherName = seq2.getId();\r
51                                 pscores.add(pscore);\r
52                                 compare(pscore, seq1.getSequence(), seq2.getSequence());\r
53                         }\r
54                 }\r
55         }\r
56 \r
57         void compare(PScore pscore, String seq1, String seq2) { \r
58                 char[] chars1 = seq1.trim().toUpperCase().toCharArray(); \r
59                 char[] chars2 = seq2.trim().toUpperCase().toCharArray();\r
60                 if(chars1.length != chars2.length) { \r
61                         throw new IllegalArgumentException("Different lenght sequence are provided but same expected! \n Sequence 1: \n" + pscore.name+ \r
62                                         "\n Length:"+chars1.length +"\n "+ \r
63                                         "Sequence 2: \n " + pscore.otherName + \r
64                                         " \n Lenght: " + chars2.length ); \r
65                 }\r
66                 compare(pscore, chars1, chars2); \r
67         }\r
68         \r
69         void compare(PScore pscore, char[] seq1, char[] seq2) { \r
70                 int sameResedue = 0; \r
71                 for (int i = 0; i < seq1.length; i++) {\r
72                         if(seq1[i]==seq2[i]) { \r
73                                 sameResedue++;\r
74                         }\r
75                 }\r
76                 pscore.score = (double)sameResedue /seq1.length;\r
77                 System.out.println(pscore.score*100);\r
78         }\r
79         \r
80         public static final void main(String[]  args) throws FileNotFoundException, IOException {\r
81                 File in = new File(args[0]);\r
82                 List<FastaSequence> fslist = SequenceUtil.readFasta(new BufferedInputStream(new FileInputStream(in)));\r
83                 Pairwise pair = new Pairwise(fslist);\r
84                 pair.compare(); \r
85                 System.out.println(pair.pscores);\r
86         }\r
87 }\r