换账号重新发布orz
/*
* Author:Henriette-L(luanxito@163.com)
* Date: 2020-9-27
* Introduction: (等我有空补)简单来说给定SNP序列的Excel文件,在源序列txt文件中查找SNP位点的上下游300个序列并且定位。
*/
package demo;
public class Bean {
private String fiveSource;
private String fiveTarget;
private String fiveStartNum;
private String threeSource;
private String threeTarget;
private String threeStartNum;
public String getFiveSource() {
return fiveSource;
}
public void setFiveSource(String fiveSource) {
this.fiveSource = fiveSource;
}
public String getFiveTarget() {
return fiveTarget;
}
public void setFiveTarget(String fiveTarget) {
this.fiveTarget = fiveTarget;
}
public String getFiveStartNum() {
return fiveStartNum;
}
public void setFiveStartNum(String fiveStartNum) {
this.fiveStartNum = fiveStartNum;
}
public String getThreeSource() {
return threeSource;
}
public void setThreeSource(String threeSource) {
this.threeSource = threeSource;
}
public String getThreeTarget() {
return threeTarget;
}
public void setThreeTarget(String threeTarget) {
this.threeTarget = threeTarget;
}
public String getThreeStartNum() {
return threeStartNum;
}
public void setThreeStartNum(String threeStartNum) {
this.threeStartNum = threeStartNum;
}
}
package demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class demo {
public static void main(String[] args) throws IOException {
List<Bean> list = new ArrayList<>();
System.out.println("1");
getExcelData(list);
System.out.println("2");
getTargetList(list);
System.out.println("3");
getTargetDataFromTXT(list);//时间较长
System.out.println("4");
setTargetDataToExcel(list);
System.out.println("end");
}
/**
* 将目标写入到excel
* @throws IOException
*/
public static void setTargetDataToExcel(List<Bean> list) throws IOException {
Workbook workbook = new XSSFWorkbook();
String [] title = {"序号","5'端起始数字","5’端前300数据","3'端起始数字","3'端后300数据"};
//设置sheet
String sheetName = "目标数据";
Sheet sheet = workbook.createSheet(sheetName);
//获取表投行
Row titleRow = sheet.createRow(0);
//创建单元格
Cell cell =null;
for(int i=0;i<title.length;i++) {
cell = titleRow.createCell(i);
cell.setCellValue(title[i]);
}
Row row = null;
for (int i = 0; i < list.size(); i++) {
//创建list.size()行数据
row = sheet.createRow(i + 1);
//把值一一写进单元格里
//设置第一列为自动递增的序号
row.createCell(0).setCellValue(i + 1);
row.createCell(1).setCellValue(list.get(i).getFiveStartNum());
row.createCell(2).setCellValue(list.get(i).getFiveTarget());
row.createCell(3).setCellValue(list.get(i).getThreeStartNum());
row.createCell(4).setCellValue(list.get(i).getThreeTarget());
}
//设置单元格宽度自适应,在此基础上把宽度调至1.5倍
for (int i = 0; i < title.length; i++) {
sheet.autoSizeColumn(i, true);
//sheet.setColumnWidth(i, sheet.getC
//上述列宽报错
//Throws:java.lang.lllegalArgumentException-if width>255*256(the maximum column width in Excel is 255characters)
//结合上述微软注意规范可知未考虑边界情况,不是String的问题是org.apache.pol.xssf.usermode.XSSFSheet的问题
//写入到excel
//修改生成文件名修改下面绿色的路径
String exportFilePath = "E:\\结果分析.xlsx";
File exportFile = new File(exportFilePath);
if(!exportFile.exists()) {
exportFile.createNewFile();
}
OutputStream fileOut = new FileOutputStream(exportFile);
workbook.write(fileOut);
fileOut.close();
}
/**
* 从TXT中获取目标数据
* @throws IOException
*/
public static void getTargetDataFromTXT(List<Bean> list) throws IOException {
//需要换数据源修改下面路径
Path path =Paths.get("E:\\G.txt");
byte [] data = Files.readAllBytes(path);//获取TXT数据
String res = new String(data);
res=res.replaceAll("\r|\n","");
for(Bean bean : list) {
//获取第五列目标
int index = res.indexOf(bean.getFiveSource());
if(index == -1) {
bean.setFiveStartNum("-1");
bean.setFiveTarget("不存在");
}else {
bean.setFiveStartNum(String.valueOf(index));
int length = bean.getFiveSource().length();
int start =index -300;
if(start<0) {
start =0;
}
bean.setFiveTarget(res.substring(start, index));
}
//获取3'端目标数据
index = res.indexOf(bean.getThreeSource());
if(index == -1) {
bean.setThreeStartNum("-1");
bean.setThreeTarget("不存在");
}else {
bean.setThreeStartNum(String.valueOf(index));
int length =bean.getThreeSource().length();
int end = index +300+ length;
if(end>res.length()) {
end = res.length() - 1;
}
bean.setThreeTarget(res.substring(index + length, end));
}
}
}
/**
* 去除[*],返回字符串
*/
public static void getTargetList(List<Bean> list) {
for (int i = 0; i < list.size(); i++) {
if(list.get(i).getFiveSource().indexOf('[') != -1) {//存在[]
StringBuffer reStringBuffer = new StringBuffer(list.get(i).getFiveSource());
int start = list.get(i).getFiveSource().indexOf('[');
int end =list.get(i).getFiveSource().indexOf(']');
reStringBuffer.replace(start, end+1, "");
list.get(i).setFiveSource(reStringBuffer.toString());
}
if(list.get(i).getThreeSource().indexOf('[') != -1) {//存在[]
StringBuffer reStringBuffer = new StringBuffer(list.get(i).getThreeSource());
int start = list.get(i).getThreeSource().indexOf('[');
int end =list.get(i).getThreeSource().indexOf(']');
reStringBuffer.replace(start, end+1, "");
list.get(i).setThreeSource(reStringBuffer.toString());
}
}
}
/**
* 获取excel数据
* @throws IOException
*/
public static void getExcelData(List<Bean> list) throws IOException {
//用流的方式先读取到你想要的excel文件
//想换查找的序列修改下面路径
String address = "C:\\芯片SNP位点.xlsx";
FileInputStream fis=new FileInputStream(address);
//定义工作簿
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fis);
//定义工作表,默认取第一个子表
XSSFSheet xssfSheet= xssfWorkbook.getSheetAt(1);
//定义行默认第一行为标题行,index = 0
XSSFRow titleRow = xssfSheet.getRow(0);
//循环每行的数据
for (int rowIndex = 8; rowIndex < xssfSheet.getPhysicalNumberOfRows(); rowIndex++) {
XSSFRow xssfRow = xssfSheet.getRow(rowIndex);
if (xssfRow == null) {
continue;
}
Bean bean = new Bean();
//循环获取每个单元格(cell)的数据
XSSFCell xssfCell5 = xssfRow.getCell(4);
bean.setFiveSource(getString(xssfCell5));
if("".equals(xssfCell5)) {
break;
}
XSSFCell xssfCell3 = xssfRow.getCell(5);
bean.setThreeSource(getString(xssfCell3));
list.add(bean);
}
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 把单元格的内容转为字符串
* @param xssfCell 单元格
* @return 字符串
*/
public static String getString(XSSFCell xssfCell) {
if (xssfCell == null) {
return "";
}
if (xssfCell.getCellTypeEnum() == CellType.NUMERIC) {
return String.valueOf(xssfCell.getNumericCellValue());
} else if (xssfCell.getCellTypeEnum() == CellType.BOOLEAN) {
return String.valueOf(xssfCell.getBooleanCellValue());
} else {
return xssfCell.getStringCellValue();
}
}
}