【问题标题】:Prolog, read a csv file and make a predicate. findallProlog,读取一个 csv 文件并做一个谓词。找到所有
【发布时间】:2014-03-23 12:40:28
【问题描述】:

我正在使用 SWI-Prolog。

我有一个 csv 文件,其中第一行是探针,然后每一行都是一个样本:

    1007_s_at   1053_at 117_at ...
GSM102447.CEL   1   0   0 ...
GSM102449.CEL   1   0   0 ...
GSM102451.CEL   1   0   0 ...
GSM102455.CEL   1   0   0 ...
GSM102507.CEL   1   0   1 ...
...

实际文件有超过 20,000 列(“probes”)和不超过 150 行(“samples”)。

我想提取每个关系并将它们作为事实打印到另一个文件中。

例如:

%probe_value_in_sample(Probe,Sample_Strip,ProbeValue).
probe_value_in_sample('1007_s_at','GSM102447',1).
etc

到目前为止我的代码:

foreach(csv_read_file_row_list('GSE2109_BarCode.csv', List), assert(['samples'|List])).

probe_value_in_sample(Probe,Sample_Strip,ProbeValue):-
[samples|[samples,Empty|ProbeList]],Empty='', %the first value is empty
indexOf(ProbeList,Probe,IndexOfProbe),
[samples|[samples,Sample|SampleValues]],Sample\='',
nth0(IndexOfProbe,SampleValues,ProbeValue),
name(Sample, CharSample),
append(Char_Sample_Strip,".CEL",CharSample),
name(Sample_Strip,Char_Sample_Strip).

%IndexOf(MyList, MyElement, MyIndex).
indexOf([Element|_], Element, 0).
indexOf([_|Tail], Element, Index):-
indexOf(Tail, Element, Index1),
Index is Index1+1.

这似乎工作得很好,但不起作用,或者它太慢以至于无法与 findall 一起使用。

知道可能是什么问题吗?

感谢您的帮助。

更新

感谢您的回复。

我已经定义了:

csv_read_file_row_list(File, List,Functor):-
csv_read_file_row(File,Row,[functor(Functor)]),Row=..List.

所以我有一个打开的文件而不是一个流,而 Functor 变量目前是多余的。

我对您如何使用 maplist 感到困惑?而且我不能让它工作。

我试过了:

:- dynamic samples/3.

csv_read_file_row_list(File, List,Functor):-
csv_read_file_row(File,Row,[functor(Functor)]),Row=..List.

prepare_db(File) :-
   ( nonvar(File) ; File = 'GSE2109_BarCode.csv' ),
   %open(File, read, S),
   csv_read_file_row_list(File,     ['thing',_Empty|ColKeys],'thing'),
 forall(csv_read_file_row_list(File,    ['thing',RowKeyDirty|Samples],'thing'),
    (   clean_rowkey(RowKeyDirty, RowKey),
        maplist(store_sample(RowKey), ColKeys, Samples)
    )).
%close(S).

store_sample(RowKey, ColKey, Sample) :-
  assertz(samples(RowKey, ColKey, Sample)).

clean_rowkey(RowKeyDirty, RowKey) :- append(RowKey, ".CEL", RowKeyDirty).

还有:

:- dynamic samples/3.

csv_read_file_row_list(File, List,Functor):-
csv_read_file_row(File,Row,[functor(Functor)]),Row=..List.

prepare_db(File) :-
( nonvar(File) ; File = 'GSE2109_BarCode.csv' ),
%open(File, read, S),
csv_read_file_row_list(File, ['thing',_Empty|ColKeys],'thing'),
forall(csv_read_file_row_list(File, ['thing',RowKeyDirty|Samples],'thing'),
    (   clean_rowkey(RowKeyDirty, RowKey),
        maplist(store_sample,[RowKey], ColKeys, Samples)
    )).
%close(S).

store_sample(RowKey, ColKey, Sample) :-
assertz(samples(RowKey, ColKey, Sample)).

