001package com.hfg.bio.seq.format.genbank;
002
003import java.util.ArrayList;
004import java.util.List;
005import java.util.regex.Matcher;
006import java.util.regex.Pattern;
007
008import com.hfg.math.Range;
009import com.hfg.util.CompareUtil;
010import com.hfg.util.StringUtil;
011
012//------------------------------------------------------------------------------
013/**
014 * Literature citation container.
015 *
016 * @author J. Alex Taylor, hairyfatguy.com
017 */
018//------------------------------------------------------------------------------
019// com.hfg XML/HTML Coding Library
020//
021// This library is free software; you can redistribute it and/or
022// modify it under the terms of the GNU Lesser General Public
023// License as published by the Free Software Foundation; either
024// version 2.1 of the License, or (at your option) any later version.
025//
026// This library is distributed in the hope that it will be useful,
027// but WITHOUT ANY WARRANTY; without even the implied warranty of
028// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
029// Lesser General Public License for more details.
030//
031// You should have received a copy of the GNU Lesser General Public
032// License along with this library; if not, write to the Free Software
033// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
034//
035// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
036// jataylor@hairyfatguy.com
037//------------------------------------------------------------------------------
038
039public class GenBankReference implements Comparable<GenBankReference>
040{
041   private List<String> mAuthors;
042   private String mTitle;
043   private String mJournal;
044   private String mSubmissionRecordInfo;
045   private Integer mVolume;
046   private Integer mIssue;
047   private Integer mYear;
048   private Range<Integer> mPages;
049   private Integer mPubmedId;
050
051   private static final Pattern sJournalPattern = Pattern.compile("^(.+?)\\s+(\\d+)\\s+\\((\\d+)\\),\\s+(\\d+)-(\\d+)\\s+\\((\\d{4})\\)$");
052
053   //##########################################################################
054   // PUBLIC METHODS
055   //##########################################################################
056
057   //--------------------------------------------------------------------------
058   @Override
059   public boolean equals(Object inObj2)
060   {
061      boolean result = false;
062
063      if (inObj2 != null
064            && inObj2 instanceof GenBankReference)
065      {
066         result = (0 == compareTo((GenBankReference)inObj2));
067      }
068
069      return result;
070   }
071
072   //--------------------------------------------------------------------------
073   @Override
074   public int hashCode()
075   {
076      int hashCode = getAuthorString().hashCode();
077      hashCode += 31 * (getTitle() != null ? getTitle().hashCode() : 0);
078      hashCode += 31 * (getJournal() != null ? getJournal().hashCode() : 0);
079      hashCode += 31 * (getVolume() != null ? getVolume().hashCode() : 0);
080      hashCode += 31 * (getIssue() != null ? getIssue().hashCode() : 0);
081      hashCode += 31 * (getYear() != null ? getYear().hashCode() : 0);
082      hashCode += 31 * (getPages() != null ? getPages().hashCode() : 0);
083
084      return hashCode;
085   }
086
087   //--------------------------------------------------------------------------
088   @Override
089   public int compareTo(GenBankReference inObj2)
090   {
091      int result = 0;
092
093      if (null == inObj2)
094      {
095         result = 1;
096      }
097      else
098      {
099         result = CompareUtil.compare(getTitle(), inObj2.getTitle());
100         if (0 == result)
101         {
102            if (getPages() != null)
103            {
104               if (inObj2.getPages() != null)
105               {
106                  result = getPages().compareTo(inObj2.getPages());
107               }
108               else
109               {
110                  result = 1;
111               }
112            }
113            else if (inObj2.getPages() != null)
114            {
115               result = -1;
116            }
117
118            if (0 == result)
119            {
120               result = CompareUtil.compare(getAuthorString(), inObj2.getAuthorString());
121
122               if (0 == result)
123               {
124                  result = CompareUtil.compare(getYear(), inObj2.getYear());
125
126                  if (0 == result)
127                  {
128                     result = CompareUtil.compare(getVolume(), inObj2.getVolume());
129
130                     if (0 == result)
131                     {
132                        result = CompareUtil.compare(getIssue(), inObj2.getIssue());
133
134                        if (0 == result)
135                        {
136                           result = CompareUtil.compare(getJournal(), inObj2.getJournal());
137                        }
138                     }
139                  }
140               }
141            }
142         }
143      }
144
145      return result;
146   }
147
148   //--------------------------------------------------------------------------
149   public GenBankReference setAuthors(String inAuthorList)
150   {
151      String[] authors = inAuthorList.split("[;,]");
152      for (String author : authors)
153      {
154         addAuthor(author);
155      }
156
157      return this;
158   }
159
160   //--------------------------------------------------------------------------
161   public GenBankReference addAuthor(String inAuthor)
162   {
163      if (null == mAuthors)
164      {
165         mAuthors = new ArrayList<String>(25);
166      }
167
168      mAuthors.add(inAuthor);
169
170      return this;
171   }
172
173   //--------------------------------------------------------------------------
174   public List<String> getAuthors()
175   {
176      return mAuthors;
177   }
178
179   //--------------------------------------------------------------------------
180   public String getAuthorString()
181   {
182      return (mAuthors != null ? StringUtil.join(mAuthors, ", ") : "");
183   }
184
185
186   //--------------------------------------------------------------------------
187   public GenBankReference setTitle(String inValue)
188   {
189      mTitle = inValue;
190      return this;
191   }
192
193   //--------------------------------------------------------------------------
194   public String getTitle()
195   {
196      return mTitle;
197   }
198
199
200   //--------------------------------------------------------------------------
201   // Simple example:
202   //     J. Exp. Med. 181 (3), 1245-1250 (1995)
203   //
204   // Submission record example:
205   //     Submitted (07-NOV-1994) Giachino C., Basel Institute for
206   //     Immunology, Grenzacherstrasse 487, Basel, Switzerland, 4005
207   //
208   public void parseJournalString(String inValue)
209   {
210      if (inValue.startsWith("Submitted"))
211      {
212         mSubmissionRecordInfo = inValue;
213      }
214      else
215      {
216         Matcher m = sJournalPattern.matcher(inValue);
217         if (m.matches())
218         {
219            mJournal = m.group(1);
220            mVolume  = Integer.parseInt(m.group(2));
221            mIssue   = Integer.parseInt(m.group(3));
222            mPages   = new Range<>(Integer.parseInt(m.group(4)), Integer.parseInt(m.group(5)));
223            mYear    = Integer.parseInt(m.group(6));
224         }
225         else
226         {
227            mJournal = inValue;
228         }
229      }
230   }
231
232   //--------------------------------------------------------------------------
233   public GenBankReference setJournal(String inValue)
234   {
235      mJournal = inValue;
236      return this;
237   }
238
239   //--------------------------------------------------------------------------
240   public String getJournal()
241   {
242      return mJournal;
243   }
244
245   //--------------------------------------------------------------------------
246   public String getSubmissionRecordInfo()
247   {
248      return mSubmissionRecordInfo;
249   }
250
251
252   //--------------------------------------------------------------------------
253   public GenBankReference setVolume(Integer inValue)
254   {
255      mVolume = inValue;
256      return this;
257   }
258
259   //--------------------------------------------------------------------------
260   public Integer getVolume()
261   {
262      return mVolume;
263   }
264
265
266   //--------------------------------------------------------------------------
267   public GenBankReference setIssue(Integer inValue)
268   {
269      mIssue = inValue;
270      return this;
271   }
272
273   //--------------------------------------------------------------------------
274   public Integer getIssue()
275   {
276      return mIssue;
277   }
278
279
280   //--------------------------------------------------------------------------
281   public GenBankReference setYear(Integer inValue)
282   {
283      mYear = inValue;
284      return this;
285   }
286
287   //--------------------------------------------------------------------------
288   public Integer getYear()
289   {
290      return mYear;
291   }
292
293
294   //--------------------------------------------------------------------------
295   public GenBankReference setPubmedId(Integer inValue)
296   {
297      mPubmedId = inValue;
298      return this;
299   }
300
301   //--------------------------------------------------------------------------
302   public Integer getPubmedId()
303   {
304      return mPubmedId;
305   }
306
307
308   //--------------------------------------------------------------------------
309   public GenBankReference setPages(Range<Integer> inValue)
310   {
311      mPages = inValue;
312      return this;
313   }
314
315   //--------------------------------------------------------------------------
316   public Range<Integer> getPages()
317   {
318      return mPages;
319   }
320
321}