001package com.hfg.bio.seq.alignment.blast;
002
003import java.io.File;
004import java.io.IOException;
005import java.util.ArrayList;
006import java.util.HashMap;
007import java.util.List;
008import java.util.Map;
009
010import com.hfg.bio.seq.BioSequenceType;
011import com.hfg.util.collection.CollectionUtil;
012import com.hfg.xml.XMLDoc;
013import com.hfg.xml.XMLException;
014import com.hfg.xml.XMLName;
015import com.hfg.xml.XMLTag;
016
017//==============================================================================
018/**
019 A BLAST sequence database manager.
020 <div>
021 @author J. Alex Taylor, hairyfatguy.com
022 </div>
023 */
024//==============================================================================
025// com.hfg XML/HTML Coding Library
026//
027// This library is free software; you can redistribute it and/or
028// modify it under the terms of the GNU Lesser General Public
029// License as published by the Free Software Foundation; either
030// version 2.1 of the License, or (at your option) any later version.
031//
032// This library is distributed in the hope that it will be useful,
033// but WITHOUT ANY WARRANTY; without even the implied warranty of
034// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
035// Lesser General Public License for more details.
036//
037// You should have received a copy of the GNU Lesser General Public
038// License along with this library; if not, write to the Free Software
039// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
040//
041// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
042// jataylor@hairyfatguy.com
043//==============================================================================
044
045public class BLAST_DatabaseMgr
046{
047   private File mFile;
048   private Map<BioSequenceType, List<BLAST_DatabaseGroup>> mGroupMap;
049
050   protected static XMLName XML_NAME = new XMLName("BLAST_DatabaseMgr");
051
052
053   //###########################################################################
054   // CONSTRUCTORS
055   //###########################################################################
056
057
058
059   //---------------------------------------------------------------------------
060   public BLAST_DatabaseMgr(File inFile)
061         throws XMLException, IOException
062   {
063      mFile = inFile;
064
065      XMLDoc doc = new XMLDoc(inFile);
066      XMLTag rootNode = (XMLTag) doc.getRootNode();
067      rootNode.verifyTagName(XML_NAME);
068
069      List<XMLTag> groupTags = rootNode.getSubtagsByName(BLAST_DatabaseGroup.XML_NAME);
070      if (CollectionUtil.hasValues(groupTags))
071      {
072         for (XMLTag groupTag : groupTags)
073         {
074            addGroup(new BLAST_DatabaseGroup(groupTag));
075         }
076      }
077   }
078
079   //###########################################################################
080   // PUBLIC METHODS
081   //###########################################################################
082
083   //---------------------------------------------------------------------------
084   public File getFile()
085   {
086      return mFile;
087   }
088
089   //---------------------------------------------------------------------------
090   public BLAST_DatabaseMgr setFile(File inValue)
091   {
092      mFile = inValue;
093      return this;
094   }
095
096   //---------------------------------------------------------------------------
097   public void persistToFile()
098   {
099      XMLDoc xmlDoc = toXMLDoc();
100      xmlDoc.toIndentedXML(mFile, 0, 2);
101   }
102
103   //---------------------------------------------------------------------------
104   public void addGroup(BLAST_DatabaseGroup inGroup)
105   {
106      if (inGroup != null)
107      {
108         if (null == mGroupMap)
109         {
110            mGroupMap = new HashMap<>(3);
111         }
112
113         List<BLAST_DatabaseGroup> groupList = mGroupMap.get(inGroup.getSeqType());
114         if (null == groupList)
115         {
116            groupList = new ArrayList<>(25);
117            mGroupMap.put(inGroup.getSeqType(), groupList);
118         }
119
120         groupList.add(inGroup);
121      }
122   }
123
124
125   //---------------------------------------------------------------------------
126   public List<BLAST_DatabaseGroup> getGroups(BioSequenceType inType)
127   {
128      return (mGroupMap != null ? mGroupMap.get(inType) : null);
129   }
130
131
132   //---------------------------------------------------------------------------
133   public XMLDoc toXMLDoc()
134   {
135      XMLTag tag = new XMLTag(XML_NAME);
136
137      if (CollectionUtil.hasValues(mGroupMap))
138      {
139         for (BioSequenceType bioSequenceType : mGroupMap.keySet())
140         {
141            for (BLAST_DatabaseGroup group : mGroupMap.get(bioSequenceType))
142            {
143               tag.addSubtag(group.toXMLTag());
144            }
145         }
146      }
147
148      XMLDoc doc = new XMLDoc();
149      doc.setRootNode(tag);
150
151      return doc;
152   }
153
154}