import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
+import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Random;
import java.util.Scanner;
import java.util.Vector;
final static String NL = "\n";
+ Random generator = new Random();
+
// current directory
String currentFolder;
while (filesRead < increments)
{
- FileParse parserSTO = new FileParse(inputSTO, "",
- DataSourceType.FILE);
- readStockholm(parserSTO);
+ readStockholm(inputSTO);
- FileParse parserHMM = new FileParse(inputHMM, "",
- DataSourceType.FILE);
- readHMM(parserHMM);
+ readHMM(inputHMM);
int count = countValidResidues();
processData(count);
int filesRead = 0;
int i = 0;
- inputSTO.mark(20);
- String check = inputSTO.readLine();
- inputSTO.reset();
- while (!"".equals(check) && !" ".equals(check) && check != null)
+ boolean endReached = atEnd(inputSTO);
+ while (!endReached)
{
- inputSTO.mark(20);
- String line = inputSTO.readLine();
- inputSTO.reset();
-
- FileParse parserSTO = new FileParse(inputSTO, "",
- DataSourceType.FILE);
- readStockholm(parserSTO);
-
- FileParse parserHMM = new FileParse(inputHMM, "",
- DataSourceType.FILE);
- readHMM(parserHMM);
+ readStockholm(inputSTO);
+ readHMM(inputHMM);
int count = countValidResidues();
processData(count);
currentFilePosition++;
System.out.println(i);
i++;
- inputSTO.mark(20);
- check = inputSTO.readLine();
- inputSTO.reset();
+ endReached = atEnd(inputSTO);
}
* @param source
* @throws IOException
*/
- public void readStockholm(FileParse source) throws IOException
+ public void readStockholm(BufferedReader inputSTO) throws IOException
{
- StockholmFile file = new StockholmFile(source);
+ FileParse parserSTO = new FileParse(inputSTO, "", DataSourceType.FILE);
+ StockholmFile file = new StockholmFile(parserSTO);
sequences = file.getSeqs();
}
* @param source
* @throws IOException
*/
- public void readHMM(FileParse source) throws IOException
+ public void readHMM(BufferedReader inputHMM) throws IOException
{
-
- HMMFile file = new HMMFile(source);
+ FileParse parserHMM = new FileParse(inputHMM, "", DataSourceType.FILE);
+ HMMFile file = new HMMFile(parserHMM);
file.parse();
hmm = file.getHMM();
}
- public void exportFile(BufferedReader br, String location)
+ public void exportFile(BufferedReader br, String location, boolean append)
throws IOException
{
String line = br.readLine();
PrintWriter writer = new PrintWriter(
- new FileOutputStream(new File(location), true));
+ new FileOutputStream(location, append));
while (!"//".equals(line))
{
writer.println(line);
new FileReader(families));
BufferedReader hmmReader = new BufferedReader(new FileReader(hmms));
HashMap<String, Integer> clanIndexes = new HashMap<>();
+ ArrayList<Integer> familyCounts = new ArrayList<>();
int filePos = 0;
int clanCount = 0;
String line;
{
clanIndexes.put(clanName, clanCount);
clanCount++;
+ familyCounts.add(0);
}
Integer clanI = clanIndexes.get(clanName);
String clanPath = directory + "/Clan" + clanI.toString();
- File clanFolder = new File(clanPath);
- String famPath = clanPath + "/Families.sto";
- String hmmPath = clanPath + "/HMMs.hmm";
- if (!clanFolder.exists())
- {
- clanFolder.mkdir();
- }
- exportFile(familyReader, famPath);
- exportFile(hmmReader, hmmPath);
-
+ createFolders(clanPath);
+
+ int index = clanIndexes.get(clanName);
+ exportFile(familyReader,
+ clanPath + "/Families/Fam" + familyCounts.get(index)
+ + ".sto",
+ false);
+ exportFile(hmmReader,
+ clanPath + "/HMMs/HMM" + familyCounts.get(index) + ".hmm",
+ false);
+
+ int count = familyCounts.get(index);
+ count++;
+ familyCounts.set(index, count);
}
line = clanFinder.readLine();
+
}
if (!inClan)
{
}
clanFinder.close();
+
+ for (int clan = 0; clan < clanCount; clan++)
+ {
+ PrintWriter writer = new PrintWriter(
+ directory + "/Clan" + clan + "/NumberOfFamilies.txt");
+ int count = familyCounts.get(clan);
+ writer.print(count);
+ writer.close();
+ }
}
this.hmms = currentFolder + hmms;
}
+ public void alignWithinClan(String exportLocation, String clansLocation)
+ throws IOException, InterruptedException
+ {
+ int alignmentsExported = 0;
+ for (int clan = 0; clan < 604; clan++)
+ {
+ int famCount = 0;
+ String clanPath = clansLocation + "/Clan" + clan;
+ int numberOfFamilies;
+ BufferedReader br = new BufferedReader(
+ new FileReader(clanPath + "/NumberOfFamilies.txt"));
+ String line = br.readLine();
+ numberOfFamilies = Integer.parseInt(line);
+ br.close();
+ String commandExportLocation = exportLocation + "/Clan" + clan;
+ createFolders(commandExportLocation);
+ for (int family = 0; family < numberOfFamilies; family++)
+ {
+ famCount++;
+ ArrayList<Integer> indexes = new ArrayList<>();
+ for (int i = 0; i < numberOfFamilies; i++)
+ {
+ if (i != family)
+ {
+ indexes.add(i);
+ }
+ }
+ int hmmIndex = getRandom(indexes);
+ String famPath = clanPath + "/Families/Fam" + family + ".sto";
+ String hmmPath = clanPath + "/HMMs/HMM" + hmmIndex + ".hmm";
+ String command = "H:/Documents/hmmalign -o " + commandExportLocation
+ + "/Fam" + family + ".sto ";
+ command += hmmPath + " ";
+ command += famPath;
+
+ final Process p = Runtime.getRuntime().exec(command);
+
+ new Thread(new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ BufferedReader input = new BufferedReader(
+ new InputStreamReader(p.getInputStream()));
+ String line = null;
+
+ try
+ {
+ while ((line = input.readLine()) != null)
+ {
+ System.out.println(line);
+ }
+ } catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+ }
+ }).start();
+ p.waitFor();
+
+ exportHMM(hmmIndex,
+ commandExportLocation + "/HMMs/HMM" + family + ".hmm");
+
+ alignmentsExported++;
+
+ System.out.println(alignmentsExported + " alignments exported");
+
+ }
+ PrintWriter writer = new PrintWriter(
+ commandExportLocation + "/NumberOfFamilies.txt");
+ writer.print(famCount);
+ writer.close();
+ }
}
+ public boolean atEnd(BufferedReader br) throws IOException
+ {
+ boolean end = false;
+ br.mark(80);
+ String line = br.readLine();
+ if ("".equals(line) || line == null)
+ {
+ end = true;
+ }
+ br.reset();
+ return end;
+ }
+
+ public int getRandom(ArrayList<Integer> list)
+ {
+ int index = generator.nextInt(list.size());
+ int value = list.get(index);
+ list.remove(index);
+ return value;
+ }
+
+ public void createFolders(String clanPath)
+ {
+ File clanFolder = new File(clanPath);
+ if (!clanFolder.exists())
+ {
+ clanFolder.mkdir();
+ }
+
+ File famFolder = new File(clanPath + "/Families");
+ File hmmFolder = new File(clanPath + "/HMMs");
+ if (!famFolder.exists())
+ {
+ famFolder.mkdir();
+ hmmFolder.mkdir();
+ }
+ }
+}
+
+
+