import java.util.*;
import java.io.*;

public class NomG{
    
    private HashMap<String, ArrayList<Group>> hlagene2groups;

    //private ArrayList<Group> groups;
    private HashMap<String, Group> allele2Group;
    
    public NomG(){
	this.hlagene2groups = new HashMap<String, ArrayList<Group>>();
	//this.groups = new ArrayList<Group>();
	this.allele2Group = new HashMap<String, Group>();
    }

    
    //returns the list of groups belonging to HLA gene
    public ArrayList<Group> getGroups(String hgn){
	return this.hlagene2groups.get(hgn);
    }


    public void loadHlaGene2Groups(String nomGFile){
	BufferedReader br = null;
	String curline = null;
	try{
	    br = new BufferedReader(new FileReader(nomGFile));
	    while( (curline=br.readLine()) != null){
		if(curline.charAt(0) != '#'){
		    //System.err.println(curline);
		    Group curgrp = new Group(curline, this);
		    String curhlagn = curgrp.getHLAGeneName();
		    ArrayList<Group> groups = this.hlagene2groups.get(curhlagn);
		    if(groups == null){
			groups = new ArrayList<Group>();
			this.hlagene2groups.put(curhlagn, groups);
		    }
		    groups.add(new Group(curline, this));
		}
	    }
	    br.close();
	}catch(IOException ioe){
	    ioe.printStackTrace();
	}
    }
    
    public void addToAllele2Group(String allele, Group g){
	this.allele2Group.put(allele, g);
    }

    public String getGroupNameForAllele(String a){
	return this.allele2Group.get(a).getGroupName();
    }
    
}

/*
 * G Group
 */
class Group{

    private String hlaGeneName; //A B C DQA1 DQB1 DRB1 etc

    private String groupname; //representatitve name: ex) 01:01:01G
    
    private HashSet<String> set; 

    public String getGroupString(){
	return hlaGeneName + "*" + groupname;
    }

    public String getHLAGeneName(){
	return hlaGeneName;
    }
    
    public String getGroupName(){
	return this.hlaGeneName + "*" + this.groupname;
    }

    public String getFirstAllele(){
	return this.hlaGeneName + "*" + set.iterator().next();
    }
    
    public Group(String line, NomG nomG){
	this.set = new HashSet<String>();
	this.process(line, nomG);
    }
    
    public void process(String line, NomG nomG){
	String[] tokens = line.split(";");
	this.hlaGeneName = tokens[0].substring(0,tokens[0].indexOf("*"));
	String gName = null;
	if(tokens.length == 2 && line.endsWith(";"))
	    this.groupname = tokens[1];
	else
	    this.groupname = tokens[2];
	
	String[] elements = tokens[1].split("/");
	for(String e : elements){
	    this.set.add(e);
	    nomG.addToAllele2Group(e, this);
	}
    }
    
}


@@ -1470,1103 +1470,5 @@
 %0A%7D%0A%0A
-/*%0A * G Group%0A */%0Aclass Group%7B%0A%0A    private String hlaGeneName; //A B C DQA1 DQB1 DRB1 etc%0A%0A    private String groupname; //representatitve name: ex) 01:01:01G%0A    %0A    private HashSet%3CString%3E set; %0A%0A    public String getGroupString()%7B%0A%09return hlaGeneName + %22*%22 + groupname;%0A    %7D%0A%0A    public String getHLAGeneName()%7B%0A%09return hlaGeneName;%0A    %7D%0A    %0A    public String getGroupName()%7B%0A%09return this.hlaGeneName + %22*%22 + this.groupname;%0A    %7D%0A%0A    public String getFirstAllele()%7B%0A%09return this.hlaGeneName + %22*%22 + set.iterator().next();%0A    %7D%0A    %0A    public Group(String line, NomG nomG)%7B%0A%09this.set = new HashSet%3CString%3E();%0A%09this.process(line, nomG);%0A    %7D%0A    %0A    public void process(String line, NomG nomG)%7B%0A%09String%5B%5D tokens = line.split(%22;%22);%0A%09this.hlaGeneName = tokens%5B0%5D.substring(0,tokens%5B0%5D.indexOf(%22*%22));%0A%09String gName = null;%0A%09if(tokens.length == 2 && line.endsWith(%22;%22))%0A%09    this.groupname = tokens%5B1%5D;%0A%09else%0A%09    this.groupname = tokens%5B2%5D;%0A%09%0A%09String%5B%5D elements = tokens%5B1%5D.split(%22/%22);%0A%09for(String e : elements)%7B%0A%09    this.set.add(e);%0A%09    nomG.addToAllele2Group(e, this);%0A%09%7D%0A    %7D%0A    %0A%7D%0A
 %0A

move Group as a separate class