Remove surplus code and add format of the source file
[proteocache.git] / datadb / compbio / cassandra / JpredParserHTTP.java
1 package compbio.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.ArrayList;
13 import java.util.Calendar;
14 import java.util.Date;
15 import java.util.List;
16
17 import compbio.cassandra.JpredParser;
18
19 public class JpredParserHTTP implements JpredParser {
20         private CassandraCreate cc = new CassandraCreate();
21         private String dirprefix;
22
23         JpredParserHTTP() {
24                 dirprefix = "http://www.compbio.dundee.ac.uk/www-jpred/results";
25         }
26
27         JpredParserHTTP(String sourceurl) {
28                 dirprefix = sourceurl;
29         }
30
31         public void setSource(String newsourceprefix) {
32                 dirprefix = newsourceprefix;
33         }
34
35         public void Parsing(String source, int nDays) {
36                 Calendar cal = Calendar.getInstance();
37                 cal.add(Calendar.DATE, -nDays);
38                 for (int i = 0; i < nDays; ++i) {
39                         cal.add(Calendar.DATE, 1);
40                         int month = cal.get(Calendar.MONTH) + 1;
41                         int year = cal.get(Calendar.YEAR);
42                         int day = cal.get(Calendar.DATE);
43                         String date = year + "/" + month + "/" + day;
44                         ParsingForDate(source, date);
45                 }
46         }
47
48         private void ParsingForDate(String input, String date) {
49                 int totalcount = 0;
50                 int countNoData = 0;
51                 int countUnclearFASTAid = 0;
52                 int countinsertions = 0;
53                 int countinserted = 0;
54                 int counAlignments = 0;
55                 int countStrange = 0;
56
57                 System.out.println("Inserting jobs for " + date);
58                 try {
59                         URL url = new URL(input);
60                         URLConnection conn = url.openConnection();
61                         BufferedReader alljobs = new BufferedReader(new InputStreamReader(conn.getInputStream()));
62                         String line;
63
64                         while ((line = alljobs.readLine()) != null) {
65                                 if (line.matches(date + "(.*)jp_[^\\s]+")) {
66                                         String[] table = line.split("\\s+");
67                                         // Format of a record:
68                                         // starttime endtime ip email jobid (directory)
69                                         // 013/10/25:21:55:7 2013/10/25:21:59:13 201.239.98.172 unknown_email jp_J9HBCBT
70                                         String id = table[table.length - 1];
71                                         totalcount++;
72                                         if (!cc.CheckID(id)) {
73                                                 String datalink = dirprefix + "/" + id + "/" + id + ".concise.fasta";
74                                                 URL urltable = new URL(datalink);
75                                                 HttpURLConnection httpConnection = (HttpURLConnection) urltable.openConnection();
76                                                 int responsecode = httpConnection.getResponseCode();
77                                                 if (199 < responsecode && responsecode < 300) {
78                                                         try {
79                                                                 final FastaReader fr = new FastaReader(urltable.openStream());
80                                                                 final List<FastaSequence> seqs = new ArrayList<FastaSequence>();
81                                                                 String newprotein = "";
82                                                                 while (fr.hasNext()) {
83                                                                         final FastaSequence fs = fr.next();
84                                                                         if (fs.getId().equals("QUERY") || fs.getId().equals(id))
85                                                                                 newprotein = fs.getSequence().replaceAll("\n", "");
86                                                                         else
87                                                                                 seqs.add(fs);
88                                                                 }
89                                                                 if (newprotein.equals("")) {
90                                                                         countUnclearFASTAid++;
91                                                                 } else {
92                                                                         SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
93                                                                         String dateInString1 = table[0].substring(0, table[0].indexOf(":"));
94                                                                         long dateWork1 = 0;
95                                                                         try {
96                                                                                 Date dat1 = formatter.parse(dateInString1);
97                                                                                 dateWork1 = dat1.getTime();
98                                                                         } catch (ParseException e) {
99                                                                                 e.printStackTrace();
100                                                                         }
101                                                                         cc.InsertData(dateWork1, table[0], table[1], table[2], id, "OK", "OK", newprotein, seqs);
102                                                                         ++countinsertions;
103                                                                         // flush every 100 insertions
104                                                                         if (0 == countinsertions % 100) {
105                                                                                 cc.flushData();
106                                                                         }
107                                                                 }
108                                                         } catch (IOException e) {
109                                                                 e.printStackTrace();
110                                                         }
111                                                 } else {
112                                                         countNoData++;
113                                                 }
114                                         } else {
115                                                 ++countinserted;
116                                         }
117                                 } else {
118                                         if (line.matches(date + "(.*)Sequence0/(.*)")) {
119                                                 ++counAlignments;
120                                         } else {
121                                                 ++countStrange;
122                                         }
123                                 }
124                         }
125                         alljobs.close();
126                         System.out.println("Total number of jobs = " + totalcount);
127                         System.out.println("   " + countinserted + " jobs inserted already");
128                         System.out.println("   " + counAlignments + " jalview jobs");
129                         System.out.println("   " + countStrange + " not analysed jobs");
130                         System.out.println("   " + countNoData + " jobs without *.concise.fasta file");
131                         System.out.println("   " + countUnclearFASTAid + " jobs with unclear FASTA protein id in *.concise.fasta");
132                         System.out.println("   " + countinsertions + " new job insertions\n");
133                 } catch (MalformedURLException e) {
134                         e.printStackTrace();
135                 } catch (IOException e) {
136                         e.printStackTrace();
137                 }
138         }
139 }