Add real NoSQL sources
[jabaws.git] / webservices / compbio / nosql / cassandra / DataParsing.java
1 package combio.cassandra;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.net.HttpURLConnection;
7 import java.net.MalformedURLException;
8 import java.net.URL;
9 import java.net.URLConnection;
10 import java.text.ParseException;
11 import java.text.SimpleDateFormat;
12 import java.util.Calendar;
13 import java.util.Date;
14
15 public class DataParsing {
16         private CassandraCreate cc = new CassandraCreate();
17         
18         public void ParsingTest(String input) {
19                 System.out.println("Inserting.....");
20                 URL url, urltable;
21 //              int count = 0;
22 //              int countNotM = 0;
23 //              int countEr = 0;
24 //              int countEmp = 0;
25                 Calendar cal = Calendar.getInstance();
26                 String date = cal.get(Calendar.YEAR) + "/" + cal.get(Calendar.MONTH) + "/" + cal.get(Calendar.DAY_OF_MONTH);     
27                 try {
28                         url = new URL(input);
29                         URLConnection conn = url.openConnection();
30                         BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
31                         String line;    
32                         
33                         while ((line = br.readLine()) != null) {
34                                 if (line.matches(date + "(.*)jp_[^\\s]+")) {    
35                                         String[] table;
36                                         table = line.split("\\s+");
37         //                              count++;
38                                                 if (!cc.CheckIP(table[table.length - 1])) {             
39         //                                              countNotM++;
40                                                         urltable = new URL("http://www.compbio.dundee.ac.uk/www-jpred/results/" +table[table.length - 1]+"/"+table[table.length - 1]+".concise.fasta");
41                                                         HttpURLConnection httpConnection = (HttpURLConnection) urltable.openConnection();
42                                                         if (httpConnection.getResponseCode() > 200) {
43 //                                                              countEr++;
44                                                                 continue;                                       
45                                                         }
46                                                         try {
47                                                                 BufferedReader br1 = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
48                                                                 String lineNext;
49                                                                 String newProt = "";
50                                                                 String jnetpred = "";
51                                                                 while ((lineNext = br1.readLine()) != null) {
52                                                                         if (lineNext.equals(">QUERY")) {
53                                                                                 while (!(lineNext = br1.readLine()).matches(">[^\\s]+"))
54                                                                                         newProt += lineNext;
55                                                                         } else if (lineNext.equals(">jnetpred")) {
56                                                                                 while (!(lineNext = br1.readLine()).matches(">[^\\s]+"))
57                                                                                         jnetpred += lineNext;
58                                                                         }                                                               
59                                                                 }
60                                                                 br1.close();
61                                                                 if (newProt.length() <= 1) {            
62                 //                                                      countEmp++;
63                                                                         continue;
64                                                                 }
65                                                                 
66                                                                 SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
67                                                 
68                                                                 String dateInString1 = table[0].substring(0, table[0].indexOf(":"));
69                                                                 
70                                                                 long dateWork1 = 0;     
71                                                                         
72                                                                 try {
73                                                                         Date dat1 = formatter.parse(dateInString1);
74                                                                         dateWork1 = dat1.getTime();
75                                                                         
76                                                                 } catch (ParseException e) {
77                                                                         e.printStackTrace();
78                                                                 }
79                                                                 cc.InsertData(dateWork1, table[0], table[1], table[2], table[table.length - 1], "OK", "OK", newProt, jnetpred);
80                                                         } catch (IOException e) {
81                                                                 //                      e.printStackTrace();
82                                                         }       
83                                         //      }       
84                                         }
85                                 } 
86                         }
87                         br.close();     
88 //                      System.out.println("Match " + count);
89 //                      System.out.println("Not Match " + countNotM);
90 //                      System.out.println("Error " + countEr);
91 //                      System.out.println("No protein " + countEmp);
92                 } catch (MalformedURLException e) {
93                         e.printStackTrace();
94                 } catch (IOException e) {
95                         e.printStackTrace();
96                 }
97         }
98 }