它应该会返回一个地图。这个Map的键应该是字符串"transversion“和"transition",Map的值应该是SNP对象的列表。“过渡”是指A<->G与C<->T之间的变化,而“转换”是A<->C、G<->T、A<->T和C<->G之间的变化。
我该怎么做这两个过滤器?
public class StreamAssignment {
private static final List<Snp> SNP_COLLECTION = Snp.getSnpCollection();
private static final String[] SNP_DATA = new String[14];
static {
SNP_DATA[0] = "100273;A;G;0.0123";
SNP_DATA[1] = "100275;A;C;0.00323";
SNP_DATA[2] = "117807;T;G;0.1915";
SNP_DATA[3] = "162889;C;G;8.72E-4";
SNP_DATA[4] = "190199;T;C;0.1019";
SNP_DATA[5] = "277614;A;G;0.0168";
SNP_DATA[6] = "372778;C;A;4.24E-5";
SNP_DATA[7] = "417752;A;G;1.8474E-10";
SNP_DATA[8] = "478808;A;G;1.535689E-8";
SNP_DATA[9] = "556920;T;G;0.1097";
SNP_DATA[10] = "676255;G;C;0.0016672";
SNP_DATA[11] = "667280;A;G;0.00287";
SNP_DATA[12] = "719876;C;A;0.006649";
SNP_DATA[13] = "828771;A;C;0.097706";
}
public static Map<String, List<Snp>> getTransversionsTransitions() {
Map<String, List<Snp>> result;
result = SNP_COLLECTION.stream().filter();
return null;
}
}snp类
package nl.bioinf.appdesign.d_streams_lambdas;
import java.util.ArrayList;
import java.util.List;
public class Snp {
private final long position;
private final char reference;
private final char alternative;
private final double minorAlleleFrequency;
public Snp(long position, char reference, char alternative, double minorAlleleFrequency) {
this.position = position;
this.reference = reference;
this.alternative = alternative;
this.minorAlleleFrequency = minorAlleleFrequency;
}
public long getPosition() {
return position;
}
public char getReference() {
return reference;
}
public char getAlternative() {
return alternative;
}
public double getMinorAlleleFrequency() {
return minorAlleleFrequency;
}
@Override
public String toString() {
return "Snp{" +
"position=" + position +
", reference=" + reference +
", alternative=" + alternative +
", minorAlleleFrequency=" + minorAlleleFrequency +
'}';
}
public final static List<Snp> getSnpCollection() {
List<Snp> snps = new ArrayList<>();
snps.add(new Snp(100273, 'A', 'G', 0.0123));
snps.add(new Snp(100275, 'A', 'C', 0.00323));
snps.add(new Snp(117807, 'T', 'G', 0.1915));
snps.add(new Snp(162889, 'C', 'G', 0.000872));
snps.add(new Snp(190199, 'T', 'C', 0.1019));
snps.add(new Snp(277614, 'A', 'G', 0.0168));
snps.add(new Snp(372778, 'C', 'A', 0.0000424));
snps.add(new Snp(417752, 'A', 'G', 1.8474e-10));
snps.add(new Snp(478808, 'A', 'G', 1.535689e-8));
snps.add(new Snp(556920, 'T', 'G', 0.1097));
snps.add(new Snp(676255, 'G', 'C', 1.6672e-3));
snps.add(new Snp(667280, 'A', 'G', 0.00287));
snps.add(new Snp(719876, 'C', 'A', 0.006649));
snps.add(new Snp(828771, 'A', 'C', 0.097706));
return snps;
}
}发布于 2022-04-17 15:26:33
因为您的Snp类不包含关于它是转换还是转换的信息,所以您需要一个包装类。我将省略这一点,但它只需要有一个String type属性、Snp snp属性和一个工厂方法(让我们称之为解析)。
result = SNP_COLLECTION.stream()
// do your filtering based on string values
.map(SnpWrapper::parse) // String to Snp conversion
// do your filtering based on Snp values
.collect(Collectors.groupingBy(
Wrapper::getType,
Collectors.mapping(Wrapper::getSnp, Collectors.toList())
));编辑:包装实际上并不需要。您只需要提供一个实用方法。它可以放在Snp中,也可以作为当前类中的私有方法。让我们假设是后者,命名为getSnpType
result = SNP_COLLECTION.stream()
// do your filtering based on string values
.map(Snp::parse) // or whatever you have for String -> Snp
// do your filtering based on Snp values
.collect(Collectors.groupingBy(StreamAssignment::getSnpType));发布于 2022-04-17 15:51:12
您可以将两个字符reference & alternative连接到一个字符串中,并使用regex检查transition或transversion。有点像
import java.util.function.BiFunction;
....
public static Map<String, List<Snp>> getTransversionsTransitions() {
BiFunction<Character,Character,String> func =
(a,b) -> (""+a+b).matches("AG|GA|CT|TC") ? "Transitions":"Transversions";
return SNP_COLLECTION.stream()
.collect(Collectors.groupingBy(snp -> func.apply(snp.getReference(),snp.getAlternative())));
}https://stackoverflow.com/questions/71902940
复制相似问题