clean_rowkey(RowKeyDirty, RowKey) :- append(RowKey, ".CEL", RowKeyDirty).

但两者都失败了。

【问题讨论】:

    标签: csv prolog prolog-findall


    【解决方案1】:

    您没有以正确的方式使用 assert/1。 Prolog 在内存中具有快速高效的 DB,但与任何 DB 一样,必须正确索引。 当然,与任何语言一样,避免每次都重复相同的操作,而是在准备 DB 时将数据格式化一次

    :- dynamic samples/3.
    
    prepare_db(File) :-
        ( nonvar(File) ; File = 'GSE2109_BarCode.csv' ),
        open(File, read, S),
        read_row(S, [_Empty|ColKeys]),
        forall(read_row(S, [RowKeyDirty|Samples]),
            (   clean_rowkey(RowKeyDirty, RowKey),
                maplist(store_sample(RowKey), ColKeys, Samples)
            )),
        close(S).
    
    store_sample(RowKey, ColKey, Sample) :-
        assertz(samples(RowKey, ColKey, Sample)).
    
    clean_rowkey(RowKeyDirty, RowKey) :- append(RowKey, ".CEL", RowKeyDirty).
    

    此代码假设第一行与所有其他行的列数非常相同。

    read_row/2 必须获取一行并拆分为 代码列表,我猜你的 csv_read_file_row_list/2 已经做到了,但我无法在发布的代码中找到你的定义。 p>

    索引对原子而不是代码列表更有效。 atom_codes/2 允许在这些表示之间切换。

    编辑

    从您的评论和附加发布的代码中,我可以看出我的回答不是很合适。 这是一个经过修改和测试的sn-p

    :- [library(csv)].
    
    :- dynamic samples/3.
    :- dynamic column_keys/1.
    
    prepare_db(File) :-
        retractall(column_keys(_)),
        retractall(samples(_,_,_)),
        ( nonvar(File) ; File = '/tmp/test.csv' ),
        forall(read_row(File, Row), store_row(Row)).
    
    store_row(Row) :-
        Row =.. [row|Cols],
        (   column_keys(ColKeys)
        ->  Cols = [RowKeyDirty|Samples],
            clean_rowkey(RowKeyDirty, RowKey),
            maplist(store_sample(RowKey), ColKeys, Samples)
        ;   assertz(column_keys(Cols))
        ).
    
    store_sample(RowKey, ColKey, Sample) :-
        assertz(samples(RowKey, ColKey, Sample)).
    
    clean_rowkey(RowKeyDirty, RowKey) :-
        atom_concat(RowKey, '.CEL', RowKeyDirty).
    
    read_row(File, Row) :-
        csv_read_file_row(File, Row, [separator(0' ), strip(true), convert(true)]),
        writeln(read_row(Row)).
    

    适用于这个测试文件

                     1007_s_at    1053_at   117_at
    GSM102447.CEL    1            0         0
    GSM102449.CEL    1            0         0
    GSM102451.CEL    1            0         0
    GSM102455.CEL    1            0         0
    GSM102507.CEL    1            0         1
    

    和产量

    ?- prepare_db(_).
    read_row(row(1007_s_at,1053_at,117_at))
    read_row(row(GSM102447.CEL,1,0,0))
    read_row(row(GSM102449.CEL,1,0,0))
    read_row(row(GSM102451.CEL,1,0,0))
    read_row(row(GSM102455.CEL,1,0,0))
    read_row(row(GSM102507.CEL,1,0,1))
    true.
    
    16 ?- samples(X,Y,Z).
    X = 'GSM102447',
    Y = '1007_s_at',
    Z = 1 ;
    X = 'GSM102447',
    Y = '1053_at',
    Z = 0 ;
    ...
    

    当然,读取行的显示只是为了调试目的

    【讨论】:

    • 我进行了更新-我对您如何使用 maplist 感到困惑?我无法让它工作。也许是因为它是文件而不是流?感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-26
    相关资源
    最近更新 更多